summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Sluift/ElementConvertors/DOMElementConvertor.cpp9
-rw-r--r--Sluift/ElementConvertors/DOMElementConvertor.h4
-rw-r--r--Sluift/ElementConvertors/DefaultElementConvertor.cpp6
-rw-r--r--Sluift/ElementConvertors/DefaultElementConvertor.h4
-rw-r--r--Sluift/ElementConvertors/PubSubEventItemConvertor.cpp2
-rw-r--r--Sluift/ElementConvertors/PubSubItemConvertor.cpp2
-rw-r--r--Sluift/ElementConvertors/RawXMLElementConvertor.cpp8
-rw-r--r--Sluift/ElementConvertors/RawXMLElementConvertor.h4
-rw-r--r--Sluift/GenericLuaElementConvertor.h8
-rw-r--r--Sluift/LuaElementConvertor.h6
-rw-r--r--Sluift/LuaElementConvertors.cpp14
-rw-r--r--Sluift/LuaElementConvertors.h12
-rw-r--r--Sluift/client.cpp2
-rw-r--r--Sluift/sluift.cpp2
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
@@ -128,67 +128,72 @@ namespace {
if (!attributeName.empty()) {
element.setAttribute(attributeName, attributeValue);
}
}
lua_pop(L, 1); // value
}
}
lua_pop(L, 1); // children
lua_getfield(L, -1, "children");
if (lua_istable(L, -1)) {
int index = Lua::absoluteOffset(L, -1);
for (lua_pushnil(L); lua_next(L, index) != 0; ) {
if (lua_isstring(L, -1)) {
element.addNode(boost::make_shared<XMLTextNode>(lua_tostring(L, -1)));
}
else if (lua_istable(L, -1)) {
element.addNode(boost::make_shared<XMLRawTextNode>(serializeElement(L)));
}
lua_pop(L, 1); // value
}
}
lua_pop(L, 1); // children
return element.serialize();
}
}
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));
bool result = parser->parse(serializedPayload);
assert(result);
// There can only be one element, so stripping the list
lua_pushnil(L);
lua_next(L, -2);
Lua::registerTableToString(L, -1);
lua_replace(L, -3);
lua_settop(L, -2);
return std::string("dom");
}
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
@@ -1,28 +1,28 @@
/*
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#pragma once
#include <Swiften/Base/Override.h>
#include <Sluift/LuaElementConvertor.h>
#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
@@ -1,30 +1,30 @@
/*
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#include <Sluift/ElementConvertors/DefaultElementConvertor.h>
#include <iostream>
#include <typeinfo>
#include <string>
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
@@ -1,22 +1,22 @@
/*
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#pragma once
#include <Swiften/Base/Override.h>
#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
@@ -12,71 +12,71 @@
#include <Sluift/LuaElementConvertors.h>
#include <Swiften/Base/foreach.h>
#pragma clang diagnostic ignored "-Wunused-private-field"
using namespace Swift;
PubSubEventItemConvertor::PubSubEventItemConvertor(LuaElementConvertors* convertors) :
GenericLuaElementConvertor<PubSubEventItem>("pubsub_event_item"),
convertors(convertors) {
}
PubSubEventItemConvertor::~PubSubEventItemConvertor() {
}
boost::shared_ptr<PubSubEventItem> PubSubEventItemConvertor::doConvertFromLua(lua_State* L) {
boost::shared_ptr<PubSubEventItem> result = boost::make_shared<PubSubEventItem>();
lua_getfield(L, -1, "node");
if (lua_isstring(L, -1)) {
result->setNode(std::string(lua_tostring(L, -1)));
}
lua_pop(L, 1);
lua_getfield(L, -1, "publisher");
if (lua_isstring(L, -1)) {
result->setPublisher(std::string(lua_tostring(L, -1)));
}
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);
lua_getfield(L, -1, "id");
if (lua_isstring(L, -1)) {
result->setID(std::string(lua_tostring(L, -1)));
}
lua_pop(L, 1);
return result;
}
void PubSubEventItemConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<PubSubEventItem> payload) {
lua_createtable(L, 0, 0);
if (payload->getNode()) {
lua_pushstring(L, (*payload->getNode()).c_str());
lua_setfield(L, -2, "node");
}
if (payload->getPublisher()) {
lua_pushstring(L, (*payload->getPublisher()).c_str());
lua_setfield(L, -2, "publisher");
}
if (!payload->getData().empty()) {
lua_createtable(L, boost::numeric_cast<int>(payload->getData().size()), 0);
{
int i = 0;
foreach(boost::shared_ptr<Payload> item, payload->getData()) {
if (convertors->convertToLua(L, item) > 0) {
lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
++i;
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
@@ -2,71 +2,71 @@
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <Sluift/ElementConvertors/PubSubItemConvertor.h>
#include <lua.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <Sluift/LuaElementConvertors.h>
#include <Swiften/Base/foreach.h>
#pragma clang diagnostic ignored "-Wunused-private-field"
using namespace Swift;
PubSubItemConvertor::PubSubItemConvertor(LuaElementConvertors* convertors) :
GenericLuaElementConvertor<PubSubItem>("pubsub_item"),
convertors(convertors) {
}
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);
lua_getfield(L, -1, "id");
if (lua_isstring(L, -1)) {
result->setID(std::string(lua_tostring(L, -1)));
}
lua_pop(L, 1);
return result;
}
void PubSubItemConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<PubSubItem> payload) {
lua_createtable(L, 0, 0);
if (!payload->getData().empty()) {
lua_createtable(L, boost::numeric_cast<int>(payload->getData().size()), 0);
{
int i = 0;
foreach(boost::shared_ptr<Payload> item, payload->getData()) {
if (convertors->convertToLua(L, item) > 0) {
lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
++i;
}
}
}
lua_setfield(L, -2, "data");
}
lua_pushstring(L, payload->getID().c_str());
lua_setfield(L, -2, "id");
}
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
@@ -1,37 +1,41 @@
/*
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#include <Sluift/ElementConvertors/RawXMLElementConvertor.h>
#include <iostream>
#include <boost/smart_ptr/make_shared.hpp>
#include <lua.hpp>
#include <Swiften/Elements/RawXMLPayload.h>
#include <Swiften/Serializer/PayloadSerializer.h>
#include <Sluift/Lua/Check.h>
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
@@ -1,26 +1,26 @@
/*
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#pragma once
#include <Swiften/Base/Override.h>
#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
@@ -1,54 +1,54 @@
/*
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#pragma once
#include <lua.hpp>
#include <string>
#include <Swiften/Base/Override.h>
#include <Sluift/LuaElementConvertor.h>
#include <Sluift/Lua/Check.h>
#include <Sluift/Lua/LuaUtils.h>
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:
virtual boost::shared_ptr<T> doConvertFromLua(lua_State*) = 0;
virtual void doConvertToLua(lua_State*, boost::shared_ptr<T>) = 0;
private:
std::string type;
};
}
diff --git a/Sluift/LuaElementConvertor.h b/Sluift/LuaElementConvertor.h
index b25f43b..5e55add 100644
--- a/Sluift/LuaElementConvertor.h
+++ b/Sluift/LuaElementConvertor.h
@@ -1,40 +1,40 @@
/*
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#pragma once
#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
@@ -22,97 +22,97 @@
#include <Sluift/ElementConvertors/VCardConvertor.h>
#include <Sluift/ElementConvertors/BodyConvertor.h>
#include <Sluift/ElementConvertors/CommandConvertor.h>
#include <Sluift/ElementConvertors/StatusShowConvertor.h>
#include <Sluift/ElementConvertors/StatusConvertor.h>
#include <Sluift/ElementConvertors/DelayConvertor.h>
#include <Sluift/Lua/LuaUtils.h>
#include <Sluift/Lua/Exception.h>
using namespace Swift;
LuaElementConvertors::LuaElementConvertors() {
registerConvertors();
convertors.push_back(boost::make_shared<StatusConvertor>());
convertors.push_back(boost::make_shared<StatusShowConvertor>());
convertors.push_back(boost::make_shared<DelayConvertor>());
convertors.push_back(boost::make_shared<CommandConvertor>(this));
convertors.push_back(boost::make_shared<PubSubEventConvertor>(this));
convertors.push_back(boost::make_shared<BodyConvertor>());
convertors.push_back(boost::make_shared<VCardConvertor>());
convertors.push_back(boost::make_shared<VCardUpdateConvertor>());
convertors.push_back(boost::make_shared<FormConvertor>());
convertors.push_back(boost::make_shared<SoftwareVersionConvertor>());
convertors.push_back(boost::make_shared<DiscoInfoConvertor>());
convertors.push_back(boost::make_shared<DiscoItemsConvertor>());
convertors.push_back(boost::make_shared<DOMElementConvertor>());
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
@@ -1,51 +1,51 @@
/*
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#pragma once
#include <Swiften/Base/Override.h>
#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
@@ -31,71 +31,71 @@
#include <Sluift/Lua/Check.h>
#include <Sluift/Lua/Value.h>
#include <Sluift/Lua/Exception.h>
#include <Sluift/Lua/LuaUtils.h>
#include <Sluift/globals.h>
using namespace Swift;
namespace lambda = boost::lambda;
static inline SluiftClient* getClient(lua_State* L) {
return *Lua::checkUserData<SluiftClient>(L, 1);
}
static inline int getGlobalTimeout(lua_State* L) {
lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.moduleLibIndex);
lua_getfield(L, -1, "timeout");
int result = boost::numeric_cast<int>(lua_tointeger(L, -1));
lua_pop(L, 2);
return result;
}
static void addPayloadsToTable(lua_State* L, const std::vector<boost::shared_ptr<Payload> >& payloads) {
if (!payloads.empty()) {
lua_createtable(L, boost::numeric_cast<int>(payloads.size()), 0);
for (size_t i = 0; i < payloads.size(); ++i) {
Sluift::globals.elementConvertor.convertToLua(L, payloads[i]);
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>();
}
}
static std::vector< boost::shared_ptr<Payload> > getPayloadsFromTable(lua_State* L, int index) {
index = Lua::absoluteOffset(L, index);
std::vector< boost::shared_ptr<Payload> > result;
lua_getfield(L, index, "payloads");
if (lua_istable(L, -1)) {
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
boost::shared_ptr<Payload> payload = getPayload(L, -1);
if (payload) {
result.push_back(payload);
}
}
}
lua_pop(L, 1);
return result;
}
SLUIFT_LUA_FUNCTION(Client, async_connect) {
SluiftClient* client = getClient(L);
std::string host = client->getOptions().manualHostname;
int port = client->getOptions().manualPort;
if (lua_istable(L, 2)) {
if (boost::optional<std::string> hostString = Lua::getStringField(L, 2, "host")) {
host = *hostString;
}
if (boost::optional<int> portInt = Lua::getIntField(L, 2, "port")) {
diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp
index 3908631..a04ceeb 100644
--- a/Sluift/sluift.cpp
+++ b/Sluift/sluift.cpp
@@ -116,71 +116,71 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
while (!watchdog.getTimedOut()) {
Swift::sleep(boost::numeric_cast<unsigned int>(std::min(100, timeout)));
Sluift::globals.eventLoop.runOnce();
}
return 0;
}
SLUIFT_LUA_FUNCTION_WITH_HELP(
Sluift, new_uuid,
"Generates a new UUID", "", ""
) {
lua_pushstring(L, IDGenerator().generateID().c_str());
return 1;
}
SLUIFT_LUA_FUNCTION_WITH_HELP(
Sluift, from_xml,
"Convert a raw XML string into a structured representation.",
"string the string to convert",
""
) {
PayloadsParserTester parser;
if (!parser.parse(Lua::checkString(L, 1))) {
throw Lua::Exception("Error in XML");
}
return Sluift::globals.elementConvertor.convertToLua(L, parser.getPayload());
}
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;
}
SLUIFT_LUA_FUNCTION_WITH_HELP(
Sluift, hexify,
"Convert binary data into hexadecimal format.",
"data the data to convert",
""
) {
if (!lua_isstring(L, 1)) {
throw Lua::Exception("Expected string");
}
size_t len;
const char* data = lua_tolstring(L, 1, &len);
lua_pushstring(L, Hexify::hexify(createByteArray(data, len)).c_str());
return 1;
}
SLUIFT_LUA_FUNCTION_WITH_HELP(
Sluift, unhexify,
"Convert hexadecimal data into binary data.",
"data the data in hexadecimal format",
""
) {
if (!lua_isstring(L, 1)) {
throw Lua::Exception("Expected string");
}