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

Last change on this file since 262 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
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
[208]11using nv::lua::linteger;
12using nv::lua::lnumber;
13using nv::lua::lunsigned;
14
[213]15int nv::lua::detail::upvalue_index( int i )
16{
17        return lua_upvalueindex(i);
18}
19
[208]20bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable )
[182]21{
[208]22        return luaL_testudata( L, index, metatable ) != 0;
[182]23}
24
[208]25void* nv::lua::detail::check_userdata( lua_State *L, int index, const char* metatable )
[182]26{
[208]27        return luaL_checkudata( L, index, metatable );
[182]28}
29
[208]30void* nv::lua::detail::allocate_userdata( lua_State *L, size_t size, const char* metatable )
[182]31{
[208]32        void* result = lua_newuserdata(L, size);
33        luaL_setmetatable( L, metatable );
34        return result;
[182]35}
36
[208]37void nv::lua::detail::pop_and_discard( lua_State *L, int count )
[182]38{
[208]39        lua_pop( L, count );
[182]40}
41
[208]42void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
[182]43{
[208]44        lua_pushunsigned( L, v );
[182]45}
46
[208]47void nv::lua::detail::push_integer ( lua_State *L, linteger v )
[182]48{
[208]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 )
[182]73{
74        lua_pushlightuserdata( L, p );
75}
76
[208]77lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index )
[206]78{
[208]79        return lua_tounsigned( L, index );
[206]80}
81
[208]82linteger    nv::lua::detail::to_integer ( lua_State *L, int index )
[182]83{
[208]84        return lua_tointeger( L, index );
[182]85}
86
[208]87lnumber     nv::lua::detail::to_number  ( lua_State *L, int index )
[182]88{
[208]89        return lua_tonumber( L, index );
[182]90}
91
[208]92bool        nv::lua::detail::to_bool    ( lua_State *L, int index )
[182]93{
[208]94        return lua_toboolean( L, index ) != 0;
[182]95}
96
[208]97std::string nv::lua::detail::to_string  ( lua_State *L, int index )
[182]98{
[208]99        return lua_tostring( L, index );
[182]100}
101
[208]102const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
[182]103{
[208]104        return lua_tostring( L, index );
105}
106
[262]107void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
[208]108{
[262]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;
[208]115        if ( lua_istable( L , index ) )
[182]116        {
117                lua_pushstring( L, "__ptr" );
[208]118                lua_rawget( L, index );
[182]119                if ( lua_isuserdata( L, -1 ) )
120                {
[262]121                        o = lua_touserdata( L, -1 );
[182]122                }
123                lua_pop( L, 1 );
124        }
[208]125        return o;
[182]126}
127
[208]128lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def )
[207]129{
[208]130        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tounsigned( L, index ) : def );
[207]131}
132
[208]133linteger    nv::lua::detail::to_integer ( lua_State *L, int index, linteger def )
[207]134{
[208]135        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tointeger( L, index ) : def );
[207]136}
137
[208]138lnumber     nv::lua::detail::to_number  ( lua_State *L, int index, lnumber def )
[207]139{
[208]140        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tonumber( L, index ) : def );
[207]141}
142
[208]143bool        nv::lua::detail::to_bool    ( lua_State *L, int index, bool def )
[207]144{
[208]145        return ( lua_type( L, index ) == LUA_TBOOLEAN ? lua_toboolean( L, index ) != 0 : def );
[207]146}
147
[208]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
[262]158void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
[208]159{
[262]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;
[208]166        if ( lua_istable( L , index ) )
167        {
168                lua_pushstring( L, "__ptr" );
169                lua_rawget( L, index );
170                if ( lua_isuserdata( L, -1 ) )
171                {
[262]172                        o = lua_touserdata( L, -1 );
[208]173                }
174                lua_pop( L, 1 );
175        }
176        return o;
177}
Note: See TracBrowser for help on using the repository browser.