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

Archive Download this file

Branches

Tags

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