Branch data Line data Source code
1 : : /*
2 : : * Copyright 2006-2009 Savarese Software Research Corporation
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * https://www.savarese.com/software/ApacheLicense-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : /**
18 : : * @file This header defines conversion functions from Wispers Properties to Lua
19 : : * tables.
20 : : */
21 : :
22 : : #ifndef __SSRC_WSPR_LUA_PROPERTIES_H
23 : : #define __SSRC_WSPR_LUA_PROPERTIES_H
24 : :
25 : : #include <ssrc/wispers/utility/Properties.h>
26 : : #include <ssrc/wispers/utility/ToString.h>
27 : :
28 : : #include <ssrc/wispers/lua/lua.h>
29 : :
30 : : __BEGIN_NS_SSRC_WSPR_LUA
31 : :
32 : : using NS_SSRC_WSPR_UTILITY::Properties;
33 : : using NS_SSRC_WSPR_UTILITY::properties_ptr;
34 : : using NS_SSRC_WSPR_UTILITY::primitive_property_vector;
35 : : using NS_SSRC_WSPR_UTILITY::property_vector;
36 : : using NS_SSRC_WSPR_UTILITY::ToString;
37 : :
38 : 1 : class PropertiesToTable {
39 : :
40 [ + - ]: 1 : struct PushValue : public boost::static_visitor<void> {
41 : : lua_State *state;
42 : : mutable ToString string_cast;
43 : : const PropertiesToTable & to_table;
44 : :
45 : 1 : PushValue(lua_State *state, const PropertiesToTable & to_table) :
46 [ + - ]: 1 : state(state), to_table(to_table)
47 : 1 : { }
48 : :
49 : 0 : void operator()(const bool v) const {
50 : 0 : lua_pushboolean(state, v);
51 : 0 : }
52 : :
53 : 33 : void operator()(const std::int32_t v) const {
54 : 33 : lua_pushinteger(state, v);
55 : 33 : }
56 : :
57 : 0 : void operator()(const std::uint32_t v) const {
58 : 0 : lua_pushinteger(state, v);
59 : 0 : }
60 : :
61 : 0 : void operator()(const std::int64_t v) const {
62 : : // TODO: Decide whether to use lightuserdata instead.
63 : 0 : const string & s = string_cast(v);
64 : 0 : lua_pushlstring(state, s.c_str(), s.size());
65 : 0 : }
66 : :
67 : 0 : void operator()(const std::uint64_t v) const {
68 : : // TODO: Decide whether to use lightuserdata instead.
69 : 0 : const string & s = string_cast(v);
70 : 0 : lua_pushlstring(state, s.c_str(), s.size());
71 : 0 : }
72 : :
73 : 0 : void operator()(const double v) const {
74 : 0 : lua_pushnumber(state, v);
75 : 0 : }
76 : :
77 : 0 : void operator()(const std::string & v) const {
78 : 0 : lua_pushlstring(state, v.c_str(), v.size());
79 : 0 : }
80 : :
81 : : void operator()(const primitive_property_vector & v) const;
82 : :
83 : : void operator()(const property_vector & v) const;
84 : :
85 : : void operator()(const Properties & properties) const;
86 : : };
87 : :
88 : :
89 : : public:
90 : :
91 : : PushValue push;
92 : :
93 : 1 : PropertiesToTable(lua_State *state) : push(state, *this) { }
94 : :
95 : 44 : lua_State * state() const { return push.state; }
96 : :
97 : : void reset(lua_State *state) { push.state = state; }
98 : :
99 : : void enter(const std::string key, const Properties *properties,
100 : : const bool) const;
101 : : void leave(const std::string key, const Properties *properties,
102 : : const bool) const;
103 : : };
104 : :
105 : 1 : inline void properties_to_table(const PropertiesToTable & to_table,
106 : : const Properties & properties)
107 : : {
108 : 1 : lua_newtable(to_table.state());
109 [ + - ]: 1 : if(!properties.is_leaf()) {
110 : 1 : properties.visit(to_table);
111 : : }
112 : 1 : }
113 : :
114 : : /**
115 : : * Converts a Properties instance to a Lua table, pushing the resulting
116 : : * table onto the Lua stack.
117 : : *
118 : : * @param state A pointer to the Lua state in which to return the table.
119 : : * @param properties The Properties instance to convert.
120 : : */
121 : 1 : inline void properties_to_table(lua_State *state,
122 : : const Properties & properties)
123 : : {
124 [ + - ]: 1 : properties_to_table(PropertiesToTable(state), properties);
125 : 1 : }
126 : :
127 : : void table_to_properties(lua_State *state, Properties & properties);
128 : :
129 : : void swig_table_to_properties(void *state, Properties & properties);
130 : :
131 : : properties_ptr load_properties(lua_State *state, const std::string & filename)
132 : : SSRC_DECL_THROW(LuaCallError);
133 : :
134 : : properties_ptr load_string_properties(lua_State *state,
135 : : const std::string & code_str)
136 : : SSRC_DECL_THROW(LuaCallError);
137 : :
138 : 17 : inline properties_ptr load_properties(const std::string & filename) {
139 : 17 : lua_State *state = luaL_newstate();
140 : 17 : luaL_openlibs(state);
141 : 34 : properties_ptr && properties = load_properties(state, filename);
142 [ + - ]: 17 : lua_close(state);
143 [ + - ]: 34 : return properties;
144 : : }
145 : :
146 : 1 : inline properties_ptr load_string_properties(const std::string & code_str) {
147 : 1 : lua_State *state = luaL_newstate();
148 : 1 : luaL_openlibs(state);
149 : 2 : properties_ptr && properties = load_string_properties(state, code_str);
150 [ + - ]: 1 : lua_close(state);
151 [ + - ]: 2 : return properties;
152 : : }
153 : :
154 : : __END_NS_SSRC_WSPR_LUA
155 : :
156 : : #endif
|