ai/lua/lua_object.hpp

Go to the documentation of this file.
00001 /* $Id: lua_object.hpp 52915 2012-02-04 14:22:40Z crab $ */
00002 /*
00003    Copyright (C) 2011 - 2012 by Dmitry Kovalenko <nephro.wes@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  * Lua object(value) wrapper implementation
00019  */
00020 
00021 
00022 #ifndef LUA_OBJECT_HPP_INCLUDED
00023 #define LUA_OBJECT_HPP_INCLUDED
00024 
00025 #include <string>
00026 #include <vector>
00027 #include <boost/shared_ptr.hpp>
00028 
00029 #include "lua/lualib.h"
00030 #include "../../scripting/lua_api.hpp"
00031 #include "config.hpp"
00032 #include "../default/contexts.hpp"
00033 #include "terrain_filter.hpp"
00034 #include "resources.hpp"
00035 
00036 namespace ai {
00037 
00038 
00039 class lua_object_base {
00040 
00041 public:
00042     lua_object_base();
00043     virtual ~lua_object_base() {}
00044 
00045     virtual void store(lua_State* L, int n) = 0;
00046 };
00047 
00048 template <typename T>
00049 class lua_object : public lua_object_base
00050 {
00051 
00052 public:
00053 
00054     lua_object()
00055         : value_()
00056     {
00057         // empty
00058     }
00059 
00060     boost::shared_ptr<T> get()
00061     {
00062         return value_;
00063     }
00064 
00065     void store(lua_State* L, int n)
00066     {
00067         this->value_ = boost::shared_ptr<T>(to_type(L, n));
00068     }
00069 
00070 protected:
00071 
00072     // A group of functions that deal with the translation of the results to C++
00073     boost::shared_ptr<T> to_type(lua_State *, int)
00074     {
00075         return boost::shared_ptr<T>();
00076     }
00077 
00078     boost::shared_ptr<T> value_;
00079 };
00080 
00081 template <>
00082 inline boost::shared_ptr<double> lua_object<double>::to_type(lua_State *L, int n)
00083 {
00084     return boost::shared_ptr<double>(new double(lua_tonumber(L, n)));
00085 }
00086 
00087 template <>
00088 inline boost::shared_ptr<std::string> lua_object<std::string>::to_type(lua_State *L, int n)
00089 {
00090     return boost::shared_ptr<std::string>(new std::string(lua_tostring(L, n)));
00091 }
00092 
00093 template <>
00094 inline boost::shared_ptr<bool> lua_object<bool>::to_type(lua_State *L, int n)
00095 {
00096 #ifdef _MSC_VER
00097 #pragma warning (push)
00098 #pragma warning (disable : 4800)
00099 #endif
00100 
00101     return boost::shared_ptr<bool>(new bool(lua_toboolean(L, n)));
00102 
00103 #ifdef _MSC_VER
00104 #pragma warning (pop)
00105 #endif
00106 }
00107 
00108 template <>
00109 inline boost::shared_ptr<int> lua_object<int>::to_type(lua_State *L, int n)
00110 {
00111     return boost::shared_ptr<int>(new int(lua_tointeger(L, n)));
00112 }
00113 
00114 template <>
00115 inline boost::shared_ptr< std::vector<std::string> > lua_object< std::vector<std::string> >::to_type(lua_State *L, int n)
00116 {
00117     boost::shared_ptr< std::vector<std::string> > v = boost::shared_ptr< std::vector<std::string> >(new std::vector<std::string>());
00118     int l = lua_rawlen(L, n);
00119     for (int i = 1; i < l + 1; ++i)
00120     {
00121         lua_pushinteger(L, i);
00122         lua_gettable(L, n);
00123         std::string  s = lua_tostring(L, -1);
00124         lua_settop(L, n);
00125         v->push_back(s);
00126     }
00127 
00128     return v;
00129 }
00130 
00131 template <>
00132 inline boost::shared_ptr<config> lua_object<config>::to_type(lua_State *L, int n)
00133 {
00134     boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config());
00135     luaW_toconfig(L, n, *cfg);
00136     return cfg;
00137 }
00138 
00139 template <>
00140 inline boost::shared_ptr<terrain_filter> lua_object<terrain_filter>::to_type(lua_State *L, int n)
00141 {
00142     // To Crab_: Is this part ok? I tested it, works fine
00143     boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config());
00144     boost::shared_ptr<vconfig> vcfg = boost::shared_ptr<vconfig>(new vconfig(*cfg));
00145     luaW_tovconfig(L, n, *vcfg);
00146     boost::shared_ptr<terrain_filter> tf = boost::shared_ptr<terrain_filter>(new terrain_filter(*vcfg, *resources::units));
00147     return tf;
00148 }
00149 
00150 template <>
00151 inline boost::shared_ptr<std::vector<target> > lua_object< std::vector<target> >::to_type(lua_State *L, int n)
00152 {
00153     boost::shared_ptr<std::vector<target> > targets = boost::shared_ptr<std::vector<target> >(new std::vector<target>());
00154     std::back_insert_iterator< std::vector<target> > tg(*targets);
00155     int l = lua_rawlen(L, n);
00156 
00157     for (int i = 1; i <= l; ++i)
00158     {
00159         lua_rawgeti(L, n, i); // st n + 1  TABLE @ N    table @ n + 1
00160 
00161         lua_pushstring(L, "loc"); // st n + 2
00162         lua_rawget(L, -2); // st n + 2
00163 
00164         lua_pushstring(L, "x"); // st n + 3
00165         lua_rawget(L, -2); // st n + 3
00166         int x = lua_tointeger(L, -1); // st n + 3
00167         lua_pop(L, 1); // st n + 2
00168 
00169         lua_pushstring(L, "y"); // st n + 3
00170         lua_rawget(L, -2); // st n + 3
00171         int y = lua_tointeger(L, -1); // st n + 3
00172 
00173         lua_pop(L, 2); // st n + 1
00174 
00175         lua_pushstring(L, "type"); // st n + 2
00176         lua_rawget(L, -2);  // st n + 2
00177         target::TYPE type = static_cast<target::TYPE>(lua_tointeger(L, -1));  // st n + 2
00178         lua_pop(L, 1); // st n + 1
00179 
00180 
00181         lua_pushstring(L, "value");
00182         lua_rawget(L, -2);
00183         int value = lua_tointeger(L, -1);
00184 
00185         map_location ml(x - 1, y - 1);
00186 
00187         *tg = target(ml, value, type);
00188     }
00189 
00190     lua_settop(L, n);
00191     return targets;
00192 }
00193 
00194 } // end of namespace ai
00195 
00196 
00197 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:44 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs