source: trunk/nv/types.hh @ 6

Last change on this file since 6 was 6, checked in by epyon, 12 years ago
  • types.hh - experimental RTTI system base
File size: 3.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 <nv/common.hh>
9#include <utility>
10#include <unordered_map>
11#include <string>
12
13namespace nv
14{
15        template <typename TYPE>
16    const char* get_type_name()
17    {
18        static_assert< FALSE >( "Type not implemented!" );
19    }
20
21    template <> const char* get_type_name<sint8> () { return "sint8"; }
22    template <> const char* get_type_name<sint16>() { return "sint16"; }
23    template <> const char* get_type_name<sint32>() { return "sint32"; }
24    template <> const char* get_type_name<sint64>() { return "sint64"; }
25
26        template <> const char* get_type_name<uint8> () { return "uint8"; }
27    template <> const char* get_type_name<uint16>() { return "uint16"; }
28    template <> const char* get_type_name<uint32>() { return "uint32"; }
29    template <> const char* get_type_name<uint64>() { return "uint64"; }
30
31        template <> const char* get_type_name<f32> () { return "f32"; }
32        template <> const char* get_type_name<f64> () { return "f64"; }
33
34        template <> const char* get_type_name<bool> () { return "bool"; }
35
36        template <> const char* get_type_name<std::string> () { return "string"; }
37
38        template <typename TYPE>
39    std::size_t get_type_id()
40    {
41        static std::size_t type_id = std::hash<std::string>()(std::string(get_type_name<TYPE>()));
42        return type_id;
43    };
44
45        struct hash_string
46    {
47        std::size_t hash;
48        const char* text;
49                hash_string() {}
50                hash_string( const char* a_text ) :
51                        text( a_text )
52                {
53                        hash = std::hash<std::string>()( std::string( a_text ) );
54                }
55                inline bool operator == (const hash_string& hs ) const
56                {
57                        return hs.hash == hash;
58                }
59    };
60
61}
62
63namespace std {
64        template<>
65        struct hash<nv::hash_string> {
66                size_t operator()(const nv::hash_string &hs) const
67                {
68                        return hs.hash;
69                }
70        };
71}
72
73namespace nv
74{
75
76    struct type
77    {
78                 // Function types for the constructor and destructor of registered types
79            typedef void (*constructor_func)(void*);
80            typedef void (*destructor_func)(void*);
81
82                // Parent type database
83        class type_database* type_db;
84
85        // Scoped C++ name of the type
86        hash_string name;
87 
88        // Pointers to the constructor and destructor functions
89        constructor_func constructor;
90        destructor_func  destructor;
91 
92        // Result of sizeof(type) operation
93        size_t size;
94    };
95 
96    class type_database
97    {
98    public:
99                template< typename TYPE >
100        type& create_type()
101                {
102                        hash_string name( get_type_name<TYPE>() );
103                        type* i_type = nullptr;
104                        type_map::iterator it = m_types.find( name );
105                        if ( it != m_types.end() )
106                        {
107                                return *(it->second);
108                        }
109                        i_type          = new type;
110                        i_type->type_db = this;
111                        i_type->name    = name;
112                        i_type->size    = sizeof(TYPE);
113                       
114                        i_type->constructor = ConstructObject<TYPE>;
115                        i_type->destructor  = DestructObject<TYPE>;
116
117                        m_types[name] = i_type;
118                        return *i_type;
119                }
120        type* get_type( hash_string name )
121                {
122                        type_map::iterator it = m_types.find( name );
123                        if ( it != m_types.end() )
124                        {
125                                return it->second;
126                        }
127                        return nullptr;
128                }
129    private:
130        typedef std::unordered_map<hash_string, type*> type_map;
131        type_map m_types;
132        };
133
134        template <typename TYPE> void ConstructObject(void* object)
135    {
136        // Use placement new to call the constructor
137        new (object) TYPE;
138    }
139    template <typename TYPE> void DestructObject(void* object)
140    {
141        // Explicit call of the destructor
142        ((TYPE*)object)->TYPE::~TYPE();
143    }
144
145}
146
147#endif NV_TYPES_HH
Note: See TracBrowser for help on using the repository browser.