source: trunk/nv/types.hh @ 54

Last change on this file since 54 was 54, checked in by epyon, 12 years ago
  • several enhancements to the RTTI system
File size: 9.9 KB
Line 
1// Copyright (C) 2012 Kornel Kisielewicz
2// This file is part of NV Libraries.
3// For conditions of distribution and use, see copyright notice in nv.hh
4
5#ifndef NV_TYPES_HH
6#define NV_TYPES_HH
7
8#include <glm/glm.hpp>
9#include <nv/common.hh>
10#include <type_traits>
11#include <utility>
12#include <unordered_map>
13#include <vector>
14#include <string>
15
16namespace nv
17{
18        enum type
19        {
20                INT,
21                BYTE,
22                SHORT,
23                UINT,
24                UBYTE,
25                USHORT,
26                FLOAT,
27                FLOAT_VECTOR_2,
28                FLOAT_VECTOR_3,
29                FLOAT_VECTOR_4,
30                FLOAT_MATRIX_2,
31                FLOAT_MATRIX_3,
32                FLOAT_MATRIX_4,
33                INT_VECTOR_2,
34                INT_VECTOR_3,
35                INT_VECTOR_4
36        };
37
38        typedef glm::vec2 vec2;
39        typedef glm::vec3 vec3;
40        typedef glm::vec4 vec4;
41
42        typedef glm::ivec2 ivec2;
43        typedef glm::ivec3 ivec3;
44        typedef glm::ivec4 ivec4;
45
46        typedef glm::mat2 mat2;
47        typedef glm::mat3 mat3;
48        typedef glm::mat4 mat4;
49
50        template < type EnumType > struct enum_to_type {};
51
52        template <> struct enum_to_type< INT >   { typedef int type; };
53        template <> struct enum_to_type< UINT >  { typedef unsigned int type; };
54        template <> struct enum_to_type< SHORT > { typedef short type; };
55        template <> struct enum_to_type< USHORT >{ typedef unsigned short type; };
56        template <> struct enum_to_type< BYTE >  { typedef char type; };
57        template <> struct enum_to_type< UBYTE > { typedef unsigned char type; };
58        template <> struct enum_to_type< FLOAT > { typedef f32 type; };
59
60        template <> struct enum_to_type< FLOAT_VECTOR_2 > { typedef vec2 type; };
61        template <> struct enum_to_type< FLOAT_VECTOR_3 > { typedef vec3 type; };
62        template <> struct enum_to_type< FLOAT_VECTOR_4 > { typedef vec4 type; };
63
64        template <> struct enum_to_type< INT_VECTOR_2 > { typedef ivec2 type; };
65        template <> struct enum_to_type< INT_VECTOR_3 > { typedef ivec3 type; };
66        template <> struct enum_to_type< INT_VECTOR_4 > { typedef ivec4 type; };
67
68        template <> struct enum_to_type< FLOAT_MATRIX_2 > { typedef mat2 type; };
69        template <> struct enum_to_type< FLOAT_MATRIX_3 > { typedef mat3 type; };
70        template <> struct enum_to_type< FLOAT_MATRIX_4 > { typedef mat4 type; };
71
72        template < typename TYPE > struct type_to_enum {};
73
74        template <> struct type_to_enum< int >           { static const type type = INT; };
75        template <> struct type_to_enum< unsigned int >  { static const type type = UINT; };
76        template <> struct type_to_enum< short >         { static const type type = SHORT; };
77        template <> struct type_to_enum< unsigned short >{ static const type type = USHORT; };
78        template <> struct type_to_enum< char >          { static const type type = BYTE; };
79        template <> struct type_to_enum< unsigned char > { static const type type = UBYTE; };
80        template <> struct type_to_enum< f32 > { static const type type = FLOAT; };
81
82        template <> struct type_to_enum< vec2 > { static const type type = FLOAT_VECTOR_2; };
83        template <> struct type_to_enum< vec3 > { static const type type = FLOAT_VECTOR_3; };
84        template <> struct type_to_enum< vec4 > { static const type type = FLOAT_VECTOR_4; };
85
86        template <> struct type_to_enum< ivec2 > { static const type type = INT_VECTOR_2; };
87        template <> struct type_to_enum< ivec3 > { static const type type = INT_VECTOR_3; };
88        template <> struct type_to_enum< ivec4 > { static const type type = INT_VECTOR_4; };
89
90        template <> struct type_to_enum< mat2 > { static const type type = FLOAT_MATRIX_2; };
91        template <> struct type_to_enum< mat3 > { static const type type = FLOAT_MATRIX_3; };
92        template <> struct type_to_enum< mat4 > { static const type type = FLOAT_MATRIX_4; };
93
94        template <typename TYPE>
95    inline const char* get_type_name()
96    {
97        static_assert< FALSE >( "Type not implemented!" );
98    }
99
100#define NV_REGISTER_NAME( s ) template <> inline const char* nv::get_type_name<s>   () { return #s; }
101
102        template <> inline const char* get_type_name<int>   () { return "sint"; }
103    template <> inline const char* get_type_name<sint8> () { return "sint8"; }
104    template <> inline const char* get_type_name<sint16>() { return "sint16"; }
105    template <> inline const char* get_type_name<sint32>() { return "sint32"; }
106    template <> inline const char* get_type_name<sint64>() { return "sint64"; }
107
108        template <> inline const char* get_type_name<unsigned int>() { return "uint"; }
109        template <> inline const char* get_type_name<uint8> ()       { return "uint8"; }
110    template <> inline const char* get_type_name<uint16>()       { return "uint16"; }
111    template <> inline const char* get_type_name<uint32>()       { return "uint32"; }
112    template <> inline const char* get_type_name<uint64>()       { return "uint64"; }
113
114        template <> inline const char* get_type_name<f32> () { return "f32"; }
115        template <> inline const char* get_type_name<f64> () { return "f64"; }
116
117        template <> inline const char* get_type_name< vec2 > () { return "vec2"; }
118        template <> inline const char* get_type_name< vec3 > () { return "vec3"; }
119        template <> inline const char* get_type_name< vec4 > () { return "vec4"; }
120
121        template <> inline const char* get_type_name< ivec2 > () { return "ivec2"; }
122        template <> inline const char* get_type_name< ivec3 > () { return "ivec3"; }
123        template <> inline const char* get_type_name< ivec4 > () { return "ivec4"; }
124
125        template <> inline const char* get_type_name< mat2 > () { return "mat2"; }
126        template <> inline const char* get_type_name< mat3 > () { return "mat3"; }
127        template <> inline const char* get_type_name< mat4 > () { return "mat4"; }
128
129        template <> inline const char* get_type_name<bool> () { return "bool"; }
130
131        template <> inline const char* get_type_name<std::string> () { return "string"; }
132
133        template <typename TYPE>
134    std::size_t get_type_id()
135    {
136        static std::size_t type_id = std::hash<std::string>()(std::string(get_type_name<TYPE>()));
137        return type_id;
138    };
139
140        struct hash_string
141    {
142        std::size_t hash;
143        const char* text;
144                hash_string() {}
145                hash_string( const char* a_text ) :
146                        text( a_text )
147                {
148                        hash = std::hash<std::string>()( std::string( a_text ) );
149                }
150                inline bool operator == (const hash_string& hs ) const
151                {
152                        return hs.hash == hash;
153                }
154    };
155
156}
157
158namespace std {
159        template<>
160        struct hash<nv::hash_string> {
161                size_t operator()(const nv::hash_string &hs) const
162                {
163                        return hs.hash;
164                }
165        };
166}
167
168namespace nv
169{
170        struct type_entry;
171
172        struct type_field
173        {
174                enum flags
175                {
176                        FPOINTER      = 0x01, //< field is a pointer
177                        FNOSERIALIZE  = 0x02, //< ignore during serialization
178                        FINVISIBLE    = 0x04, //< field invisible to API
179                        FREADONLY     = 0x08, //< read only field
180                        FSIMPLETYPE   = 0x10, //< raw binary I/O possible
181                        FOWNED        = 0x20,
182                };
183                hash_string  name;      //< name of the field
184                hash_string  type_name; //< name of the type of the field
185                type_entry*  type;      //< pointer to field type
186                unsigned int flags;     //< flags
187                size_t       offset;
188
189                template< typename TOBJECT, typename TFIELD>
190                type_field( hash_string name, TFIELD TOBJECT::*field )
191                        : name(name)
192                        , type_name( get_type_name< std::remove_pointer<TFIELD>::type >() )
193                        , type( nullptr )
194                        , flags( 0 )
195                        , offset( offsetof( TOBJECT, *field ) )
196                {
197                        flags =
198                                ( std::is_pointer<TFIELD>::value ? FPOINTER : 0 ) |
199                                ( std::is_pod<TFIELD>::value ? FSIMPLETYPE : 0 );
200                }
201        };
202
203        struct type_enum
204        {
205                hash_string name;
206                int         value;
207                type_enum( hash_string name, int value ) : name(name), value(value) {}
208        };
209
210    struct type_entry
211    {
212                 // Function types for the constructor and destructor of registered types
213            typedef void (*constructor_func)(void*);
214            typedef void (*destructor_func)(void*);
215
216                // Parent type database
217        class type_database* type_db;
218
219        // Scoped C++ name of the type
220        hash_string name;
221 
222        // Pointers to the constructor and destructor functions
223        constructor_func constructor;
224        destructor_func  destructor;
225 
226        // Result of sizeof(type) operation
227        size_t size;
228
229                // Base type
230                type_entry* base_type;
231
232                // Field list
233                std::vector<type_field> field_list;
234
235                // Enum list
236                std::vector<type_enum> enum_list;
237
238                template <int TYPE>
239                type_entry& base()
240                {
241                        base_type = type_db->get_type( get_type_name<TYPE>() )
242                }
243
244                template <int SIZE>
245                type_entry& fields( type_field (&init_fields)[SIZE] )
246                {
247                        for (int i = 0; i < SIZE; i++)
248                        {
249                                type_field f = init_fields[i];
250                                f.type = type_db->get_type(f.type_name);
251                                field_list.push_back(f);
252                        }
253                        return *this;
254                }
255
256                template <int SIZE>
257                type_entry& enums( type_enum (&init_enums)[SIZE] )
258                {
259                        for (int i = 0; i < SIZE; i++)
260                        {
261                                enum_list.push_back( init_enums[i] )
262                        }
263                        return *this;
264                }
265    };
266
267    class type_database
268    {
269    public:
270                template< typename TYPE >
271        type_entry& create_type()
272                {
273                        hash_string name( get_type_name<TYPE>() );
274                        type_entry* i_type = nullptr;
275                        type_map::iterator it = m_types.find( name );
276                        if ( it != m_types.end() )
277                        {
278                                return *(it->second);
279                        }
280                        i_type          = new type_entry;
281                        i_type->type_db = this;
282                        i_type->name    = name;
283                        i_type->size    = sizeof(TYPE);
284                       
285                        i_type->constructor = ConstructObject<TYPE>;
286                        i_type->destructor  = DestructObject<TYPE>;
287
288                        m_types[name] = i_type;
289                        return *i_type;
290                }
291
292        type_entry* get_type( hash_string name )
293                {
294                        type_map::iterator it = m_types.find( name );
295                        if ( it != m_types.end() )
296                        {
297                                return it->second;
298                        }
299                        return nullptr;
300                }
301    private:
302        typedef std::unordered_map<hash_string, type_entry*> type_map;
303        type_map m_types;
304        };
305
306        template <typename TYPE> void ConstructObject(void* object)
307    {
308        // Use placement new to call the constructor
309        new (object) TYPE;
310    }
311    template <typename TYPE> void DestructObject(void* object)
312    {
313        // Explicit call of the destructor
314        ((TYPE*)object)->TYPE::~TYPE();
315    }
316
317}
318
319#endif NV_TYPES_HH
Note: See TracBrowser for help on using the repository browser.