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

Last change on this file since 228 was 213, checked in by epyon, 12 years ago
  • lua/state - upvalue_index moved to lua/values
File size: 4.6 KB
RevLine 
[182]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
[208]12using nv::lua::linteger;
13using nv::lua::lnumber;
14using nv::lua::lunsigned;
15
[213]16int nv::lua::detail::upvalue_index( int i )
17{
18        return lua_upvalueindex(i);
19}
20
[208]21bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable )
[182]22{
[208]23        return luaL_testudata( L, index, metatable ) != 0;
[182]24}
25
[208]26void* nv::lua::detail::check_userdata( lua_State *L, int index, const char* metatable )
[182]27{
[208]28        return luaL_checkudata( L, index, metatable );
[182]29}
30
[208]31void* nv::lua::detail::allocate_userdata( lua_State *L, size_t size, const char* metatable )
[182]32{
[208]33        void* result = lua_newuserdata(L, size);
34        luaL_setmetatable( L, metatable );
35        return result;
[182]36}
37
[208]38void nv::lua::detail::pop_and_discard( lua_State *L, int count )
[182]39{
[208]40        lua_pop( L, count );
[182]41}
42
[208]43void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
[182]44{
[208]45        lua_pushunsigned( L, v );
[182]46}
47
[208]48void nv::lua::detail::push_integer ( lua_State *L, linteger v )
[182]49{
[208]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{
[182]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
[208]85void nv::lua::detail::push_pointer ( lua_State *L, void* p )
[182]86{
87        lua_pushlightuserdata( L, p );
88}
89
[208]90lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index )
[206]91{
[208]92        return lua_tounsigned( L, index );
[206]93}
94
[208]95linteger    nv::lua::detail::to_integer ( lua_State *L, int index )
[182]96{
[208]97        return lua_tointeger( L, index );
[182]98}
99
[208]100lnumber     nv::lua::detail::to_number  ( lua_State *L, int index )
[182]101{
[208]102        return lua_tonumber( L, index );
[182]103}
104
[208]105bool        nv::lua::detail::to_bool    ( lua_State *L, int index )
[182]106{
[208]107        return lua_toboolean( L, index ) != 0;
[182]108}
109
[208]110std::string nv::lua::detail::to_string  ( lua_State *L, int index )
[182]111{
[208]112        return lua_tostring( L, index );
[182]113}
114
[208]115const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
[182]116{
[208]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 ) )
[182]124        {
125                lua_pushstring( L, "__ptr" );
[208]126                lua_rawget( L, index );
[182]127                if ( lua_isuserdata( L, -1 ) )
128                {
[208]129                        o = static_cast<object*>( lua_touserdata( L, -1 ) );
[182]130                }
131                lua_pop( L, 1 );
132        }
[208]133        return o;
[182]134}
135
[208]136void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
[182]137{
[208]138        return lua_touserdata( L, index );
[182]139}
140
[208]141lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def )
[207]142{
[208]143        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tounsigned( L, index ) : def );
[207]144}
145
[208]146linteger    nv::lua::detail::to_integer ( lua_State *L, int index, linteger def )
[207]147{
[208]148        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tointeger( L, index ) : def );
[207]149}
150
[208]151lnumber     nv::lua::detail::to_number  ( lua_State *L, int index, lnumber def )
[207]152{
[208]153        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tonumber( L, index ) : def );
[207]154}
155
[208]156bool        nv::lua::detail::to_bool    ( lua_State *L, int index, bool def )
[207]157{
[208]158        return ( lua_type( L, index ) == LUA_TBOOLEAN ? lua_toboolean( L, index ) != 0 : def );
[207]159}
160
[208]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.