libwreport 3.36
lua.h
1/*
2 * wreport/lua - Utilities used to interface with Lua
3 * This is not part of the wreport API!
4 *
5 * Copyright (C) 2014 ARPA-SIM <urpsim@smr.arpa.emr.it>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Author: Enrico Zini <enrico@enricozini.com>
21 */
22
23#ifndef WREPORT_UTILS_LUA_H
24#define WREPORT_UTILS_LUA_H
25
26#include "config.h"
27
28#ifndef HAVE_LUA
29#ifdef WREPORT_LUA_REQUIRED
30#error This source requires Lua to compile
31#endif
32#else
33extern "C" {
34#include <lauxlib.h>
35#include <lualib.h>
36}
37
38namespace wreport {
39namespace lua {
40
41template<typename T>
42void push_object(lua_State* L, T* obj, const char* class_name, const luaL_Reg* lib)
43{
44 // The object we create is a userdata that holds a pointer to obj
45 T** s = (T**)lua_newuserdata(L, sizeof(T*));
46 *s = obj;
47
48 // Set the metatable for the userdata
49 if (luaL_newmetatable(L, class_name))
50 {
51 // If the metatable wasn't previously created, create it now
52 lua_pushstring(L, "__index");
53 lua_pushvalue(L, -2); /* pushes the metatable */
54 lua_settable(L, -3); /* metatable.__index = metatable */
55
56 // Load normal methods
57#if LUA_VERSION_NUM >= 502
58 luaL_setfuncs(L, lib, 0);
59#else
60 luaL_register(L, NULL, lib);
61#endif
62 }
63
64 lua_setmetatable(L, -2);
65}
66
67}
68}
69
70#endif
71
72#endif
String functions.
Definition: benchmark.h:13