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

Last change on this file since 215 was 213, checked in by epyon, 12 years ago
  • lua/state - upvalue_index moved to lua/values
File size: 4.6 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#include "nv/object.hh"
11
12using nv::lua::linteger;
13using nv::lua::lnumber;
14using nv::lua::lunsigned;
15
16int nv::lua::detail::upvalue_index( int i )
17{
18        return lua_upvalueindex(i);
19}
20
21bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable )
22{
23        return luaL_testudata( L, index, metatable ) != 0;
24}
25
26void* nv::lua::detail::check_userdata( lua_State *L, int index, const char* metatable )
27{
28        return luaL_checkudata( L, index, metatable );
29}
30
31void* nv::lua::detail::allocate_userdata( lua_State *L, size_t size, const char* metatable )
32{
33        void* result = lua_newuserdata(L, size);
34        luaL_setmetatable( L, metatable );
35        return result;
36}
37
38void nv::lua::detail::pop_and_discard( lua_State *L, int count )
39{
40        lua_pop( L, count );
41}
42
43void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
44{
45        lua_pushunsigned( L, v );
46}
47
48void nv::lua::detail::push_integer ( lua_State *L, linteger v )
49{
50        lua_pushinteger( L, v );
51}
52
53void nv::lua::detail::push_number  ( lua_State *L, lnumber v )
54{
55        lua_pushnumber( L, v );
56}
57
58void nv::lua::detail::push_bool    ( lua_State *L, bool v )
59{
60        lua_pushboolean( L, v );
61}
62
63void nv::lua::detail::push_string  ( lua_State *L, const std::string& s )
64{
65        lua_pushlstring( L, s.c_str(), s.size() );
66}
67
68void nv::lua::detail::push_cstring ( lua_State *L, const char* s )
69{
70        lua_pushstring( L, s );
71}
72
73void nv::lua::detail::push_object  ( lua_State *L, object* o )
74{
75        if ( o == nullptr )
76        {
77                lua_pushnil( L );
78        }
79        else
80        {
81                lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
82        }
83}
84
85void nv::lua::detail::push_pointer ( lua_State *L, void* p )
86{
87        lua_pushlightuserdata( L, p );
88}
89
90lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index )
91{
92        return lua_tounsigned( L, index );
93}
94
95linteger    nv::lua::detail::to_integer ( lua_State *L, int index )
96{
97        return lua_tointeger( L, index );
98}
99
100lnumber     nv::lua::detail::to_number  ( lua_State *L, int index )
101{
102        return lua_tonumber( L, index );
103}
104
105bool        nv::lua::detail::to_bool    ( lua_State *L, int index )
106{
107        return lua_toboolean( L, index ) != 0;
108}
109
110std::string nv::lua::detail::to_string  ( lua_State *L, int index )
111{
112        return lua_tostring( L, index );
113}
114
115const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
116{
117        return lua_tostring( L, index );
118}
119
120nv::object* nv::lua::detail::to_object  ( lua_State *L, int index )
121{
122        object* o = nullptr;
123        if ( lua_istable( L , index ) )
124        {
125                lua_pushstring( L, "__ptr" );
126                lua_rawget( L, index );
127                if ( lua_isuserdata( L, -1 ) )
128                {
129                        o = static_cast<object*>( lua_touserdata( L, -1 ) );
130                }
131                lua_pop( L, 1 );
132        }
133        return o;
134}
135
136void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
137{
138        return lua_touserdata( L, index );
139}
140
141lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def )
142{
143        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tounsigned( L, index ) : def );
144}
145
146linteger    nv::lua::detail::to_integer ( lua_State *L, int index, linteger def )
147{
148        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tointeger( L, index ) : def );
149}
150
151lnumber     nv::lua::detail::to_number  ( lua_State *L, int index, lnumber def )
152{
153        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tonumber( L, index ) : def );
154}
155
156bool        nv::lua::detail::to_bool    ( lua_State *L, int index, bool def )
157{
158        return ( lua_type( L, index ) == LUA_TBOOLEAN ? lua_toboolean( L, index ) != 0 : def );
159}
160
161std::string nv::lua::detail::to_string  ( lua_State *L, int index, const std::string& def )
162{
163        return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
164}
165
166const char* nv::lua::detail::to_cstring ( lua_State *L, int index, const char* def )
167{
168        return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
169}
170
171nv::object* nv::lua::detail::to_object  ( lua_State *L, int index, nv::object* def )
172{
173        object* o = def;
174        if ( lua_istable( L , index ) )
175        {
176                lua_pushstring( L, "__ptr" );
177                lua_rawget( L, index );
178                if ( lua_isuserdata( L, -1 ) )
179                {
180                        o = static_cast<object*>( lua_touserdata( L, -1 ) );
181                }
182                lua_pop( L, 1 );
183        }
184        return o;
185}
186
187void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
188{
189        return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );
190}
Note: See TracBrowser for help on using the repository browser.