diff options
| author | Richard Maudsley <richard.maudsley@isode.com> | 2014-04-29 08:57:17 (GMT) |
|---|---|---|
| committer | Richard Maudsley <richard.maudsley@isode.com> | 2014-04-30 12:35:31 (GMT) |
| commit | 481f33cdee8a907c98f1b87dd1b65418b096c4f6 (patch) | |
| tree | 7a53d23cc300f2aeecd5e3ea73b86bc09f318bce /Sluift | |
| parent | c4b64106e7b9493c7b28cc653797457d5f7fbdda (diff) | |
| download | swift-contrib-481f33cdee8a907c98f1b87dd1b65418b096c4f6.zip swift-contrib-481f33cdee8a907c98f1b87dd1b65418b096c4f6.tar.bz2 | |
Update LuaElementConvertor interface to work with Element instead of Payload.
Change-Id: I4f8b69b1a13fff21c605011f45763e01f03259cf
Diffstat (limited to 'Sluift')
| -rw-r--r-- | Sluift/ElementConvertors/DOMElementConvertor.cpp | 9 | ||||
| -rw-r--r-- | Sluift/ElementConvertors/DOMElementConvertor.h | 4 | ||||
| -rw-r--r-- | Sluift/ElementConvertors/DefaultElementConvertor.cpp | 6 | ||||
| -rw-r--r-- | Sluift/ElementConvertors/DefaultElementConvertor.h | 4 | ||||
| -rw-r--r-- | Sluift/ElementConvertors/PubSubEventItemConvertor.cpp | 2 | ||||
| -rw-r--r-- | Sluift/ElementConvertors/PubSubItemConvertor.cpp | 2 | ||||
| -rw-r--r-- | Sluift/ElementConvertors/RawXMLElementConvertor.cpp | 8 | ||||
| -rw-r--r-- | Sluift/ElementConvertors/RawXMLElementConvertor.h | 4 | ||||
| -rw-r--r-- | Sluift/GenericLuaElementConvertor.h | 8 | ||||
| -rw-r--r-- | Sluift/LuaElementConvertor.h | 6 | ||||
| -rw-r--r-- | Sluift/LuaElementConvertors.cpp | 14 | ||||
| -rw-r--r-- | Sluift/LuaElementConvertors.h | 12 | ||||
| -rw-r--r-- | Sluift/client.cpp | 2 | ||||
| -rw-r--r-- | Sluift/sluift.cpp | 2 |
14 files changed, 46 insertions, 37 deletions
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 @@ -154,28 +154,33 @@ namespace { } } DOMElementConvertor::DOMElementConvertor() { } DOMElementConvertor::~DOMElementConvertor() { } -boost::shared_ptr<Payload> DOMElementConvertor::convertFromLua(lua_State* L, int index, const std::string& type) { +boost::shared_ptr<Element> DOMElementConvertor::convertFromLua(lua_State* L, int index, const std::string& type) { if (!lua_istable(L, index) || type != "dom") { return boost::shared_ptr<Payload>(); } return boost::make_shared<RawXMLPayload>(serializeElement(L).c_str()); } boost::optional<std::string> DOMElementConvertor::convertToLua( - lua_State* L, boost::shared_ptr<Payload> payload) { + lua_State* L, boost::shared_ptr<Element> element) { // Serialize payload to XML + boost::shared_ptr<Payload> payload = boost::dynamic_pointer_cast<Payload>(element); + if (!payload) { + return boost::optional<std::string>(); + } + PayloadSerializer* serializer = serializers.getPayloadSerializer(payload); assert(serializer); std::string serializedPayload = serializer->serialize(payload); lua_newtable(L); // Parse the payload again ParserClient parserClient(L); boost::shared_ptr<XMLParser> parser(parsers.createXMLParser(&parserClient)); 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 @@ -12,17 +12,17 @@ #include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> #include <Swiften/Parser/PlatformXMLParserFactory.h> namespace Swift { class DOMElementConvertor : public LuaElementConvertor { public: DOMElementConvertor(); virtual ~DOMElementConvertor(); - virtual boost::shared_ptr<Payload> convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; - virtual boost::optional<std::string> convertToLua(lua_State*, boost::shared_ptr<Payload>) SWIFTEN_OVERRIDE; + virtual boost::shared_ptr<Element> convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; + virtual boost::optional<std::string> convertToLua(lua_State*, boost::shared_ptr<Element>) SWIFTEN_OVERRIDE; private: PlatformXMLParserFactory parsers; FullPayloadSerializerCollection serializers; }; } 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 @@ -12,19 +12,19 @@ using namespace Swift; DefaultElementConvertor::DefaultElementConvertor() { } DefaultElementConvertor::~DefaultElementConvertor() { } -boost::shared_ptr<Payload> DefaultElementConvertor::convertFromLua(lua_State*, int, const std::string& type) { +boost::shared_ptr<Element> DefaultElementConvertor::convertFromLua(lua_State*, int, const std::string& type) { std::cerr << "Warning: Unable to convert type '" << type << "'" << std::endl; - return boost::shared_ptr<Payload>(); + return boost::shared_ptr<Element>(); } -boost::optional<std::string> DefaultElementConvertor::convertToLua(lua_State*, boost::shared_ptr<Payload>) { +boost::optional<std::string> DefaultElementConvertor::convertToLua(lua_State*, boost::shared_ptr<Element>) { // 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 @@ -10,13 +10,13 @@ #include <Sluift/LuaElementConvertor.h> namespace Swift { class DefaultElementConvertor : public LuaElementConvertor { public: DefaultElementConvertor(); virtual ~DefaultElementConvertor(); - virtual boost::shared_ptr<Payload> convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; - virtual boost::optional<std::string> convertToLua(lua_State*, boost::shared_ptr<Payload>) SWIFTEN_OVERRIDE; + virtual boost::shared_ptr<Element> convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; + virtual boost::optional<std::string> convertToLua(lua_State*, boost::shared_ptr<Element>) 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 @@ -38,19 +38,19 @@ boost::shared_ptr<PubSubEventItem> PubSubEventItemConvertor::doConvertFromLua(lu } lua_pop(L, 1); lua_getfield(L, -1, "data"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< boost::shared_ptr<Payload> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { - if (boost::shared_ptr<Payload> payload = convertors->convertFromLua(L, -1)) { + if (boost::shared_ptr<Payload> payload = boost::dynamic_pointer_cast<Payload>(convertors->convertFromLua(L, -1))) { items.push_back(payload); } } lua_pop(L, 1); } result->setData(items); } lua_pop(L, 1); 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 @@ -28,19 +28,19 @@ PubSubItemConvertor::~PubSubItemConvertor() { boost::shared_ptr<PubSubItem> PubSubItemConvertor::doConvertFromLua(lua_State* L) { boost::shared_ptr<PubSubItem> result = boost::make_shared<PubSubItem>(); lua_getfield(L, -1, "data"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< boost::shared_ptr<Payload> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { - if (boost::shared_ptr<Payload> payload = convertors->convertFromLua(L, -1)) { + if (boost::shared_ptr<Payload> payload = boost::dynamic_pointer_cast<Payload>(convertors->convertFromLua(L, -1))) { items.push_back(payload); } } lua_pop(L, 1); } result->setData(items); } lua_pop(L, 1); 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 @@ -16,22 +16,26 @@ using namespace Swift; RawXMLElementConvertor::RawXMLElementConvertor() { } RawXMLElementConvertor::~RawXMLElementConvertor() { } -boost::shared_ptr<Payload> RawXMLElementConvertor::convertFromLua(lua_State* L, int index, const std::string& type) { +boost::shared_ptr<Element> RawXMLElementConvertor::convertFromLua(lua_State* L, int index, const std::string& type) { if (type == "xml") { return boost::make_shared<RawXMLPayload>(std::string(Lua::checkString(L, index))); } return boost::shared_ptr<Payload>(); } -boost::optional<std::string> RawXMLElementConvertor::convertToLua(lua_State* L, boost::shared_ptr<Payload> payload) { +boost::optional<std::string> RawXMLElementConvertor::convertToLua(lua_State* L, boost::shared_ptr<Element> element) { + boost::shared_ptr<Payload> payload = boost::dynamic_pointer_cast<Payload>(element); + if (!payload) { + return boost::optional<std::string>(); + } PayloadSerializer* serializer = serializers.getPayloadSerializer(payload); assert(serializer); lua_pushstring(L, serializer->serialize(payload).c_str()); return std::string("xml"); } 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 @@ -11,16 +11,16 @@ #include <Sluift/LuaElementConvertor.h> #include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> namespace Swift { class RawXMLElementConvertor : public LuaElementConvertor { public: RawXMLElementConvertor(); virtual ~RawXMLElementConvertor(); - virtual boost::shared_ptr<Payload> convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; - virtual boost::optional<std::string> convertToLua(lua_State*, boost::shared_ptr<Payload>) SWIFTEN_OVERRIDE; + virtual boost::shared_ptr<Element> convertFromLua(lua_State*, int index, const std::string& type) SWIFTEN_OVERRIDE; + virtual boost::optional<std::string> convertToLua(lua_State*, boost::shared_ptr<Element>) 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 @@ -17,31 +17,31 @@ namespace Swift { template<typename T> class GenericLuaElementConvertor : public LuaElementConvertor { public: GenericLuaElementConvertor(const std::string& type) : type(type) { } virtual ~GenericLuaElementConvertor() {} - virtual boost::shared_ptr<Payload> convertFromLua(lua_State* L, int index, const std::string& payloadType) SWIFTEN_OVERRIDE { + virtual boost::shared_ptr<Element> 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<Payload> result = doConvertFromLua(L); + boost::shared_ptr<Element> result = doConvertFromLua(L); lua_pop(L, 1); return result; } - return boost::shared_ptr<Payload>(); + return boost::shared_ptr<Element>(); } virtual boost::optional<std::string> convertToLua( - lua_State* L, boost::shared_ptr<Payload> payload) SWIFTEN_OVERRIDE { + lua_State* L, boost::shared_ptr<Element> payload) SWIFTEN_OVERRIDE { if (boost::shared_ptr<T> actualPayload = boost::dynamic_pointer_cast<T>(payload)) { doConvertToLua(L, actualPayload); assert(lua_type(L, -1) == LUA_TTABLE); return type; } return NO_RESULT; } protected: diff --git a/Sluift/LuaElementConvertor.h b/Sluift/LuaElementConvertor.h index b25f43b..5e55add 100644 --- a/Sluift/LuaElementConvertor.h +++ b/Sluift/LuaElementConvertor.h @@ -9,32 +9,32 @@ #include <Swiften/Base/Override.h> #include <string> #include <boost/shared_ptr.hpp> #include <boost/optional.hpp> struct lua_State; namespace Swift { - class Payload; + class Element; class LuaElementConvertor { public: static boost::optional<std::string> NO_RESULT; struct Documentation { Documentation(const std::string& className, const std::string& description) : className(className), description(description) {} std::string className; std::string description; }; virtual ~LuaElementConvertor(); - virtual boost::shared_ptr<Payload> convertFromLua(lua_State*, int index, const std::string& type) = 0; - virtual boost::optional<std::string> convertToLua(lua_State*, boost::shared_ptr<Payload>) = 0; + virtual boost::shared_ptr<Element> convertFromLua(lua_State*, int index, const std::string& type) = 0; + virtual boost::optional<std::string> convertToLua(lua_State*, boost::shared_ptr<Element>) = 0; virtual boost::optional<Documentation> getDocumentation() const { return boost::optional<Documentation>(); } }; } diff --git a/Sluift/LuaElementConvertors.cpp b/Sluift/LuaElementConvertors.cpp index ba86c06..a79b578 100644 --- a/Sluift/LuaElementConvertors.cpp +++ b/Sluift/LuaElementConvertors.cpp @@ -48,69 +48,69 @@ LuaElementConvertors::LuaElementConvertors() { convertors.push_back(boost::make_shared<RawXMLElementConvertor>()); convertors.push_back(boost::make_shared<DefaultElementConvertor>()); } LuaElementConvertors::~LuaElementConvertors() { } #include <Sluift/ElementConvertors/ElementConvertors.ipp> -boost::shared_ptr<Payload> LuaElementConvertors::convertFromLua(lua_State* L, int index) { +boost::shared_ptr<Element> LuaElementConvertors::convertFromLua(lua_State* L, int index) { if (lua_isstring(L, index)) { return convertFromLuaUntyped(L, index, "xml"); } else if (lua_istable(L, index)) { lua_getfield(L, index, "_type"); if (lua_isstring(L, -1)) { std::string type = lua_tostring(L, -1); lua_pop(L, 1); return convertFromLuaUntyped(L, index, type); } lua_pop(L, 1); } throw Lua::Exception("Unable to determine type"); } -boost::shared_ptr<Payload> LuaElementConvertors::convertFromLuaUntyped(lua_State* L, int index, const std::string& type) { +boost::shared_ptr<Element> LuaElementConvertors::convertFromLuaUntyped(lua_State* L, int index, const std::string& type) { index = Lua::absoluteOffset(L, index); foreach (boost::shared_ptr<LuaElementConvertor> convertor, convertors) { - if (boost::shared_ptr<Payload> result = convertor->convertFromLua(L, index, type)) { + if (boost::shared_ptr<Element> result = convertor->convertFromLua(L, index, type)) { return result; } } - return boost::shared_ptr<Payload>(); + return boost::shared_ptr<Element>(); } -int LuaElementConvertors::convertToLua(lua_State* L, boost::shared_ptr<Payload> payload) { +int LuaElementConvertors::convertToLua(lua_State* L, boost::shared_ptr<Element> payload) { if (boost::optional<std::string> type = doConvertToLuaUntyped(L, payload)) { if (lua_istable(L, -1)) { lua_pushstring(L, type->c_str()); lua_setfield(L, -2, "_type"); Lua::registerTableToString(L, -1); } else { assert(*type == "xml"); } return 1; } return 0; } -int LuaElementConvertors::convertToLuaUntyped(lua_State* L, boost::shared_ptr<Payload> payload) { +int LuaElementConvertors::convertToLuaUntyped(lua_State* L, boost::shared_ptr<Element> payload) { if (doConvertToLuaUntyped(L, payload)) { return 1; } return 0; } boost::optional<std::string> LuaElementConvertors::doConvertToLuaUntyped( - lua_State* L, boost::shared_ptr<Payload> payload) { + lua_State* L, boost::shared_ptr<Element> payload) { if (!payload) { return LuaElementConvertor::NO_RESULT; } foreach (boost::shared_ptr<LuaElementConvertor> convertor, convertors) { if (boost::optional<std::string> type = convertor->convertToLua(L, payload)) { return *type; } } 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 @@ -10,42 +10,42 @@ #include <vector> #include <boost/shared_ptr.hpp> #include <boost/optional.hpp> struct lua_State; namespace Swift { class LuaElementConvertor; - class Payload; + class Element; class LuaElementConvertors { public: LuaElementConvertors(); virtual ~LuaElementConvertors(); - boost::shared_ptr<Payload> convertFromLua(lua_State*, int index); - int convertToLua(lua_State*, boost::shared_ptr<Payload>); + boost::shared_ptr<Element> convertFromLua(lua_State*, int index); + int convertToLua(lua_State*, boost::shared_ptr<Element>); /** * Adds a toplevel type+data table with the given type. */ - boost::shared_ptr<Payload> convertFromLuaUntyped(lua_State*, int index, const std::string& type); + boost::shared_ptr<Element> 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<Payload>); + int convertToLuaUntyped(lua_State*, boost::shared_ptr<Element>); const std::vector< boost::shared_ptr<LuaElementConvertor> >& getConvertors() const { return convertors; } private: - boost::optional<std::string> doConvertToLuaUntyped(lua_State*, boost::shared_ptr<Payload>); + boost::optional<std::string> doConvertToLuaUntyped(lua_State*, boost::shared_ptr<Element>); void registerConvertors(); private: std::vector< boost::shared_ptr<LuaElementConvertor> > convertors; }; } diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 63e3bf1..db259cd 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -57,19 +57,19 @@ static void addPayloadsToTable(lua_State* L, const std::vector<boost::shared_ptr lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); } Lua::registerGetByTypeIndex(L, -1); lua_setfield(L, -2, "payloads"); } } static boost::shared_ptr<Payload> 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<Payload>(Sluift::globals.elementConvertor.convertFromLua(L, index)); } else if (lua_type(L, index) == LUA_TSTRING) { return boost::make_shared<RawXMLPayload>(Lua::checkString(L, index)); } else { return boost::shared_ptr<Payload>(); } } diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index 3908631..a04ceeb 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -142,19 +142,19 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( } SLUIFT_LUA_FUNCTION_WITH_HELP( Sluift, to_xml, "Convert a structured element into XML.", "element the element to convert", "" ) { static FullPayloadSerializerCollection serializers; - boost::shared_ptr<Payload> payload = Sluift::globals.elementConvertor.convertFromLua(L, 1); + boost::shared_ptr<Payload> payload = boost::dynamic_pointer_cast<Payload>(Sluift::globals.elementConvertor.convertFromLua(L, 1)); if (!payload) { throw Lua::Exception("Unrecognized XML"); } PayloadSerializer* serializer = serializers.getPayloadSerializer(payload); if (!payload) { throw Lua::Exception("Unrecognized XML"); } lua_pushstring(L, serializer->serialize(payload).c_str()); return 1; |
Swift