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