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

Last change on this file since 368 was 360, checked in by epyon, 10 years ago
  • more string_ref usage
  • string_ref moved to string.hh
  • initial const_string implemenation
File size: 4.9 KB
Line 
1// Copyright (C) 2012-2014 ChaosForge Ltd
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_nil( lua_State *L )
43{
44        lua_pushnil( L );
45}
46
47void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
48{
49        lua_pushunsigned( L, v );
50}
51
52void nv::lua::detail::push_integer ( lua_State *L, linteger v )
53{
54        lua_pushinteger( L, v );
55}
56
57void nv::lua::detail::push_number  ( lua_State *L, lnumber v )
58{
59        lua_pushnumber( L, v );
60}
61
62void nv::lua::detail::push_bool    ( lua_State *L, bool v )
63{
64        lua_pushboolean( L, v );
65}
66
67void nv::lua::detail::push_string  ( lua_State *L, const std::string& s )
68{
69        lua_pushlstring( L, s.c_str(), s.size() );
70}
71
72void nv::lua::detail::push_cstring ( lua_State *L, const char* s )
73{
74        lua_pushstring( L, s );
75}
76
77void nv::lua::detail::push_string_ref( lua_State *L, string_ref s )
78{
79        lua_pushlstring( L, s.data(), s.size() );
80}
81
82void nv::lua::detail::push_pointer ( lua_State *L, void* p )
83{
84        lua_pushlightuserdata( L, p );
85}
86
87void nv::lua::detail::push_ref_object ( lua_State *L, ref object )
88{
89        lua_rawgeti( L, LUA_REGISTRYINDEX, object.get() );
90}
91
92
93lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index )
94{
95        return lua_tounsigned( L, index );
96}
97
98linteger    nv::lua::detail::to_integer ( lua_State *L, int index )
99{
100        return lua_tointeger( L, index );
101}
102
103lnumber     nv::lua::detail::to_number  ( lua_State *L, int index )
104{
105        return lua_tonumber( L, index );
106}
107
108bool        nv::lua::detail::to_bool    ( lua_State *L, int index )
109{
110        return lua_toboolean( L, index ) != 0;
111}
112
113std::string nv::lua::detail::to_string  ( lua_State *L, int index )
114{
115        size_t length = 0;
116        const char* result = lua_tolstring( L, index, &length );
117        return std::string( result, length );
118}
119
120const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
121{
122        return lua_tolstring( L, index, nullptr );
123}
124
125nv::string_ref nv::lua::detail::to_string_ref( lua_State *L, int index )
126{
127        size_t length = 0;
128        const char* result = lua_tolstring( L, index, &length );
129        return string_ref( result, length );
130}
131
132void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
133{
134        return lua_touserdata( L, index );
135}
136
137void* nv::lua::detail::to_ref_object  ( lua_State *L, int index )
138{
139        void* o = nullptr;
140        if ( lua_istable( L , index ) )
141        {
142                lua_pushstring( L, "__ptr" );
143                lua_rawget( L, index );
144                if ( lua_isuserdata( L, -1 ) )
145                {
146                        o = lua_touserdata( L, -1 );
147                }
148                lua_pop( L, 1 );
149        }
150        return o;
151}
152
153lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def )
154{
155        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tounsigned( L, index ) : def );
156}
157
158linteger    nv::lua::detail::to_integer ( lua_State *L, int index, linteger def )
159{
160        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tointeger( L, index ) : def );
161}
162
163lnumber     nv::lua::detail::to_number  ( lua_State *L, int index, lnumber def )
164{
165        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tonumber( L, index ) : def );
166}
167
168bool        nv::lua::detail::to_bool    ( lua_State *L, int index, bool def )
169{
170        return ( lua_type( L, index ) == LUA_TBOOLEAN ? lua_toboolean( L, index ) != 0 : def );
171}
172
173std::string nv::lua::detail::to_string  ( lua_State *L, int index, const std::string& def )
174{
175        return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
176}
177
178const char* nv::lua::detail::to_cstring ( lua_State *L, int index, const char* def )
179{
180        return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
181}
182
183void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
184{
185        return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );
186}
187
188void* nv::lua::detail::to_ref_object( lua_State *L, int index, void* def )
189{
190        void* o = def;
191        if ( lua_istable( L , index ) )
192        {
193                lua_pushliteral( L, "__ptr" );
194                lua_rawget( L, index );
195                if ( lua_isuserdata( L, -1 ) )
196                {
197                        o = lua_touserdata( L, -1 );
198                }
199                lua_pop( L, 1 );
200        }
201        return o;
202}
Note: See TracBrowser for help on using the repository browser.