aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/LuaUtil.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/LuaUtil.hh')
-rw-r--r--src/FbTk/LuaUtil.hh78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/FbTk/LuaUtil.hh b/src/FbTk/LuaUtil.hh
new file mode 100644
index 0000000..cdd1934
--- /dev/null
+++ b/src/FbTk/LuaUtil.hh
@@ -0,0 +1,78 @@
1// LuaUtil: Various additional functions for working with lua
2// Copyright (C) 2011 Pavel Labath
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22#ifndef FBTK_LUAUTIL_HH
23#define FBTK_LUAUTIL_HH
24
25#include <vector>
26
27#include "Luamm.hh"
28
29namespace FbTk {
30
31/*
32 * This class augments lua::state with additional functions/features. Every object automatically
33 * calls registered init functions, which can be used to initialize/create global variables in
34 * the lua state. Functions can be registered by calling registerInitFunction or by creating
35 * objects of type RegisterInitFunction.
36 */
37class Lua: public lua::state {
38public:
39 Lua();
40
41 /*
42 * makeReadOnly() makes the table at the specified index "read only". This means that any
43 * attempt to modify a table entry will result in an error (if only_existing_fields is
44 * false). If only_existing_fields is true then only fields that were present at the time of
45 * the call will be protected - user can add new entries and modify them afterwards. You
46 * should avoid raw access to "read only" tables -- it might not do what you think it will.
47 */
48 void makeReadOnly(int index, bool only_existing_fields = false);
49
50 /*
51 * readOnlySet() is the equivalent of settable, except that it works on "read only" tables.
52 * It can be used to modify protected entries or create new ones.
53 */
54 void readOnlySet(int index);
55
56 template<typename Functor>
57 static void registerInitFunction(const Functor &fn) {
58 s_init_functions.push_back(new SlotImpl<Functor, void, Lua &>(fn));
59 }
60
61 class RegisterInitFunction {
62 public:
63 template<typename Functor>
64 RegisterInitFunction(const Functor &fn) {
65 registerInitFunction(fn);
66 }
67 };
68
69private:
70 typedef Slot<void, Lua &> InitFunction;
71 typedef std::vector<InitFunction *> InitFunctions;
72
73 static InitFunctions s_init_functions;
74};
75
76} // namespace FbTk
77
78#endif // FBTK_LUAUTIL_HH