pinocchio  2.1.3
lua_tables.hpp
1 /*
2  * LuaTables++
3  * Code comming from https://bitbucket.org/rbdl/rbdl
4  * Copyright (c) 2013-2014 Martin Felis <martin@fyxs.org>
5  * Copyright (c) 2015 Justin Carpentier <jcarpent@laas.fr>
6  * All rights reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #ifndef __pinocchio_parser_lua_tables_hpp____
29 #define __pinocchio_parser_lua_tables_hpp____
30 
31 #include <iostream>
32 #include <assert.h>
33 #include <cstdlib>
34 #include <string>
35 #include <vector>
36 
37 
38 // Forward declaration for Lua
39 extern "C" {
40  struct lua_State;
41 }
42 
43 struct LuaKey {
44  enum Type {
45  String,
46  Integer
47  };
48 
49  Type type;
50  int int_value;
51  std::string string_value;
52 
53  bool operator<( const LuaKey & rhs ) const {
54  if (type == String && rhs.type == Integer) {
55  return false;
56  } else if (type == Integer && rhs.type == String) {
57  return true;
58  } else if (type == Integer && rhs.type == Integer) {
59  return int_value < rhs.int_value;
60  }
61 
62  return string_value < rhs.string_value;
63  }
64 
65  LuaKey (const char* key_value) :
66  type (String),
67  int_value (0),
68  string_value (key_value) { }
69  LuaKey (int key_value) :
70  type (Integer),
71  int_value (key_value),
72  string_value ("") {}
73 };
74 
75 inline std::ostream & operator<<(std::ostream & output, const LuaKey & key) {
76  if (key.type == LuaKey::Integer)
77  output << key.int_value << "(int)";
78  else
79  output << key.string_value << "(string)";
80  return output;
81 }
82 struct LuaTable;
83 struct LuaTableNode;
84 
85 struct LuaTableNode {
86  LuaTableNode() :
87  parent (NULL),
88  luaTable (NULL),
89  key("")
90  { }
91  LuaTableNode operator[](const char* child_str) {
92  LuaTableNode child_node;
93 
94  child_node.luaTable = luaTable;
95  child_node.parent = this;
96  child_node.key = LuaKey (child_str);
97 
98  return child_node;
99  }
100  LuaTableNode operator[](int child_index) {
101  LuaTableNode child_node;
102 
103  child_node.luaTable = luaTable;
104  child_node.parent = this;
105  child_node.key = LuaKey (child_index);
106 
107  return child_node;
108  }
109  bool stackQueryValue();
110  void stackPushKey();
111  void stackCreateValue();
112  void stackRestore();
113  LuaTable stackQueryTable();
114  LuaTable stackCreateLuaTable();
115 
116  std::vector<LuaKey> getKeyStack();
117  std::string keyStackToString();
118 
119  bool exists();
120  void remove();
121  size_t length();
122  std::vector<LuaKey> keys();
123 
124  // Templates for setters and getters. Can be specialized for custom
125  // types.
126  template <typename T>
127  void set (const T & value);
128  template <typename T>
129  T getDefault (const T & default_value);
130 
131  template <typename T>
132  T get() {
133  if (!exists()) {
134  std::cerr << "Error: could not find value " << keyStackToString() << "." << std::endl;
135  abort();
136  }
137  return getDefault (T());
138  }
139 
140  // convenience operators (assignment, conversion, comparison)
141  template <typename T>
142  void operator=(const T & value) {
143  set<T>(value);
144  }
145  template <typename T>
146  operator T() {
147  return get<T>();
148  }
149  template <typename T>
150  bool operator==(T value) {
151  return value == get<T>();
152  }
153  template <typename T>
154  bool operator!=(T value) {
155  return value != get<T>();
156  }
157 
158  LuaTableNode *parent;
159  LuaTable *luaTable;
160  LuaKey key;
161  int stackTop;
162 };
163 
164 template<typename T>
165 bool operator==(T value, LuaTableNode node) {
166  return value == (T) node;
167 }
168 template<typename T>
169 bool operator!=(T value, LuaTableNode node) {
170  return value != (T) node;
171 }
172 
173 template<> bool LuaTableNode::getDefault<bool>(const bool & default_value);
174 template<> double LuaTableNode::getDefault<double>(const double & default_value);
175 template<> float LuaTableNode::getDefault<float>(const float & default_value);
176 template<> std::string LuaTableNode::getDefault<std::string>(const std::string & default_value);
177 
178 template<> void LuaTableNode::set<bool>(const bool & value);
179 template<> void LuaTableNode::set<float>(const float & value);
180 template<> void LuaTableNode::set<double>(const double & value);
181 template<> void LuaTableNode::set<std::string>(const std::string & value);
182 
183 struct LuaTable {
184  LuaTable() :
185  filename (""),
186  L (NULL),
187  deleteLuaState (false)
188  {}
189  LuaTable & operator= (const LuaTable & luatable);
190  ~LuaTable();
191 
192  LuaTableNode operator[] (const char* key) {
193  LuaTableNode root_node;
194  root_node.key = LuaKey (key);
195  root_node.parent = NULL;
196  root_node.luaTable = this;
197 
198  return root_node;
199  }
200  LuaTableNode operator[] (int key) {
201  LuaTableNode root_node;
202  root_node.key = LuaKey (key);
203  root_node.parent = NULL;
204  root_node.luaTable = this;
205 
206  return root_node;
207  }
208  int length();
209  void addSearchPath (const char* path);
210  std::string serialize ();
211  std::string orderedSerialize ();
212 
213  static LuaTable fromFile (const char *_filename);
214  static LuaTable fromLuaExpression (const char* lua_expr);
215  static LuaTable fromLuaState (lua_State *L);
216 
217  std::string filename;
218  lua_State *L;
219  bool deleteLuaState;
220 };
221 
222 
223 #endif // ifndef __pinocchio_parser_lua_tables_hpp____