From 481f33cdee8a907c98f1b87dd1b65418b096c4f6 Mon Sep 17 00:00:00 2001
From: Richard Maudsley <richard.maudsley@isode.com>
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<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>();
 	}
@@ -168,8 +168,13 @@ boost::shared_ptr<Payload> DOMElementConvertor::convertFromLua(lua_State* L, int
 }
 
 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);
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<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;
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<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
@@ -16,7 +16,7 @@ namespace Swift {
 			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
@@ -44,7 +44,7 @@ boost::shared_ptr<PubSubEventItem> PubSubEventItemConvertor::doConvertFromLua(lu
 			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);
 				}
 			}
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<PubSubItem> PubSubItemConvertor::doConvertFromLua(lua_State* L
 			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);
 				}
 			}
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<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());
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<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
@@ -23,19 +23,19 @@ namespace Swift {
 
 			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);
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<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
@@ -54,7 +54,7 @@ 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");
 	}
@@ -70,18 +70,18 @@ boost::shared_ptr<Payload> LuaElementConvertors::convertFromLua(lua_State* L, in
 	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());
@@ -96,7 +96,7 @@ int LuaElementConvertors::convertToLua(lua_State* L, boost::shared_ptr<Payload>
 	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;
 	}
@@ -104,7 +104,7 @@ int LuaElementConvertors::convertToLuaUntyped(lua_State* L, boost::shared_ptr<Pa
 }
 
 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;
 	}
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<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:
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<boost::shared_ptr
 
 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));
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> 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");
 	}
-- 
cgit v0.10.2-6-g49f6