- Timestamp:
- 06/01/13 23:50:55 (12 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/io_event.hh
r68 r78 27 27 # undef NV_KEY 28 28 }; 29 NV_REGISTER_NAME( key_code ); 30 29 31 30 // Generate the mouse_code enum 32 31 enum mouse_code … … 36 35 # undef NV_MOUSE 37 36 }; 38 NV_REGISTER_NAME( mouse_code );39 37 40 38 // Generate the io_event_code enum … … 45 43 # undef NV_IO_EVENT 46 44 }; 47 NV_REGISTER_NAME( io_event_code );48 45 49 46 struct key_event … … 67 64 bool pressed; 68 65 }; 69 NV_REGISTER_NAME( key_event );70 66 71 67 struct mouse_button_event … … 80 76 mouse_code code; 81 77 }; 82 NV_REGISTER_NAME( mouse_button_event );83 78 84 79 struct mouse_move_event … … 93 88 mouse_code code; 94 89 }; 95 NV_REGISTER_NAME( mouse_move_event );96 90 97 91 struct system_event … … 101 95 uint32 param2; 102 96 }; 103 NV_REGISTER_NAME( system_event );104 97 105 98 struct io_event … … 114 107 }; 115 108 }; 116 NV_REGISTER_NAME( io_event );117 109 118 110 const char* get_key_name( key_code key ); -
trunk/nv/types.hh
r77 r78 16 16 #include <vector> 17 17 #include <string> 18 19 #define NV_REGISTER_NAME( s ) template <> inline const char* nv::get_type_name<s> () { return #s; }20 18 21 19 namespace nv … … 147 145 template <> struct type_to_enum< mat4 > { static const datatype type = FLOAT_MATRIX_4; }; 148 146 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 192 147 template<typename T> 193 148 struct is_container … … 207 162 }; 208 163 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_string218 {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 ) const228 {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) const239 {240 return hs.hash;241 }242 };243 }244 245 namespace nv246 {247 164 struct type_entry; 248 165 … … 260 177 struct type_field 261 178 { 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; 267 185 268 186 template< typename TOBJECT, typename TFIELD > 269 type_field( hash_stringname, 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 ) 270 188 : 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 ) ) 272 191 , type( nullptr ) 273 192 , flags( 0 ) … … 281 200 282 201 template< typename TOBJECT, typename TFIELD > 283 type_field( hash_stringname, 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 ) 284 203 : 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 ) ) 286 206 , type( nullptr ) 287 207 , flags( 0 ) … … 303 223 struct type_enum 304 224 { 305 hash_string name;225 std::string name; 306 226 int value; 307 type_enum( hash_stringname, int value ) : name(name), value(value) {}227 type_enum( const char* name, int value ) : name(name), value(value) {} 308 228 }; 309 229 … … 318 238 319 239 // Scoped C++ name of the type 320 hash_string name;240 std::string name; 321 241 322 242 // Pointers to the constructor and destructor functions … … 339 259 type_entry& base() 340 260 { 341 base_type = type_db->get_type( get_type_name<TYPE>() )261 base_type = type_db->get_type( typeid(TYPE) ); 342 262 } 343 263 … … 348 268 { 349 269 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; 351 272 field_list.push_back(f); 352 273 } … … 371 292 public: 372 293 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 { 376 296 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() ) 379 299 { 380 300 return *(it->second); … … 388 308 i_type->destructor = DestructObject<TYPE>; 389 309 390 m_ types[name]= i_type;310 m_name_types[name] = i_type; 391 311 m_idx_types[typeid(TYPE)] = i_type; 392 312 return *i_type; 393 313 } 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() ) 399 319 { 400 320 return it->second; … … 402 322 return nullptr; 403 323 } 324 404 325 type_entry* get_type( const type_info& t ) 405 326 { … … 418 339 }; 419 340 420 typedef std::unordered_map< hash_string, type_entry*> type_map;341 typedef std::unordered_map<std::string, type_entry*> type_name_map; 421 342 typedef std::unordered_map<std::type_index, type_entry*> type_info_map; 422 type_ map m_types;343 type_name_map m_name_types; 423 344 type_info_map m_idx_types; 424 345 }; -
trunk/src/io_event.cc
r67 r78 49 49 # undef NV_KEY 50 50 }; 51 db->create_type<key_code>( ).enums( key_enums );51 db->create_type<key_code>("key_code").enums( key_enums ); 52 52 53 53 type_enum mouse_enums[] = { … … 56 56 # undef NV_MOUSE 57 57 }; 58 db->create_type<mouse_code>( ).enums( mouse_enums );58 db->create_type<mouse_code>("mouse_code").enums( mouse_enums ); 59 59 60 60 type_enum io_event_enums[] = { … … 63 63 # undef NV_IO_EVENT 64 64 }; 65 db->create_type<io_event_code>( ).enums( io_event_enums );65 db->create_type<io_event_code>("event_code").enums( io_event_enums ); 66 66 67 67 type_field key_fields[] = { … … 73 73 type_field( "pressed", &key_event::pressed ), 74 74 }; 75 db->create_type<key_event>( ).fields( key_fields );75 db->create_type<key_event>("key_event").fields( key_fields ); 76 76 77 77 type_field mouse_button_fields[] = { … … 82 82 type_field( "code", &mouse_button_event::code ), 83 83 }; 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 ); 85 85 86 86 type_field mouse_move_fields[] = { … … 92 92 type_field( "code", &mouse_move_event::code ), 93 93 }; 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 ); 95 95 96 96 type_field system_fields[] = { … … 99 99 type_field( "param2", &system_event::param2 ), 100 100 }; 101 db->create_type<system_event>( ).fields( system_fields );101 db->create_type<system_event>("system_event").fields( system_fields ); 102 102 } -
trunk/src/lua/lua_state.cc
r77 r78 296 296 if (!t) return ref_none; 297 297 stack_guard guard( this ); 298 lua_getglobal( L, t->name. text);298 lua_getglobal( L, t->name.c_str() ); 299 299 if ( lua_isnil( L, -1 ) ) 300 300 { 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!" ); 302 302 } 303 303 deep_pointer_copy( -1, o ); -
trunk/src/object.cc
r77 r78 192 192 type_field("children" , &object::m_children).flag( TF_READONLY ), 193 193 }; 194 db->create_type<object>( ).fields(fields);194 db->create_type<object>("object").fields(fields); 195 195 } -
trunk/tests/lualib_test/lualib_test.cc
r76 r78 17 17 }; 18 18 19 NV_REGISTER_NAME( test_struct )20 21 19 int main(int, char* []) 22 20 { 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 34 21 nv::logger log(nv::LOG_TRACE); 35 22 log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
Note: See TracChangeset
for help on using the changeset viewer.