Custom Type d on Type Traits
#include <stdint.h>
#include <string>
#include <iostream>
enum class SimpleType {
ST_INVALID,
ST_INT64,
ST_UINT64,
ST_DOUBLE,
ST_STRING
};
template<SimpleType stype>
struct SimpleType2BuiltinType {
struct InvalidType {};
typedef InvalidType BuiltinType;
};
#define SimpleType2BuiltinTypeTraits(stype, btype) \\
template<> \\
struct SimpleType2BuiltinType<stype> { \\
typedef btype BuiltinType; \\
}
SimpleType2BuiltinTypeTraits(SimpleType::ST_INT64, int64_t);
SimpleType2BuiltinTypeTraits(SimpleType::ST_UINT64, uint64_t);
SimpleType2BuiltinTypeTraits(SimpleType::ST_DOUBLE, double);
SimpleType2BuiltinTypeTraits(SimpleType::ST_STRING, std::string);
#undef SimpleType2BuiltinTypeTraits
class {
public:
(SimpleType stype)
: type_(stype) {}
~ () {}
public:
virtual SimpleType type() const {
return type_;
}
virtual void type(SimpleType stype) {
type_ = stype;
}
private:
SimpleType type_;
};
template<class T>
class BasicType : public {
public:
BasicType(SimpleType stype)
: (stype) {}
~BasicType() {}
public:
virtual void value(const T& value) {
value_ = value;
}
virtual const T& value() const {
return value_;
}
private:
T value_;
};
typedef BasicType<int64_t> Int64;
typedef BasicType<uint64_t> UInt64;
typedef BasicType<double> Double;
typedef BasicType<std::string> String;
#define SIMPLE_VALUE_HELPER(stype, obj) \\
case stype: { \\
typedef SimpleType2BuiltinType<stype>::BuiltinType Type; \\
BasicType<Type>* typed = static_cast<BasicType<Type>*>(obj); \\
const Type& v = typed->value(); \\
std::cout << v << std::endl; \\
break; \\
}
#define SIMPLE_TYPE_MACRO_HEPLER(MY_MACRO, obj) \\
MY_MACRO(SimpleType::ST_INT64, obj) \\
MY_MACRO(SimpleType::ST_UINT64, obj) \\
MY_MACRO(SimpleType::ST_DOUBLE, obj) \\
MY_MACRO(SimpleType::ST_STRING, obj)
int main(int argc, char *argv[]) {
std::string v(\"foo\");
String foo(SimpleType::ST_STRING);
foo.value(v);
* = &foo;
switch ( ->type()) {
SIMPLE_TYPE_MACRO_HEPLER(SIMPLE_VALUE_HELPER, );
default:
break;
}
return 0;
}
继续阅读与本文标签相同的文章
下一篇 :
DeepMind研究员让AI找出其脆弱点
-
跟并列式人民日报时评学布局谋篇
2026-05-19栏目: 教程
-
阿里开发者技术交流钉钉群汇总【2019】
2026-05-19栏目: 教程
-
9月书讯:别抱怨读书苦,那是你看世界的路
2026-05-19栏目: 教程
-
首页流量波动大?如何避开猜你喜欢的n个雷区
2026-05-19栏目: 教程
-
Linux基础技术实践#网络安全基础技术实践课程
2026-05-19栏目: 教程
