Changeset 450 for trunk


Ignore:
Timestamp:
07/30/15 14:43:02 (10 years ago)
Author:
epyon
Message:
  • fixed offset_of
  • types.hh support unioned fields
  • io_event io_event struct type loading added
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/base/common.hh

    r406 r450  
    253253
    254254        template <typename OBJ, typename T>
    255         inline size_t offset_of( T OBJ::*ptr )
    256         {
    257                 return static_cast<size_t>( &( ( static_cast<OBJ*>(0) )->*ptr ) );
     255        inline ptrdiff_t offset_of( T OBJ::*ptr )
     256        {
     257                return reinterpret_cast<ptrdiff_t>( &reinterpret_cast<const volatile char&>( ( static_cast<OBJ*>(0) )->*ptr ) );
    258258        }
    259259
  • trunk/nv/core/types.hh

    r447 r450  
    3333                // TODO: enforce exact type?
    3434                constexpr thash64() : inherited_type() {}
    35                 template < typename T >
    36                 constexpr thash64() : inherited_type( rtti_type_hash< T >::hash() ) {}
    3735                constexpr explicit thash64( hash_type value ) : inherited_type( value ) {}
    3836                constexpr thash64( const thash64& value ) = default;
     37
     38                template < typename T >
     39                static thash64 create() { return thash64( rtti_type_hash< T >::hash() ); }
    3940        };
    4041
     
    7879                uint32      flags;    //!< flags
    7980                uint32      offset;   //!< offset into parent
     81                type_field* control;  //!< pointer to field control (in unions)
     82                sint32      enumidx;  //!< field index (in unions)
    8083        };
    8184
     
    115118                type_creator field( const string_view& aname, TFIELD TOBJECT::*field, typename enable_if< !is_container<TFIELD>::value, void* >::type = nullptr );
    116119
     120                template< typename TOBJECT, typename TFIELD, typename ENUM_VALUE >
     121                type_creator union_field( const string_view& aname, TFIELD TOBJECT::*field, const string_view& control_name, ENUM_VALUE control_value, typename enable_if< is_container<TFIELD>::value, void* >::type = nullptr );
     122
     123                template< typename TOBJECT, typename TFIELD, typename ENUM_VALUE >
     124                type_creator union_field( const string_view& aname, TFIELD TOBJECT::*field, const string_view& control_name, ENUM_VALUE control_value, typename enable_if< !is_container<TFIELD>::value, void* >::type = nullptr );
     125
    117126                type_creator value( const string_view& aname, sint32 value );
    118127        private:
     
    129138                type_creator create_type()
    130139                {
    131                         return create_type<TYPE>( rtti_type_hash< TYPE >.name() );
     140                        return create_type<TYPE>( rtti_type_hash< TYPE >::name() );
    132141                }
    133142
     
    143152                        i_type->constructor = raw_construct_object < TYPE >;
    144153                        i_type->destructor  = raw_destroy_object < TYPE >;
    145                         m_index_by_type[ thash64< TYPE >() ] = i_type;
     154                        m_index_by_type[ thash64::create< TYPE >() ] = i_type;
    146155                        m_index_by_name[ i_type->name ] = i_type;
    147156                        m_type_list.push_back( i_type );
     
    164173                type_entry* get_type()
    165174                {
    166                         auto it = m_index_by_type.find( thash64< T >() );
     175                        auto it = m_index_by_type.find( thash64::create< T >() );
    167176                        return it != m_index_by_type.end() ? it->second : nullptr;
    168177                }
     
    197206                NV_ASSERT( m_entry->enum_list.empty(), "Type cannot have both enums and fields!" );
    198207                type_field f;
    199                 f.name = m_database->m_names.insert( name );
    200                 f.type = type_db->get_type< remove_pointer_t<TFIELD> >();
     208                f.name = m_database->m_names.insert( aname );
     209                f.type = m_database->get_type< remove_pointer_t<TFIELD> >();
    201210                f.flags = TF_CONTAINER |
    202211                        ( is_pointer<field_type>::value ? TF_POINTER : 0 ) |
    203212                        ( is_pod<field_type>::value ? TF_SIMPLETYPE : 0 );
    204213                f.offset = offset_of( field );
    205                 m_entry->field_list.push_back( f );
     214                f.control = nullptr;
     215                f.enumidx = 0;
     216                m_entry->field_list.push_back( f );
     217                m_entry->field_names[f.name] = &m_entry->field_list.back();
    206218                return *this;
    207219        }
     
    212224                NV_ASSERT( m_entry->enum_list.empty(), "Type cannot have both enums and fields!" );
    213225                type_field f;
    214                 f.name = m_database->m_names.insert( name );
    215                 f.type = type_db->get_type< remove_pointer_t<TFIELD> >();
     226                f.name = m_database->m_names.insert( aname );
     227                f.type = m_database->get_type< remove_pointer_t<TFIELD> >();
    216228                f.flags =
    217229                        ( is_pointer<TFIELD>::value ? TF_POINTER : 0 ) |
    218230                        ( is_pod<TFIELD>::value ? TF_SIMPLETYPE : 0 );
    219231                f.offset = offset_of( field );
    220                 m_entry->field_list.push_back( f );
     232                f.control = nullptr;
     233                f.enumidx = 0;
     234                m_entry->field_list.push_back( f );
     235                m_entry->field_names[f.name] = &m_entry->field_list.back();
     236                return *this;
     237        }
     238
     239        template< typename TOBJECT, typename TFIELD, typename ENUM_VALUE >
     240        type_creator type_creator::union_field( const string_view& aname, TFIELD TOBJECT::*field, const string_view& control_name, ENUM_VALUE control_value, typename enable_if< is_container<TFIELD>::value, void* >::type )
     241        {
     242                typedef typename TFIELD::value_type field_type;
     243                NV_ASSERT( m_entry->enum_list.empty(), "Type cannot have both enums and fields!" );
     244                type_field f;
     245                f.name = m_database->m_names.insert( aname );
     246                f.type = m_database->get_type< remove_pointer_t<TFIELD> >();
     247                f.flags = TF_CONTAINER |
     248                        ( is_pointer<field_type>::value ? TF_POINTER : 0 ) |
     249                        ( is_pod<field_type>::value ? TF_SIMPLETYPE : 0 );
     250                f.offset = offset_of( field );
     251                f.control = m_entry->field_names[ control_name ];
     252                f.enumidx = static_cast< sint32 >( control_value );
     253                m_entry->field_list.push_back( f );
     254                m_entry->field_names[f.name] = &m_entry->field_list.back();
     255                return *this;
     256        }
     257
     258        template< typename TOBJECT, typename TFIELD, typename ENUM_VALUE >
     259        type_creator type_creator::union_field( const string_view& aname, TFIELD TOBJECT::*field, const string_view& control_name, ENUM_VALUE control_value, typename enable_if< !is_container<TFIELD>::value, void* >::type )
     260        {
     261                NV_ASSERT( m_entry->enum_list.empty(), "Type cannot have both enums and fields!" );
     262                type_field f;
     263                f.name = m_database->m_names.insert( aname );
     264                f.type = m_database->get_type< remove_pointer_t<TFIELD> >();
     265                f.flags =
     266                        ( is_pointer<TFIELD>::value ? TF_POINTER : 0 ) |
     267                        ( is_pod<TFIELD>::value ? TF_SIMPLETYPE : 0 );
     268                f.offset = offset_of( field );
     269                f.control = m_entry->field_names[control_name];
     270                f.enumidx = static_cast<sint32>( control_value );
     271                m_entry->field_list.push_back( f );
     272                m_entry->field_names[f.name] = &m_entry->field_list.back();
    221273                return *this;
    222274        }
     
    229281                e.value = value;
    230282                m_entry->enum_list.push_back( e );
     283                m_entry->enum_names[e.name] = &m_entry->enum_list.back();
    231284                return *this;
    232285        }
  • trunk/src/core/io_event.cc

    r447 r450  
    141141                .field( "param2",   &system_event::param2 );
    142142
    143         // TODO: io_event
     143        db->create_type<io_event>()
     144                .field( "type", &io_event::type )
     145                .union_field( "key",     &io_event::key,    "type", EV_KEY )
     146                .union_field( "mbutton", &io_event::mbutton,"type", EV_MOUSE_BUTTON )
     147                .union_field( "mmove",   &io_event::mmove,  "type", EV_MOUSE_MOVE )
     148                .union_field( "mwheel",  &io_event::mwheel, "type", EV_MOUSE_WHEEL )
     149                .union_field( "pbutton", &io_event::pbutton,"type", EV_PAD_BUTTON )
     150                .union_field( "paxis",   &io_event::paxis,  "type", EV_PAD_AXIS )
     151                .union_field( "jbutton", &io_event::jbutton,"type", EV_JOY_BUTTON )
     152                .union_field( "jaxis",   &io_event::jaxis,  "type", EV_JOY_AXIS )
     153                .union_field( "jhat",    &io_event::jhat,   "type", EV_JOY_HAT )
     154                .union_field( "jball",   &io_event::jball,  "type", EV_JOY_BALL )
     155                .union_field( "resize",  &io_event::resize, "type", EV_ACTIVE )
     156                .union_field( "active",  &io_event::active, "type", EV_RESIZE )
     157                .union_field( "system",  &io_event::system, "type", EV_SYSTEM );
     158
    144159}
Note: See TracChangeset for help on using the changeset viewer.