[538] | 1 | // Copyright (C) 2017-2017 ChaosForge Ltd
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
| 6 |
|
---|
| 7 | #include "nv/lua/lua_proxy.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/lua/lua_raw.hh"
|
---|
| 10 | #include "nv/lua/lua_state.hh"
|
---|
[540] | 11 | #include "nv/lua/lua_types.hh"
|
---|
[538] | 12 |
|
---|
| 13 | using namespace nv;
|
---|
| 14 | using namespace nv::lua;
|
---|
| 15 |
|
---|
[539] | 16 | uint32 nv::lua::stack_proxy::get_uint32( uint32 def ) const
|
---|
[538] | 17 | {
|
---|
[539] | 18 | return lua_type( *m_state, m_index ) == LUA_TNUMBER ? nlua_tounsigned( *m_state, m_index ) : def;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | sint32 nv::lua::stack_proxy::get_sint32( sint32 def ) const
|
---|
| 22 | {
|
---|
| 23 | return lua_type( *m_state, m_index ) == LUA_TNUMBER ? static_cast<sint32>( lua_tointeger( *m_state, m_index ) ) : def;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | char nv::lua::stack_proxy::get_char( char def /*= ' ' */ ) const
|
---|
| 27 | {
|
---|
| 28 | return ( lua_type( *m_state, m_index ) == LUA_TSTRING && nlua_rawlen( *m_state, m_index ) > 0 ) ? lua_tostring( *m_state, m_index )[0] : def;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | nv::f64 nv::lua::stack_proxy::get_f64( f64 def /*= 0.0 */ ) const
|
---|
| 32 | {
|
---|
| 33 | return lua_type( *m_state, m_index ) == LUA_TNUMBER ? lua_tonumber( *m_state, m_index ) : def;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | nv::f32 nv::lua::stack_proxy::get_f32( f32 def /*= 0.0f */ ) const
|
---|
| 37 | {
|
---|
| 38 | return lua_type( *m_state, m_index ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( *m_state, m_index ) ) : def;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | bool nv::lua::stack_proxy::get_bool( bool def /*= false */ ) const
|
---|
| 42 | {
|
---|
| 43 | return lua_type( *m_state, m_index ) == LUA_TBOOLEAN ? lua_toboolean( *m_state, m_index ) != 0 : def;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | bool nv::lua::stack_proxy::is_number() const
|
---|
| 47 | {
|
---|
| 48 | return lua_type( *m_state, m_index ) == LUA_TNUMBER;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | bool nv::lua::stack_proxy::is_bool() const
|
---|
| 52 | {
|
---|
| 53 | return lua_type( *m_state, m_index ) == LUA_TBOOLEAN;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | bool nv::lua::stack_proxy::is_string() const
|
---|
| 57 | {
|
---|
| 58 | return lua_type( *m_state, m_index ) == LUA_TSTRING;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[540] | 61 | bool nv::lua::stack_proxy::is_valid() const
|
---|
| 62 | {
|
---|
| 63 | return !( lua_isnil( *m_state, m_index ) );
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | bool 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 |
|
---|
[539] | 73 | bool nv::lua::stack_proxy::is_table() const
|
---|
| 74 | {
|
---|
| 75 | return lua_type( *m_state, m_index ) == LUA_TTABLE;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | nv::string_view nv::lua::stack_proxy::get_string_view() const
|
---|
| 79 | {
|
---|
[538] | 80 | size_t l = 0;
|
---|
| 81 | const char* str = nullptr;
|
---|
| 82 | if ( lua_type( *m_state, m_index ) == LUA_TSTRING )
|
---|
| 83 | {
|
---|
| 84 | str = lua_tolstring( *m_state, m_index, &l );
|
---|
| 85 | }
|
---|
[539] | 86 | return string_view( str, static_cast<uint32>( l ) );
|
---|
[538] | 87 | }
|
---|
| 88 |
|
---|
[539] | 89 | nv::string_view nv::lua::stack_proxy::as_string_view()
|
---|
[538] | 90 | {
|
---|
| 91 | size_t l = 0;
|
---|
| 92 | const char* str = nullptr;
|
---|
| 93 | str = lua_tolstring( *m_state, m_index, &l );
|
---|
[539] | 94 | return string_view( str, static_cast<uint32>( l ) );
|
---|
[538] | 95 | }
|
---|
[539] | 96 |
|
---|
| 97 | nv::lua::temporary_proxy::temporary_proxy( state* state )
|
---|
[541] | 98 | : stack_proxy( state, lua_gettop( *state ) )
|
---|
[539] | 99 | {
|
---|
| 100 |
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | nv::lua::temporary_proxy::~temporary_proxy()
|
---|
| 104 | {
|
---|
| 105 | lua_pop( *m_state, 1 );
|
---|
| 106 | }
|
---|