Changeset 57 for trunk/nv/types.hh


Ignore:
Timestamp:
05/29/13 22:25:30 (12 years ago)
Author:
epyon
Message:
  • nv/object - object class added with implementation and type registration
  • nv/types - container support (currently only storage and detection)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/types.hh

    r55 r57  
    88#include <glm/glm.hpp>
    99#include <nv/common.hh>
     10#include <nv/object.hh>
    1011#include <type_traits>
    1112#include <utility>
     
    1415#include <string>
    1516
     17#define NV_REGISTER_NAME( s ) template <> inline const char* nv::get_type_name<s>   () { return #s; }
     18
    1619namespace nv
    1720{
     21
    1822        enum type
    1923        {
     
    9599    inline const char* get_type_name()
    96100    {
    97         static_assert< FALSE >( "Type not implemented!" );
     101        static_assert( false, "Type not implemented!" );
    98102    }
    99 
    100 #define NV_REGISTER_NAME( s ) template <> inline const char* nv::get_type_name<s>   () { return #s; }
    101103
    102104        template <> inline const char* get_type_name<int>   () { return "sint"; }
     
    130132
    131133        template <> inline const char* get_type_name<std::string> () { return "string"; }
     134        template <> inline const char* get_type_name<object>      () { return "object"; }
     135
     136        template<typename T>
     137        struct is_container
     138        {
     139        private:
     140                typedef char                      yes;
     141                typedef struct { char array[2]; } no;
     142                template<typename C> static yes test(typename C::iterator*);
     143                template<typename C> static no  test(...);
     144        public:
     145                static const bool value = sizeof(test<T>(0)) == sizeof(yes);
     146        };
     147
     148        template<>
     149        struct is_container< std::string > {
     150                static const bool value = false;
     151        };
     152
    132153
    133154        template <typename TYPE>
     
    170191        struct type_entry;
    171192
     193        enum type_flag
     194        {
     195                TF_POINTER      = 0x01, //< field is a pointer
     196                TF_NOSERIALIZE  = 0x02, //< ignore during serialization
     197                TF_INVISIBLE    = 0x04, //< field invisible to API
     198                TF_READONLY     = 0x08, //< read only field
     199                TF_SIMPLETYPE   = 0x10, //< raw binary I/O possible
     200                TF_OWNED        = 0x20,
     201                TF_CONTAINER    = 0x40, //< is a container
     202        };
     203
    172204        struct type_field
    173205        {
    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                 };
    183206                hash_string  name;      //< name of the field
    184207                hash_string  type_name; //< name of the type of the field
     
    187210                size_t       offset;
    188211
    189                 template< typename TOBJECT, typename TFIELD>
    190                 type_field( hash_string name, TFIELD TOBJECT::*field )
     212                template< typename TOBJECT, typename TFIELD >
     213                type_field( hash_string name, TFIELD TOBJECT::*field, typename std::enable_if< is_container<TFIELD>::value, void* >::type = nullptr )
     214                        : name(name)
     215                        , type_name( get_type_name< std::remove_pointer<TFIELD::value_type>::type >() )
     216                        , type( nullptr )
     217                        , flags( 0 )
     218                        , offset( offsetof( TOBJECT, *field ) )
     219                        // NOTE: if offsetof behaves badly, check offset_of in common.hh
     220                {
     221                        flags = FCONTAINER |
     222                                ( std::is_pointer<TFIELD::value_type>::value ? FPOINTER : 0 ) |
     223                                ( std::is_pod<TFIELD::value_type>::value ? FSIMPLETYPE : 0 );
     224                }
     225
     226                template< typename TOBJECT, typename TFIELD >
     227                type_field( hash_string name, TFIELD TOBJECT::*field, typename std::enable_if< !is_container<TFIELD>::value, void* >::type = nullptr )
    191228                        : name(name)
    192229                        , type_name( get_type_name< std::remove_pointer<TFIELD>::type >() )
     
    199236                                ( std::is_pointer<TFIELD>::value ? FPOINTER : 0 ) |
    200237                                ( std::is_pod<TFIELD>::value ? FSIMPLETYPE : 0 );
     238                }
     239
     240                type_field& flag( unsigned int f )
     241                {
     242                        flags |= f;
     243                        return *this;
    201244                }
    202245        };
Note: See TracChangeset for help on using the changeset viewer.