diff options
author | Richard Maudsley <richard.maudsley@isode.com> | 2014-02-28 14:38:04 (GMT) |
---|---|---|
committer | Richard Maudsley <richard.maudsley@isode.com> | 2014-05-02 08:47:32 (GMT) |
commit | 47ba7eb4a5d3a48f4aa554ff07d20cc7c8682bae (patch) | |
tree | e6a6d69e187e75fbf34ffa89370476a9a3bc9752 /Sluift/ElementConvertors/MAMResultConvertor.cpp | |
parent | 481f33cdee8a907c98f1b87dd1b65418b096c4f6 (diff) | |
download | swift-47ba7eb4a5d3a48f4aa554ff07d20cc7c8682bae.zip swift-47ba7eb4a5d3a48f4aa554ff07d20cc7c8682bae.tar.bz2 |
Added Sluift MAM convertors.
Change-Id: I472023726e84fbdd14af1fd9f57de411a20eb584
Diffstat (limited to 'Sluift/ElementConvertors/MAMResultConvertor.cpp')
-rw-r--r-- | Sluift/ElementConvertors/MAMResultConvertor.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Sluift/ElementConvertors/MAMResultConvertor.cpp b/Sluift/ElementConvertors/MAMResultConvertor.cpp new file mode 100644 index 0000000..8ba4de7 --- /dev/null +++ b/Sluift/ElementConvertors/MAMResultConvertor.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2014 Kevin Smith and Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <boost/numeric/conversion/cast.hpp> +#include <boost/smart_ptr/make_shared.hpp> +#include <lua.hpp> +#include <Sluift/ElementConvertors/MAMResultConvertor.h> +#include <Sluift/LuaElementConvertors.h> +#include <Swiften/Elements/Forwarded.h> + + +#pragma clang diagnostic ignored "-Wunused-private-field" + +using namespace Swift; + +MAMResultConvertor::MAMResultConvertor(LuaElementConvertors* convertors) : + GenericLuaElementConvertor<MAMResult>("mam_result"), + convertors(convertors) { +} + +MAMResultConvertor::~MAMResultConvertor() { +} + +boost::shared_ptr<MAMResult> MAMResultConvertor::doConvertFromLua(lua_State* L) { + boost::shared_ptr<MAMResult> result = boost::make_shared<MAMResult>(); + lua_getfield(L, -1, "payload"); + if (!lua_isnil(L, -1)) { + boost::shared_ptr<Forwarded> payload = boost::dynamic_pointer_cast<Forwarded>(convertors->convertFromLuaUntyped(L, -1, "payload")); + if (!!payload) { + result->setPayload(payload); + } + } + 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); + lua_getfield(L, -1, "query_id"); + if (lua_isstring(L, -1)) { + result->setQueryID(std::string(lua_tostring(L, -1))); + } + lua_pop(L, 1); + return result; +} + +void MAMResultConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<MAMResult> payload) { + lua_createtable(L, 0, 0); + if (convertors->convertToLuaUntyped(L, payload->getPayload()) > 0) { + lua_setfield(L, -2, "payload"); + } + lua_pushstring(L, payload->getID().c_str()); + lua_setfield(L, -2, "id"); + if (payload->getQueryID()) { + lua_pushstring(L, (*payload->getQueryID()).c_str()); + lua_setfield(L, -2, "query_id"); + } +} + +boost::optional<LuaElementConvertor::Documentation> MAMResultConvertor::getDocumentation() const { + return Documentation( + "MAMResult", + "This table has the following fields:\n\n" + "- `payload`: @{Forwarded}\n" + "- `id`: string\n" + "- `query_id`: string (Optional)\n" + ); +} |