ReFRACtor
luabind_object.h
Go to the documentation of this file.
1 #ifndef LUABIND_OBJECT_H
2 #define LUABIND_OBJECT_H
3 #include "printable.h"
4 #include "register_lua.h"
5 #include "fp_exception.h"
6 #include "dir_change.h"
7 #include "lua_state.h"
8 #include <blitz/array.h>
9 
10 namespace FullPhysics {
11  class LuabindObject;
12 
13 /****************************************************************/
23 public:
25 
26 //-----------------------------------------------------------------------
29 //-----------------------------------------------------------------------
30 
34  const = 0;
35 
36 //-----------------------------------------------------------------------
39 //-----------------------------------------------------------------------
40 
42  const = 0;
43 
44 //-----------------------------------------------------------------------
46 //-----------------------------------------------------------------------
47 
48  virtual bool set_value(LuabindObject& Lobj,
49  const std::string& Vname,
50  const boost::shared_ptr<GenericObject>& Obj) = 0;
51 
52 //-----------------------------------------------------------------------
54 //-----------------------------------------------------------------------
55 
56  virtual bool set_index(LuabindObject& Lobj,
57  int Vidx,
58  const boost::shared_ptr<GenericObject>& Obj) = 0;
59 };
60 
61 /****************************************************************/
65 class LuabindObject : public Printable<LuabindObject> {
66 public:
68 
69 //-----------------------------------------------------------------------
71 //-----------------------------------------------------------------------
72  LuabindObject(const luabind::object& Obj,
73  const boost::shared_ptr<LuaState>& Ls) : ls(Ls), obj(Obj) {}
74 
75 //-----------------------------------------------------------------------
77 //-----------------------------------------------------------------------
79  { return LuabindObject(luabind::object(),Ls); }
80 
81 //-----------------------------------------------------------------------
83 //-----------------------------------------------------------------------
84 
85  template<class T> LuabindObject(const boost::shared_ptr<LuaState>& Ls,
86  const T& V)
87  : ls(Ls), obj(Ls->lua_state(), V) {}
88 
89 // Nice not to have to treat LuabindObject as a special case, so just
90 // copy if T happens to be type LuabindObject
92  const LuabindObject& V)
93  : ls(V.lua_state()), obj(V.object()) {}
94 
95  virtual ~LuabindObject() {}
96 
97  void print(std::ostream& Os) const { Os << "LuabindObject";}
98 
99 //-----------------------------------------------------------------------
102 //-----------------------------------------------------------------------
103 
104  static std::vector<boost::shared_ptr<BridgeLuabindAndGenericBase> >
106 
107 //-----------------------------------------------------------------------
110 //-----------------------------------------------------------------------
111 
112  static std::vector<boost::shared_ptr<BridgeLuabindAndGenericBase> >
114 
115  boost::shared_ptr<GenericObject> value_generic_object() const;
116  void set_value(const std::string& Vname,
118  void set_index(int Vidx, const boost::shared_ptr<GenericObject>& V);
120  create_luabind_object(const boost::shared_ptr<LuaState>& Ls,
122 
123 //-----------------------------------------------------------------------
125 //-----------------------------------------------------------------------
126 
127  LuabindObject operator[](const std::string& Vname) const
128  { luabind::object r = obj[Vname]; return LuabindObject(r, ls);}
129 
130  LuabindObject get_index(int Vidx) const
131  { luabind::object r = obj[Vidx]; return LuabindObject(r, ls);}
132 
133 //-----------------------------------------------------------------------
135 //-----------------------------------------------------------------------
136 
137  template<class T> T
138  value() const
139  {
140  try {
141  return luabind::object_cast<T>(obj);
142  } catch(std::exception eoriginal) {
143  Exception e;
144  e << "Error converting lua value to\n"
145  << "Requested type: " << typeid(T).name() << "\n";
146  if (is_nil())
147  e << "Source object is nil";
148  else
149  e << "Source object is of type: " << typeid(obj).name();
150  throw e;
151  }
152  }
153 
154 //-----------------------------------------------------------------------
158 //-----------------------------------------------------------------------
159 
160  template<class T> boost::shared_ptr<T>
161  value_ptr() const
162  {
163  return value<boost::shared_ptr<T> >();
164  }
165 
166 //-----------------------------------------------------------------------
169 //-----------------------------------------------------------------------
170 
171  template<class T> bool is_type() const
172  { return luabind::object_cast_nothrow<T>(obj) != boost::none; }
173 
174 //-----------------------------------------------------------------------
178 //-----------------------------------------------------------------------
179 
180  template<class T> bool is_type_ptr() const
181  {
182  return is_type<boost::shared_ptr<T> >();
183  }
184 
185 //-----------------------------------------------------------------------
187 //-----------------------------------------------------------------------
188 
189  template<class T> void set_value(const std::string& Vname,
190  const boost::shared_ptr<T>& V)
191  {
192  obj[Vname] = V;
193  }
194  template<class T, int D> void set_value(const std::string& Vname,
195  const blitz::Array<T, D>& V)
196  {
197  obj[Vname] = V.copy();
198  }
199  void set_value(const std::string& Vname, const std::string& V)
200  {
201  obj[Vname] = V;
202  }
203  void set_value(const std::string& Vname, const char* V)
204  {
205  obj[Vname] = V;
206  }
207  void set_value(const std::string& Vname, int V)
208  {
209  obj[Vname] = V;
210  }
211  void set_value(const std::string& Vname, double V)
212  {
213  obj[Vname] = V;
214  }
215  void set_value(const std::string& Vname, bool V)
216  {
217  obj[Vname] = V;
218  }
219  void set_value(const std::string& Vname, const LuabindObject& V)
220  {
221  obj[Vname] = V.object();
222  }
223 
224 //-----------------------------------------------------------------------
226 //-----------------------------------------------------------------------
227 
228  template<class T> void set_index(int Vidx,
229  const boost::shared_ptr<T>& V)
230  {
231  obj[Vidx] = V;
232  }
233  void set_index(int Vidx, const std::string& V)
234  {
235  obj[Vidx] = V;
236  }
237  void set_index(int Vidx, const char* V)
238  {
239  obj[Vidx] = V;
240  }
241  void set_index(int Vidx, int V)
242  {
243  obj[Vidx] = V;
244  }
245  void set_index(int Vidx, double V)
246  {
247  obj[Vidx] = V;
248  }
249  void set_index(int Vidx, bool V)
250  {
251  obj[Vidx] = V;
252  }
253  void set_index(int Vidx, const LuabindObject& V)
254  {
255  obj[Vidx] = V.object();
256  }
257 
258 //-----------------------------------------------------------------------
260 //-----------------------------------------------------------------------
261 
262  bool is_nil() const { return luabind::type(obj) == LUA_TNIL; }
263  bool is_boolean() const { return luabind::type(obj) == LUA_TBOOLEAN; }
264  bool is_number() const { return luabind::type(obj) == LUA_TNUMBER; }
265  bool is_string() const { return luabind::type(obj) == LUA_TSTRING; }
266  bool is_table() const { return luabind::type(obj) == LUA_TTABLE; }
267  bool is_function() const { return luabind::type(obj) == LUA_TFUNCTION; }
268 
269 //-----------------------------------------------------------------------
272 //-----------------------------------------------------------------------
273 
274  int length(int index = 0) const
275  {
276  return lua_rawlen(obj.interpreter(), index);
277  }
278 
279 //-----------------------------------------------------------------------
281 //-----------------------------------------------------------------------
283  {
284  DirChange dc(ls->base_dir_name());
285  try {
286  return LuabindObject((luabind::object)
287  luabind::call_function<luabind::object>(obj), ls);
288  } catch(const luabind::error& eoriginal) {
289  Exception e;
290  e << "Lua error: " << lua_tostring(lua_state()->lua_state(), -1) << "\n";
291  lua_pop(lua_state()->lua_state(), 1); // Remove error message from stack.
292  throw e;
293  }
294  }
295  template<class T1>
296  LuabindObject call(const T1& Arg1)
297  {
298  DirChange dc(ls->base_dir_name());
299  try {
300  return LuabindObject((luabind::object)
301  luabind::call_function<luabind::object>(obj, Arg1),
302  ls);
303  } catch(const luabind::error& eoriginal) {
304  Exception e;
305  e << "Lua error: " << lua_tostring(lua_state()->lua_state(), -1) << "\n";
306  lua_pop(lua_state()->lua_state(), 1); // Remove error message from stack.
307  throw e;
308  }
309  }
310  template<class T1, class T2>
311  LuabindObject call(const T1& Arg1, const T2& Arg2)
312  {
313  DirChange dc(ls->base_dir_name());
314  try {
315  return LuabindObject((luabind::object)
316  luabind::call_function<luabind::object>
317  (obj, Arg1, Arg2),
318  ls);
319  } catch(const luabind::error& eoriginal) {
320  Exception e;
321  e << "Lua error: " << lua_tostring(lua_state()->lua_state(), -1) << "\n";
322  lua_pop(lua_state()->lua_state(), 1); // Remove error message from stack.
323  throw e;
324  }
325  }
326  template<class T1, class T2, class T3>
327  LuabindObject call(const T1& Arg1, const T2& Arg2, const T3& Arg3)
328  {
329  DirChange dc(ls->base_dir_name());
330  try {
331  return LuabindObject((luabind::object)
332  luabind::call_function<luabind::object>
333  (obj, Arg1, Arg2, Arg3),
334  ls);
335  } catch(const luabind::error& eoriginal) {
336  Exception e;
337  e << "Lua error: " << lua_tostring(lua_state()->lua_state(), -1) << "\n";
338  lua_pop(lua_state()->lua_state(), 1); // Remove error message from stack.
339  throw e;
340  }
341  }
342  template<class T1, class T2, class T3, class T4>
343  LuabindObject call(const T1& Arg1, const T2& Arg2, const T3& Arg3,
344  const T4& Arg4)
345  {
346  DirChange dc(ls->base_dir_name());
347  try {
348  return LuabindObject((luabind::object)
349  luabind::call_function<luabind::object>
350  (obj, Arg1, Arg2, Arg3, Arg4),
351  ls);
352  } catch(const luabind::error& eoriginal) {
353  Exception e;
354  e << "Lua error: " << lua_tostring(lua_state()->lua_state(), -1) << "\n";
355  lua_pop(lua_state()->lua_state(), 1); // Remove error message from stack.
356  throw e;
357  }
358 
359  }
360  template<class T1, class T2, class T3, class T4, class T5>
361  LuabindObject call(const T1& Arg1, const T2& Arg2, const T3& Arg3,
362  const T4& Arg4, const T5& Arg5)
363  {
364  DirChange dc(ls->base_dir_name());
365  try{
366  return LuabindObject((luabind::object)
367  luabind::call_function<luabind::object>
368  (obj, Arg1, Arg2, Arg3, Arg4, Arg5),
369  ls);
370  } catch(const luabind::error& eoriginal) {
371  Exception e;
372  e << "Lua error: " << lua_tostring(lua_state()->lua_state(), -1) << "\n";
373  lua_pop(lua_state()->lua_state(), 1); // Remove error message from stack.
374  throw e;
375  }
376  }
377  template<class T1, class T2, class T3, class T4, class T5, class T6>
378  LuabindObject call(const T1& Arg1, const T2& Arg2, const T3& Arg3,
379  const T4& Arg4, const T5& Arg5, const T6& Arg6)
380  {
381  DirChange dc(ls->base_dir_name());
382  try {
383  return LuabindObject((luabind::object)
384  luabind::call_function<luabind::object>
385  (obj, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6),
386  ls);
387  } catch(const luabind::error& eoriginal) {
388  Exception e;
389  e << "Lua error: " << lua_tostring(lua_state()->lua_state(), -1) << "\n";
390  lua_pop(lua_state()->lua_state(), 1); // Remove error message from stack.
391  throw e;
392  }
393  }
394  template<class T1, class T2, class T3, class T4, class T5, class T6,
395  class T7>
396  LuabindObject call(const T1& Arg1, const T2& Arg2, const T3& Arg3,
397  const T4& Arg4, const T5& Arg5, const T6& Arg6,
398  const T7& Arg7)
399  {
400  DirChange dc(ls->base_dir_name());
401  try {
402  return LuabindObject((luabind::object)
403  luabind::call_function<luabind::object>
404  (obj, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7),
405  ls);
406  } catch(const luabind::error& eoriginal) {
407  Exception e;
408  e << "Lua error: " << lua_tostring(lua_state()->lua_state(), -1) << "\n";
409  lua_pop(lua_state()->lua_state(), 1); // Remove error message from stack.
410  throw e;
411  }
412  }
413  template<class T1, class T2, class T3, class T4, class T5, class T6,
414  class T7, class T8>
415  LuabindObject call(const T1& Arg1, const T2& Arg2, const T3& Arg3,
416  const T4& Arg4, const T5& Arg5, const T6& Arg6,
417  const T7& Arg7, const T8& Arg8)
418  {
419  DirChange dc(ls->base_dir_name());
420  try {
421  return LuabindObject((luabind::object)
422  luabind::call_function<luabind::object>
423  (obj, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7,
424  Arg8), ls);
425  } catch(const luabind::error& eoriginal) {
426  Exception e;
427  e << "Lua error: " << lua_tostring(lua_state()->lua_state(), -1) << "\n";
428  lua_pop(lua_state()->lua_state(), 1); // Remove error message from stack.
429  throw e;
430  }
431  }
432 
433 //-----------------------------------------------------------------------
435 //-----------------------------------------------------------------------
436 
438  return LuabindObject(luabind::newtable(ls->lua_state()), ls);
439  }
440 
441 //-----------------------------------------------------------------------
444 //-----------------------------------------------------------------------
445 
446  luabind::object& object() {return obj;}
447  const luabind::object& object() const {return obj;}
448 
449 //-----------------------------------------------------------------------
452 //-----------------------------------------------------------------------
453 
455  {return ls;}
456 private:
457  // Note order here is important. We need obj to be deleted first,
458  // before ls is.
460  luabind::object obj;
461 };
462 
463 /****************************************************************/
468 class LuabindIterator : public luabind::iterator {
469 public:
470  //-----------------------------------------------------------------------
472  //-----------------------------------------------------------------------
474  const boost::shared_ptr<LuaState>& Ls) : luabind::iterator(Lbo.object()), ls(Ls) {}
475 
476  bool at_end() const { return *this == luabind::iterator(); }
477  LuabindObject key() const { return LuabindObject(luabind::iterator::key(), ls); }
478  LuabindObject next() { LuabindObject res = LuabindObject(operator*(), ls); (*this)++; return res; }
479 private:
481 };
482 
483 /****************************************************************/
486 template<class T> class BridgeLuabindAndGeneric :
488 public:
494  const
495  {
496  boost::shared_ptr<T> v = boost::dynamic_pointer_cast<T>(Obj);
498  if(v)
499  res.reset(new LuabindObject(Ls, v));
500  return res;
501  }
502 
504  const
505  {
506  if(Lobj.is_type_ptr<T>())
507  return boost::dynamic_pointer_cast<GenericObject>(Lobj.value_ptr<T>());
508  else
510  }
511  virtual bool set_value(LuabindObject& Lobj,
512  const std::string& Vname,
514  {
515  boost::shared_ptr<T> v = boost::dynamic_pointer_cast<T>(Obj);
516  if(v) {
517  Lobj.set_value(Vname, v);
518  return true;
519  }
520  return false;
521  }
522  virtual bool set_index(LuabindObject& Lobj,
523  int Vidx,
525  {
526  boost::shared_ptr<T> v = boost::dynamic_pointer_cast<T>(Obj);
527  if(v) {
528  Lobj.set_index(Vidx, v);
529  return true;
530  }
531  return false;
532  }
533 };
534 
535 }
536 #endif
For use with python, it is useful to map all the LuabindObject that are a class defined in full physi...
void set_value(const std::string &Vname, const boost::shared_ptr< GenericObject > &V)
Set value as a GenericObject, returning true if this succeeds.
This is a light wrapper around the luabind::iterator.
LuabindObject call(const T1 &Arg1, const T2 &Arg2, const T3 &Arg3, const T4 &Arg4, const T5 &Arg5)
static std::vector< boost::shared_ptr< BridgeLuabindAndGenericBase > > base_bridge
Map used for mapping base classes to and from Lua and GenericObject.
boost::shared_ptr< T > value_ptr() const
Return value of a variable found in this table.
void set_value(const std::string &Vname, double V)
void set_index(int Vidx, const boost::shared_ptr< T > &V)
Set an index.
virtual bool set_index(LuabindObject &Lobj, int Vidx, const boost::shared_ptr< GenericObject > &Obj)=0
Set a value if we can. Return true if we set the value, false otherwise.
bool is_type_ptr() const
Test if value in a table is the given type.
This is the base of the exception hierarchy for Full Physics code.
Definition: fp_exception.h:16
void set_index(int Vidx, const boost::shared_ptr< GenericObject > &V)
Set value as a GenericObject, returning true if this succeeds.
LuabindObject call(const T1 &Arg1, const T2 &Arg2, const T3 &Arg3, const T4 &Arg4, const T5 &Arg5, const T6 &Arg6)
virtual boost::shared_ptr< GenericObject > value(const LuabindObject &Lobj) const =0
Return a GenericObject if we can convert, or a null pointer if we can&#39;t.
virtual boost::shared_ptr< LuabindObject > luabind_object(const boost::shared_ptr< LuaState > &Ls, const boost::shared_ptr< GenericObject > &Obj) const =0
Create a LuabindObject from the given type, or return a null pointer if we can&#39;t. ...
void set_value(const std::string &Vname, int V)
This is a light wrapper around the luabind::object.
void set_value(const std::string &Vname, bool V)
void set_index(int Vidx, bool V)
void set_index(int Vidx, const std::string &V)
This is a Mixin for classes that can be printed.
Definition: printable.h:24
LuabindIterator(const LuabindObject &Lbo, const boost::shared_ptr< LuaState > &Ls)
Constructor.
const boost::shared_ptr< LuaState > & lua_state() const
Lua state pointer.
void set_index(int Vidx, const char *V)
LuabindObject call(const T1 &Arg1, const T2 &Arg2, const T3 &Arg3)
void set_value(const std::string &Vname, const char *V)
void set_value(const std::string &Vname, const boost::shared_ptr< T > &V)
Set a value.
virtual boost::shared_ptr< GenericObject > value(const LuabindObject &Lobj) const
Return a GenericObject if we can convert, or a null pointer if we can&#39;t.
LuabindObject call(const T1 &Arg1, const T2 &Arg2, const T3 &Arg3, const T4 &Arg4, const T5 &Arg5, const T6 &Arg6, const T7 &Arg7)
LuabindObject operator[](const std::string &Vname) const
Return a variable found in this table as another LuabindObject.
luabind::object & object()
Underlying object.
void set_value(const std::string &Vname, const LuabindObject &V)
LuabindObject call()
Call a function pointed to by this object.
Instance of BridgeLuabindAndGenericBase for a particular type.
LuabindObject call(const T1 &Arg1, const T2 &Arg2)
bool is_type() const
Test if value in a table is the given type.
LuabindObject(const boost::shared_ptr< LuaState > &Ls, const LuabindObject &V)
LuabindObject get_index(int Vidx) const
void set_index(int Vidx, int V)
void print(std::ostream &Os) const
LuabindObject(const luabind::object &Obj, const boost::shared_ptr< LuaState > &Ls)
Constructor.
virtual boost::shared_ptr< LuabindObject > luabind_object(const boost::shared_ptr< LuaState > &Ls, const boost::shared_ptr< GenericObject > &Obj) const
Create a LuabindObject from the given type, or return a null pointer if we can&#39;t. ...
int length(int index=0) const
Return the "length" of the value at the given index using the Lua interpreter method that evaluates a...
static LuabindObject nil(const boost::shared_ptr< LuaState > &Ls)
Return a nil value.
LuabindObject new_table()
Creates a new table object.
virtual bool set_value(LuabindObject &Lobj, const std::string &Vname, const boost::shared_ptr< GenericObject > &Obj)=0
Set a value if we can. Return true if we set the value, false otherwise.
T value() const
Return value of a variable.
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
LuabindObject call(const T1 &Arg1, const T2 &Arg2, const T3 &Arg3, const T4 &Arg4, const T5 &Arg5, const T6 &Arg6, const T7 &Arg7, const T8 &Arg8)
For use with SWIG, it is useful to have a base class that everything can be cast to.
Utility class.
Definition: dir_change.h:15
LuabindObject call(const T1 &Arg1, const T2 &Arg2, const T3 &Arg3, const T4 &Arg4)
bool is_nil() const
Test type of an object.
virtual bool set_index(LuabindObject &Lobj, int Vidx, const boost::shared_ptr< GenericObject > &Obj)
Set a value if we can. Return true if we set the value, false otherwise.
static std::vector< boost::shared_ptr< BridgeLuabindAndGenericBase > > derived_bridge
Map used for mapping derived class to and from Lua and GenericObject.
void set_index(int Vidx, const LuabindObject &V)
void set_value(const std::string &Vname, const std::string &V)
void set_index(int Vidx, double V)
const luabind::object & object() const
LuabindObject(const boost::shared_ptr< LuaState > &Ls, const T &V)
Conversion constructor.
LuabindObject call(const T1 &Arg1)
virtual bool set_value(LuabindObject &Lobj, const std::string &Vname, const boost::shared_ptr< GenericObject > &Obj)
Set a value if we can. Return true if we set the value, false otherwise.
void set_value(const std::string &Vname, const blitz::Array< T, D > &V)
LuabindObject key() const

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:10