1 //------------------------------------------------------------------------------
3 https://github.com/vinniefalco/LuaBridge
5 Copyright 2016, Robin Gareus <robin@gareus.org>
6 Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
7 Copyright 2007, Nathan Reed
9 License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 //==============================================================================
31 //------------------------------------------------------------------------------
33 Receive the lua_State* as an argument.
36 struct Stack <lua_State*>
38 static lua_State* get (lua_State* L, int)
44 //------------------------------------------------------------------------------
49 struct Stack <lua_CFunction>
51 static void push (lua_State* L, lua_CFunction f)
53 lua_pushcfunction (L, f);
56 static lua_CFunction get (lua_State* L, int index)
58 return lua_tocfunction (L, index);
62 //------------------------------------------------------------------------------
64 Stack specialization for `int`.
69 static inline void push (lua_State* L, int value)
71 lua_pushinteger (L, static_cast <lua_Integer> (value));
74 static inline int get (lua_State* L, int index)
76 return static_cast <int> (luaL_checkinteger (L, index));
81 struct Stack <int const&>
83 static inline void push (lua_State* L, int value)
85 lua_pushnumber (L, static_cast <lua_Number> (value));
88 static inline int get (lua_State* L, int index)
90 return static_cast <int > (luaL_checknumber (L, index));
93 //------------------------------------------------------------------------------
95 Stack specialization for `unsigned int`.
98 struct Stack <unsigned int>
100 static inline void push (lua_State* L, unsigned int value)
102 lua_pushinteger (L, static_cast <lua_Integer> (value));
105 static inline unsigned int get (lua_State* L, int index)
107 return static_cast <unsigned int> (luaL_checkinteger (L, index));
112 struct Stack <unsigned int const&>
114 static inline void push (lua_State* L, unsigned int value)
116 lua_pushnumber (L, static_cast <lua_Number> (value));
119 static inline unsigned int get (lua_State* L, int index)
121 return static_cast <unsigned int > (luaL_checknumber (L, index));
125 //------------------------------------------------------------------------------
127 Stack specialization for `unsigned char`.
130 struct Stack <unsigned char>
132 static inline void push (lua_State* L, unsigned char value)
134 lua_pushinteger (L, static_cast <lua_Integer> (value));
137 static inline unsigned char get (lua_State* L, int index)
139 return static_cast <unsigned char> (luaL_checkinteger (L, index));
144 struct Stack <unsigned char const&>
146 static inline void push (lua_State* L, unsigned char value)
148 lua_pushnumber (L, static_cast <lua_Number> (value));
151 static inline unsigned char get (lua_State* L, int index)
153 return static_cast <unsigned char> (luaL_checknumber (L, index));
157 //------------------------------------------------------------------------------
159 Stack specialization for `short`.
164 static inline void push (lua_State* L, short value)
166 lua_pushinteger (L, static_cast <lua_Integer> (value));
169 static inline short get (lua_State* L, int index)
171 return static_cast <short> (luaL_checkinteger (L, index));
176 struct Stack <short const&>
178 static inline void push (lua_State* L, short value)
180 lua_pushnumber (L, static_cast <lua_Number> (value));
183 static inline short get (lua_State* L, int index)
185 return static_cast <short> (luaL_checknumber (L, index));
189 //------------------------------------------------------------------------------
191 Stack specialization for `unsigned short`.
194 struct Stack <unsigned short>
196 static inline void push (lua_State* L, unsigned short value)
198 lua_pushinteger (L, static_cast <lua_Integer> (value));
201 static inline unsigned short get (lua_State* L, int index)
203 return static_cast <unsigned short> (luaL_checkinteger (L, index));
208 struct Stack <unsigned short const&>
210 static inline void push (lua_State* L, unsigned short value)
212 lua_pushnumber (L, static_cast <lua_Number> (value));
215 static inline unsigned short get (lua_State* L, int index)
217 return static_cast <unsigned short> (luaL_checknumber (L, index));
221 //------------------------------------------------------------------------------
223 Stack specialization for `long`.
228 static inline void push (lua_State* L, long value)
230 lua_pushinteger (L, static_cast <lua_Integer> (value));
233 static inline long get (lua_State* L, int index)
235 return static_cast <long> (luaL_checkinteger (L, index));
240 struct Stack <long const&>
242 static inline void push (lua_State* L, long value)
244 lua_pushnumber (L, static_cast <lua_Number> (value));
247 static inline long get (lua_State* L, int index)
249 return static_cast <long> (luaL_checknumber (L, index));
253 //------------------------------------------------------------------------------
255 Stack specialization for `unsigned long`.
258 struct Stack <unsigned long>
260 static inline void push (lua_State* L, unsigned long value)
262 lua_pushinteger (L, static_cast <lua_Integer> (value));
265 static inline unsigned long get (lua_State* L, int index)
267 return static_cast <unsigned long> (luaL_checkinteger (L, index));
272 struct Stack <unsigned long const&>
274 static inline void push (lua_State* L, unsigned long value)
276 lua_pushnumber (L, static_cast <lua_Number> (value));
279 static inline unsigned long get (lua_State* L, int index)
281 return static_cast <unsigned long> (luaL_checknumber (L, index));
285 //------------------------------------------------------------------------------
287 Stack specialization for `long long`.
290 struct Stack <long long>
292 static inline void push (lua_State* L, long long value)
294 lua_pushinteger (L, static_cast <lua_Integer> (value));
297 static inline long long get (lua_State* L, int index)
299 return static_cast <long long> (luaL_checkinteger (L, index));
304 struct Stack <long long const&>
306 static inline void push (lua_State* L, long long value)
308 lua_pushnumber (L, static_cast <lua_Number> (value));
311 static inline long long get (lua_State* L, int index)
313 return static_cast <long long> (luaL_checknumber (L, index));
317 //------------------------------------------------------------------------------
319 Stack specialization for `unsigned long long`.
322 struct Stack <unsigned long long>
324 static inline void push (lua_State* L, unsigned long long value)
326 lua_pushinteger (L, static_cast <lua_Integer> (value));
329 static inline unsigned long long get (lua_State* L, int index)
331 return static_cast <unsigned long long> (luaL_checkinteger (L, index));
336 struct Stack <unsigned long long const&>
338 static inline void push (lua_State* L, unsigned long long value)
340 lua_pushnumber (L, static_cast <lua_Number> (value));
343 static inline unsigned long long get (lua_State* L, int index)
345 return static_cast <unsigned long long> (luaL_checknumber (L, index));
349 //------------------------------------------------------------------------------
351 Stack specialization for `float`.
356 static inline void push (lua_State* L, float value)
358 lua_pushnumber (L, static_cast <lua_Number> (value));
361 static inline float get (lua_State* L, int index)
363 return static_cast <float> (luaL_checknumber (L, index));
368 struct Stack <float const&>
370 static inline void push (lua_State* L, float value)
372 lua_pushnumber (L, static_cast <lua_Number> (value));
375 static inline float get (lua_State* L, int index)
377 return static_cast <float> (luaL_checknumber (L, index));
381 //------------------------------------------------------------------------------
383 Stack specialization for `double`.
385 template <> struct Stack <double>
387 static inline void push (lua_State* L, double value)
389 lua_pushnumber (L, static_cast <lua_Number> (value));
392 static inline double get (lua_State* L, int index)
394 return static_cast <double> (luaL_checknumber (L, index));
398 template <> struct Stack <double const&>
400 static inline void push (lua_State* L, double value)
402 lua_pushnumber (L, static_cast <lua_Number> (value));
405 static inline double get (lua_State* L, int index)
407 return static_cast <double> (luaL_checknumber (L, index));
411 //------------------------------------------------------------------------------
413 Stack specialization for `bool`.
416 struct Stack <bool> {
417 static inline void push (lua_State* L, bool value)
419 lua_pushboolean (L, value ? 1 : 0);
422 static inline bool get (lua_State* L, int index)
424 return lua_toboolean (L, index) ? true : false;
429 struct Stack <bool const&> {
430 static inline void push (lua_State* L, bool value)
432 lua_pushboolean (L, value ? 1 : 0);
435 static inline bool get (lua_State* L, int index)
437 return lua_toboolean (L, index) ? true : false;
441 //------------------------------------------------------------------------------
443 Stack specialization for `char`.
448 static inline void push (lua_State* L, char value)
450 char str [2] = { value, 0 };
451 lua_pushstring (L, str);
454 static inline char get (lua_State* L, int index)
456 return luaL_checkstring (L, index) [0];
461 struct Stack <char const&>
463 static inline void push (lua_State* L, char value)
465 char str [2] = { value, 0 };
466 lua_pushstring (L, str);
469 static inline char get (lua_State* L, int index)
471 return luaL_checkstring (L, index) [0];
475 //------------------------------------------------------------------------------
477 Stack specialization for `float`.
480 struct Stack <char const*>
482 static inline void push (lua_State* L, char const* str)
485 lua_pushstring (L, str);
490 static inline char const* get (lua_State* L, int index)
492 return lua_isnil (L, index) ? 0 : luaL_checkstring (L, index);
496 //------------------------------------------------------------------------------
498 Stack specialization for `std::string`.
501 struct Stack <std::string>
503 static inline void push (lua_State* L, std::string const& str)
505 lua_pushlstring (L, str.c_str (), str.size());
508 static inline std::string get (lua_State* L, int index)
511 const char *str = luaL_checklstring(L, index, &len);
512 return std::string (str, len);
516 //------------------------------------------------------------------------------
518 Stack specialization for `std::string const&`.
521 struct Stack <std::string const&>
523 static inline void push (lua_State* L, std::string const& str)
525 lua_pushstring (L, str.c_str());
528 static inline std::string get (lua_State* L, int index)
531 const char *str = luaL_checklstring(L, index, &len);
532 return std::string (str, len);