summaryrefslogtreecommitdiffstats
blob: b59744ba6f5982d04bb56da367781a84586ab44a (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
/*
 * Copyright (c) 2014-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Sluift/ElementConvertors/DelayConvertor.h>

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

#include <lua.hpp>

#include <Swiften/Base/DateTime.h>

#include <Sluift/Lua/Check.h>

using namespace Swift;

DelayConvertor::DelayConvertor() : GenericLuaElementConvertor<Delay>("delay") {
}

DelayConvertor::~DelayConvertor() {
}

boost::shared_ptr<Delay> DelayConvertor::doConvertFromLua(lua_State* L) {
    boost::shared_ptr<Delay> result = boost::make_shared<Delay>();
    lua_getfield(L, -1, "stamp");
    if (lua_isstring(L, -1)) {
        result->setStamp(stringToDateTime(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);

    lua_getfield(L, -1, "from");
    if (lua_isstring(L, -1)) {
        result->setFrom(lua_tostring(L, -1));
    }
    lua_pop(L, 1);
    return result;
}

void DelayConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<Delay> payload) {
    lua_createtable(L, 0, 0);
    if (payload->getFrom()) {
        lua_pushstring(L, (*payload->getFrom()).toString().c_str());
        lua_setfield(L, -2, "from");
    }
    lua_pushstring(L, dateTimeToString(payload->getStamp()).c_str());
    lua_setfield(L, -2, "stamp");
}