Changeset 540 for trunk


Ignore:
Timestamp:
01/25/17 20:20:45 (8 years ago)
Author:
epyon
Message:
  • lua::stack_proxy RTTI read support
  • missing RTTI declarations
  • new lua::table_guard syntax
Location:
trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/ecs/component.hh

    r538 r540  
    2525namespace nv
    2626{
     27        namespace lua
     28        {
     29                class stack_proxy;
     30        }
    2731
    2832        namespace ecs
     
    6165                                }
    6266                        }
     67
     68                        virtual void initialize( handle_type, lua::stack_proxy& ) {};
    6369
    6470                        inline value_type& insert( handle_type h )
  • trunk/nv/ecs/ecs.hh

    r537 r540  
    2626{
    2727
     28        namespace lua
     29        {
     30                class stack_proxy;
     31        }
     32
    2833        namespace ecs
    2934        {
     
    6570                        {
    6671                        public:
     72                                virtual void initialize( handle_type, lua::stack_proxy& ) = 0;
    6773                                virtual void remove( handle_type h ) = 0;
    6874                                virtual void* get_raw( handle_type h ) = 0;
     
    109115                                register_receiver( name, c );
    110116                                m_component_map[thash64::create<Component>()] = c;
     117                                m_component_map_by_name[name] = c;
    111118                        }
    112119
     
    249256                        }
    250257
     258                        component_interface* get_component( shash64 component_name )
     259                        {
     260                                auto it = m_component_map_by_name.find( component_name );
     261                                return it != m_component_map_by_name.end() ? it->second : nullptr;
     262                        }
     263
     264                        const component_interface* get_component( shash64 component_name ) const
     265                        {
     266                                auto it = m_component_map_by_name.find( component_name );
     267                                return it != m_component_map_by_name.end() ? it->second : nullptr;
     268                        }
     269
    251270                        template < typename Component >
    252271                        Component* get( handle_type h )
     
    267286                        vector< receiver_interface* >               m_receivers;
    268287                        hash_store< thash64, component_interface* > m_component_map;
     288                        hash_store< shash64, component_interface* > m_component_map_by_name;
    269289                        time_type                                   m_time = time_type(0);
    270290                        priority_queue< message, vector< message >, message_compare_type > m_pqueue;
  • trunk/nv/lua/lua_proxy.hh

    r539 r540  
    99
    1010#include <nv/common.hh>
     11#include <nv/core/types.hh>
    1112#include <nv/stl/string.hh>
    1213#include <nv/stl/string_table.hh>
     
    5657                        bool is_bool() const;
    5758                        bool is_string() const;
     59                        bool is_valid() const;
     60                        bool is_nil() const { return !is_valid(); }
     61                        operator bool() const { return is_valid(); }
    5862
    5963                        const_string get_string() const { return const_string( get_string_view() ); }
     
    8084                        }
    8185
     86                        template < typename T >
     87                        bool read( T& t ) const
     88                        {
     89                                NV_ASSERT( m_state->get_type_data(), "stack_proxy::read - type database not created!" );
     90                                const type_database* tdb = m_state->get_type_data()->get_type_database();
     91                                NV_ASSERT( tdb, "stack_proxy::read - type database not set!" );
     92                                const type_entry* entry = tdb->get_type<T>();
     93                                return read( entry, &t );
     94                        }
     95                        bool read( const type_entry* entry, void* object ) const;
     96// bool write?
     97
    8298                        // TO FUCKING DO : non-copyable stringview
    8399//                      string_view get_string_view() const;
     
    91107                        state* m_state;
    92108                        sint32 m_index;
     109                        friend class table_guard;
    93110                };
    94111
  • trunk/nv/lua/lua_state.hh

    r539 r540  
    376376                {
    377377                public:
     378                        table_guard( stack_proxy& proxy );
    378379                        table_guard( state* lstate, const path& p, bool global = true );
    379380                        table_guard( const table_guard& parent, const path& p );
    380381                        virtual ~table_guard();
    381                         uint32 get_size();
     382                        uint32 size();
    382383                        iterator_provider< kv_iterator > pairs()
    383384                        {
    384                                 return iterator_provider< kv_iterator >( m_parent, -1 );
     385                                return iterator_provider< kv_iterator >( m_parent, m_index );
    385386                        }
    386387                        iterator_provider< key_iterator > keys()
    387388                        {
    388                                 return iterator_provider< key_iterator >( m_parent, -1 );
     389                                return iterator_provider< key_iterator >( m_parent, m_index );
    389390                        }
    390391                        iterator_provider< value_iterator > values()
    391392                        {
    392                                 return iterator_provider< value_iterator >( m_parent, -1 );
     393                                return iterator_provider< value_iterator >( m_parent, m_index );
    393394                        }
    394395
     
    399400//                      temporary_proxy operator[]( sint32 key );
    400401
    401 
    402                         bool has_field( string_view element );
    403                        
    404 //                      shash64 get_string_hash_64( string_view element, uint64 defval = 0 );
    405 //                      shash64 get_string( string_view element, string_table* table, uint64 defval = 0 );
    406 //                      const_string get_string( string_view element, string_view defval = string_view() );
    407 //                      string128 get_string128( string_view element, string_view defval = string_view() );
    408 //                      string64 get_string64( string_view element, string_view defval = string_view() );
    409 //                      string32 get_string32( string_view element, string_view defval = string_view() );
    410 //
    411 //                      char get_char( string_view element, char defval = ' ' );
    412 //                      int get_integer( string_view element, int defval = 0 );
    413 //                      unsigned get_unsigned( string_view element, unsigned defval = 0 );
    414 //                      double get_double( string_view element, double defval = 0.0 );
    415 //                      float get_float( string_view element, float defval = 0.0 );
    416 //                      bool get_boolean( string_view element, bool defval = false );
    417 //
    418 //                      shash64 get_string_hash_64( int i, uint64 defval = 0 );
    419 //                      shash64 get_string( int i, string_table* table, uint64 defval = 0 );
    420 //                      const_string get_string( int i, string_view defval = string_view() );
    421 //                      string128 get_string128( int i, string_view defval = string_view() );
    422 //
    423 //                      char get_char( int i, char defval = ' ' );
    424 //                      int get_integer( int i, int defval = 0 );
    425 //                      unsigned get_unsigned( int i, unsigned defval = 0 );
    426 //                      double get_double( int i, double defval = 0.0 );
    427 //                      float get_float( int i, float defval = 0.0 );
    428 //                      bool get_boolean( int i, bool defval = false );
    429 //
    430 //                      bool is_table( string_view element );
    431 //                      bool is_number( string_view element );
    432 //                      bool is_boolean( string_view element );
    433 //                      bool is_string( string_view element );
    434 //                      bool is_table( int i );
    435 //                      bool is_number( int i );
    436 //                      bool is_boolean( int i );
    437 //                      bool is_string( int i );
    438 
    439                         template < typename T >
    440                         bool read( const string_view& element, T& t )
    441                         {
    442                                 NV_ASSERT( m_lua_types, "table_guard::read - type database not created!" );
    443                                 NV_ASSERT( m_lua_types->get_type_database(), "table_guard::read - type database not set!" );
    444                                 const type_entry* entry = m_lua_types->get_type_database()->get_type<T>();
    445                                 return read( element, entry, &t );
    446                         }
    447                         bool read( const string_view& element, const type_entry* entry, void* object );
    448 
    449402                private:
    450403                        state* m_parent;
    451404                        int m_level;
     405                        int m_index;
    452406                };
    453407
  • trunk/nv/stl/string.hh

    r533 r540  
    153153using nv::operator "" _sh64;
    154154
     155NV_RTTI_DECLARE( nv::shash32 )
     156NV_RTTI_DECLARE( nv::shash64 )
     157
     158
     159
    155160#endif // NV_STL_STRING_HH
  • trunk/src/core/types.cc

    r505 r540  
    2828        db->create_type<ivec4>();
    2929        db->create_type<quat>();
     30        db->create_type<shash32>();
     31        db->create_type<shash64>();
    3032        db->create_type<string32>();
    3133        db->create_type<string64>();
  • trunk/src/engine/animation.cc

    r539 r540  
    2727nv::resource< animator_data > nv::animator_manager::load_animator( nv::lua::table_guard& table, nv::shash64 id, nv::pose_data_set* poses /*= nullptr */ )
    2828{
    29         uint32 count = table.get_size();
     29        uint32 count = table.size();
    3030        if ( count == 0 )
    3131        {
     
    7373                {
    7474                        nv::lua::table_guard states_table( layer_table, "states" );
    75                         uint32 state_count = states_table.get_size();
     75                        uint32 state_count = states_table.size();
    7676                        if ( state_count > 0 )
    7777                        {
     
    119119                                        {
    120120                                                nv::lua::table_guard transitions_table( state_table, "transitions" );
    121                                                 uint32 transition_count = transitions_table.get_size();
     121                                                uint32 transition_count = transitions_table.size();
    122122                                                if ( transition_count > 0 )
    123123                                                {
     
    167167        result.in = table["ease_in"].get_enum( result.in );
    168168        result.out = table["ease_out"].get_enum( nv::easing_type::NONE );
    169         if ( table.has_field( "ease_in_out" ) )
     169        if ( table[ "ease_in_out" ] )
    170170        {
    171171                result.in = result.out = table["ease_in_out"].get_enum( nv::easing_type::NONE );
  • trunk/src/engine/model_manager.cc

    r539 r540  
    3030                gm->ragdoll_id = table["ragdoll"].get_string32();
    3131       
    32         if ( table.has_field( "animator" ) )
     32        if ( table["animator"] )
    3333        {
    3434                gm->animator = m_rm->get< animator_data >( table["animator"].get_string() );
     
    4040        }
    4141
    42         if ( table.has_field( "phx_mesh" ) )
     42        if ( table[ "phx_mesh" ] )
    4343        {
    4444                nv::string128 cpath( table["phx_mesh"].get_string128() );
     
    7777                node->tag = table["tag"].get_string32();
    7878
    79         if ( table.has_field( "path" ) )
     79        if ( table[ "path" ] )
    8080        {
    8181                nv::string128 cpath( table["path"].get_string128() );
     
    8585        }
    8686
    87         if ( table.has_field( "phx_hextents" ) )
     87        if ( table[ "phx_hextents"] )
    8888                node->phx_hextents = table["phx_hextents"].as<vec3>();
    89         if ( table.has_field( "phx_offset" ) )
     89        if ( table[ "phx_offset"] )
    9090                node->phx_offset = table["phx_offset"].as<vec3>();
    91         if ( table.has_field( "phx_mass" ) )
     91        if ( table[ "phx_mass"] )
    9292                node->phx_mass = table["phx_mass"].as<float>( 0.0f );
    93         if ( table.has_field( "phx_mass" ) )
     93        if ( table["phx_mass"] )
    9494                node->phx_shape = nv::phx_shape( table["phx_shape"].as<int>( 0 ) );
    9595
    96         if ( table.has_field( "local_position" ) )
     96        if ( table["local_position"] )
    9797                node->local.set_position( table["local_position"].as<vec3>() );
    98         if ( table.has_field( "local_orientation" ) )
     98        if ( table["local_orientation"] )
    9999                node->local.set_orientation( vec4_to_quat( table["local_orientation"].as<vec4>( vec4( 0.0f, 0.0f, 0.0f, 1.0f ) ) ) );
    100100
    101         if ( table.has_field( "position" ) )
     101        if ( table["position"] )
    102102        {
    103103                node->position.min = table["position"].as<vec3>();
    104104                node->position.max = node->position.min;
    105105        }
    106         if ( table.has_field( "rotation" ) )
     106        if ( table["rotation"] )
    107107        {
    108108                node->rotation.min = table["rotation"].as<vec3>();
    109109                node->rotation.max = node->rotation.min;
    110110        }
    111         if ( table.has_field( "position_min" ) )  node->position.min  = table["position_min"].as<vec3>();
    112         if ( table.has_field( "position_max" ) )  node->position.max  = table["position_max"].as<vec3>();
    113         if ( table.has_field( "position_dist" ) ) node->position.dist = random_dist( table["position_dist"].get_uint32() );
    114         if ( table.has_field( "rotation_min" ) )  node->rotation.min  = table["rotation_min"].as<vec3>();
    115         if ( table.has_field( "rotation_max" ) )  node->rotation.max  = table["rotation_max"].as<vec3>();
    116         if ( table.has_field( "rotation_dist" ) ) node->rotation.dist = random_dist( table["rotation_dist"].get_uint32() );
     111        if ( table["position_min"] )  node->position.min  = table["position_min"].as<vec3>();
     112        if ( table["position_max"] )  node->position.max  = table["position_max"].as<vec3>();
     113        if ( table["position_dist"] ) node->position.dist = random_dist( table["position_dist"].get_uint32() );
     114        if ( table["rotation_min"] )  node->rotation.min  = table["rotation_min"].as<vec3>();
     115        if ( table["rotation_max"] )  node->rotation.max  = table["rotation_max"].as<vec3>();
     116        if ( table["rotation_dist"] ) node->rotation.dist = random_dist( table["rotation_dist"].get_uint32() );
    117117
    118         if ( table.has_field( "attach" ) )
     118        if ( table["attach"] )
    119119        {
    120120                if ( table["attach"].is_number() )
     
    140140        node->material  = cmaterial;
    141141
    142         for ( uint32 i = 1; i <= table.get_size(); ++i )
     142        for ( uint32 i = 1; i <= table.size(); ++i )
    143143        {
    144144                lua::table_guard child_table( table, i );
  • trunk/src/engine/particle_manager.cc

    r539 r540  
    6868
    6969        vec2 def_size        = table.get<vec2>("size", vec2(0.1,0.1) );
    70         uint32 elements = table.get_size();
     70        uint32 elements = table.size();
    7171        for ( uint32 i = 0; i < elements; ++i )
    7272        {
  • trunk/src/engine/program_manager.cc

    r539 r540  
    6868        {
    6969                lua::table_guard inctable( table, "files" );
    70                 uint32 count = inctable.get_size();
     70                uint32 count = inctable.size();
    7171                for ( uint32 i = 1; i <= count; ++i )
    7272                {
  • trunk/src/engine/ragdoll_manager.cc

    r539 r540  
    7575                float radius = table["radius"].get_f32();
    7676                float length = 0.0f;
    77                 if ( table.has_field( "target" ) )
     77                if ( table[ "target" ] )
    7878                {
    7979                        const auto& of = bind_data->get_bone_transforms().m_offsets;
     
    9797                        part.limits     = table["limits"].as< vec3 >();
    9898                }
    99                 uint32 child_count = table.get_size();
     99                uint32 child_count = table.size();
    100100                bool   result = true;
    101101                if ( child_count > 0 )
  • trunk/src/lua/lua_proxy.cc

    r539 r540  
    99#include "nv/lua/lua_raw.hh"
    1010#include "nv/lua/lua_state.hh"
     11#include "nv/lua/lua_types.hh"
    1112
    1213using namespace nv;
     
    5859}
    5960
     61bool nv::lua::stack_proxy::is_valid() const
     62{
     63        return !( lua_isnil( *m_state, m_index ) );
     64}
     65
     66bool nv::lua::stack_proxy::read( const type_entry* entry, void* object ) const
     67{
     68        NV_ASSERT_ALWAYS( m_state->get_type_data()->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" );
     69        if ( lua_type( *m_state, m_index ) != LUA_TTABLE ) return false;
     70        return nv::lua::read_rtti_type( m_state, entry, object, m_index );
     71}
     72
    6073bool nv::lua::stack_proxy::is_table() const
    6174{
  • trunk/src/lua/lua_state.cc

    r539 r540  
    153153                // TODO : error handling
    154154        }
     155        m_index = -1;
    155156}
    156157
     
    165166                // TODO : error handling
    166167        }
     168        m_index = -1;
    167169}
    168170
    169171lua::table_guard::~table_guard()
    170172{
    171         lua_settop( m_state, m_level );
    172 }
    173 
    174 nv::uint32 lua::table_guard::get_size()
    175 {
    176         return nlua_rawlen( m_state, -1 );
    177 }
    178 
    179 bool lua::table_guard::has_field( string_view element )
    180 {
    181         lua_getfield( m_state, -1, element.data() );
    182         bool result = !( lua_isnil( m_state, -1 ) );
    183         lua_pop( m_state, 1 );
    184         return result;
     173        if ( m_index == -1 )
     174                lua_settop( m_state, m_level );
     175}
     176
     177nv::uint32 lua::table_guard::size()
     178{
     179        return nlua_rawlen( m_state, m_index );
     180}
     181
     182nv::lua::table_guard::table_guard( stack_proxy& proxy )
     183        : state_wrapper( *proxy.m_state, proxy.m_state->m_lua_types, false ), m_parent( proxy.m_state ), m_level( 0 )
     184{
     185        m_level = lua_gettop( m_state );
     186        m_index = proxy.m_index;
    185187}
    186188
    187189const nv::lua::temporary_proxy nv::lua::table_guard::operator[]( const string_view& key ) const
    188190{
    189         lua_getfield( m_state, -1, key.data() );
     191        lua_getfield( m_state, m_index, key.data() );
    190192        return temporary_proxy( m_parent );
    191193}
     
    193195const nv::lua::temporary_proxy nv::lua::table_guard::operator[]( sint32 key ) const
    194196{
    195         lua_rawgeti( m_state, -1, key );
     197        lua_rawgeti( m_state, m_index, key );
    196198        return temporary_proxy( m_parent );
    197 }
    198 
    199 bool nv::lua::table_guard::read( const string_view& element, const type_entry* entry, void* object )
    200 {
    201         NV_ASSERT_ALWAYS( m_lua_types->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" );
    202         lua_getfield( m_state, -1, element.data() );
    203         if ( lua_type( m_state, -1 ) != LUA_TTABLE )
    204         {
    205                 lua_pop( m_state, 1 );
    206                 return false;
    207         }
    208         if ( !nv::lua::read_rtti_type( m_parent, entry, object, -1 ) )
    209         {
    210                 lua_pop( m_state, 1 );
    211                 return false;
    212         }
    213         lua_pop( m_state, 1 );
    214         return true;
    215199}
    216200
     
    590574}
    591575
     576template < typename T >
     577bool nlua_rtti_string_read( nv::lua::state* state, const type_entry*, void* object, int index )
     578{
     579        T* value = reinterpret_cast<T*>( object );
     580        if ( lua_type( state->get_raw(), index ) == LUA_TSTRING )
     581        {
     582                *value = T( nlua_tostringview( state->get_raw(), index ) );
     583                return true;
     584        }
     585        return false;
     586}
     587
    592588
    593589void nv::lua::type_data::insert( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
     
    608604        insert<nv::f32>   ( nlua_rtti_floating_push<nv::f32>,    nlua_rtti_floating_read<nv::f32> );
    609605        insert<nv::f64>   ( nlua_rtti_floating_push<nv::f64>,    nlua_rtti_floating_read<nv::f64> );
    610 //      insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> );
     606        insert<nv::shash32>  ( nullptr, nlua_rtti_string_read<nv::shash32> );
     607        insert<nv::shash64>  ( nullptr, nlua_rtti_string_read<nv::shash64> );
     608        insert<nv::string32> ( nullptr, nlua_rtti_string_read<nv::string32> );
     609        insert<nv::string64> ( nullptr, nlua_rtti_string_read<nv::string64> );
     610        insert<nv::string128>( nullptr, nlua_rtti_string_read<nv::string128> );
     611        //      insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> );
    611612//      insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> );
    612613}
Note: See TracChangeset for help on using the changeset viewer.