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 passing references to built-in types.
66 This allows to call functions using primitives, but
67 the value assigned by the C++ function is *lost*.
69 http://sourceforge.net/p/luabind/mailman/message/32692027/
72 - wrap all C++ function that have non const reference arguments
75 - use a struct to wrap the value (not a primitive)
76 (needs major work to special case arguments in CFunc::)
78 - wrap the function and provide the assigned value
79 as addition return values
81 Either would place hard constraints on the lua code though.
83 We currently only need this for Ardour::Editor::do_import() framecnt_t&
84 and the returned position is not important.
87 //------------------------------------------------------------------------------
89 Stack specialization for `int`.
94 static inline void push (lua_State* L, int value)
96 lua_pushinteger (L, static_cast <lua_Integer> (value));
99 static inline int get (lua_State* L, int index)
101 return static_cast <int> (luaL_checkinteger (L, index));
106 struct Stack <int const&>
108 static inline void push (lua_State* L, int value)
110 lua_pushnumber (L, static_cast <lua_Number> (value));
113 static inline int get (lua_State* L, int index)
115 return static_cast <int > (luaL_checknumber (L, index));
122 static inline void push (lua_State* L, int &value)
124 lua_pushnumber (L, static_cast <lua_Number> (value));
127 static inline int& get (lua_State* L, int index)
129 int l = static_cast <int> (luaL_checknumber (L, index));
130 int* x = new (lua_newuserdata (L, sizeof (int))) int (l);
135 //------------------------------------------------------------------------------
137 Stack specialization for `unsigned int`.
140 struct Stack <unsigned int>
142 static inline void push (lua_State* L, unsigned int value)
144 lua_pushinteger (L, static_cast <lua_Integer> (value));
147 static inline unsigned int get (lua_State* L, int index)
149 return static_cast <unsigned int> (luaL_checkinteger (L, index));
154 struct Stack <unsigned int const&>
156 static inline void push (lua_State* L, unsigned int value)
158 lua_pushnumber (L, static_cast <lua_Number> (value));
161 static inline unsigned int get (lua_State* L, int index)
163 return static_cast <unsigned int > (luaL_checknumber (L, index));
168 struct Stack <unsigned int &>
170 static inline void push (lua_State* L, unsigned int& value)
172 lua_pushnumber (L, static_cast <lua_Number> (value));
175 static inline unsigned int& get (lua_State* L, int index)
177 unsigned int l = static_cast <unsigned int> (luaL_checknumber (L, index));
178 unsigned int* x = new (lua_newuserdata (L, sizeof (unsigned int))) unsigned int (l);
183 //------------------------------------------------------------------------------
185 Stack specialization for `unsigned char`.
188 struct Stack <unsigned char>
190 static inline void push (lua_State* L, unsigned char value)
192 lua_pushinteger (L, static_cast <lua_Integer> (value));
195 static inline unsigned char get (lua_State* L, int index)
197 return static_cast <unsigned char> (luaL_checkinteger (L, index));
202 struct Stack <unsigned char const&>
204 static inline void push (lua_State* L, unsigned char value)
206 lua_pushnumber (L, static_cast <lua_Number> (value));
209 static inline unsigned char get (lua_State* L, int index)
211 return static_cast <unsigned char> (luaL_checknumber (L, index));
216 struct Stack <unsigned char &>
218 static inline void push (lua_State* L, unsigned char& value)
220 lua_pushnumber (L, static_cast <lua_Number> (value));
223 static inline unsigned char& get (lua_State* L, int index)
225 unsigned char l = static_cast <unsigned char> (luaL_checknumber (L, index));
226 unsigned char* x = new (lua_newuserdata (L, sizeof (unsigned char))) unsigned char (l);
231 //------------------------------------------------------------------------------
233 Stack specialization for `short`.
238 static inline void push (lua_State* L, short value)
240 lua_pushinteger (L, static_cast <lua_Integer> (value));
243 static inline short get (lua_State* L, int index)
245 return static_cast <short> (luaL_checkinteger (L, index));
250 struct Stack <short const&>
252 static inline void push (lua_State* L, short value)
254 lua_pushnumber (L, static_cast <lua_Number> (value));
257 static inline short get (lua_State* L, int index)
259 return static_cast <short> (luaL_checknumber (L, index));
264 struct Stack <short &>
266 static inline void push (lua_State* L, short& value)
268 lua_pushnumber (L, static_cast <lua_Number> (value));
271 static inline short& get (lua_State* L, int index)
273 short l = static_cast <short> (luaL_checknumber (L, index));
274 short* x = new (lua_newuserdata (L, sizeof (short))) short (l);
279 //------------------------------------------------------------------------------
281 Stack specialization for `unsigned short`.
284 struct Stack <unsigned short>
286 static inline void push (lua_State* L, unsigned short value)
288 lua_pushinteger (L, static_cast <lua_Integer> (value));
291 static inline unsigned short get (lua_State* L, int index)
293 return static_cast <unsigned short> (luaL_checkinteger (L, index));
298 struct Stack <unsigned short const&>
300 static inline void push (lua_State* L, unsigned short value)
302 lua_pushnumber (L, static_cast <lua_Number> (value));
305 static inline unsigned short get (lua_State* L, int index)
307 return static_cast <unsigned short> (luaL_checknumber (L, index));
312 struct Stack <unsigned short &>
314 static inline void push (lua_State* L, unsigned short& value)
316 lua_pushnumber (L, static_cast <lua_Number> (value));
319 static inline unsigned short& get (lua_State* L, int index)
321 unsigned short l = static_cast <unsigned short> (luaL_checknumber (L, index));
322 unsigned short* x = new (lua_newuserdata (L, sizeof (unsigned short))) unsigned short (l);
327 //------------------------------------------------------------------------------
329 Stack specialization for `long`.
334 static inline void push (lua_State* L, long value)
336 lua_pushinteger (L, static_cast <lua_Integer> (value));
339 static inline long get (lua_State* L, int index)
341 return static_cast <long> (luaL_checkinteger (L, index));
346 struct Stack <long const&>
348 static inline void push (lua_State* L, long value)
350 lua_pushnumber (L, static_cast <lua_Number> (value));
353 static inline long get (lua_State* L, int index)
355 return static_cast <long> (luaL_checknumber (L, index));
360 struct Stack <long &>
362 static inline void push (lua_State* L, long& value)
364 lua_pushnumber (L, static_cast <lua_Number> (value));
367 static inline long& get (lua_State* L, int index)
369 long l = static_cast <long> (luaL_checknumber (L, index));
370 long* x = new (lua_newuserdata (L, sizeof (long))) long (l);
375 //------------------------------------------------------------------------------
377 Stack specialization for `unsigned long`.
380 struct Stack <unsigned long>
382 static inline void push (lua_State* L, unsigned long value)
384 lua_pushinteger (L, static_cast <lua_Integer> (value));
387 static inline unsigned long get (lua_State* L, int index)
389 return static_cast <unsigned long> (luaL_checkinteger (L, index));
394 struct Stack <unsigned long const&>
396 static inline void push (lua_State* L, unsigned long value)
398 lua_pushnumber (L, static_cast <lua_Number> (value));
401 static inline unsigned long get (lua_State* L, int index)
403 return static_cast <unsigned long> (luaL_checknumber (L, index));
408 struct Stack <unsigned long &>
410 static inline void push (lua_State* L, unsigned long& value)
412 lua_pushnumber (L, static_cast <lua_Number> (value));
415 static inline unsigned long& get (lua_State* L, int index)
417 unsigned long l = static_cast <unsigned long> (luaL_checknumber (L, index));
418 unsigned long* x = new (lua_newuserdata (L, sizeof (unsigned long))) unsigned long (l);
423 //------------------------------------------------------------------------------
425 Stack specialization for `long long`.
428 struct Stack <long long>
430 static inline void push (lua_State* L, long long value)
432 lua_pushinteger (L, static_cast <lua_Integer> (value));
435 static inline long long get (lua_State* L, int index)
437 return static_cast <long long> (luaL_checkinteger (L, index));
442 struct Stack <long long const&>
444 static inline void push (lua_State* L, long long value)
446 lua_pushnumber (L, static_cast <lua_Number> (value));
449 static inline long long get (lua_State* L, int index)
451 return static_cast <long long> (luaL_checknumber (L, index));
456 struct Stack <long long &>
458 static inline void push (lua_State* L, long long& value)
460 lua_pushnumber (L, static_cast <lua_Number> (value));
463 static inline long long& get (lua_State* L, int index)
465 long long l = static_cast <long long> (luaL_checknumber (L, index));
466 long long* x = new (lua_newuserdata (L, sizeof (long long))) long long (l);
471 //------------------------------------------------------------------------------
473 Stack specialization for `unsigned long long`.
476 struct Stack <unsigned long long>
478 static inline void push (lua_State* L, unsigned long long value)
480 lua_pushinteger (L, static_cast <lua_Integer> (value));
483 static inline unsigned long long get (lua_State* L, int index)
485 return static_cast <unsigned long long> (luaL_checkinteger (L, index));
490 struct Stack <unsigned long long const&>
492 static inline void push (lua_State* L, unsigned long long value)
494 lua_pushnumber (L, static_cast <lua_Number> (value));
497 static inline unsigned long long get (lua_State* L, int index)
499 return static_cast <unsigned long long> (luaL_checknumber (L, index));
503 //------------------------------------------------------------------------------
505 Stack specialization for `float`.
510 static inline void push (lua_State* L, float value)
512 lua_pushnumber (L, static_cast <lua_Number> (value));
515 static inline float get (lua_State* L, int index)
517 return static_cast <float> (luaL_checknumber (L, index));
522 struct Stack <float const&>
524 static inline void push (lua_State* L, float value)
526 lua_pushnumber (L, static_cast <lua_Number> (value));
529 static inline float get (lua_State* L, int index)
531 return static_cast <float> (luaL_checknumber (L, index));
536 struct Stack <float &>
538 static inline void push (lua_State* L, float& value)
540 lua_pushnumber (L, static_cast <lua_Number> (value));
543 static inline float& get (lua_State* L, int index)
545 float l = static_cast <float> (luaL_checknumber (L, index));
546 float* x = new (lua_newuserdata (L, sizeof (float))) float (l);
551 //------------------------------------------------------------------------------
553 Stack specialization for `double`.
555 template <> struct Stack <double>
557 static inline void push (lua_State* L, double value)
559 lua_pushnumber (L, static_cast <lua_Number> (value));
562 static inline double get (lua_State* L, int index)
564 return static_cast <double> (luaL_checknumber (L, index));
568 template <> struct Stack <double const&>
570 static inline void push (lua_State* L, double value)
572 lua_pushnumber (L, static_cast <lua_Number> (value));
575 static inline double get (lua_State* L, int index)
577 return static_cast <double> (luaL_checknumber (L, index));
581 template <> struct Stack <double &>
583 static inline void push (lua_State* L, double& value)
585 lua_pushnumber (L, static_cast <lua_Number> (value));
588 static inline double& get (lua_State* L, int index)
590 double l = static_cast <double> (luaL_checknumber (L, index));
591 double* x = new (lua_newuserdata (L, sizeof (double))) double (l);
596 //------------------------------------------------------------------------------
598 Stack specialization for `bool`.
601 struct Stack <bool> {
602 static inline void push (lua_State* L, bool value)
604 lua_pushboolean (L, value ? 1 : 0);
607 static inline bool get (lua_State* L, int index)
609 return lua_toboolean (L, index) ? true : false;
614 struct Stack <bool const&> {
615 static inline void push (lua_State* L, bool value)
617 lua_pushboolean (L, value ? 1 : 0);
620 static inline bool get (lua_State* L, int index)
622 return lua_toboolean (L, index) ? true : false;
627 struct Stack <bool &> {
628 static inline void push (lua_State* L, bool& value)
630 lua_pushboolean (L, value ? 1 : 0);
633 static inline bool& get (lua_State* L, int index)
635 bool l = lua_toboolean (L, index) ? true : false;
636 bool* x = new (lua_newuserdata (L, sizeof (bool))) bool (l);
641 //------------------------------------------------------------------------------
643 Stack specialization for `char`.
648 static inline void push (lua_State* L, char value)
650 char str [2] = { value, 0 };
651 lua_pushstring (L, str);
654 static inline char get (lua_State* L, int index)
656 return luaL_checkstring (L, index) [0];
661 struct Stack <char const&>
663 static inline void push (lua_State* L, char value)
665 char str [2] = { value, 0 };
666 lua_pushstring (L, str);
669 static inline char get (lua_State* L, int index)
671 return luaL_checkstring (L, index) [0];
676 struct Stack <char const *>
678 static inline void push (lua_State* L, char const* str)
681 lua_pushstring (L, str);
686 static inline char const *get (lua_State* L, int index)
688 return lua_isnil (L, index) ? 0 : luaL_checkstring (L, index);
693 struct Stack <char &>
695 static inline void push (lua_State* L, char& value)
697 char str [2] = { value, 0 };
698 lua_pushstring (L, str);
701 static inline char get (lua_State* L, int index)
703 char l = luaL_checkstring (L, index) [0];
704 char* x = new (lua_newuserdata (L, sizeof (char))) char (l);
709 //------------------------------------------------------------------------------
711 Stack specialization for `std::string`.
714 struct Stack <std::string>
716 static inline void push (lua_State* L, std::string const& str)
718 lua_pushlstring (L, str.c_str (), str.size());
721 static inline std::string get (lua_State* L, int index)
724 const char *str = luaL_checklstring(L, index, &len);
725 return std::string (str, len);
730 struct Stack <std::string const&>
732 static inline void push (lua_State* L, std::string const& str)
734 lua_pushstring (L, str.c_str());
737 static inline std::string get (lua_State* L, int index)
740 const char *str = luaL_checklstring(L, index, &len);
741 return std::string (str, len);
746 struct Stack <std::string &>
748 static inline void push (lua_State* L, std::string& str)
750 lua_pushlstring (L, str.c_str (), str.size());
753 static inline std::string& get (lua_State* L, int index)
756 const char *str = luaL_checklstring(L, index, &len);
757 std::string* x = new (lua_newuserdata (L, sizeof (std::string))) std::string (str, len);