summaryrefslogtreecommitdiffstats
blob: 977dbc967ab091ed7702ac4ddd71489485e94fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (c) 2014-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Sluift/ElementConvertors/MAMFinConvertor.h>

#include <boost/numeric/conversion/cast.hpp>
#include <boost/smart_ptr/make_shared.hpp>

#include <lua.hpp>

#include <Swiften/Elements/ResultSet.h>

#include <Sluift/LuaElementConvertors.h>

using namespace Swift;

MAMFinConvertor::MAMFinConvertor(LuaElementConvertors* convertors) : 
		GenericLuaElementConvertor<MAMFin>("mam_fin"),
		convertors(convertors) {
}

MAMFinConvertor::~MAMFinConvertor() {
}

boost::shared_ptr<MAMFin> MAMFinConvertor::doConvertFromLua(lua_State* L) {
	boost::shared_ptr<MAMFin> result = boost::make_shared<MAMFin>();
	lua_getfield(L, -1, "query_id");
	if (lua_isstring(L, -1)) {
		result->setQueryID(std::string(lua_tostring(L, -1)));
	}
	lua_pop(L, 1);
	lua_getfield(L, -1, "complete");
	if (lua_isboolean(L, -1)) {
		result->setComplete(lua_toboolean(L, -1) != 0);
	}
	lua_pop(L, 1);
	lua_getfield(L, -1, "stable");
	if (!lua_isnil(L, -1)) {
		result->setStable(lua_toboolean(L, -1) != 0);
	}
	lua_pop(L, 1);
	lua_getfield(L, -1, "result_set");
	if (!lua_isnil(L, -1)) {
		boost::shared_ptr<ResultSet> resultSet = boost::dynamic_pointer_cast<ResultSet>(convertors->convertFromLuaUntyped(L, -1, "result_set"));
		if (!!resultSet) {
			result->setResultSet(resultSet);
		}
	}
	lua_pop(L, 1);
	return result;
}

void MAMFinConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<MAMFin> payload) {
	lua_createtable(L, 0, 0);
	if (payload->getQueryID()) {
		lua_pushstring(L, (*payload->getQueryID()).c_str());
		lua_setfield(L, -2, "query_id");
	}
	lua_pushboolean(L, payload->isComplete());
	lua_setfield(L, -2, "complete");
	lua_pushboolean(L, payload->isStable());
	lua_setfield(L, -2, "stable");
	if (convertors->convertToLuaUntyped(L, payload->getResultSet()) > 0) {
		lua_setfield(L, -2, "result_set");
	}
}

boost::optional<LuaElementConvertor::Documentation> MAMFinConvertor::getDocumentation() const {
	return Documentation(
		"MAMFin",
		"This table has the following fields:\n\n"
		"- `query_id`: string (Optional)\n"
		"- `complete`: boolean (Optional)\n"
		"- `stable`: boolean (Optional)\n"
		"- `result_set`: @{ResultSet} (Optional)\n"
	);
}