- Timestamp:
- 08/19/13 10:54:28 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_values.cc
r207 r208 10 10 #include "nv/object.hh" 11 11 12 void nv::lua::detail::push_value( lua_State *L, long n ) 13 { 14 lua_pushinteger( L, n ); 15 } 16 17 void nv::lua::detail::push_value( lua_State *L, double d ) 18 { 19 lua_pushnumber( L, d ); 20 } 21 22 void nv::lua::detail::push_value( lua_State *L, bool n ) 23 { 24 lua_pushboolean( L, n ); 25 } 26 27 void nv::lua::detail::push_value( lua_State *L, const char* s ) 28 { 29 lua_pushstring( L, s ); 30 } 31 32 void nv::lua::detail::push_value( lua_State *L, const std::string& s ) 33 { 34 lua_pushstring( L, s.c_str() ); 35 } 36 37 void 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 49 void nv::lua::detail::push_value( lua_State *L, void* p ) 50 { 51 lua_pushlightuserdata( L, p ); 52 } 53 54 void nv::lua::detail::pop_value( lua_State *L, int& n ) 55 { 56 n = lua_tointeger( L, -1 ); 57 lua_pop( L, 1 ); 58 } 59 60 void nv::lua::detail::pop_value( lua_State *L, long& n ) 61 { 62 n = lua_tointeger( L, -1 ); 63 lua_pop( L, 1 ); 64 } 65 66 void nv::lua::detail::pop_value( lua_State *L, double& d ) 67 { 68 d = lua_tonumber( L, -1 ); 69 lua_pop( L, 1 ); 70 } 71 72 void nv::lua::detail::pop_value( lua_State *L, bool& n ) 73 { 74 n = lua_toboolean( L, -1 ) != 0; 75 lua_pop( L, 1 ); 76 } 77 78 void nv::lua::detail::pop_value( lua_State *L, std::string& s ) 79 { 80 s = lua_tostring( L, -1 ); 81 lua_pop( L, 1 ); 82 } 83 84 void nv::lua::detail::pop_value( lua_State *L, object*& o ) 85 { 86 o = nullptr; 87 if ( lua_istable( L , -1 ) ) 88 { 89 lua_pushstring( L, "__ptr" ); 90 lua_rawget( L, -1 ); 91 if ( lua_isuserdata( L, -1 ) ) 92 { 93 o = (object*)( lua_touserdata( L, -1 ) ); 94 } 95 lua_pop( L, 1 ); 96 } 97 lua_pop( L, 1 ); 98 } 99 100 void nv::lua::detail::pop_value( lua_State *L, void*& p ) 101 { 102 p = lua_touserdata( L, -1 ); 103 lua_pop( L, 1 ); 104 } 12 using nv::lua::linteger; 13 using nv::lua::lnumber; 14 using nv::lua::lunsigned; 105 15 106 16 bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable ) … … 126 36 } 127 37 38 void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v ) 39 { 40 lua_pushunsigned( L, v ); 41 } 42 43 void nv::lua::detail::push_integer ( lua_State *L, linteger v ) 44 { 45 lua_pushinteger( L, v ); 46 } 47 48 void nv::lua::detail::push_number ( lua_State *L, lnumber v ) 49 { 50 lua_pushnumber( L, v ); 51 } 52 53 void nv::lua::detail::push_bool ( lua_State *L, bool v ) 54 { 55 lua_pushboolean( L, v ); 56 } 57 58 void nv::lua::detail::push_string ( lua_State *L, const std::string& s ) 59 { 60 lua_pushlstring( L, s.c_str(), s.size() ); 61 } 62 63 void nv::lua::detail::push_cstring ( lua_State *L, const char* s ) 64 { 65 lua_pushstring( L, s ); 66 } 67 68 void 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 80 void nv::lua::detail::push_pointer ( lua_State *L, void* p ) 81 { 82 lua_pushlightuserdata( L, p ); 83 } 84 85 lunsigned nv::lua::detail::to_unsigned( lua_State *L, int index ) 86 { 87 return lua_tounsigned( L, index ); 88 } 89 90 linteger nv::lua::detail::to_integer ( lua_State *L, int index ) 91 { 92 return lua_tointeger( L, index ); 93 } 94 95 lnumber nv::lua::detail::to_number ( lua_State *L, int index ) 96 { 97 return lua_tonumber( L, index ); 98 } 99 100 bool nv::lua::detail::to_bool ( lua_State *L, int index ) 101 { 102 return lua_toboolean( L, index ) != 0; 103 } 104 105 std::string nv::lua::detail::to_string ( lua_State *L, int index ) 106 { 107 return lua_tostring( L, index ); 108 } 109 110 const char* nv::lua::detail::to_cstring ( lua_State *L, int index ) 111 { 112 return lua_tostring( L, index ); 113 } 114 115 nv::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 131 void* nv::lua::detail::to_pointer ( lua_State *L, int index ) 132 { 133 return lua_touserdata( L, index ); 134 } 135 136 lunsigned 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 141 linteger 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 146 lnumber 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 151 bool 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 156 std::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 161 const 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 166 nv::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 182 void* 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 TracChangeset
for help on using the changeset viewer.