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

Last change on this file since 182 was 182, checked in by epyon, 12 years ago
  • lua/values - universal template-friendly any value push and retrieval with support for custom types
  • lua/function - powertool - std::function/boost::function compatible lua function storage with support for any return type and arbitrary parameters
File size: 2.0 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
12void nv::lua::detail::push_value( lua_State *L, long n )
13{
14        lua_pushinteger( L, n );
15}
16
17void nv::lua::detail::push_value( lua_State *L, double d )
18{
19        lua_pushnumber( L, d );
20}
21
22void nv::lua::detail::push_value( lua_State *L, bool n )
23{
24        lua_pushboolean( L, n );
25}
26
27void nv::lua::detail::push_value( lua_State *L, const char* s )
28{
29        lua_pushstring( L, s );
30}
31
32void nv::lua::detail::push_value( lua_State *L, const std::string& s )
33{
34        lua_pushstring( L, s.c_str() );
35}
36
37void nv::lua::detail::push_value( lua_State *L, object* o )
38{
39        if ( o == nullptr )
40        {
41                lua_pushnil( L );
42        }
43        else
44        {
45                lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
46        }
47}
48
49void nv::lua::detail::push_value( lua_State *L, void* p )
50{
51        lua_pushlightuserdata( L, p );
52}
53
54void nv::lua::detail::push_value( lua_State *L, const passer& p )
55{
56        p.push(L);
57}
58
59void nv::lua::detail::pop_value( lua_State *L, long& n )
60{
61        n = lua_tointeger( L, -1 );
62        lua_pop( L, 1 );
63}
64
65void nv::lua::detail::pop_value( lua_State *L, double& d )
66{
67        d = lua_tonumber( L, -1 );
68        lua_pop( L, 1 );
69}
70
71void nv::lua::detail::pop_value( lua_State *L, bool& n )
72{
73        n = lua_toboolean( L, -1 ) != 0;
74        lua_pop( L, 1 );
75}
76
77void nv::lua::detail::pop_value( lua_State *L, std::string& s )
78{
79        s = lua_tostring( L, -1 );
80        lua_pop( L, 1 );
81}
82
83void nv::lua::detail::pop_value( lua_State *L, object*& o )
84{
85        o = nullptr;
86        if ( lua_istable( L , -1 ) )
87        {
88                lua_pushstring( L, "__ptr" );
89                lua_rawget( L, -1 );
90                if ( lua_isuserdata( L, -1 ) )
91                {
92                        o = (object*)( lua_touserdata( L, -1 ) );
93                }
94                lua_pop( L, 1 );
95        }
96        lua_pop( L, 1 );
97}
98
99void nv::lua::detail::pop_value( lua_State *L, void*& p )
100{
101        p = lua_touserdata( L, -1 );
102        lua_pop( L, 1 );
103}
104
Note: See TracBrowser for help on using the repository browser.