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