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

Last change on this file since 212 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
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
16bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable )
[182]17{
[208]18        return luaL_testudata( L, index, metatable ) != 0;
[182]19}
20
[208]21void* nv::lua::detail::check_userdata( lua_State *L, int index, const char* metatable )
[182]22{
[208]23        return luaL_checkudata( L, index, metatable );
[182]24}
25
[208]26void* nv::lua::detail::allocate_userdata( lua_State *L, size_t size, const char* metatable )
[182]27{
[208]28        void* result = lua_newuserdata(L, size);
29        luaL_setmetatable( L, metatable );
30        return result;
[182]31}
32
[208]33void nv::lua::detail::pop_and_discard( lua_State *L, int count )
[182]34{
[208]35        lua_pop( L, count );
[182]36}
37
[208]38void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
[182]39{
[208]40        lua_pushunsigned( L, v );
[182]41}
42
[208]43void nv::lua::detail::push_integer ( lua_State *L, linteger v )
[182]44{
[208]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{
[182]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
[208]80void nv::lua::detail::push_pointer ( lua_State *L, void* p )
[182]81{
82        lua_pushlightuserdata( L, p );
83}
84
[208]85lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index )
[206]86{
[208]87        return lua_tounsigned( L, index );
[206]88}
89
[208]90linteger    nv::lua::detail::to_integer ( lua_State *L, int index )
[182]91{
[208]92        return lua_tointeger( L, index );
[182]93}
94
[208]95lnumber     nv::lua::detail::to_number  ( lua_State *L, int index )
[182]96{
[208]97        return lua_tonumber( L, index );
[182]98}
99
[208]100bool        nv::lua::detail::to_bool    ( lua_State *L, int index )
[182]101{
[208]102        return lua_toboolean( L, index ) != 0;
[182]103}
104
[208]105std::string nv::lua::detail::to_string  ( lua_State *L, int index )
[182]106{
[208]107        return lua_tostring( L, index );
[182]108}
109
[208]110const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
[182]111{
[208]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 ) )
[182]119        {
120                lua_pushstring( L, "__ptr" );
[208]121                lua_rawget( L, index );
[182]122                if ( lua_isuserdata( L, -1 ) )
123                {
[208]124                        o = static_cast<object*>( lua_touserdata( L, -1 ) );
[182]125                }
126                lua_pop( L, 1 );
127        }
[208]128        return o;
[182]129}
130
[208]131void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
[182]132{
[208]133        return lua_touserdata( L, index );
[182]134}
135
[208]136lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def )
[207]137{
[208]138        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tounsigned( L, index ) : def );
[207]139}
140
[208]141linteger    nv::lua::detail::to_integer ( lua_State *L, int index, linteger def )
[207]142{
[208]143        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tointeger( L, index ) : def );
[207]144}
145
[208]146lnumber     nv::lua::detail::to_number  ( lua_State *L, int index, lnumber def )
[207]147{
[208]148        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tonumber( L, index ) : def );
[207]149}
150
[208]151bool        nv::lua::detail::to_bool    ( lua_State *L, int index, bool def )
[207]152{
[208]153        return ( lua_type( L, index ) == LUA_TBOOLEAN ? lua_toboolean( L, index ) != 0 : def );
[207]154}
155
[208]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.