monotone

monotone Mtn Source Tree

Root/lua.hh

1#ifndef __LUA_HH__
2#define __LUA_HH__
3
4#include "lua.h"
5#include "lualib.h"
6#include "lauxlib.h"
7
8#include <map>
9#include <set>
10
11struct lua_State;
12
13// This Lua object represents a single imperative transaction with the lua
14// interpreter. if it fails at any point, all further commands in the
15// transaction are ignored. it cleans the lua stack up when it is
16// destructed, so no need to pop values when you're done.
17
18struct
19Lua
20{
21 lua_State * st;
22 bool failed;
23 static std::set<std::string> missing_functions;
24
25 Lua(lua_State * s);
26 ~Lua();
27
28 void fail(std::string const & reason);
29 bool ok();
30 void report_error();
31
32 // getters
33 Lua & get(int idx = LUA_GLOBALSINDEX);
34 Lua & get_fn(int idx = LUA_GLOBALSINDEX);
35 Lua & get_tab(int idx = LUA_GLOBALSINDEX);
36 Lua & get_str(int idx = LUA_GLOBALSINDEX);
37 Lua & get_num(int idx = LUA_GLOBALSINDEX);
38 Lua & get_bool(int idx = LUA_GLOBALSINDEX);
39
40 // extractors
41 Lua & extract_str_nolog(std::string & str);
42 Lua & extract_str(std::string & str);
43 Lua & extract_classified_str(std::string & str);
44 Lua & extract_int(int & i);
45 Lua & extract_double(double & i);
46 Lua & extract_bool(bool & i);
47
48 // table iteration
49 Lua & begin();
50 bool next();
51
52 // pushers
53 Lua & push_str(std::string const & str);
54 Lua & push_int(int num);
55 Lua & push_double(double num);
56 Lua & push_bool(bool b);
57 Lua & push_nil();
58 Lua & push_table();
59 Lua & set_field(std::string const & key, int idx = -2);
60 Lua & set_table(int idx = -3);
61 Lua & call(int in, int out);
62 Lua & pop(int count = 1);
63 Lua & func(std::string const & fname);
64 Lua & loadstring(char const * str, char const * identity);
65 Lua & loadfile(char const * filename);
66};
67
68bool run_string(lua_State * st, char const * str, char const * identity);
69bool run_file(lua_State * st, char const * filename);
70void run_directory(lua_State * st, char const * dirname, char const * pattern);
71void add_functions(lua_State * st);
72
73namespace luaext
74{
75 typedef std::map<std::string, int (*)(lua_State*)> fmap;
76 typedef std::map<std::string, fmap> ftmap;
77 extern ftmap *fns;
78 struct extfn
79 {
80 extfn(std::string const & name, std::string const & table, int (*func) (lua_State *));
81 };
82}
83
84// use as:
85// LUAEXT(foo, ) {...}
86// -- a function available to lua as "foo".
87// LUAEXT(foo, bar) {...}
88// -- a function available to lua as member "foo" of table "bar"
89#define LUAEXT(NAME, TABLE) \
90namespace luaext { \
91 struct extfn_ ## NAME ## _ ## TABLE : public extfn { \
92 extfn_ ## NAME ## _ ## TABLE (); \
93 int call(lua_State * L); \
94 }; \
95 extfn_ ## NAME ## _ ## TABLE TABLE ## _ ## NAME ## _extfn; \
96 extern "C" { \
97 static int TABLE ## _ ## NAME ## _for_lua(lua_State * L) \
98 { \
99 return TABLE ## _ ## NAME ## _extfn . call(L); \
100 } \
101 } \
102 extfn_ ## NAME ## _ ## TABLE :: extfn_ ## NAME ## _ ## TABLE () \
103 : extfn( #NAME , #TABLE , & TABLE ## _## NAME ## _for_lua ) {} \
104} \
105int luaext :: extfn_ ## NAME ## _ ## TABLE :: call(lua_State * L)
106
107// Local Variables:
108// mode: C++
109// fill-column: 76
110// c-file-style: "gnu"
111// indent-tabs-mode: nil
112// End:
113// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
114
115#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status