zoukankan      html  css  js  c++  java
  • C++ RTTI

    • Custom RunTime Type Info
    
    #include <iostream>
    #include <string>
    
    enum class SimpleType {
        ST_INVALID, ST_INT64, ST_UINT64, ST_DOUBLE, ST_STRING
    };
    
    #define SIMPLE_TYPE_MACRO(MY_MACRO, ...)                            
        MY_MACRO(SimpleType::ST_INT64,  int64_t,     ##__VA_ARGS__);    
        MY_MACRO(SimpleType::ST_UINT64, uint64_t,    ##__VA_ARGS__);    
        MY_MACRO(SimpleType::ST_DOUBLE, double,      ##__VA_ARGS__);    
        MY_MACRO(SimpleType::ST_STRING, std::string, ##__VA_ARGS__)
    
    template<SimpleType stype>
    struct SimpleType2BuiltinType {
        struct InvalidType {};
        typedef InvalidType BuiltinType;
    };
    
    #define SimpleType2BuiltinTypeTraits(stype, btype, ...) 
        template<>                                          
        struct SimpleType2BuiltinType<stype> {              
            typedef btype BuiltinType;                      
        }
    
    SIMPLE_TYPE_MACRO(SimpleType2BuiltinTypeTraits);
    #undef SimpleType2BuiltinTypeTraits
    
    class Object {
    public:
        Object(SimpleType stype)
            : type_(stype) {}
        ~Object() {}
    
    public:
        virtual SimpleType type() const {
            return type_;
        }
        virtual void type(SimpleType stype) {
            type_ = stype;
        }
    
    private:
        SimpleType type_;
    };
    
    template<class T>
    class BasicType : public Object {
    public:
        BasicType(SimpleType stype)
            : Object(stype) {}
        ~BasicType() {}
    
    public:
        virtual void value(const T& value) {
            value_ = value;
        }
        virtual const T& value() const {
            return value_;
        }
    
    private:
        T value_;
    };
    typedef BasicType<std::string> String;
    
    int main(int argc, char *argv[]) {
        std::string v("foo");
        String foo(SimpleType::ST_STRING);
        foo.value(v);
        Object* object = &foo;
    
        switch (object->type()) {
        #define GET_VALUE(stype, ...)                                               
                case stype: {                                                       
                    typedef SimpleType2BuiltinType<stype>::BuiltinType Type;        
                    BasicType<Type>* typed = static_cast<BasicType<Type>*>(object); 
                    const Type& v = typed->value();                                 
                    std::cout << v << std::endl;                                    
                    break;                                                          
                }
    
        SIMPLE_TYPE_MACRO(GET_VALUE, object);
        #undef GET_VALUE
        default:
            break;
        }
        return 0;
    }
    

  • 相关阅读:
    FreeMarker常用语法学习
    Oracle如何实现创建数据库、备份数据库及数据导出导入的一条龙操作-------sql方式
    Oracle Partition 分区详细总结
    oracle 当中,(+)是什么意思
    SQL中EXISTS的用法
    JS return false 与 return true
    Merge into语句用法及其效率问题
    几种设置表单元素中文本输入框不可编辑的方法
    Oracle存储过程基本语法
    UNIX网络编程——Socket粘包问题
  • 原文地址:https://www.cnblogs.com/rain-blog/p/cpp-type-traits-rtti.html
Copyright © 2011-2022 走看看