source: trunk/nv/types.hh @ 53

Last change on this file since 53 was 53, checked in by epyon, 12 years ago
  • lua_glm - powertool - almost complete implementation of glm/glsl like vectors (templated lua registration)
  • lua_glm - ivec2, ivec3, ivec3, vec2, vec3, vec4 supported, adding others is trivial (one line)
  • lua_glm - full support for swizzle and partial construction
File size: 7.7 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 <utility>
11#include <unordered_map>
12#include <string>
13
14namespace 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< vec2 > () { return "vec2"; }
112        template <> inline const char* get_type_name< vec3 > () { return "vec3"; }
113        template <> inline const char* get_type_name< vec4 > () { return "vec4"; }
114
115        template <> inline const char* get_type_name< ivec2 > () { return "ivec2"; }
116        template <> inline const char* get_type_name< ivec3 > () { return "ivec3"; }
117        template <> inline const char* get_type_name< ivec4 > () { return "ivec4"; }
118
119        template <> inline const char* get_type_name< mat2 > () { return "mat2"; }
120        template <> inline const char* get_type_name< mat3 > () { return "mat3"; }
121        template <> inline const char* get_type_name< mat4 > () { return "mat4"; }
122
123        template <> inline const char* get_type_name<bool> () { return "bool"; }
124
125        template <> inline const char* get_type_name<std::string> () { return "string"; }
126
127        template <typename TYPE>
128    std::size_t get_type_id()
129    {
130        static std::size_t type_id = std::hash<std::string>()(std::string(get_type_name<TYPE>()));
131        return type_id;
132    };
133
134        struct hash_string
135    {
136        std::size_t hash;
137        const char* text;
138                hash_string() {}
139                hash_string( const char* a_text ) :
140                        text( a_text )
141                {
142                        hash = std::hash<std::string>()( std::string( a_text ) );
143                }
144                inline bool operator == (const hash_string& hs ) const
145                {
146                        return hs.hash == hash;
147                }
148    };
149
150}
151
152namespace std {
153        template<>
154        struct hash<nv::hash_string> {
155                size_t operator()(const nv::hash_string &hs) const
156                {
157                        return hs.hash;
158                }
159        };
160}
161
162namespace nv
163{
164
165    struct type_entry
166    {
167                 // Function types for the constructor and destructor of registered types
168            typedef void (*constructor_func)(void*);
169            typedef void (*destructor_func)(void*);
170
171                // Parent type database
172        class type_database* type_db;
173
174        // Scoped C++ name of the type
175        hash_string name;
176 
177        // Pointers to the constructor and destructor functions
178        constructor_func constructor;
179        destructor_func  destructor;
180 
181        // Result of sizeof(type) operation
182        size_t size;
183    };
184 
185    class type_database
186    {
187    public:
188                template< typename TYPE >
189        type_entry& create_type()
190                {
191                        hash_string name( get_type_name<TYPE>() );
192                        type_entry* i_type = nullptr;
193                        type_map::iterator it = m_types.find( name );
194                        if ( it != m_types.end() )
195                        {
196                                return *(it->second);
197                        }
198                        i_type          = new type_entry;
199                        i_type->type_db = this;
200                        i_type->name    = name;
201                        i_type->size    = sizeof(TYPE);
202                       
203                        i_type->constructor = ConstructObject<TYPE>;
204                        i_type->destructor  = DestructObject<TYPE>;
205
206                        m_types[name] = i_type;
207                        return *i_type;
208                }
209        type_entry* get_type( hash_string name )
210                {
211                        type_map::iterator it = m_types.find( name );
212                        if ( it != m_types.end() )
213                        {
214                                return it->second;
215                        }
216                        return nullptr;
217                }
218    private:
219        typedef std::unordered_map<hash_string, type_entry*> type_map;
220        type_map m_types;
221        };
222
223        template <typename TYPE> void ConstructObject(void* object)
224    {
225        // Use placement new to call the constructor
226        new (object) TYPE;
227    }
228    template <typename TYPE> void DestructObject(void* object)
229    {
230        // Explicit call of the destructor
231        ((TYPE*)object)->TYPE::~TYPE();
232    }
233
234}
235
236#endif NV_TYPES_HH
Note: See TracBrowser for help on using the repository browser.