libwreport 3.40
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,
43 const luaL_Reg* lib)
44{
45 // The object we create is a userdata that holds a pointer to obj
46 T** s = (T**)lua_newuserdata(L, sizeof(T*));
47 *s = obj;
48
49 // Set the metatable for the userdata
50 if (luaL_newmetatable(L, class_name))
51 {
52 // If the metatable wasn't previously created, create it now
53 lua_pushstring(L, "__index");
54 lua_pushvalue(L, -2); /* pushes the metatable */
55 lua_settable(L, -3); /* metatable.__index = metatable */
56
57 // Load normal methods
58#if LUA_VERSION_NUM >= 502
59 luaL_setfuncs(L, lib, 0);
60#else
61 luaL_register(L, NULL, lib);
62#endif
63 }
64
65 lua_setmetatable(L, -2);
66}
67
68} // namespace lua
69} // namespace wreport
70
71#endif
72
73#endif
String functions.
Definition benchmark.h:13