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

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