/* * Copyright (c) 2013-2017 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace Swift; LuaElementConvertors::LuaElementConvertors() { registerConvertors(); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); } LuaElementConvertors::~LuaElementConvertors() { } #include std::shared_ptr 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"); } std::shared_ptr LuaElementConvertors::convertFromLuaUntyped(lua_State* L, int index, const std::string& type) { index = Lua::absoluteOffset(L, index); for (auto&& convertor : convertors) { if (std::shared_ptr result = convertor->convertFromLua(L, index, type)) { return result; } } return std::shared_ptr(); } int LuaElementConvertors::convertToLua(lua_State* L, std::shared_ptr payload) { if (boost::optional 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, std::shared_ptr payload) { if (doConvertToLuaUntyped(L, payload)) { return 1; } return 0; } boost::optional LuaElementConvertors::doConvertToLuaUntyped( lua_State* L, std::shared_ptr payload) { if (!payload) { return LuaElementConvertor::NO_RESULT; } for (auto&& convertor : convertors) { if (boost::optional type = convertor->convertToLua(L, payload)) { return *type; } } return LuaElementConvertor::NO_RESULT; }