customize LuaBridge
[ardour.git] / libs / lua / LuaBridge / detail / Stack.h
1 //------------------------------------------------------------------------------
2 /*
3   https://github.com/vinniefalco/LuaBridge
4
5   Copyright 2016, Robin Gareus <robin@gareus.org>
6   Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
7   Copyright 2007, Nathan Reed
8
9   License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
10
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:
17
18   The above copyright notice and this permission notice shall be included in all
19   copies or substantial portions of the Software.
20
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
27   SOFTWARE.
28 */
29 //==============================================================================
30
31 //------------------------------------------------------------------------------
32 /**
33     Receive the lua_State* as an argument.
34 */
35 template <>
36 struct Stack <lua_State*>
37 {
38   static lua_State* get (lua_State* L, int)
39   {
40     return L;
41   }
42 };
43
44 //------------------------------------------------------------------------------
45 /**
46     Push a lua_CFunction.
47 */
48 template <>
49 struct Stack <lua_CFunction>
50 {
51   static void push (lua_State* L, lua_CFunction f)
52   {
53     lua_pushcfunction (L, f);
54   }
55
56   static lua_CFunction get (lua_State* L, int index)
57   {
58     return lua_tocfunction (L, index);
59   }
60 };
61
62 //------------------------------------------------------------------------------
63 /**
64     Stack specialization for `int`.
65 */
66 template <>
67 struct Stack <int>
68 {
69   static inline void push (lua_State* L, int value)
70   {
71     lua_pushinteger (L, static_cast <lua_Integer> (value));
72   }
73
74   static inline int get (lua_State* L, int index)
75   {
76     return static_cast <int> (luaL_checkinteger (L, index));
77   }
78 };
79
80 template <>
81 struct Stack <int const&>
82 {
83   static inline void push (lua_State* L, int value)
84   {
85     lua_pushnumber (L, static_cast <lua_Number> (value));
86   }
87
88   static inline int get (lua_State* L, int index)
89   {
90     return static_cast <int > (luaL_checknumber (L, index));
91   }
92 };
93 //------------------------------------------------------------------------------
94 /**
95     Stack specialization for `unsigned int`.
96 */
97 template <>
98 struct Stack <unsigned int>
99 {
100   static inline void push (lua_State* L, unsigned int value)
101   {
102     lua_pushinteger (L, static_cast <lua_Integer> (value));
103   }
104
105   static inline unsigned int get (lua_State* L, int index)
106   {
107     return static_cast <unsigned int> (luaL_checkinteger (L, index));
108   }
109 };
110
111 template <>
112 struct Stack <unsigned int const&>
113 {
114   static inline void push (lua_State* L, unsigned int value)
115   {
116     lua_pushnumber (L, static_cast <lua_Number> (value));
117   }
118
119   static inline unsigned int get (lua_State* L, int index)
120   {
121     return static_cast <unsigned int > (luaL_checknumber (L, index));
122   }
123 };
124
125 //------------------------------------------------------------------------------
126 /**
127     Stack specialization for `unsigned char`.
128 */
129 template <>
130 struct Stack <unsigned char>
131 {
132   static inline void push (lua_State* L, unsigned char value)
133   {
134     lua_pushinteger (L, static_cast <lua_Integer> (value));
135   }
136
137   static inline unsigned char get (lua_State* L, int index)
138   {
139     return static_cast <unsigned char> (luaL_checkinteger (L, index));
140   }
141 };
142
143 template <>
144 struct Stack <unsigned char const&>
145 {
146   static inline void push (lua_State* L, unsigned char value)
147   {
148     lua_pushnumber (L, static_cast <lua_Number> (value));
149   }
150
151   static inline unsigned char get (lua_State* L, int index)
152   {
153     return static_cast <unsigned char> (luaL_checknumber (L, index));
154   }
155 };
156
157 //------------------------------------------------------------------------------
158 /**
159     Stack specialization for `short`.
160 */
161 template <>
162 struct Stack <short>
163 {
164   static inline void push (lua_State* L, short value)
165   {
166     lua_pushinteger (L, static_cast <lua_Integer> (value));
167   }
168
169   static inline short get (lua_State* L, int index)
170   {
171     return static_cast <short> (luaL_checkinteger (L, index));
172   }
173 };
174
175 template <>
176 struct Stack <short const&>
177 {
178   static inline void push (lua_State* L, short value)
179   {
180     lua_pushnumber (L, static_cast <lua_Number> (value));
181   }
182
183   static inline short get (lua_State* L, int index)
184   {
185     return static_cast <short> (luaL_checknumber (L, index));
186   }
187 };
188
189 //------------------------------------------------------------------------------
190 /**
191     Stack specialization for `unsigned short`.
192 */
193 template <>
194 struct Stack <unsigned short>
195 {
196   static inline void push (lua_State* L, unsigned short value)
197   {
198     lua_pushinteger (L, static_cast <lua_Integer> (value));
199   }
200
201   static inline unsigned short get (lua_State* L, int index)
202   {
203     return static_cast <unsigned short> (luaL_checkinteger (L, index));
204   }
205 };
206
207 template <>
208 struct Stack <unsigned short const&>
209 {
210   static inline void push (lua_State* L, unsigned short value)
211   {
212     lua_pushnumber (L, static_cast <lua_Number> (value));
213   }
214
215   static inline unsigned short get (lua_State* L, int index)
216   {
217     return static_cast <unsigned short> (luaL_checknumber (L, index));
218   }
219 };
220
221 //------------------------------------------------------------------------------
222 /**
223     Stack specialization for `long`.
224 */
225 template <>
226 struct Stack <long>
227 {
228   static inline void push (lua_State* L, long value)
229   {
230     lua_pushinteger (L, static_cast <lua_Integer> (value));
231   }
232
233   static inline long get (lua_State* L, int index)
234   {
235     return static_cast <long> (luaL_checkinteger (L, index));
236   }
237 };
238
239 template <>
240 struct Stack <long const&>
241 {
242   static inline void push (lua_State* L, long value)
243   {
244     lua_pushnumber (L, static_cast <lua_Number> (value));
245   }
246
247   static inline long get (lua_State* L, int index)
248   {
249     return static_cast <long> (luaL_checknumber (L, index));
250   }
251 };
252
253 //------------------------------------------------------------------------------
254 /**
255     Stack specialization for `unsigned long`.
256 */
257 template <>
258 struct Stack <unsigned long>
259 {
260   static inline void push (lua_State* L, unsigned long value)
261   {
262     lua_pushinteger (L, static_cast <lua_Integer> (value));
263   }
264
265   static inline unsigned long get (lua_State* L, int index)
266   {
267     return static_cast <unsigned long> (luaL_checkinteger (L, index));
268   }
269 };
270
271 template <>
272 struct Stack <unsigned long const&>
273 {
274   static inline void push (lua_State* L, unsigned long value)
275   {
276     lua_pushnumber (L, static_cast <lua_Number> (value));
277   }
278
279   static inline unsigned long get (lua_State* L, int index)
280   {
281     return static_cast <unsigned long> (luaL_checknumber (L, index));
282   }
283 };
284
285 //------------------------------------------------------------------------------
286 /**
287     Stack specialization for `long long`.
288 */
289 template <>
290 struct Stack <long long>
291 {
292   static inline void push (lua_State* L, long long value)
293   {
294     lua_pushinteger (L, static_cast <lua_Integer> (value));
295   }
296
297   static inline long long get (lua_State* L, int index)
298   {
299     return static_cast <long long> (luaL_checkinteger (L, index));
300   }
301 };
302
303 template <>
304 struct Stack <long long const&>
305 {
306   static inline void push (lua_State* L, long long value)
307   {
308     lua_pushnumber (L, static_cast <lua_Number> (value));
309   }
310
311   static inline long long get (lua_State* L, int index)
312   {
313     return static_cast <long long> (luaL_checknumber (L, index));
314   }
315 };
316
317 //------------------------------------------------------------------------------
318 /**
319     Stack specialization for `unsigned long long`.
320 */
321 template <>
322 struct Stack <unsigned long long>
323 {
324   static inline void push (lua_State* L, unsigned long long value)
325   {
326     lua_pushinteger (L, static_cast <lua_Integer> (value));
327   }
328
329   static inline unsigned long long get (lua_State* L, int index)
330   {
331     return static_cast <unsigned long long> (luaL_checkinteger (L, index));
332   }
333 };
334
335 template <>
336 struct Stack <unsigned long long const&>
337 {
338   static inline void push (lua_State* L, unsigned long long value)
339   {
340     lua_pushnumber (L, static_cast <lua_Number> (value));
341   }
342
343   static inline unsigned long long get (lua_State* L, int index)
344   {
345     return static_cast <unsigned long long> (luaL_checknumber (L, index));
346   }
347 };
348
349 //------------------------------------------------------------------------------
350 /**
351     Stack specialization for `float`.
352 */
353 template <>
354 struct Stack <float>
355 {
356   static inline void push (lua_State* L, float value)
357   {
358     lua_pushnumber (L, static_cast <lua_Number> (value));
359   }
360
361   static inline float get (lua_State* L, int index)
362   {
363     return static_cast <float> (luaL_checknumber (L, index));
364   }
365 };
366
367 template <>
368 struct Stack <float const&>
369 {
370   static inline void push (lua_State* L, float value)
371   {
372     lua_pushnumber (L, static_cast <lua_Number> (value));
373   }
374
375   static inline float get (lua_State* L, int index)
376   {
377     return static_cast <float> (luaL_checknumber (L, index));
378   }
379 };
380
381 //------------------------------------------------------------------------------
382 /**
383     Stack specialization for `double`.
384 */
385 template <> struct Stack <double>
386 {
387   static inline void push (lua_State* L, double value)
388   {
389     lua_pushnumber (L, static_cast <lua_Number> (value));
390   }
391
392   static inline double get (lua_State* L, int index)
393   {
394     return static_cast <double> (luaL_checknumber (L, index));
395   }
396 };
397
398 template <> struct Stack <double const&>
399 {
400   static inline void push (lua_State* L, double value)
401   {
402     lua_pushnumber (L, static_cast <lua_Number> (value));
403   }
404
405   static inline double get (lua_State* L, int index)
406   {
407     return static_cast <double> (luaL_checknumber (L, index));
408   }
409 };
410
411 //------------------------------------------------------------------------------
412 /**
413     Stack specialization for `bool`.
414 */
415 template <>
416 struct Stack <bool> {
417   static inline void push (lua_State* L, bool value)
418   {
419     lua_pushboolean (L, value ? 1 : 0);
420   }
421
422   static inline bool get (lua_State* L, int index)
423   {
424     return lua_toboolean (L, index) ? true : false;
425   }
426 };
427
428 template <>
429 struct Stack <bool const&> {
430   static inline void push (lua_State* L, bool value)
431   {
432     lua_pushboolean (L, value ? 1 : 0);
433   }
434
435   static inline bool get (lua_State* L, int index)
436   {
437     return lua_toboolean (L, index) ? true : false;
438   }
439 };
440
441 //------------------------------------------------------------------------------
442 /**
443     Stack specialization for `char`.
444 */
445 template <>
446 struct Stack <char>
447 {
448   static inline void push (lua_State* L, char value)
449   {
450     char str [2] = { value, 0 };
451     lua_pushstring (L, str);
452   }
453
454   static inline char get (lua_State* L, int index)
455   {
456     return luaL_checkstring (L, index) [0];
457   }
458 };
459
460 template <>
461 struct Stack <char const&>
462 {
463   static inline void push (lua_State* L, char value)
464   {
465     char str [2] = { value, 0 };
466     lua_pushstring (L, str);
467   }
468
469   static inline char get (lua_State* L, int index)
470   {
471     return luaL_checkstring (L, index) [0];
472   }
473 };
474
475 //------------------------------------------------------------------------------
476 /**
477     Stack specialization for `float`.
478 */
479 template <>
480 struct Stack <char const*>
481 {
482   static inline void push (lua_State* L, char const* str)
483   {
484     if (str != 0)
485       lua_pushstring (L, str);
486     else
487       lua_pushnil (L);
488   }
489
490   static inline char const* get (lua_State* L, int index)
491   {
492     return lua_isnil (L, index) ? 0 : luaL_checkstring (L, index);
493   }
494 };
495
496 //------------------------------------------------------------------------------
497 /**
498     Stack specialization for `std::string`.
499 */
500 template <>
501 struct Stack <std::string>
502 {
503   static inline void push (lua_State* L, std::string const& str)
504   {
505     lua_pushlstring (L, str.c_str (), str.size());
506   }
507
508   static inline std::string get (lua_State* L, int index)
509   {
510     size_t len;
511     const char *str = luaL_checklstring(L, index, &len);
512     return std::string (str, len);
513   }
514 };
515
516 //------------------------------------------------------------------------------
517 /**
518     Stack specialization for `std::string const&`.
519 */
520 template <>
521 struct Stack <std::string const&>
522 {
523   static inline void push (lua_State* L, std::string const& str)
524   {
525     lua_pushstring (L, str.c_str());
526   }
527
528   static inline std::string get (lua_State* L, int index)
529   {
530     size_t len;
531     const char *str = luaL_checklstring(L, index, &len);
532     return std::string (str, len);
533   }
534 };