source: trunk/src/lua/lua_proxy.cc @ 540

Last change on this file since 540 was 540, checked in by epyon, 8 years ago
  • lua::stack_proxy RTTI read support
  • missing RTTI declarations
  • new lua::table_guard syntax
File size: 3.0 KB
Line 
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"
11#include "nv/lua/lua_types.hh"
12
13using namespace nv;
14using namespace nv::lua;
15
16uint32 nv::lua::stack_proxy::get_uint32( uint32 def ) const
17{
18        return lua_type( *m_state, m_index ) == LUA_TNUMBER ? nlua_tounsigned( *m_state, m_index ) : def;
19}
20
21sint32 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
26char 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
31nv::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
36nv::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
41bool 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
46bool nv::lua::stack_proxy::is_number() const
47{
48        return lua_type( *m_state, m_index ) == LUA_TNUMBER;
49}
50
51bool nv::lua::stack_proxy::is_bool() const
52{
53        return lua_type( *m_state, m_index ) == LUA_TBOOLEAN;
54}
55
56bool nv::lua::stack_proxy::is_string() const
57{
58        return lua_type( *m_state, m_index ) == LUA_TSTRING;
59}
60
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
73bool nv::lua::stack_proxy::is_table() const
74{
75        return lua_type( *m_state, m_index ) == LUA_TTABLE;
76}
77
78nv::string_view nv::lua::stack_proxy::get_string_view() const
79{
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        }
86        return string_view( str, static_cast<uint32>( l ) );
87}
88
89nv::string_view nv::lua::stack_proxy::as_string_view()
90{
91        size_t l = 0;
92        const char* str = nullptr;
93        str = lua_tolstring( *m_state, m_index, &l );
94        return string_view( str, static_cast<uint32>( l ) );
95}
96
97nv::lua::temporary_proxy::temporary_proxy( state* state )
98        : stack_proxy( state, -1 )
99{
100
101}
102
103nv::lua::temporary_proxy::~temporary_proxy()
104{
105        lua_pop( *m_state, 1 );
106}
Note: See TracBrowser for help on using the repository browser.