Changeset 54


Ignore:
Timestamp:
05/29/13 17:35:48 (12 years ago)
Author:
epyon
Message:
  • several enhancements to the RTTI system
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/types.hh

    r53 r54  
    88#include <glm/glm.hpp>
    99#include <nv/common.hh>
     10#include <type_traits>
    1011#include <utility>
    1112#include <unordered_map>
     13#include <vector>
    1214#include <string>
    1315
     
    6870        template <> struct enum_to_type< FLOAT_MATRIX_4 > { typedef mat4 type; };
    6971
    70         template < typename Type > struct type_to_enum {};
     72        template < typename TYPE > struct type_to_enum {};
    7173
    7274        template <> struct type_to_enum< int >           { static const type type = INT; };
     
    9698    }
    9799
     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"; }
    98103    template <> inline const char* get_type_name<sint8> () { return "sint8"; }
    99104    template <> inline const char* get_type_name<sint16>() { return "sint16"; }
     
    101106    template <> inline const char* get_type_name<sint64>() { return "sint64"; }
    102107
    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"; }
     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"; }
    107113
    108114        template <> inline const char* get_type_name<f32> () { return "f32"; }
     
    162168namespace nv
    163169{
     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        };
    164209
    165210    struct type_entry
     
    181226        // Result of sizeof(type) operation
    182227        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                }
    183265    };
    184  
     266
    185267    class type_database
    186268    {
     
    207289                        return *i_type;
    208290                }
     291
    209292        type_entry* get_type( hash_string name )
    210293                {
Note: See TracChangeset for help on using the changeset viewer.