Changeset 78


Ignore:
Timestamp:
06/01/13 23:50:55 (12 years ago)
Author:
epyon
Message:
  • types.hh - basing totally on typeid now
  • types.hh - removed hash_string and name registration defines (get_type_name template)
  • types.hh - name is defined at registration via create_type
  • types.hh - overall the code is much cleaner now
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/io_event.hh

    r68 r78  
    2727#       undef NV_KEY
    2828        };
    29         NV_REGISTER_NAME( key_code );
    30 
     29       
    3130        // Generate the mouse_code enum
    3231        enum mouse_code
     
    3635#       undef NV_MOUSE
    3736        };
    38         NV_REGISTER_NAME( mouse_code );
    3937
    4038        // Generate the io_event_code enum
     
    4543#       undef NV_IO_EVENT
    4644        };
    47         NV_REGISTER_NAME( io_event_code );
    4845
    4946        struct key_event
     
    6764                bool pressed;
    6865        };
    69         NV_REGISTER_NAME( key_event );
    7066
    7167        struct mouse_button_event
     
    8076                mouse_code code;
    8177        };
    82         NV_REGISTER_NAME( mouse_button_event );
    8378
    8479        struct mouse_move_event
     
    9388                mouse_code code;
    9489        };
    95         NV_REGISTER_NAME( mouse_move_event );
    9690
    9791        struct system_event
     
    10195                uint32 param2;
    10296        };
    103         NV_REGISTER_NAME( system_event );
    10497
    10598        struct io_event
     
    114107                };
    115108        };
    116         NV_REGISTER_NAME( io_event );
    117109
    118110        const char* get_key_name( key_code key );
  • trunk/nv/types.hh

    r77 r78  
    1616#include <vector>
    1717#include <string>
    18 
    19 #define NV_REGISTER_NAME( s ) template <> inline const char* nv::get_type_name<s>   () { return #s; }
    2018
    2119namespace nv
     
    147145        template <> struct type_to_enum< mat4 > { static const datatype type = FLOAT_MATRIX_4; };
    148146
    149         template <typename TYPE>
    150     inline const char* get_type_name()
    151     {
    152         static_assert(sizeof(TYPE) == 0, "Type not implemented!");
    153         return NULL;
    154     }
    155 
    156         template <> inline const char* get_type_name<int>   () { return "sint"; }
    157     template <> inline const char* get_type_name<sint8> () { return "sint8"; }
    158     template <> inline const char* get_type_name<sint16>() { return "sint16"; }
    159     template <> inline const char* get_type_name<sint32>() { return "sint32"; }
    160     template <> inline const char* get_type_name<sint64>() { return "sint64"; }
    161 
    162         template <> inline const char* get_type_name<unsigned int>() { return "uint"; }
    163         template <> inline const char* get_type_name<uint8> ()       { return "uint8"; }
    164     template <> inline const char* get_type_name<uint16>()       { return "uint16"; }
    165     template <> inline const char* get_type_name<uint32>()       { return "uint32"; }
    166     template <> inline const char* get_type_name<uint64>()       { return "uint64"; }
    167 
    168         template <> inline const char* get_type_name<f32> () { return "f32"; }
    169         template <> inline const char* get_type_name<f64> () { return "f64"; }
    170 
    171         template <> inline const char* get_type_name< vec2 > () { return "vec2"; }
    172         template <> inline const char* get_type_name< vec3 > () { return "vec3"; }
    173         template <> inline const char* get_type_name< vec4 > () { return "vec4"; }
    174 
    175         template <> inline const char* get_type_name< ivec2 > () { return "ivec2"; }
    176         template <> inline const char* get_type_name< ivec3 > () { return "ivec3"; }
    177         template <> inline const char* get_type_name< ivec4 > () { return "ivec4"; }
    178 
    179         template <> inline const char* get_type_name< i8vec2 > () { return "i8vec2"; }
    180         template <> inline const char* get_type_name< i8vec3 > () { return "i8vec3"; }
    181         template <> inline const char* get_type_name< i8vec4 > () { return "i8vec4"; }
    182 
    183         template <> inline const char* get_type_name< mat2 > () { return "mat2"; }
    184         template <> inline const char* get_type_name< mat3 > () { return "mat3"; }
    185         template <> inline const char* get_type_name< mat4 > () { return "mat4"; }
    186 
    187         template <> inline const char* get_type_name<bool> () { return "bool"; }
    188 
    189         template <> inline const char* get_type_name<std::string> () { return "string"; }
    190         template <> inline const char* get_type_name<object>      () { return "object"; }
    191 
    192147        template<typename T>
    193148        struct is_container
     
    207162        };
    208163
    209 
    210         template <typename TYPE>
    211     std::size_t get_type_id()
    212     {
    213         static std::size_t type_id = std::hash<std::string>()(std::string(get_type_name<TYPE>()));
    214         return type_id;
    215     };
    216 
    217         struct hash_string
    218     {
    219         std::size_t hash;
    220         const char* text;
    221                 hash_string() {}
    222                 hash_string( const char* a_text ) :
    223                         text( a_text )
    224                 {
    225                         hash = std::hash<std::string>()( std::string( a_text ) );
    226                 }
    227                 inline bool operator == (const hash_string& hs ) const
    228                 {
    229                         return hs.hash == hash;
    230                 }
    231     };
    232 
    233 }
    234 
    235 namespace std {
    236         template<>
    237         struct hash<nv::hash_string> {
    238                 size_t operator()(const nv::hash_string &hs) const
    239                 {
    240                         return hs.hash;
    241                 }
    242         };
    243 }
    244 
    245 namespace nv
    246 {
    247164        struct type_entry;
    248165
     
    260177        struct type_field
    261178        {
    262                 hash_string  name;      //< name of the field
    263                 hash_string  type_name; //< name of the type of the field
    264                 type_entry*  type;      //< pointer to field type
    265                 unsigned int flags;     //< flags
    266                 size_t       offset;
     179                std::string      name;      //< name of the field
     180                std::string      type_name; //< name of the type of the field
     181                const type_info* type_inf;  //< typeinfo for later retrieval of type
     182                type_entry*      type;      //< pointer to field type
     183                unsigned int     flags;     //< flags
     184                size_t           offset;
    267185
    268186                template< typename TOBJECT, typename TFIELD >
    269                 type_field( hash_string name, TFIELD TOBJECT::*field, typename std::enable_if< is_container<TFIELD>::value, void* >::type = nullptr )
     187                type_field( const char* name, TFIELD TOBJECT::*field, typename std::enable_if< is_container<TFIELD>::value, void* >::type = nullptr )
    270188                        : name(name)
    271                         , type_name( get_type_name< std::remove_pointer<typename TFIELD::value_type>::type >() )
     189                        , type_name()
     190                        , type_inf( &typeid( std::remove_pointer<typename TFIELD::value_type>::type ) )
    272191                        , type( nullptr )
    273192                        , flags( 0 )
     
    281200
    282201                template< typename TOBJECT, typename TFIELD >
    283                 type_field( hash_string name, TFIELD TOBJECT::*field, typename std::enable_if< !is_container<TFIELD>::value, void* >::type = nullptr )
     202                type_field( const char* name, TFIELD TOBJECT::*field, typename std::enable_if< !is_container<TFIELD>::value, void* >::type = nullptr )
    284203                        : name(name)
    285                         , type_name( get_type_name< std::remove_pointer<TFIELD>::type >() )
     204                        , type_name()
     205                        , type_inf( &typeid( std::remove_pointer<TFIELD>::type ) )
    286206                        , type( nullptr )
    287207                        , flags( 0 )
     
    303223        struct type_enum
    304224        {
    305                 hash_string name;
     225                std::string name;
    306226                int         value;
    307                 type_enum( hash_string name, int value ) : name(name), value(value) {}
     227                type_enum( const char* name, int value ) : name(name), value(value) {}
    308228        };
    309229
     
    318238
    319239        // Scoped C++ name of the type
    320         hash_string name;
     240        std::string name;
    321241 
    322242        // Pointers to the constructor and destructor functions
     
    339259                type_entry& base()
    340260                {
    341                         base_type = type_db->get_type( get_type_name<TYPE>() )
     261                        base_type = type_db->get_type( typeid(TYPE) );
    342262                }
    343263
     
    348268                        {
    349269                                type_field f = init_fields[i];
    350                                 f.type = type_db->get_type(f.type_name);
     270                                f.type = type_db->get_type(*(f.type_inf));
     271                                f.type_name = f.type->name;
    351272                                field_list.push_back(f);
    352273                        }
     
    371292    public:
    372293                template< typename TYPE >
    373         type_entry& create_type()
    374                 {
    375                         hash_string name( get_type_name<TYPE>() );
     294        type_entry& create_type( const char* name )
     295                {
    376296                        type_entry* i_type = nullptr;
    377                         type_map::iterator it = m_types.find( name );
    378                         if ( it != m_types.end() )
     297                        type_name_map::iterator it = m_name_types.find( name );
     298                        if ( it != m_name_types.end() )
    379299                        {
    380300                                return *(it->second);
     
    388308                        i_type->destructor  = DestructObject<TYPE>;
    389309
    390                         m_types[name]             = i_type;
     310                        m_name_types[name]        = i_type;
    391311                        m_idx_types[typeid(TYPE)] = i_type;
    392312                        return *i_type;
    393313                }
    394                 // TODO: delete?
    395         type_entry* get_type( hash_string name )
    396                 {
    397                         type_map::iterator it = m_types.find( name );
    398                         if ( it != m_types.end() )
     314
     315                type_entry* get_type( const std::string name )
     316                {
     317                        type_name_map::iterator it = m_name_types.find( name );
     318                        if ( it != m_name_types.end() )
    399319                        {
    400320                                return it->second;
     
    402322                        return nullptr;
    403323                }
     324
    404325                type_entry* get_type( const type_info& t )
    405326                {
     
    418339                };
    419340
    420         typedef std::unordered_map<hash_string, type_entry*>     type_map;
     341        typedef std::unordered_map<std::string, type_entry*>     type_name_map;
    421342                typedef std::unordered_map<std::type_index, type_entry*> type_info_map;
    422         type_map      m_types;
     343        type_name_map m_name_types;
    423344                type_info_map m_idx_types;
    424345        };
  • trunk/src/io_event.cc

    r67 r78  
    4949#       undef NV_KEY
    5050        };
    51         db->create_type<key_code>().enums( key_enums );
     51        db->create_type<key_code>("key_code").enums( key_enums );
    5252
    5353        type_enum mouse_enums[] = {
     
    5656#       undef NV_MOUSE
    5757        };
    58         db->create_type<mouse_code>().enums( mouse_enums );
     58        db->create_type<mouse_code>("mouse_code").enums( mouse_enums );
    5959
    6060        type_enum io_event_enums[] = {
     
    6363#       undef NV_IO_EVENT
    6464        };
    65         db->create_type<io_event_code>().enums( io_event_enums );
     65        db->create_type<io_event_code>("event_code").enums( io_event_enums );
    6666
    6767        type_field key_fields[] = {
     
    7373                type_field( "pressed", &key_event::pressed ),
    7474        };
    75         db->create_type<key_event>().fields( key_fields );
     75        db->create_type<key_event>("key_event").fields( key_fields );
    7676
    7777        type_field mouse_button_fields[] = {
     
    8282                type_field( "code",    &mouse_button_event::code ),
    8383        };
    84         db->create_type<mouse_button_event>().fields( mouse_button_fields );
     84        db->create_type<mouse_button_event>("mouse_button_event").fields( mouse_button_fields );
    8585
    8686        type_field mouse_move_fields[] = {
     
    9292                type_field( "code",    &mouse_move_event::code ),
    9393        };
    94         db->create_type<mouse_move_event>().fields( mouse_move_fields );
     94        db->create_type<mouse_move_event>("mouse_move_event").fields( mouse_move_fields );
    9595
    9696        type_field system_fields[] = {
     
    9999                type_field( "param2",   &system_event::param2 ),
    100100        };
    101         db->create_type<system_event>().fields( system_fields );
     101        db->create_type<system_event>("system_event").fields( system_fields );
    102102}
  • trunk/src/lua/lua_state.cc

    r77 r78  
    296296        if (!t) return ref_none;
    297297        stack_guard guard( this );
    298         lua_getglobal( L, t->name.text );
     298        lua_getglobal( L, t->name.c_str() );
    299299        if ( lua_isnil( L, -1 ) )
    300300        {
    301                 NV_THROW( runtime_error, std::string( t->name.text ) + " type not registered!" );
     301                NV_THROW( runtime_error, std::string( t->name ) + " type not registered!" );
    302302        }
    303303        deep_pointer_copy( -1, o );
  • trunk/src/object.cc

    r77 r78  
    192192                type_field("children"   , &object::m_children).flag( TF_READONLY ),
    193193        };
    194         db->create_type<object>().fields(fields);
     194        db->create_type<object>("object").fields(fields);
    195195}
  • trunk/tests/lualib_test/lualib_test.cc

    r76 r78  
    1717};
    1818
    19 NV_REGISTER_NAME( test_struct )
    20 
    2119int main(int, char* [])
    2220{
    23         nv::type_database db;
    24         nv::object::register_type( &db );
    25 
    26         db.create_type<int>();
    27         db.create_type<std::string>();
    28         nv::type_field fields[] = {
    29                 nv::type_field("f", &test_struct::f ),
    30                 nv::type_field("i", &test_struct::i ),
    31         };
    32         db.create_type<test_struct>().fields( fields );
    33 
    3421        nv::logger log(nv::LOG_TRACE);
    3522        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
Note: See TracChangeset for help on using the changeset viewer.