1 //------------------------------------------------------------------------------
3 https://github.com/vinniefalco/LuaBridge
5 Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
6 Copyright 2007, Nathan Reed
8 License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 //==============================================================================
30 //------------------------------------------------------------------------------
32 Receive the lua_State* as an argument.
35 struct Stack <lua_State*>
37 static lua_State* get (lua_State* L, int)
43 //------------------------------------------------------------------------------
48 struct Stack <lua_CFunction>
50 static void push (lua_State* L, lua_CFunction f)
52 lua_pushcfunction (L, f);
55 static lua_CFunction get (lua_State* L, int index)
57 return lua_tocfunction (L, index);
61 //------------------------------------------------------------------------------
63 Stack specialization for `int`.
68 static inline void push (lua_State* L, int value)
70 lua_pushinteger (L, static_cast <lua_Integer> (value));
73 static inline int get (lua_State* L, int index)
75 return static_cast <int> (luaL_checkinteger (L, index));
80 struct Stack <int const&>
82 static inline void push (lua_State* L, int value)
84 lua_pushnumber (L, static_cast <lua_Number> (value));
87 static inline int get (lua_State* L, int index)
89 return static_cast <int > (luaL_checknumber (L, index));
92 //------------------------------------------------------------------------------
94 Stack specialization for `unsigned int`.
97 struct Stack <unsigned int>
99 static inline void push (lua_State* L, unsigned int value)
101 lua_pushinteger (L, static_cast <lua_Integer> (value));
104 static inline unsigned int get (lua_State* L, int index)
106 return static_cast <unsigned int> (luaL_checkinteger (L, index));
111 struct Stack <unsigned int const&>
113 static inline void push (lua_State* L, unsigned int value)
115 lua_pushnumber (L, static_cast <lua_Number> (value));
118 static inline unsigned int get (lua_State* L, int index)
120 return static_cast <unsigned int > (luaL_checknumber (L, index));
124 //------------------------------------------------------------------------------
126 Stack specialization for `unsigned char`.
129 struct Stack <unsigned char>
131 static inline void push (lua_State* L, unsigned char value)
133 lua_pushinteger (L, static_cast <lua_Integer> (value));
136 static inline unsigned char get (lua_State* L, int index)
138 return static_cast <unsigned char> (luaL_checkinteger (L, index));
143 struct Stack <unsigned char const&>
145 static inline void push (lua_State* L, unsigned char value)
147 lua_pushnumber (L, static_cast <lua_Number> (value));
150 static inline unsigned char get (lua_State* L, int index)
152 return static_cast <unsigned char> (luaL_checknumber (L, index));
156 //------------------------------------------------------------------------------
158 Stack specialization for `short`.
163 static inline void push (lua_State* L, short value)
165 lua_pushinteger (L, static_cast <lua_Integer> (value));
168 static inline short get (lua_State* L, int index)
170 return static_cast <short> (luaL_checkinteger (L, index));
175 struct Stack <short const&>
177 static inline void push (lua_State* L, short value)
179 lua_pushnumber (L, static_cast <lua_Number> (value));
182 static inline short get (lua_State* L, int index)
184 return static_cast <short> (luaL_checknumber (L, index));
188 //------------------------------------------------------------------------------
190 Stack specialization for `unsigned short`.
193 struct Stack <unsigned short>
195 static inline void push (lua_State* L, unsigned short value)
197 lua_pushinteger (L, static_cast <lua_Integer> (value));
200 static inline unsigned short get (lua_State* L, int index)
202 return static_cast <unsigned short> (luaL_checkinteger (L, index));
207 struct Stack <unsigned short const&>
209 static inline void push (lua_State* L, unsigned short value)
211 lua_pushnumber (L, static_cast <lua_Number> (value));
214 static inline unsigned short get (lua_State* L, int index)
216 return static_cast <unsigned short> (luaL_checknumber (L, index));
220 //------------------------------------------------------------------------------
222 Stack specialization for `long`.
227 static inline void push (lua_State* L, long value)
229 lua_pushinteger (L, static_cast <lua_Integer> (value));
232 static inline long get (lua_State* L, int index)
234 return static_cast <long> (luaL_checkinteger (L, index));
239 struct Stack <long const&>
241 static inline void push (lua_State* L, long value)
243 lua_pushnumber (L, static_cast <lua_Number> (value));
246 static inline long get (lua_State* L, int index)
248 return static_cast <long> (luaL_checknumber (L, index));
252 //------------------------------------------------------------------------------
254 Stack specialization for `unsigned long`.
257 struct Stack <unsigned long>
259 static inline void push (lua_State* L, unsigned long value)
261 lua_pushinteger (L, static_cast <lua_Integer> (value));
264 static inline unsigned long get (lua_State* L, int index)
266 return static_cast <unsigned long> (luaL_checkinteger (L, index));
271 struct Stack <unsigned long const&>
273 static inline void push (lua_State* L, unsigned long value)
275 lua_pushnumber (L, static_cast <lua_Number> (value));
278 static inline unsigned long get (lua_State* L, int index)
280 return static_cast <unsigned long> (luaL_checknumber (L, index));
284 //------------------------------------------------------------------------------
286 Stack specialization for `float`.
291 static inline void push (lua_State* L, float value)
293 lua_pushnumber (L, static_cast <lua_Number> (value));
296 static inline float get (lua_State* L, int index)
298 return static_cast <float> (luaL_checknumber (L, index));
303 struct Stack <float const&>
305 static inline void push (lua_State* L, float value)
307 lua_pushnumber (L, static_cast <lua_Number> (value));
310 static inline float get (lua_State* L, int index)
312 return static_cast <float> (luaL_checknumber (L, index));
316 //------------------------------------------------------------------------------
318 Stack specialization for `double`.
320 template <> struct Stack <double>
322 static inline void push (lua_State* L, double value)
324 lua_pushnumber (L, static_cast <lua_Number> (value));
327 static inline double get (lua_State* L, int index)
329 return static_cast <double> (luaL_checknumber (L, index));
333 template <> struct Stack <double const&>
335 static inline void push (lua_State* L, double value)
337 lua_pushnumber (L, static_cast <lua_Number> (value));
340 static inline double get (lua_State* L, int index)
342 return static_cast <double> (luaL_checknumber (L, index));
346 //------------------------------------------------------------------------------
348 Stack specialization for `bool`.
351 struct Stack <bool> {
352 static inline void push (lua_State* L, bool value)
354 lua_pushboolean (L, value ? 1 : 0);
357 static inline bool get (lua_State* L, int index)
359 return lua_toboolean (L, index) ? true : false;
364 struct Stack <bool const&> {
365 static inline void push (lua_State* L, bool value)
367 lua_pushboolean (L, value ? 1 : 0);
370 static inline bool get (lua_State* L, int index)
372 return lua_toboolean (L, index) ? true : false;
376 //------------------------------------------------------------------------------
378 Stack specialization for `char`.
383 static inline void push (lua_State* L, char value)
385 char str [2] = { value, 0 };
386 lua_pushstring (L, str);
389 static inline char get (lua_State* L, int index)
391 return luaL_checkstring (L, index) [0];
396 struct Stack <char const&>
398 static inline void push (lua_State* L, char value)
400 char str [2] = { value, 0 };
401 lua_pushstring (L, str);
404 static inline char get (lua_State* L, int index)
406 return luaL_checkstring (L, index) [0];
410 //------------------------------------------------------------------------------
412 Stack specialization for `float`.
415 struct Stack <char const*>
417 static inline void push (lua_State* L, char const* str)
420 lua_pushstring (L, str);
425 static inline char const* get (lua_State* L, int index)
427 return lua_isnil (L, index) ? 0 : luaL_checkstring (L, index);
431 //------------------------------------------------------------------------------
433 Stack specialization for `std::string`.
436 struct Stack <std::string>
438 static inline void push (lua_State* L, std::string const& str)
440 lua_pushlstring (L, str.c_str (), str.size());
443 static inline std::string get (lua_State* L, int index)
446 const char *str = luaL_checklstring(L, index, &len);
447 return std::string (str, len);
451 //------------------------------------------------------------------------------
453 Stack specialization for `std::string const&`.
456 struct Stack <std::string const&>
458 static inline void push (lua_State* L, std::string const& str)
460 lua_pushstring (L, str.c_str());
463 static inline std::string get (lua_State* L, int index)
466 const char *str = luaL_checklstring(L, index, &len);
467 return std::string (str, len);