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 <utility>
|
---|
11 | #include <unordered_map>
|
---|
12 | #include <string>
|
---|
13 |
|
---|
14 | namespace nv
|
---|
15 | {
|
---|
16 | enum type
|
---|
17 | {
|
---|
18 | INT,
|
---|
19 | BYTE,
|
---|
20 | SHORT,
|
---|
21 | UINT,
|
---|
22 | UBYTE,
|
---|
23 | USHORT,
|
---|
24 | FLOAT,
|
---|
25 | FLOAT_VECTOR_2,
|
---|
26 | FLOAT_VECTOR_3,
|
---|
27 | FLOAT_VECTOR_4,
|
---|
28 | FLOAT_MATRIX_2,
|
---|
29 | FLOAT_MATRIX_3,
|
---|
30 | FLOAT_MATRIX_4,
|
---|
31 | INT_VECTOR_2,
|
---|
32 | INT_VECTOR_3,
|
---|
33 | INT_VECTOR_4
|
---|
34 | };
|
---|
35 |
|
---|
36 | typedef glm::vec2 vec2;
|
---|
37 | typedef glm::vec3 vec3;
|
---|
38 | typedef glm::vec4 vec4;
|
---|
39 |
|
---|
40 | typedef glm::ivec2 ivec2;
|
---|
41 | typedef glm::ivec3 ivec3;
|
---|
42 | typedef glm::ivec4 ivec4;
|
---|
43 |
|
---|
44 | typedef glm::mat2 mat2;
|
---|
45 | typedef glm::mat3 mat3;
|
---|
46 | typedef glm::mat4 mat4;
|
---|
47 |
|
---|
48 | template < type EnumType > struct enum_to_type {};
|
---|
49 |
|
---|
50 | template <> struct enum_to_type< INT > { typedef int type; };
|
---|
51 | template <> struct enum_to_type< UINT > { typedef unsigned int type; };
|
---|
52 | template <> struct enum_to_type< SHORT > { typedef short type; };
|
---|
53 | template <> struct enum_to_type< USHORT >{ typedef unsigned short type; };
|
---|
54 | template <> struct enum_to_type< BYTE > { typedef char type; };
|
---|
55 | template <> struct enum_to_type< UBYTE > { typedef unsigned char type; };
|
---|
56 | template <> struct enum_to_type< FLOAT > { typedef f32 type; };
|
---|
57 |
|
---|
58 | template <> struct enum_to_type< FLOAT_VECTOR_2 > { typedef vec2 type; };
|
---|
59 | template <> struct enum_to_type< FLOAT_VECTOR_3 > { typedef vec3 type; };
|
---|
60 | template <> struct enum_to_type< FLOAT_VECTOR_4 > { typedef vec4 type; };
|
---|
61 |
|
---|
62 | template <> struct enum_to_type< INT_VECTOR_2 > { typedef ivec2 type; };
|
---|
63 | template <> struct enum_to_type< INT_VECTOR_3 > { typedef ivec3 type; };
|
---|
64 | template <> struct enum_to_type< INT_VECTOR_4 > { typedef ivec4 type; };
|
---|
65 |
|
---|
66 | template <> struct enum_to_type< FLOAT_MATRIX_2 > { typedef mat2 type; };
|
---|
67 | template <> struct enum_to_type< FLOAT_MATRIX_3 > { typedef mat3 type; };
|
---|
68 | template <> struct enum_to_type< FLOAT_MATRIX_4 > { typedef mat4 type; };
|
---|
69 |
|
---|
70 | template < typename Type > struct type_to_enum {};
|
---|
71 |
|
---|
72 | template <> struct type_to_enum< int > { static const type type = INT; };
|
---|
73 | template <> struct type_to_enum< unsigned int > { static const type type = UINT; };
|
---|
74 | template <> struct type_to_enum< short > { static const type type = SHORT; };
|
---|
75 | template <> struct type_to_enum< unsigned short >{ static const type type = USHORT; };
|
---|
76 | template <> struct type_to_enum< char > { static const type type = BYTE; };
|
---|
77 | template <> struct type_to_enum< unsigned char > { static const type type = UBYTE; };
|
---|
78 | template <> struct type_to_enum< f32 > { static const type type = FLOAT; };
|
---|
79 |
|
---|
80 | template <> struct type_to_enum< vec2 > { static const type type = FLOAT_VECTOR_2; };
|
---|
81 | template <> struct type_to_enum< vec3 > { static const type type = FLOAT_VECTOR_3; };
|
---|
82 | template <> struct type_to_enum< vec4 > { static const type type = FLOAT_VECTOR_4; };
|
---|
83 |
|
---|
84 | template <> struct type_to_enum< ivec2 > { static const type type = INT_VECTOR_2; };
|
---|
85 | template <> struct type_to_enum< ivec3 > { static const type type = INT_VECTOR_3; };
|
---|
86 | template <> struct type_to_enum< ivec4 > { static const type type = INT_VECTOR_4; };
|
---|
87 |
|
---|
88 | template <> struct type_to_enum< mat2 > { static const type type = FLOAT_MATRIX_2; };
|
---|
89 | template <> struct type_to_enum< mat3 > { static const type type = FLOAT_MATRIX_3; };
|
---|
90 | template <> struct type_to_enum< mat4 > { static const type type = FLOAT_MATRIX_4; };
|
---|
91 |
|
---|
92 | template <typename TYPE>
|
---|
93 | inline const char* get_type_name()
|
---|
94 | {
|
---|
95 | static_assert< FALSE >( "Type not implemented!" );
|
---|
96 | }
|
---|
97 |
|
---|
98 | template <> inline const char* get_type_name<sint8> () { return "sint8"; }
|
---|
99 | template <> inline const char* get_type_name<sint16>() { return "sint16"; }
|
---|
100 | template <> inline const char* get_type_name<sint32>() { return "sint32"; }
|
---|
101 | template <> inline const char* get_type_name<sint64>() { return "sint64"; }
|
---|
102 |
|
---|
103 | template <> inline const char* get_type_name<uint8> () { return "uint8"; }
|
---|
104 | template <> inline const char* get_type_name<uint16>() { return "uint16"; }
|
---|
105 | template <> inline const char* get_type_name<uint32>() { return "uint32"; }
|
---|
106 | template <> inline const char* get_type_name<uint64>() { return "uint64"; }
|
---|
107 |
|
---|
108 | template <> inline const char* get_type_name<f32> () { return "f32"; }
|
---|
109 | template <> inline const char* get_type_name<f64> () { return "f64"; }
|
---|
110 |
|
---|
111 | template <> inline const char* get_type_name<bool> () { return "bool"; }
|
---|
112 |
|
---|
113 | template <> inline const char* get_type_name<std::string> () { return "string"; }
|
---|
114 |
|
---|
115 | template <typename TYPE>
|
---|
116 | std::size_t get_type_id()
|
---|
117 | {
|
---|
118 | static std::size_t type_id = std::hash<std::string>()(std::string(get_type_name<TYPE>()));
|
---|
119 | return type_id;
|
---|
120 | };
|
---|
121 |
|
---|
122 | struct hash_string
|
---|
123 | {
|
---|
124 | std::size_t hash;
|
---|
125 | const char* text;
|
---|
126 | hash_string() {}
|
---|
127 | hash_string( const char* a_text ) :
|
---|
128 | text( a_text )
|
---|
129 | {
|
---|
130 | hash = std::hash<std::string>()( std::string( a_text ) );
|
---|
131 | }
|
---|
132 | inline bool operator == (const hash_string& hs ) const
|
---|
133 | {
|
---|
134 | return hs.hash == hash;
|
---|
135 | }
|
---|
136 | };
|
---|
137 |
|
---|
138 | }
|
---|
139 |
|
---|
140 | namespace std {
|
---|
141 | template<>
|
---|
142 | struct hash<nv::hash_string> {
|
---|
143 | size_t operator()(const nv::hash_string &hs) const
|
---|
144 | {
|
---|
145 | return hs.hash;
|
---|
146 | }
|
---|
147 | };
|
---|
148 | }
|
---|
149 |
|
---|
150 | namespace nv
|
---|
151 | {
|
---|
152 |
|
---|
153 | struct type_entry
|
---|
154 | {
|
---|
155 | // Function types for the constructor and destructor of registered types
|
---|
156 | typedef void (*constructor_func)(void*);
|
---|
157 | typedef void (*destructor_func)(void*);
|
---|
158 |
|
---|
159 | // Parent type database
|
---|
160 | class type_database* type_db;
|
---|
161 |
|
---|
162 | // Scoped C++ name of the type
|
---|
163 | hash_string name;
|
---|
164 |
|
---|
165 | // Pointers to the constructor and destructor functions
|
---|
166 | constructor_func constructor;
|
---|
167 | destructor_func destructor;
|
---|
168 |
|
---|
169 | // Result of sizeof(type) operation
|
---|
170 | size_t size;
|
---|
171 | };
|
---|
172 |
|
---|
173 | class type_database
|
---|
174 | {
|
---|
175 | public:
|
---|
176 | template< typename TYPE >
|
---|
177 | type_entry& create_type()
|
---|
178 | {
|
---|
179 | hash_string name( get_type_name<TYPE>() );
|
---|
180 | type_entry* i_type = nullptr;
|
---|
181 | type_map::iterator it = m_types.find( name );
|
---|
182 | if ( it != m_types.end() )
|
---|
183 | {
|
---|
184 | return *(it->second);
|
---|
185 | }
|
---|
186 | i_type = new type_entry;
|
---|
187 | i_type->type_db = this;
|
---|
188 | i_type->name = name;
|
---|
189 | i_type->size = sizeof(TYPE);
|
---|
190 |
|
---|
191 | i_type->constructor = ConstructObject<TYPE>;
|
---|
192 | i_type->destructor = DestructObject<TYPE>;
|
---|
193 |
|
---|
194 | m_types[name] = i_type;
|
---|
195 | return *i_type;
|
---|
196 | }
|
---|
197 | type_entry* get_type( hash_string name )
|
---|
198 | {
|
---|
199 | type_map::iterator it = m_types.find( name );
|
---|
200 | if ( it != m_types.end() )
|
---|
201 | {
|
---|
202 | return it->second;
|
---|
203 | }
|
---|
204 | return nullptr;
|
---|
205 | }
|
---|
206 | private:
|
---|
207 | typedef std::unordered_map<hash_string, type_entry*> type_map;
|
---|
208 | type_map m_types;
|
---|
209 | };
|
---|
210 |
|
---|
211 | template <typename TYPE> void ConstructObject(void* object)
|
---|
212 | {
|
---|
213 | // Use placement new to call the constructor
|
---|
214 | new (object) TYPE;
|
---|
215 | }
|
---|
216 | template <typename TYPE> void DestructObject(void* object)
|
---|
217 | {
|
---|
218 | // Explicit call of the destructor
|
---|
219 | ((TYPE*)object)->TYPE::~TYPE();
|
---|
220 | }
|
---|
221 |
|
---|
222 | }
|
---|
223 |
|
---|
224 | #endif NV_TYPES_HH |
---|