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 |
|
---|
12 | using namespace nv;
|
---|
13 | using namespace nv::lua;
|
---|
14 |
|
---|
15 | uint32 nv::lua::stack_proxy::get_uint32( uint32 def ) const
|
---|
16 | {
|
---|
17 | return lua_type( *m_state, m_index ) == LUA_TNUMBER ? nlua_tounsigned( *m_state, m_index ) : def;
|
---|
18 | }
|
---|
19 |
|
---|
20 | sint32 nv::lua::stack_proxy::get_sint32( sint32 def ) const
|
---|
21 | {
|
---|
22 | return lua_type( *m_state, m_index ) == LUA_TNUMBER ? static_cast<sint32>( lua_tointeger( *m_state, m_index ) ) : def;
|
---|
23 | }
|
---|
24 |
|
---|
25 | char nv::lua::stack_proxy::get_char( char def /*= ' ' */ ) const
|
---|
26 | {
|
---|
27 | return ( lua_type( *m_state, m_index ) == LUA_TSTRING && nlua_rawlen( *m_state, m_index ) > 0 ) ? lua_tostring( *m_state, m_index )[0] : def;
|
---|
28 | }
|
---|
29 |
|
---|
30 | nv::f64 nv::lua::stack_proxy::get_f64( f64 def /*= 0.0 */ ) const
|
---|
31 | {
|
---|
32 | return lua_type( *m_state, m_index ) == LUA_TNUMBER ? lua_tonumber( *m_state, m_index ) : def;
|
---|
33 | }
|
---|
34 |
|
---|
35 | nv::f32 nv::lua::stack_proxy::get_f32( f32 def /*= 0.0f */ ) const
|
---|
36 | {
|
---|
37 | return lua_type( *m_state, m_index ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( *m_state, m_index ) ) : def;
|
---|
38 | }
|
---|
39 |
|
---|
40 | bool nv::lua::stack_proxy::get_bool( bool def /*= false */ ) const
|
---|
41 | {
|
---|
42 | return lua_type( *m_state, m_index ) == LUA_TBOOLEAN ? lua_toboolean( *m_state, m_index ) != 0 : def;
|
---|
43 | }
|
---|
44 |
|
---|
45 | bool nv::lua::stack_proxy::is_number() const
|
---|
46 | {
|
---|
47 | return lua_type( *m_state, m_index ) == LUA_TNUMBER;
|
---|
48 | }
|
---|
49 |
|
---|
50 | bool nv::lua::stack_proxy::is_bool() const
|
---|
51 | {
|
---|
52 | return lua_type( *m_state, m_index ) == LUA_TBOOLEAN;
|
---|
53 | }
|
---|
54 |
|
---|
55 | bool nv::lua::stack_proxy::is_string() const
|
---|
56 | {
|
---|
57 | return lua_type( *m_state, m_index ) == LUA_TSTRING;
|
---|
58 | }
|
---|
59 |
|
---|
60 | bool nv::lua::stack_proxy::is_table() const
|
---|
61 | {
|
---|
62 | return lua_type( *m_state, m_index ) == LUA_TTABLE;
|
---|
63 | }
|
---|
64 |
|
---|
65 | nv::string_view nv::lua::stack_proxy::get_string_view() const
|
---|
66 | {
|
---|
67 | size_t l = 0;
|
---|
68 | const char* str = nullptr;
|
---|
69 | if ( lua_type( *m_state, m_index ) == LUA_TSTRING )
|
---|
70 | {
|
---|
71 | str = lua_tolstring( *m_state, m_index, &l );
|
---|
72 | }
|
---|
73 | return string_view( str, static_cast<uint32>( l ) );
|
---|
74 | }
|
---|
75 |
|
---|
76 | nv::string_view nv::lua::stack_proxy::as_string_view()
|
---|
77 | {
|
---|
78 | size_t l = 0;
|
---|
79 | const char* str = nullptr;
|
---|
80 | str = lua_tolstring( *m_state, m_index, &l );
|
---|
81 | return string_view( str, static_cast<uint32>( l ) );
|
---|
82 | }
|
---|
83 |
|
---|
84 | nv::lua::temporary_proxy::temporary_proxy( state* state )
|
---|
85 | : stack_proxy( state, -1 )
|
---|
86 | {
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | nv::lua::temporary_proxy::~temporary_proxy()
|
---|
91 | {
|
---|
92 | lua_pop( *m_state, 1 );
|
---|
93 | }
|
---|