source: trunk/src/lua/lua_values.cc @ 264

Last change on this file since 264 was 262, checked in by epyon, 11 years ago
  • major decoupling lua system independent of nv::object nv::object support for lua opt-in lua system independent of types.hh
File size: 4.3 KB
Line 
1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/lua/lua_values.hh"
8
9#include "nv/lua/lua_raw.hh"
10
11using nv::lua::linteger;
12using nv::lua::lnumber;
13using nv::lua::lunsigned;
14
15int nv::lua::detail::upvalue_index( int i )
16{
17        return lua_upvalueindex(i);
18}
19
20bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable )
21{
22        return luaL_testudata( L, index, metatable ) != 0;
23}
24
25void* nv::lua::detail::check_userdata( lua_State *L, int index, const char* metatable )
26{
27        return luaL_checkudata( L, index, metatable );
28}
29
30void* nv::lua::detail::allocate_userdata( lua_State *L, size_t size, const char* metatable )
31{
32        void* result = lua_newuserdata(L, size);
33        luaL_setmetatable( L, metatable );
34        return result;
35}
36
37void nv::lua::detail::pop_and_discard( lua_State *L, int count )
38{
39        lua_pop( L, count );
40}
41
42void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
43{
44        lua_pushunsigned( L, v );
45}
46
47void nv::lua::detail::push_integer ( lua_State *L, linteger v )
48{
49        lua_pushinteger( L, v );
50}
51
52void nv::lua::detail::push_number  ( lua_State *L, lnumber v )
53{
54        lua_pushnumber( L, v );
55}
56
57void nv::lua::detail::push_bool    ( lua_State *L, bool v )
58{
59        lua_pushboolean( L, v );
60}
61
62void nv::lua::detail::push_string  ( lua_State *L, const std::string& s )
63{
64        lua_pushlstring( L, s.c_str(), s.size() );
65}
66
67void nv::lua::detail::push_cstring ( lua_State *L, const char* s )
68{
69        lua_pushstring( L, s );
70}
71
72void nv::lua::detail::push_pointer ( lua_State *L, void* p )
73{
74        lua_pushlightuserdata( L, p );
75}
76
77lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index )
78{
79        return lua_tounsigned( L, index );
80}
81
82linteger    nv::lua::detail::to_integer ( lua_State *L, int index )
83{
84        return lua_tointeger( L, index );
85}
86
87lnumber     nv::lua::detail::to_number  ( lua_State *L, int index )
88{
89        return lua_tonumber( L, index );
90}
91
92bool        nv::lua::detail::to_bool    ( lua_State *L, int index )
93{
94        return lua_toboolean( L, index ) != 0;
95}
96
97std::string nv::lua::detail::to_string  ( lua_State *L, int index )
98{
99        return lua_tostring( L, index );
100}
101
102const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
103{
104        return lua_tostring( L, index );
105}
106
107void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
108{
109        return lua_touserdata( L, index );
110}
111
112void* nv::lua::detail::to_ref_object  ( lua_State *L, int index )
113{
114        void* o = nullptr;
115        if ( lua_istable( L , index ) )
116        {
117                lua_pushstring( L, "__ptr" );
118                lua_rawget( L, index );
119                if ( lua_isuserdata( L, -1 ) )
120                {
121                        o = lua_touserdata( L, -1 );
122                }
123                lua_pop( L, 1 );
124        }
125        return o;
126}
127
128lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def )
129{
130        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tounsigned( L, index ) : def );
131}
132
133linteger    nv::lua::detail::to_integer ( lua_State *L, int index, linteger def )
134{
135        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tointeger( L, index ) : def );
136}
137
138lnumber     nv::lua::detail::to_number  ( lua_State *L, int index, lnumber def )
139{
140        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tonumber( L, index ) : def );
141}
142
143bool        nv::lua::detail::to_bool    ( lua_State *L, int index, bool def )
144{
145        return ( lua_type( L, index ) == LUA_TBOOLEAN ? lua_toboolean( L, index ) != 0 : def );
146}
147
148std::string nv::lua::detail::to_string  ( lua_State *L, int index, const std::string& def )
149{
150        return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
151}
152
153const char* nv::lua::detail::to_cstring ( lua_State *L, int index, const char* def )
154{
155        return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
156}
157
158void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
159{
160        return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );
161}
162
163void* nv::lua::detail::to_ref_object( lua_State *L, int index, void* def )
164{
165        void* o = def;
166        if ( lua_istable( L , index ) )
167        {
168                lua_pushstring( L, "__ptr" );
169                lua_rawget( L, index );
170                if ( lua_isuserdata( L, -1 ) )
171                {
172                        o = lua_touserdata( L, -1 );
173                }
174                lua_pop( L, 1 );
175        }
176        return o;
177}
Note: See TracBrowser for help on using the repository browser.