diff options
Diffstat (limited to 'Sluift/ElementConvertors/StatusShowConvertor.cpp')
-rw-r--r-- | Sluift/ElementConvertors/StatusShowConvertor.cpp | 63 |
1 files changed, 39 insertions, 24 deletions
diff --git a/Sluift/ElementConvertors/StatusShowConvertor.cpp b/Sluift/ElementConvertors/StatusShowConvertor.cpp index c8a5bd6..d8e24ab 100644 --- a/Sluift/ElementConvertors/StatusShowConvertor.cpp +++ b/Sluift/ElementConvertors/StatusShowConvertor.cpp @@ -10,6 +10,7 @@ #include <boost/smart_ptr/make_shared.hpp> #include <boost/numeric/conversion/cast.hpp> #include <Sluift/Lua/Check.h> +#include <Sluift/Lua/Exception.h> using namespace Swift; @@ -23,21 +24,7 @@ boost::shared_ptr<StatusShow> StatusShowConvertor::doConvertFromLua(lua_State* L boost::shared_ptr<StatusShow> result = boost::make_shared<StatusShow>(); lua_getfield(L, -1, "type"); if (lua_isstring(L, -1)) { - if (std::string(lua_tostring(L, -1)) == "online") { - result->setType(StatusShow::Online); - } - if (std::string(lua_tostring(L, -1)) == "ffc") { - result->setType(StatusShow::FFC); - } - if (std::string(lua_tostring(L, -1)) == "away") { - result->setType(StatusShow::Away); - } - if (std::string(lua_tostring(L, -1)) == "xa") { - result->setType(StatusShow::XA); - } - if (std::string(lua_tostring(L, -1)) == "dnd") { - result->setType(StatusShow::DND); - } + result->setType(convertStatusShowTypeFromString(lua_tostring(L, -1))); } lua_pop(L, 1); return result; @@ -45,15 +32,43 @@ boost::shared_ptr<StatusShow> StatusShowConvertor::doConvertFromLua(lua_State* L void StatusShowConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<StatusShow> payload) { lua_createtable(L, 0, 0); - if (payload->getType() != StatusShow::None) { - switch (payload->getType()) { - case StatusShow::Online: lua_pushstring(L, "online"); break; - case StatusShow::FFC: lua_pushstring(L, "ffc"); break; - case StatusShow::Away: lua_pushstring(L, "away"); break; - case StatusShow::XA: lua_pushstring(L, "xa"); break; - case StatusShow::DND: lua_pushstring(L, "dnd"); break; - case StatusShow::None: assert(false); break; - } + const std::string show = convertStatusShowTypeToString(payload->getType()); + if (!show.empty()) { + lua_pushstring(L, show.c_str()); lua_setfield(L, -2, "type"); } } + +std::string StatusShowConvertor::convertStatusShowTypeToString(const StatusShow::Type &show) { + switch (show) { + case StatusShow::Online: return "online"; break; + case StatusShow::FFC: return "ffc"; break; + case StatusShow::Away: return "away"; break; + case StatusShow::XA: return "xa"; break; + case StatusShow::DND: return "dnd"; break; + case StatusShow::None: return ""; break; + } + assert(false); + return ""; +} + +StatusShow::Type StatusShowConvertor::convertStatusShowTypeFromString(const std::string& show) { + if (show == "online") { + return StatusShow::Online; + } + else if (show == "ffc") { + return StatusShow::FFC; + } + else if (show == "away") { + return StatusShow::Away; + } + else if (show == "xa") { + return StatusShow::XA; + } + else if (show == "dnd") { + return StatusShow::DND; + } + else { + throw Lua::Exception("Illegal status show: '" + show + "'"); + } +} |