From 481f33cdee8a907c98f1b87dd1b65418b096c4f6 Mon Sep 17 00:00:00 2001 From: Richard Maudsley Date: Tue, 29 Apr 2014 09:57:17 +0100 Subject: Update LuaElementConvertor interface to work with Element instead of Payload. Change-Id: I4f8b69b1a13fff21c605011f45763e01f03259cf diff --git a/Sluift/ElementConvertors/DOMElementConvertor.cpp b/Sluift/ElementConvertors/DOMElementConvertor.cpp index bb4256d..fb1f658 100644 --- a/Sluift/ElementConvertors/DOMElementConvertor.cpp +++ b/Sluift/ElementConvertors/DOMElementConvertor.cpp @@ -160,7 +160,7 @@ DOMElementConvertor::DOMElementConvertor() { DOMElementConvertor::~DOMElementConvertor() { } -boost::shared_ptr DOMElementConvertor::convertFromLua(lua_State* L, int index, const std::string& type) { +boost::shared_ptr DOMElementConvertor::convertFromLua(lua_State* L, int index, const std::string& type) { if (!lua_istable(L, index) || type != "dom") { return boost::shared_ptr(); } @@ -168,8 +168,13 @@ boost::shared_ptr DOMElementConvertor::convertFromLua(lua_State* L, int } boost::optional DOMElementConvertor::convertToLua( - lua_State* L, boost::shared_ptr payload) { + lua_State* L, boost::shared_ptr element) { // Serialize payload to XML + boost::shared_ptr payload = boost::dynamic_pointer_cast(element); + if (!payload) { + return boost::optional(); + } + PayloadSerializer* serializer = serializers.getPayloadSerializer(payload); assert(serializer); std::string serializedPayload = serializer->serialize(payload); diff --git a/Sluift/ElementConvertors/DOMElementConvertor.h b/Sluift/ElementConvertors/DOMElementConvertor.h index 94d0669..fdd7304 100644 --- a/Sluift/ElementConvertors/DOMElementConvertor.h +++ b/Sluift/ElementConvertors/DOMElementConvertor.h @@ -18,8 +18,8 @@ namespace Swift { DOMElementConvertor(); virtual ~DOMElementConvertor(); - virtual boost::shared_ptr convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; - virtual boost::optional convertToLua(lua_State*, boost::shared_ptr) SWIFTEN_OVERRIDE; + virtual boost::shared_ptr convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; + virtual boost::optional convertToLua(lua_State*, boost::shared_ptr) SWIFTEN_OVERRIDE; private: PlatformXMLParserFactory parsers; diff --git a/Sluift/ElementConvertors/DefaultElementConvertor.cpp b/Sluift/ElementConvertors/DefaultElementConvertor.cpp index 62c799b..cc326df 100644 --- a/Sluift/ElementConvertors/DefaultElementConvertor.cpp +++ b/Sluift/ElementConvertors/DefaultElementConvertor.cpp @@ -18,12 +18,12 @@ DefaultElementConvertor::DefaultElementConvertor() { DefaultElementConvertor::~DefaultElementConvertor() { } -boost::shared_ptr DefaultElementConvertor::convertFromLua(lua_State*, int, const std::string& type) { +boost::shared_ptr DefaultElementConvertor::convertFromLua(lua_State*, int, const std::string& type) { std::cerr << "Warning: Unable to convert type '" << type << "'" << std::endl; - return boost::shared_ptr(); + return boost::shared_ptr(); } -boost::optional DefaultElementConvertor::convertToLua(lua_State*, boost::shared_ptr) { +boost::optional DefaultElementConvertor::convertToLua(lua_State*, boost::shared_ptr) { // Should have been handled by the raw XML convertor assert(false); return NO_RESULT; diff --git a/Sluift/ElementConvertors/DefaultElementConvertor.h b/Sluift/ElementConvertors/DefaultElementConvertor.h index ad8fe75..5a2975b 100644 --- a/Sluift/ElementConvertors/DefaultElementConvertor.h +++ b/Sluift/ElementConvertors/DefaultElementConvertor.h @@ -16,7 +16,7 @@ namespace Swift { DefaultElementConvertor(); virtual ~DefaultElementConvertor(); - virtual boost::shared_ptr convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; - virtual boost::optional convertToLua(lua_State*, boost::shared_ptr) SWIFTEN_OVERRIDE; + virtual boost::shared_ptr convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; + virtual boost::optional convertToLua(lua_State*, boost::shared_ptr) SWIFTEN_OVERRIDE; }; } diff --git a/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp b/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp index 4b150c4..9905df3 100644 --- a/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp @@ -44,7 +44,7 @@ boost::shared_ptr PubSubEventItemConvertor::doConvertFromLua(lu lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { - if (boost::shared_ptr payload = convertors->convertFromLua(L, -1)) { + if (boost::shared_ptr payload = boost::dynamic_pointer_cast(convertors->convertFromLua(L, -1))) { items.push_back(payload); } } diff --git a/Sluift/ElementConvertors/PubSubItemConvertor.cpp b/Sluift/ElementConvertors/PubSubItemConvertor.cpp index e9ed753..dcaa600 100644 --- a/Sluift/ElementConvertors/PubSubItemConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubItemConvertor.cpp @@ -34,7 +34,7 @@ boost::shared_ptr PubSubItemConvertor::doConvertFromLua(lua_State* L lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { - if (boost::shared_ptr payload = convertors->convertFromLua(L, -1)) { + if (boost::shared_ptr payload = boost::dynamic_pointer_cast(convertors->convertFromLua(L, -1))) { items.push_back(payload); } } diff --git a/Sluift/ElementConvertors/RawXMLElementConvertor.cpp b/Sluift/ElementConvertors/RawXMLElementConvertor.cpp index 35a53ca..e4cfe05 100644 --- a/Sluift/ElementConvertors/RawXMLElementConvertor.cpp +++ b/Sluift/ElementConvertors/RawXMLElementConvertor.cpp @@ -22,14 +22,18 @@ RawXMLElementConvertor::RawXMLElementConvertor() { RawXMLElementConvertor::~RawXMLElementConvertor() { } -boost::shared_ptr RawXMLElementConvertor::convertFromLua(lua_State* L, int index, const std::string& type) { +boost::shared_ptr RawXMLElementConvertor::convertFromLua(lua_State* L, int index, const std::string& type) { if (type == "xml") { return boost::make_shared(std::string(Lua::checkString(L, index))); } return boost::shared_ptr(); } -boost::optional RawXMLElementConvertor::convertToLua(lua_State* L, boost::shared_ptr payload) { +boost::optional RawXMLElementConvertor::convertToLua(lua_State* L, boost::shared_ptr element) { + boost::shared_ptr payload = boost::dynamic_pointer_cast(element); + if (!payload) { + return boost::optional(); + } PayloadSerializer* serializer = serializers.getPayloadSerializer(payload); assert(serializer); lua_pushstring(L, serializer->serialize(payload).c_str()); diff --git a/Sluift/ElementConvertors/RawXMLElementConvertor.h b/Sluift/ElementConvertors/RawXMLElementConvertor.h index 6087ba0..2ee76c5 100644 --- a/Sluift/ElementConvertors/RawXMLElementConvertor.h +++ b/Sluift/ElementConvertors/RawXMLElementConvertor.h @@ -17,8 +17,8 @@ namespace Swift { RawXMLElementConvertor(); virtual ~RawXMLElementConvertor(); - virtual boost::shared_ptr convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; - virtual boost::optional convertToLua(lua_State*, boost::shared_ptr) SWIFTEN_OVERRIDE; + virtual boost::shared_ptr convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; + virtual boost::optional convertToLua(lua_State*, boost::shared_ptr) SWIFTEN_OVERRIDE; private: FullPayloadSerializerCollection serializers; diff --git a/Sluift/GenericLuaElementConvertor.h b/Sluift/GenericLuaElementConvertor.h index afad481..3753348 100644 --- a/Sluift/GenericLuaElementConvertor.h +++ b/Sluift/GenericLuaElementConvertor.h @@ -23,19 +23,19 @@ namespace Swift { virtual ~GenericLuaElementConvertor() {} - virtual boost::shared_ptr convertFromLua(lua_State* L, int index, const std::string& payloadType) SWIFTEN_OVERRIDE { + virtual boost::shared_ptr convertFromLua(lua_State* L, int index, const std::string& payloadType) SWIFTEN_OVERRIDE { if (payloadType == type) { Lua::checkType(L, index, LUA_TTABLE); lua_pushvalue(L, index); - boost::shared_ptr result = doConvertFromLua(L); + boost::shared_ptr result = doConvertFromLua(L); lua_pop(L, 1); return result; } - return boost::shared_ptr(); + return boost::shared_ptr(); } virtual boost::optional convertToLua( - lua_State* L, boost::shared_ptr payload) SWIFTEN_OVERRIDE { + lua_State* L, boost::shared_ptr payload) SWIFTEN_OVERRIDE { if (boost::shared_ptr actualPayload = boost::dynamic_pointer_cast(payload)) { doConvertToLua(L, actualPayload); assert(lua_type(L, -1) == LUA_TTABLE); diff --git a/Sluift/LuaElementConvertor.h b/Sluift/LuaElementConvertor.h index b25f43b..5e55add 100644 --- a/Sluift/LuaElementConvertor.h +++ b/Sluift/LuaElementConvertor.h @@ -15,7 +15,7 @@ struct lua_State; namespace Swift { - class Payload; + class Element; class LuaElementConvertor { public: @@ -30,8 +30,8 @@ namespace Swift { virtual ~LuaElementConvertor(); - virtual boost::shared_ptr convertFromLua(lua_State*, int index, const std::string& type) = 0; - virtual boost::optional convertToLua(lua_State*, boost::shared_ptr) = 0; + virtual boost::shared_ptr convertFromLua(lua_State*, int index, const std::string& type) = 0; + virtual boost::optional convertToLua(lua_State*, boost::shared_ptr) = 0; virtual boost::optional getDocumentation() const { return boost::optional(); diff --git a/Sluift/LuaElementConvertors.cpp b/Sluift/LuaElementConvertors.cpp index ba86c06..a79b578 100644 --- a/Sluift/LuaElementConvertors.cpp +++ b/Sluift/LuaElementConvertors.cpp @@ -54,7 +54,7 @@ LuaElementConvertors::~LuaElementConvertors() { #include -boost::shared_ptr LuaElementConvertors::convertFromLua(lua_State* L, int index) { +boost::shared_ptr LuaElementConvertors::convertFromLua(lua_State* L, int index) { if (lua_isstring(L, index)) { return convertFromLuaUntyped(L, index, "xml"); } @@ -70,18 +70,18 @@ boost::shared_ptr LuaElementConvertors::convertFromLua(lua_State* L, in throw Lua::Exception("Unable to determine type"); } -boost::shared_ptr LuaElementConvertors::convertFromLuaUntyped(lua_State* L, int index, const std::string& type) { +boost::shared_ptr LuaElementConvertors::convertFromLuaUntyped(lua_State* L, int index, const std::string& type) { index = Lua::absoluteOffset(L, index); foreach (boost::shared_ptr convertor, convertors) { - if (boost::shared_ptr result = convertor->convertFromLua(L, index, type)) { + if (boost::shared_ptr result = convertor->convertFromLua(L, index, type)) { return result; } } - return boost::shared_ptr(); + return boost::shared_ptr(); } -int LuaElementConvertors::convertToLua(lua_State* L, boost::shared_ptr payload) { +int LuaElementConvertors::convertToLua(lua_State* L, boost::shared_ptr payload) { if (boost::optional type = doConvertToLuaUntyped(L, payload)) { if (lua_istable(L, -1)) { lua_pushstring(L, type->c_str()); @@ -96,7 +96,7 @@ int LuaElementConvertors::convertToLua(lua_State* L, boost::shared_ptr return 0; } -int LuaElementConvertors::convertToLuaUntyped(lua_State* L, boost::shared_ptr payload) { +int LuaElementConvertors::convertToLuaUntyped(lua_State* L, boost::shared_ptr payload) { if (doConvertToLuaUntyped(L, payload)) { return 1; } @@ -104,7 +104,7 @@ int LuaElementConvertors::convertToLuaUntyped(lua_State* L, boost::shared_ptr LuaElementConvertors::doConvertToLuaUntyped( - lua_State* L, boost::shared_ptr payload) { + lua_State* L, boost::shared_ptr payload) { if (!payload) { return LuaElementConvertor::NO_RESULT; } diff --git a/Sluift/LuaElementConvertors.h b/Sluift/LuaElementConvertors.h index 65b1f04..d2e2a43 100644 --- a/Sluift/LuaElementConvertors.h +++ b/Sluift/LuaElementConvertors.h @@ -16,33 +16,33 @@ struct lua_State; namespace Swift { class LuaElementConvertor; - class Payload; + class Element; class LuaElementConvertors { public: LuaElementConvertors(); virtual ~LuaElementConvertors(); - boost::shared_ptr convertFromLua(lua_State*, int index); - int convertToLua(lua_State*, boost::shared_ptr); + boost::shared_ptr convertFromLua(lua_State*, int index); + int convertToLua(lua_State*, boost::shared_ptr); /** * Adds a toplevel type+data table with the given type. */ - boost::shared_ptr convertFromLuaUntyped(lua_State*, int index, const std::string& type); + boost::shared_ptr convertFromLuaUntyped(lua_State*, int index, const std::string& type); /** * Strips the toplevel type+data table, and only return the * data. */ - int convertToLuaUntyped(lua_State*, boost::shared_ptr); + int convertToLuaUntyped(lua_State*, boost::shared_ptr); const std::vector< boost::shared_ptr >& getConvertors() const { return convertors; } private: - boost::optional doConvertToLuaUntyped(lua_State*, boost::shared_ptr); + boost::optional doConvertToLuaUntyped(lua_State*, boost::shared_ptr); void registerConvertors(); private: diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 63e3bf1..db259cd 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -63,7 +63,7 @@ static void addPayloadsToTable(lua_State* L, const std::vector getPayload(lua_State* L, int index) { if (lua_type(L, index) == LUA_TTABLE) { - return Sluift::globals.elementConvertor.convertFromLua(L, index); + return boost::dynamic_pointer_cast(Sluift::globals.elementConvertor.convertFromLua(L, index)); } else if (lua_type(L, index) == LUA_TSTRING) { return boost::make_shared(Lua::checkString(L, index)); diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index 3908631..a04ceeb 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -148,7 +148,7 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( "" ) { static FullPayloadSerializerCollection serializers; - boost::shared_ptr payload = Sluift::globals.elementConvertor.convertFromLua(L, 1); + boost::shared_ptr payload = boost::dynamic_pointer_cast(Sluift::globals.elementConvertor.convertFromLua(L, 1)); if (!payload) { throw Lua::Exception("Unrecognized XML"); } -- cgit v0.10.2-6-g49f6