Savarese Software Research Corporation
lua/Properties.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WSPR_LUA_PROPERTIES_H
00023 #define __SSRC_WSPR_LUA_PROPERTIES_H
00024 
00025 #include <ssrc/wispers/utility/Properties.h>
00026 #include <ssrc/wispers/utility/ToString.h>
00027 
00028 #include <ssrc/wispers/lua/lua.h>
00029 
00030 __BEGIN_NS_SSRC_WSPR_LUA
00031 
00032 using NS_SSRC_WSPR_UTILITY::Properties;
00033 using NS_SSRC_WSPR_UTILITY::properties_ptr;
00034 using NS_SSRC_WSPR_UTILITY::primitive_property_vector;
00035 using NS_SSRC_WSPR_UTILITY::property_vector;
00036 using NS_SSRC_WSPR_UTILITY::ToString;
00037 
00038 class PropertiesToTable {
00039 
00040   struct PushValue : public boost::static_visitor<void> {
00041     lua_State *state;
00042     mutable ToString string_cast;
00043     const PropertiesToTable & to_table;
00044 
00045     PushValue(lua_State *state, const PropertiesToTable & to_table) :
00046       state(state), to_table(to_table)
00047     { }
00048 
00049     void operator()(const bool v) const {
00050       lua_pushboolean(state, v);
00051     }
00052 
00053     void operator()(const std::int32_t v) const {
00054       lua_pushinteger(state, v);
00055     }
00056 
00057     void operator()(const std::uint32_t v) const {
00058       lua_pushinteger(state, v);
00059     }
00060 
00061     void operator()(const std::int64_t v) const {
00062       // TODO: Decide whether to use lightuserdata instead.
00063       const string & s = string_cast(v);
00064       lua_pushlstring(state, s.c_str(), s.size());
00065     }
00066 
00067     void operator()(const std::uint64_t v) const {
00068       // TODO: Decide whether to use lightuserdata instead.
00069       const string & s = string_cast(v);
00070       lua_pushlstring(state, s.c_str(), s.size());
00071     }
00072 
00073     void operator()(const double v) const {
00074       lua_pushnumber(state, v);
00075     }
00076 
00077     void operator()(const std::string & v) const {
00078       lua_pushlstring(state, v.c_str(), v.size());
00079     }
00080 
00081     void operator()(const primitive_property_vector & v) const;
00082 
00083     void operator()(const property_vector & v) const;
00084 
00085     void operator()(const Properties & properties) const;
00086   };
00087 
00088 
00089 public:
00090 
00091   PushValue push;
00092 
00093   PropertiesToTable(lua_State *state) : push(state, *this) { }
00094 
00095   lua_State * state() const { return push.state; }
00096 
00097   void reset(lua_State *state) { push.state = state; }
00098 
00099   void enter(const std::string key, const Properties *properties,
00100              const bool) const;
00101   void leave(const std::string key, const Properties *properties,
00102              const bool) const;
00103 };
00104 
00105 inline void properties_to_table(const PropertiesToTable & to_table,
00106                                 const Properties & properties)
00107 {
00108   lua_newtable(to_table.state());
00109   if(!properties.is_leaf()) {
00110     properties.visit(to_table);
00111   }
00112 }
00113 
00121 inline void properties_to_table(lua_State *state,
00122                                 const Properties & properties)
00123 {
00124   properties_to_table(PropertiesToTable(state), properties);
00125 }
00126 
00127 void table_to_properties(lua_State *state, Properties & properties);
00128 
00129 void swig_table_to_properties(void *state, Properties & properties);
00130 
00131 properties_ptr load_properties(lua_State *state, const std::string & filename)
00132   SSRC_DECL_THROW(LuaCallError);
00133 
00134 properties_ptr load_string_properties(lua_State *state,
00135                                       const std::string & code_str)
00136   SSRC_DECL_THROW(LuaCallError);
00137 
00138 inline properties_ptr load_properties(const std::string & filename) {
00139   lua_State *state = luaL_newstate();
00140   luaL_openlibs(state);
00141   properties_ptr && properties = load_properties(state, filename);
00142   lua_close(state);
00143   return properties;
00144 }
00145 
00146 inline properties_ptr load_string_properties(const std::string & code_str) {
00147   lua_State *state = luaL_newstate();
00148   luaL_openlibs(state);
00149   properties_ptr && properties = load_string_properties(state, code_str);
00150   lua_close(state);
00151   return properties;
00152 }
00153 
00154 __END_NS_SSRC_WSPR_LUA
00155 
00156 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.