diff options
Diffstat (limited to 'Sluift')
23 files changed, 43 insertions, 78 deletions
diff --git a/Sluift/ElementConvertors/CommandConvertor.cpp b/Sluift/ElementConvertors/CommandConvertor.cpp index 1e2f8e3..de7a439 100644 --- a/Sluift/ElementConvertors/CommandConvertor.cpp +++ b/Sluift/ElementConvertors/CommandConvertor.cpp @@ -1,46 +1,44 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/CommandConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/Lua/Check.h> #include <Sluift/Lua/Value.h> #include <Sluift/LuaElementConvertors.h> using namespace Swift; static Command::Action convertActionFromString(const std::string& action) { if (action == "cancel") { return Command::Cancel; } else if (action == "execute") { return Command::Execute; } else if (action == "complete") { return Command::Complete; } else if (action == "prev") { return Command::Prev; } else if (action == "next") { return Command::Next; } return Command::NoAction; } static std::string convertActionToString(Command::Action action) { switch (action) { case Command::Cancel: return "cancel"; case Command::Execute: return "execute"; case Command::Complete: return "complete"; case Command::Prev: return "prev"; case Command::Next: return "next"; case Command::NoAction: assert(false); return ""; } assert(false); return ""; } CommandConvertor::CommandConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<Command>("command"), @@ -127,71 +125,71 @@ std::shared_ptr<Command> CommandConvertor::doConvertFromLua(lua_State* L) { lua_pop(L, 1); lua_getfield(L, -1, "form"); if (!lua_isnil(L, -1)) { if (std::shared_ptr<Form> form = std::dynamic_pointer_cast<Form>(convertors->convertFromLuaUntyped(L, -1, "form"))) { result->setForm(form); } } lua_pop(L, 1); return result; } void CommandConvertor::doConvertToLua(lua_State* L, std::shared_ptr<Command> payload) { Lua::Table result; if (!payload->getNode().empty()) { result["node"] = Lua::valueRef(payload->getNode()); } if (!payload->getSessionID().empty()) { result["session_id"] = Lua::valueRef(payload->getSessionID()); } switch (payload->getStatus()) { case Command::Executing: result["status"] = Lua::valueRef("executing"); break; case Command::Completed: result["status"] = Lua::valueRef("completed"); break; case Command::Canceled: result["status"] = Lua::valueRef("canceled"); break; case Command::NoStatus: break; } if (!payload->getNotes().empty()) { std::vector<Lua::Value> notes; - foreach (const Command::Note& note, payload->getNotes()) { + for (const auto& note : payload->getNotes()) { Lua::Table noteTable; if (!note.note.empty()) { noteTable["note"] = Lua::valueRef(note.note); } switch (note.type) { case Command::Note::Info: noteTable["type"] = Lua::valueRef("info"); break; case Command::Note::Warn: noteTable["type"] = Lua::valueRef("warn"); break; case Command::Note::Error: noteTable["type"] = Lua::valueRef("error"); break; } notes.push_back(noteTable); } result["notes"] = Lua::valueRef(notes); } if (payload->getAction() != Command::NoAction) { result["action"] = Lua::valueRef(convertActionToString(payload->getAction())); } if (payload->getExecuteAction() != Command::NoAction) { result["execute_action"] = Lua::valueRef(convertActionToString(payload->getAction())); } if (!payload->getAvailableActions().empty()) { std::vector<Lua::Value> availableActions; - foreach (const Command::Action& action, payload->getAvailableActions()) { + for (const auto& action : payload->getAvailableActions()) { if (action != Command::NoAction) { availableActions.push_back(convertActionToString(action)); } } result["available_actions"] = Lua::valueRef(availableActions); } Lua::pushValue(L, result); if (payload->getForm()) { bool result = convertors->convertToLuaUntyped(L, payload->getForm()); assert(result); lua_setfield(L, -2, "form"); } } diff --git a/Sluift/ElementConvertors/DOMElementConvertor.cpp b/Sluift/ElementConvertors/DOMElementConvertor.cpp index c03eb8c..b957686 100644 --- a/Sluift/ElementConvertors/DOMElementConvertor.cpp +++ b/Sluift/ElementConvertors/DOMElementConvertor.cpp @@ -1,81 +1,80 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/DOMElementConvertor.h> #include <memory> #include <lua.hpp> -#include <Swiften/Base/foreach.h> #include <Swiften/Elements/RawXMLPayload.h> #include <Swiften/Parser/Attribute.h> #include <Swiften/Parser/AttributeMap.h> #include <Swiften/Parser/XMLParser.h> #include <Swiften/Parser/XMLParserClient.h> #include <Swiften/Serializer/PayloadSerializer.h> #include <Swiften/Serializer/XML/XMLElement.h> #include <Swiften/Serializer/XML/XMLRawTextNode.h> #include <Swiften/Serializer/XML/XMLTextNode.h> #include <Sluift/Lua/Check.h> #include <Sluift/Lua/Debug.h> #include <Sluift/Lua/LuaUtils.h> using namespace Swift; namespace { class ParserClient : public XMLParserClient { public: ParserClient(lua_State* L) : L(L), currentIndex(1) { } virtual void handleStartElement( const std::string& element, const std::string& ns, const AttributeMap& attributes) SWIFTEN_OVERRIDE { lua_checkstack(L, 6); lua_pushnumber(L, currentIndex); lua_newtable(L); lua_pushstring(L, element.c_str()); lua_setfield(L, -2, "tag"); if (!ns.empty()) { lua_pushstring(L, ns.c_str()); lua_setfield(L, -2, "ns"); } if (!attributes.getEntries().empty()) { lua_newtable(L); int i = 1; - foreach(const AttributeMap::Entry& entry, attributes.getEntries()) { + for (const auto& entry : attributes.getEntries()) { lua_pushnumber(L, i); lua_newtable(L); lua_pushstring(L, entry.getAttribute().getName().c_str()); lua_setfield(L, -2, "name"); if (!entry.getAttribute().getNamespace().empty()) { lua_pushstring(L, entry.getAttribute().getNamespace().c_str()); lua_setfield(L, -2, "ns"); } lua_pushstring(L, entry.getValue().c_str()); lua_setfield(L, -2, "value"); lua_settable(L, -3); ++i; } lua_setfield(L, -2, "attributes"); } indexStack.push_back(currentIndex); currentIndex = 1; lua_newtable(L); } virtual void handleEndElement( const std::string&, const std::string&) SWIFTEN_OVERRIDE { lua_setfield(L, -2, "children"); lua_settable(L, -3); currentIndex = indexStack.back(); indexStack.pop_back(); currentIndex++; } diff --git a/Sluift/ElementConvertors/FormConvertor.cpp b/Sluift/ElementConvertors/FormConvertor.cpp index 85f40a1..90fd9fe 100644 --- a/Sluift/ElementConvertors/FormConvertor.cpp +++ b/Sluift/ElementConvertors/FormConvertor.cpp @@ -1,48 +1,46 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/FormConvertor.h> #include <memory> #include <sstream> #include <boost/assign/list_of.hpp> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/Lua/Check.h> #include <Sluift/Lua/Value.h> using namespace Swift; namespace { int formIndex(lua_State* L) { lua_getfield(L, 1, "fields"); if (lua_type(L, -1) != LUA_TTABLE) { return 0; } int index = Lua::absoluteOffset(L, -1); lua_pushnil(L); for (lua_pushnil(L); lua_next(L, index) != 0; ) { lua_getfield(L, -1, "name"); if (lua_equal(L, -1, 2)) { lua_pop(L, 1); return 1; } lua_pop(L, 2); } return 0; } int formNewIndex(lua_State* L) { lua_getfield(L, 1, "fields"); bool foundField = false; if (lua_type(L, -1) == LUA_TTABLE) { for (lua_pushnil(L); lua_next(L, -2) != 0; ) { lua_getfield(L, -1, "name"); @@ -78,74 +76,74 @@ namespace { case FormField::TextMultiType: type = "text-multi"; break; case FormField::TextPrivateType: type = "text-private"; break; case FormField::TextSingleType: type = "text-single"; break; case FormField::JIDSingleType: type = "jid-single"; break; case FormField::JIDMultiType: type = "jid-multi"; break; case FormField::ListMultiType: type = "list-multi"; break; } if (!type.empty()) { luaField["type"] = Lua::valueRef(type); } if (!field->getLabel().empty()) { luaField["label"] = Lua::valueRef(field->getLabel()); } if (field->getRequired()) { luaField["required"] = Lua::boolRef(field->getRequired()); } if (!field->getDescription().empty()) { luaField["description"] = Lua::valueRef(field->getDescription()); } if (field->getType() == FormField::BooleanType) { luaField["value"] = Lua::boolRef(field->getBoolValue()); } else if (field->getValues().size() > 1) { luaField["value"] = Lua::valueRef(Lua::Array(field->getValues().begin(), field->getValues().end())); } else if (field->getValues().size() == 1) { luaField["value"] = Lua::valueRef(field->getValues()[0]); } if (!field->getOptions().empty()) { Lua::Array options; - foreach(const FormField::Option& option, field->getOptions()) { + for (const auto& option : field->getOptions()) { Lua::Table luaOption = boost::assign::map_list_of ("label", Lua::valueRef(option.label)) ("value", Lua::valueRef(option.value)); options.push_back(luaOption); } luaField["options"] = valueRef(options); } return luaField; } Lua::Array convertFieldListToLua(const std::vector< std::shared_ptr<FormField> >& fieldList) { Lua::Array fields; - foreach(std::shared_ptr<FormField> field, fieldList) { + for (auto&& field : fieldList) { fields.push_back(convertFieldToLua(field)); } return fields; } std::shared_ptr<FormField> convertFieldFromLua(lua_State* L) { std::shared_ptr<FormField> result = std::make_shared<FormField>(); FormField::Type fieldType = FormField::UnknownType; boost::optional<std::string> type = Lua::getStringField(L, -1, "type"); if (type) { if (*type == "boolean") { fieldType = FormField::BooleanType; } if (*type == "fixed") { fieldType = FormField::FixedType; } if (*type == "hidden") { fieldType = FormField::HiddenType; } if (*type == "list-single") { fieldType = FormField::ListSingleType; } if (*type == "text-multi") { fieldType = FormField::TextMultiType; } if (*type == "text-private") { fieldType = FormField::TextPrivateType; } if (*type == "text-single") { @@ -218,138 +216,138 @@ namespace { result.push_back(convertFieldFromLua(L)); lua_pop(L, 1); } return result; } std::shared_ptr<Form> convertFormFromLua(lua_State* L) { std::shared_ptr<Form> result = std::make_shared<Form>(); if (boost::optional<std::string> title = Lua::getStringField(L, -1, "title")) { result->setTitle(*title); } if (boost::optional<std::string> instructions = Lua::getStringField(L, -1, "instructions")) { result->setInstructions(*instructions); } if (boost::optional<std::string> type = Lua::getStringField(L, -1, "type")) { Form::Type formType = Form::FormType; if (*type == "submit") { formType = Form::SubmitType; } else if (*type == "cancel") { formType = Form::CancelType; } else if (*type == "result") { formType = Form::ResultType; } result->setType(formType); } lua_getfield(L, -1, "fields"); if (lua_istable(L, -1)) { - foreach (std::shared_ptr<FormField> formField, convertFieldListFromLua(L)) { + for (auto&& formField : convertFieldListFromLua(L)) { result->addField(formField); } } lua_pop(L, 1); lua_getfield(L, -1, "reported_fields"); if (lua_istable(L, -1)) { - foreach (std::shared_ptr<FormField> formField, convertFieldListFromLua(L)) { + for (auto&& formField : convertFieldListFromLua(L)) { result->addReportedField(formField); } } lua_pop(L, 1); lua_getfield(L, -1, "items"); if (lua_istable(L, -1)) { for (lua_pushnil(L); lua_next(L, -2);) { result->addItem(convertFieldListFromLua(L)); lua_pop(L, 1); } } lua_pop(L, 1); return result; } void convertFormToLua(lua_State* L, std::shared_ptr<Form> payload) { std::string type; switch (payload->getType()) { case Form::FormType: type = "form"; break; case Form::SubmitType: type = "submit"; break; case Form::CancelType: type = "cancel"; break; case Form::ResultType: type = "result"; break; } Lua::Table result = boost::assign::map_list_of("type", Lua::valueRef(type)); if (!payload->getTitle().empty()) { result["title"] = Lua::valueRef(payload->getTitle()); } if (!payload->getInstructions().empty()) { result["instructions"] = Lua::valueRef(payload->getInstructions()); } result["fields"] = valueRef(convertFieldListToLua(payload->getFields())); if (!payload->getReportedFields().empty()) { result["reported_fields"] = valueRef(convertFieldListToLua(payload->getReportedFields())); } if (!payload->getItems().empty()) { Lua::Array luaItems; - foreach(const Form::FormItem& item, payload->getItems()) { + for (const auto& item : payload->getItems()) { if (!item.empty()) { luaItems.push_back(convertFieldListToLua(item)); } } result["items"] = valueRef(luaItems); } Lua::pushValue(L, result); lua_newtable(L); lua_pushcfunction(L, formIndex); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, formNewIndex); lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); } int createSubmission(lua_State* L) { std::shared_ptr<Form> form = convertFormFromLua(L); // Remove all redundant elements form->setInstructions(""); form->setTitle(""); form->clearItems(); form->clearReportedFields(); std::vector< std::shared_ptr<FormField> > fields(form->getFields()); form->clearFields(); - foreach (std::shared_ptr<FormField> field, fields) { + for (auto&& field : fields) { if (field->getType() == FormField::FixedType) { continue; } field->clearOptions(); field->setLabel(""); field->setType(FormField::UnknownType); field->setDescription(""); form->addField(field); } form->setType(Form::SubmitType); // Convert back convertFormToLua(L, form); Lua::registerTableToString(L, -1); return 1; } } FormConvertor::FormConvertor() : GenericLuaElementConvertor<Form>("form") { } FormConvertor::~FormConvertor() { } std::shared_ptr<Form> FormConvertor::doConvertFromLua(lua_State* L) { return convertFormFromLua(L); } void FormConvertor::doConvertToLua(lua_State* L, std::shared_ptr<Form> payload) { diff --git a/Sluift/ElementConvertors/ForwardedConvertor.cpp b/Sluift/ElementConvertors/ForwardedConvertor.cpp index 8474252..b353eea 100644 --- a/Sluift/ElementConvertors/ForwardedConvertor.cpp +++ b/Sluift/ElementConvertors/ForwardedConvertor.cpp @@ -1,45 +1,44 @@ /* * Copyright (c) 2014-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/ForwardedConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> #include <Swiften/Elements/Delay.h> #include <Swiften/Elements/IQ.h> #include <Swiften/Elements/Message.h> #include <Swiften/Elements/Presence.h> #include <Sluift/LuaElementConvertors.h> using namespace Swift; ForwardedConvertor::ForwardedConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<Forwarded>("forwarded"), convertors(convertors) { } ForwardedConvertor::~ForwardedConvertor() { } std::shared_ptr<Forwarded> ForwardedConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<Forwarded> result = std::make_shared<Forwarded>(); lua_getfield(L, -1, "delay"); if (!lua_isnil(L, -1)) { std::shared_ptr<Delay> delay = std::dynamic_pointer_cast<Delay>(convertors->convertFromLuaUntyped(L, -1, "delay")); if (!!delay) { result->setDelay(delay); } } lua_pop(L, 1); lua_getfield(L, -1, "stanza"); if (!lua_isnil(L, -1)) { std::shared_ptr<Stanza> stanza = std::dynamic_pointer_cast<Stanza>(convertors->convertFromLua(L, -1)); diff --git a/Sluift/ElementConvertors/PubSubAffiliationsConvertor.cpp b/Sluift/ElementConvertors/PubSubAffiliationsConvertor.cpp index 8eae795..c6ba09e 100644 --- a/Sluift/ElementConvertors/PubSubAffiliationsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubAffiliationsConvertor.cpp @@ -1,80 +1,78 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubAffiliationsConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubAffiliationsConvertor::PubSubAffiliationsConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubAffiliations>("pubsub_affiliations"), convertors(convertors) { } PubSubAffiliationsConvertor::~PubSubAffiliationsConvertor() { } std::shared_ptr<PubSubAffiliations> PubSubAffiliationsConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubAffiliations> result = std::make_shared<PubSubAffiliations>(); lua_getfield(L, -1, "node"); if (lua_isstring(L, -1)) { result->setNode(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<PubSubAffiliation> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<PubSubAffiliation> payload = std::dynamic_pointer_cast<PubSubAffiliation>(convertors->convertFromLuaUntyped(L, -1, "pubsub_affiliation"))) { items.push_back(payload); } } lua_pop(L, 1); } result->setAffiliations(items); } return result; } void PubSubAffiliationsConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubAffiliations> payload) { lua_createtable(L, 0, 0); if (payload->getNode()) { lua_pushstring(L, (*payload->getNode()).c_str()); lua_setfield(L, -2, "node"); } if (!payload->getAffiliations().empty()) { { int i = 0; - foreach(std::shared_ptr<PubSubAffiliation> item, payload->getAffiliations()) { + for (auto&& item : payload->getAffiliations()) { if (convertors->convertToLuaUntyped(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } } } boost::optional<LuaElementConvertor::Documentation> PubSubAffiliationsConvertor::getDocumentation() const { return Documentation( "PubSubAffiliations", "This table has the following fields:\n\n" "- `node`: string (Optional)\n" "- `affiliations`: array<@{PubSubAffiliation}>\n" ); } diff --git a/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp b/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp index e8ba5b5..ac86024 100644 --- a/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubEventItemConvertor.cpp @@ -1,46 +1,44 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubEventItemConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubEventItemConvertor::PubSubEventItemConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubEventItem>("pubsub_event_item"), convertors(convertors) { } PubSubEventItemConvertor::~PubSubEventItemConvertor() { } std::shared_ptr<PubSubEventItem> PubSubEventItemConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubEventItem> result = std::make_shared<PubSubEventItem>(); lua_getfield(L, -1, "node"); if (lua_isstring(L, -1)) { result->setNode(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); lua_getfield(L, -1, "publisher"); if (lua_isstring(L, -1)) { result->setPublisher(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); lua_getfield(L, -1, "data"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<Payload> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); @@ -50,55 +48,55 @@ std::shared_ptr<PubSubEventItem> PubSubEventItemConvertor::doConvertFromLua(lua_ } } lua_pop(L, 1); } result->setData(items); } 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); return result; } void PubSubEventItemConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubEventItem> payload) { lua_createtable(L, 0, 0); if (payload->getNode()) { lua_pushstring(L, (*payload->getNode()).c_str()); lua_setfield(L, -2, "node"); } if (payload->getPublisher()) { lua_pushstring(L, (*payload->getPublisher()).c_str()); lua_setfield(L, -2, "publisher"); } if (!payload->getData().empty()) { lua_createtable(L, boost::numeric_cast<int>(payload->getData().size()), 0); { int i = 0; - foreach(std::shared_ptr<Payload> item, payload->getData()) { + for (auto&& item : payload->getData()) { if (convertors->convertToLua(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } lua_setfield(L, -2, "data"); } if (payload->getID()) { lua_pushstring(L, (*payload->getID()).c_str()); lua_setfield(L, -2, "id"); } } boost::optional<LuaElementConvertor::Documentation> PubSubEventItemConvertor::getDocumentation() const { return Documentation( "PubSubEventItem", "This table has the following fields:\n\n" "- `node`: string (Optional)\n" "- `publisher`: string (Optional)\n" "- `data`: array<element (table)>\n" "- `id`: string (Optional)\n" ); } diff --git a/Sluift/ElementConvertors/PubSubEventItemsConvertor.cpp b/Sluift/ElementConvertors/PubSubEventItemsConvertor.cpp index c89c4a6..7a3cde1 100644 --- a/Sluift/ElementConvertors/PubSubEventItemsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubEventItemsConvertor.cpp @@ -1,46 +1,44 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubEventItemsConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubEventItemsConvertor::PubSubEventItemsConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubEventItems>("pubsub_event_items"), convertors(convertors) { } PubSubEventItemsConvertor::~PubSubEventItemsConvertor() { } std::shared_ptr<PubSubEventItems> PubSubEventItemsConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubEventItems> result = std::make_shared<PubSubEventItems>(); lua_getfield(L, -1, "node"); if (lua_isstring(L, -1)) { result->setNode(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); lua_getfield(L, -1, "items"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<PubSubEventItem> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<PubSubEventItem> payload = std::dynamic_pointer_cast<PubSubEventItem>(convertors->convertFromLuaUntyped(L, -1, "pubsub_event_item"))) { items.push_back(payload); } } @@ -51,63 +49,63 @@ std::shared_ptr<PubSubEventItems> PubSubEventItemsConvertor::doConvertFromLua(lu } lua_pop(L, 1); lua_getfield(L, -1, "retracts"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<PubSubEventRetract> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<PubSubEventRetract> payload = std::dynamic_pointer_cast<PubSubEventRetract>(convertors->convertFromLuaUntyped(L, -1, "pubsub_event_retract"))) { items.push_back(payload); } } lua_pop(L, 1); } result->setRetracts(items); } lua_pop(L, 1); return result; } void PubSubEventItemsConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubEventItems> payload) { lua_createtable(L, 0, 0); lua_pushstring(L, payload->getNode().c_str()); lua_setfield(L, -2, "node"); if (!payload->getItems().empty()) { lua_createtable(L, boost::numeric_cast<int>(payload->getItems().size()), 0); { int i = 0; - foreach(std::shared_ptr<PubSubEventItem> item, payload->getItems()) { + for (auto&& item : payload->getItems()) { if (convertors->convertToLuaUntyped(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } lua_setfield(L, -2, "items"); } if (!payload->getRetracts().empty()) { lua_createtable(L, boost::numeric_cast<int>(payload->getRetracts().size()), 0); { int i = 0; - foreach(std::shared_ptr<PubSubEventRetract> item, payload->getRetracts()) { + for (auto&& item : payload->getRetracts()) { if (convertors->convertToLuaUntyped(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } lua_setfield(L, -2, "retracts"); } } boost::optional<LuaElementConvertor::Documentation> PubSubEventItemsConvertor::getDocumentation() const { return Documentation( "PubSubEventItems", "This table has the following fields:\n\n" "- `node`: string\n" "- `items`: array<@{PubSubEventItem}>\n" "- `retracts`: array<@{PubSubEventRetract}>\n" ); } diff --git a/Sluift/ElementConvertors/PubSubItemConvertor.cpp b/Sluift/ElementConvertors/PubSubItemConvertor.cpp index 99802bf..27fd4a3 100644 --- a/Sluift/ElementConvertors/PubSubItemConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubItemConvertor.cpp @@ -1,82 +1,80 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubItemConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubItemConvertor::PubSubItemConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubItem>("pubsub_item"), convertors(convertors) { } PubSubItemConvertor::~PubSubItemConvertor() { } std::shared_ptr<PubSubItem> PubSubItemConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubItem> result = std::make_shared<PubSubItem>(); lua_getfield(L, -1, "data"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<Payload> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<Payload> payload = std::dynamic_pointer_cast<Payload>(convertors->convertFromLua(L, -1))) { items.push_back(payload); } } lua_pop(L, 1); } result->setData(items); } 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); return result; } void PubSubItemConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubItem> payload) { lua_createtable(L, 0, 0); if (!payload->getData().empty()) { lua_createtable(L, boost::numeric_cast<int>(payload->getData().size()), 0); { int i = 0; - foreach(std::shared_ptr<Payload> item, payload->getData()) { + for (auto&& item : payload->getData()) { if (convertors->convertToLua(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } lua_setfield(L, -2, "data"); } lua_pushstring(L, payload->getID().c_str()); lua_setfield(L, -2, "id"); } boost::optional<LuaElementConvertor::Documentation> PubSubItemConvertor::getDocumentation() const { return Documentation( "PubSubItem", "This table has the following fields:\n\n" "- `data`: array<element (table)>\n" "- `id`: string\n" ); } diff --git a/Sluift/ElementConvertors/PubSubItemsConvertor.cpp b/Sluift/ElementConvertors/PubSubItemsConvertor.cpp index 8e1f08d..5fa1bd3 100644 --- a/Sluift/ElementConvertors/PubSubItemsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubItemsConvertor.cpp @@ -1,98 +1,96 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubItemsConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubItemsConvertor::PubSubItemsConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubItems>("pubsub_items"), convertors(convertors) { } PubSubItemsConvertor::~PubSubItemsConvertor() { } std::shared_ptr<PubSubItems> PubSubItemsConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubItems> result = std::make_shared<PubSubItems>(); lua_getfield(L, -1, "node"); if (lua_isstring(L, -1)) { result->setNode(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<PubSubItem> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<PubSubItem> payload = std::dynamic_pointer_cast<PubSubItem>(convertors->convertFromLuaUntyped(L, -1, "pubsub_item"))) { items.push_back(payload); } } lua_pop(L, 1); } result->setItems(items); } lua_getfield(L, -1, "maximum_items"); if (lua_isnumber(L, -1)) { result->setMaximumItems(boost::numeric_cast<unsigned int>(lua_tonumber(L, -1))); } lua_pop(L, 1); lua_getfield(L, -1, "subscription_id"); if (lua_isstring(L, -1)) { result->setSubscriptionID(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); return result; } void PubSubItemsConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubItems> payload) { lua_createtable(L, 0, 0); lua_pushstring(L, payload->getNode().c_str()); lua_setfield(L, -2, "node"); if (!payload->getItems().empty()) { { int i = 0; - foreach(std::shared_ptr<PubSubItem> item, payload->getItems()) { + for (auto&& item : payload->getItems()) { if (convertors->convertToLuaUntyped(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } } if (payload->getMaximumItems()) { lua_pushnumber(L, (*payload->getMaximumItems())); lua_setfield(L, -2, "maximum_items"); } if (payload->getSubscriptionID()) { lua_pushstring(L, (*payload->getSubscriptionID()).c_str()); lua_setfield(L, -2, "subscription_id"); } } boost::optional<LuaElementConvertor::Documentation> PubSubItemsConvertor::getDocumentation() const { return Documentation( "PubSubItems", "This table has the following fields:\n\n" "- `node`: string\n" "- `items`: array<@{PubSubItem}>\n" "- `maximum_items`: number (Optional)\n" "- `subscription_id`: string (Optional)\n" ); } diff --git a/Sluift/ElementConvertors/PubSubOwnerAffiliationsConvertor.cpp b/Sluift/ElementConvertors/PubSubOwnerAffiliationsConvertor.cpp index b66443f..ee8a8cb 100644 --- a/Sluift/ElementConvertors/PubSubOwnerAffiliationsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubOwnerAffiliationsConvertor.cpp @@ -1,78 +1,76 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubOwnerAffiliationsConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubOwnerAffiliationsConvertor::PubSubOwnerAffiliationsConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubOwnerAffiliations>("pubsub_owner_affiliations"), convertors(convertors) { } PubSubOwnerAffiliationsConvertor::~PubSubOwnerAffiliationsConvertor() { } std::shared_ptr<PubSubOwnerAffiliations> PubSubOwnerAffiliationsConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubOwnerAffiliations> result = std::make_shared<PubSubOwnerAffiliations>(); lua_getfield(L, -1, "node"); if (lua_isstring(L, -1)) { result->setNode(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<PubSubOwnerAffiliation> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<PubSubOwnerAffiliation> payload = std::dynamic_pointer_cast<PubSubOwnerAffiliation>(convertors->convertFromLuaUntyped(L, -1, "pubsub_owner_affiliation"))) { items.push_back(payload); } } lua_pop(L, 1); } result->setAffiliations(items); } return result; } void PubSubOwnerAffiliationsConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubOwnerAffiliations> payload) { lua_createtable(L, 0, 0); lua_pushstring(L, payload->getNode().c_str()); lua_setfield(L, -2, "node"); if (!payload->getAffiliations().empty()) { { int i = 0; - foreach(std::shared_ptr<PubSubOwnerAffiliation> item, payload->getAffiliations()) { + for (auto&& item : payload->getAffiliations()) { if (convertors->convertToLuaUntyped(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } } } boost::optional<LuaElementConvertor::Documentation> PubSubOwnerAffiliationsConvertor::getDocumentation() const { return Documentation( "PubSubOwnerAffiliations", "This table has the following fields:\n\n" "- `node`: string\n" "- `affiliations`: array<@{PubSubOwnerAffiliation}>\n" ); } diff --git a/Sluift/ElementConvertors/PubSubOwnerSubscriptionsConvertor.cpp b/Sluift/ElementConvertors/PubSubOwnerSubscriptionsConvertor.cpp index 50cfb9b..88085b5 100644 --- a/Sluift/ElementConvertors/PubSubOwnerSubscriptionsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubOwnerSubscriptionsConvertor.cpp @@ -1,78 +1,76 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubOwnerSubscriptionsConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubOwnerSubscriptionsConvertor::PubSubOwnerSubscriptionsConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubOwnerSubscriptions>("pubsub_owner_subscriptions"), convertors(convertors) { } PubSubOwnerSubscriptionsConvertor::~PubSubOwnerSubscriptionsConvertor() { } std::shared_ptr<PubSubOwnerSubscriptions> PubSubOwnerSubscriptionsConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubOwnerSubscriptions> result = std::make_shared<PubSubOwnerSubscriptions>(); lua_getfield(L, -1, "node"); if (lua_isstring(L, -1)) { result->setNode(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<PubSubOwnerSubscription> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<PubSubOwnerSubscription> payload = std::dynamic_pointer_cast<PubSubOwnerSubscription>(convertors->convertFromLuaUntyped(L, -1, "pubsub_owner_subscription"))) { items.push_back(payload); } } lua_pop(L, 1); } result->setSubscriptions(items); } return result; } void PubSubOwnerSubscriptionsConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubOwnerSubscriptions> payload) { lua_createtable(L, 0, 0); lua_pushstring(L, payload->getNode().c_str()); lua_setfield(L, -2, "node"); if (!payload->getSubscriptions().empty()) { { int i = 0; - foreach(std::shared_ptr<PubSubOwnerSubscription> item, payload->getSubscriptions()) { + for (auto&& item : payload->getSubscriptions()) { if (convertors->convertToLuaUntyped(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } } } boost::optional<LuaElementConvertor::Documentation> PubSubOwnerSubscriptionsConvertor::getDocumentation() const { return Documentation( "PubSubOwnerSubscriptions", "This table has the following fields:\n\n" "- `node`: string\n" "- `subscriptions`: array<@{PubSubOwnerSubscription}>\n" ); } diff --git a/Sluift/ElementConvertors/PubSubPublishConvertor.cpp b/Sluift/ElementConvertors/PubSubPublishConvertor.cpp index 38aca0a..63c97bc 100644 --- a/Sluift/ElementConvertors/PubSubPublishConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubPublishConvertor.cpp @@ -1,82 +1,80 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubPublishConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubPublishConvertor::PubSubPublishConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubPublish>("pubsub_publish"), convertors(convertors) { } PubSubPublishConvertor::~PubSubPublishConvertor() { } std::shared_ptr<PubSubPublish> PubSubPublishConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubPublish> result = std::make_shared<PubSubPublish>(); lua_getfield(L, -1, "node"); if (lua_isstring(L, -1)) { result->setNode(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); lua_getfield(L, -1, "items"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<PubSubItem> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<PubSubItem> payload = std::dynamic_pointer_cast<PubSubItem>(convertors->convertFromLuaUntyped(L, -1, "pubsub_item"))) { items.push_back(payload); } } lua_pop(L, 1); } result->setItems(items); } lua_pop(L, 1); return result; } void PubSubPublishConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubPublish> payload) { lua_createtable(L, 0, 0); lua_pushstring(L, payload->getNode().c_str()); lua_setfield(L, -2, "node"); if (!payload->getItems().empty()) { lua_createtable(L, boost::numeric_cast<int>(payload->getItems().size()), 0); { int i = 0; - foreach(std::shared_ptr<PubSubItem> item, payload->getItems()) { + for (auto&& item : payload->getItems()) { if (convertors->convertToLuaUntyped(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } lua_setfield(L, -2, "items"); } } boost::optional<LuaElementConvertor::Documentation> PubSubPublishConvertor::getDocumentation() const { return Documentation( "PubSubPublish", "This table has the following fields:\n\n" "- `node`: string\n" "- `items`: array<@{PubSubItem}>\n" ); } diff --git a/Sluift/ElementConvertors/PubSubRetractConvertor.cpp b/Sluift/ElementConvertors/PubSubRetractConvertor.cpp index 38e15a1..c070ad6 100644 --- a/Sluift/ElementConvertors/PubSubRetractConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubRetractConvertor.cpp @@ -1,92 +1,90 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubRetractConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubRetractConvertor::PubSubRetractConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubRetract>("pubsub_retract"), convertors(convertors) { } PubSubRetractConvertor::~PubSubRetractConvertor() { } std::shared_ptr<PubSubRetract> PubSubRetractConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubRetract> result = std::make_shared<PubSubRetract>(); lua_getfield(L, -1, "node"); if (lua_isstring(L, -1)) { result->setNode(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); lua_getfield(L, -1, "items"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<PubSubItem> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<PubSubItem> payload = std::dynamic_pointer_cast<PubSubItem>(convertors->convertFromLuaUntyped(L, -1, "pubsub_item"))) { items.push_back(payload); } } lua_pop(L, 1); } result->setItems(items); } lua_pop(L, 1); lua_getfield(L, -1, "notify"); if (lua_isboolean(L, -1)) { result->setNotify(lua_toboolean(L, -1)); } lua_pop(L, 1); return result; } void PubSubRetractConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubRetract> payload) { lua_createtable(L, 0, 0); lua_pushstring(L, payload->getNode().c_str()); lua_setfield(L, -2, "node"); if (!payload->getItems().empty()) { lua_createtable(L, boost::numeric_cast<int>(payload->getItems().size()), 0); { int i = 0; - foreach(std::shared_ptr<PubSubItem> item, payload->getItems()) { + for (auto&& item : payload->getItems()) { if (convertors->convertToLuaUntyped(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } lua_setfield(L, -2, "items"); } if (payload->isNotify().is_initialized()) { lua_pushboolean(L, payload->isNotify().get_value_or(false)); lua_setfield(L, -2, "notify"); } } boost::optional<LuaElementConvertor::Documentation> PubSubRetractConvertor::getDocumentation() const { return Documentation( "PubSubRetract", "This table has the following fields:\n\n" "- `node`: string\n" "- `items`: array<@{PubSubItem}>\n" "- `notify`: boolean\n" ); } diff --git a/Sluift/ElementConvertors/PubSubSubscriptionsConvertor.cpp b/Sluift/ElementConvertors/PubSubSubscriptionsConvertor.cpp index 4cc5686..3712192 100644 --- a/Sluift/ElementConvertors/PubSubSubscriptionsConvertor.cpp +++ b/Sluift/ElementConvertors/PubSubSubscriptionsConvertor.cpp @@ -1,80 +1,78 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/PubSubSubscriptionsConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - #include <Sluift/LuaElementConvertors.h> using namespace Swift; PubSubSubscriptionsConvertor::PubSubSubscriptionsConvertor(LuaElementConvertors* convertors) : GenericLuaElementConvertor<PubSubSubscriptions>("pubsub_subscriptions"), convertors(convertors) { } PubSubSubscriptionsConvertor::~PubSubSubscriptionsConvertor() { } std::shared_ptr<PubSubSubscriptions> PubSubSubscriptionsConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<PubSubSubscriptions> result = std::make_shared<PubSubSubscriptions>(); lua_getfield(L, -1, "node"); if (lua_isstring(L, -1)) { result->setNode(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::shared_ptr<PubSubSubscription> > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { if (std::shared_ptr<PubSubSubscription> payload = std::dynamic_pointer_cast<PubSubSubscription>(convertors->convertFromLuaUntyped(L, -1, "pubsub_subscription"))) { items.push_back(payload); } } lua_pop(L, 1); } result->setSubscriptions(items); } return result; } void PubSubSubscriptionsConvertor::doConvertToLua(lua_State* L, std::shared_ptr<PubSubSubscriptions> payload) { lua_createtable(L, 0, 0); if (payload->getNode()) { lua_pushstring(L, (*payload->getNode()).c_str()); lua_setfield(L, -2, "node"); } if (!payload->getSubscriptions().empty()) { { int i = 0; - foreach(std::shared_ptr<PubSubSubscription> item, payload->getSubscriptions()) { + for (auto&& item : payload->getSubscriptions()) { if (convertors->convertToLuaUntyped(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } } } boost::optional<LuaElementConvertor::Documentation> PubSubSubscriptionsConvertor::getDocumentation() const { return Documentation( "PubSubSubscriptions", "This table has the following fields:\n\n" "- `node`: string (Optional)\n" "- `subscriptions`: array<@{PubSubSubscription}>\n" ); } diff --git a/Sluift/ElementConvertors/SecurityLabelConvertor.cpp b/Sluift/ElementConvertors/SecurityLabelConvertor.cpp index 133b123..21d9a8f 100644 --- a/Sluift/ElementConvertors/SecurityLabelConvertor.cpp +++ b/Sluift/ElementConvertors/SecurityLabelConvertor.cpp @@ -1,100 +1,98 @@ /* * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/ElementConvertors/SecurityLabelConvertor.h> #include <memory> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> -#include <Swiften/Base/foreach.h> - using namespace Swift; SecurityLabelConvertor::SecurityLabelConvertor() : GenericLuaElementConvertor<SecurityLabel>("security_label") { } SecurityLabelConvertor::~SecurityLabelConvertor() { } std::shared_ptr<SecurityLabel> SecurityLabelConvertor::doConvertFromLua(lua_State* L) { std::shared_ptr<SecurityLabel> result = std::make_shared<SecurityLabel>(); lua_getfield(L, -1, "equivalent_labels"); if (lua_type(L, -1) == LUA_TTABLE) { std::vector< std::string > items; for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (lua_isstring(L, -1)) { items.push_back(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); } result->setEquivalentLabels(items); } lua_pop(L, 1); lua_getfield(L, -1, "foreground_color"); if (lua_isstring(L, -1)) { result->setForegroundColor(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); lua_getfield(L, -1, "display_marking"); if (lua_isstring(L, -1)) { result->setDisplayMarking(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); lua_getfield(L, -1, "background_color"); if (lua_isstring(L, -1)) { result->setBackgroundColor(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); lua_getfield(L, -1, "label"); if (lua_isstring(L, -1)) { result->setLabel(std::string(lua_tostring(L, -1))); } lua_pop(L, 1); return result; } void SecurityLabelConvertor::doConvertToLua(lua_State* L, std::shared_ptr<SecurityLabel> payload) { lua_createtable(L, 0, 0); if (!payload->getEquivalentLabels().empty()) { lua_createtable(L, boost::numeric_cast<int>(payload->getEquivalentLabels().size()), 0); { int i = 0; - foreach(const std::string& item, payload->getEquivalentLabels()) { + for (const auto& item : payload->getEquivalentLabels()) { lua_pushstring(L, item.c_str()); lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } lua_setfield(L, -2, "equivalent_labels"); } lua_pushstring(L, payload->getForegroundColor().c_str()); lua_setfield(L, -2, "foreground_color"); lua_pushstring(L, payload->getDisplayMarking().c_str()); lua_setfield(L, -2, "display_marking"); lua_pushstring(L, payload->getBackgroundColor().c_str()); lua_setfield(L, -2, "background_color"); lua_pushstring(L, payload->getLabel().c_str()); lua_setfield(L, -2, "label"); } boost::optional<LuaElementConvertor::Documentation> SecurityLabelConvertor::getDocumentation() const { return Documentation( "SecurityLabel", "This table has the following fields:\n\n" "- `equivalent_labels`: array<string>\n" "- `foreground_color`: string\n" "- `display_marking`: string\n" "- `background_color`: string\n" "- `label`: string\n" ); } diff --git a/Sluift/ElementConvertors/StanzaConvertor.h b/Sluift/ElementConvertors/StanzaConvertor.h index e1d0cb3..bdaaad3 100644 --- a/Sluift/ElementConvertors/StanzaConvertor.h +++ b/Sluift/ElementConvertors/StanzaConvertor.h @@ -1,41 +1,40 @@ /* * Copyright (c) 2014-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <boost/numeric/conversion/cast.hpp> -#include <Swiften/Base/foreach.h> #include <Swiften/Elements/IQ.h> #include <Swiften/Elements/Message.h> #include <Swiften/Elements/Payload.h> #include <Swiften/Elements/Presence.h> #include <Sluift/GenericLuaElementConvertor.h> #include <Sluift/Lua/Exception.h> #include <Sluift/LuaElementConvertors.h> namespace Swift { template <typename T> class StanzaConvertor : public GenericLuaElementConvertor<T> { public: StanzaConvertor(const std::string& tag) : GenericLuaElementConvertor<T>(tag) { } virtual ~StanzaConvertor() { } std::shared_ptr<T> getStanza(lua_State* L, LuaElementConvertors* convertors) { std::shared_ptr<T> result = std::make_shared<T>(); lua_getfield(L, -1, "id"); if (lua_isstring(L, -1)) { result->setID(lua_tostring(L, -1)); } lua_pop(L, 1); lua_getfield(L, -1, "from"); if (lua_isstring(L, -1)) { result->setFrom(lua_tostring(L, -1)); } @@ -48,42 +47,42 @@ namespace Swift { lua_getfield(L, -1, "payloads"); if (lua_type(L, -1) == LUA_TTABLE) { for(size_t i = 0; i < lua_objlen(L, -1); ++i) { lua_pushnumber(L, i + 1); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { std::shared_ptr<Payload> payload = std::dynamic_pointer_cast<Payload>(convertors->convertFromLua(L, -1)); if (!!payload) { result->addPayload(payload); } } lua_pop(L, 1); } } lua_pop(L, 1); return result; } void pushStanza(lua_State* L, const std::shared_ptr<T> stanza, LuaElementConvertors* convertors) { lua_createtable(L, 0, 0); lua_pushstring(L, stanza->getID().c_str()); lua_setfield(L, -2, "id"); lua_pushstring(L, stanza->getFrom().toString().c_str()); lua_setfield(L, -2, "from"); lua_pushstring(L, stanza->getTo().toString().c_str()); lua_setfield(L, -2, "to"); if (!stanza->getPayloads().empty()) { lua_createtable(L, boost::numeric_cast<int>(stanza->getPayloads().size()), 0); { int i = 0; - foreach(const std::shared_ptr<Payload> &item, stanza->getPayloads()) { + for (const auto& item : stanza->getPayloads()) { if (convertors->convertToLua(L, item) > 0) { lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); ++i; } } } lua_setfield(L, -2, "payloads"); } } }; } diff --git a/Sluift/Lua/FunctionRegistry.cpp b/Sluift/Lua/FunctionRegistry.cpp index ebbd087..46c6d18 100644 --- a/Sluift/Lua/FunctionRegistry.cpp +++ b/Sluift/Lua/FunctionRegistry.cpp @@ -1,59 +1,57 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/Lua/FunctionRegistry.h> -#include <Swiften/Base/foreach.h> - #include <Sluift/Lua/Exception.h> #include <Sluift/Lua/LuaUtils.h> #include <Sluift/globals.h> using namespace Swift::Lua; FunctionRegistry::FunctionRegistry() { } FunctionRegistry::~FunctionRegistry() { } FunctionRegistry& FunctionRegistry::getInstance() { static FunctionRegistry instance; return instance; } void FunctionRegistry::addFunction( const std::string& name, lua_CFunction function, const std::string& type, const std::string& helpDescription, const std::string& helpParameters, const std::string& helpOptions) { Registration registration; registration.name = name; registration.function = function; registration.type = type; registration.helpDescription = helpDescription; registration.helpParameters = helpParameters; registration.helpOptions = helpOptions; registrations.push_back(registration); } void FunctionRegistry::createFunctionTable(lua_State* L, const std::string& type) { lua_newtable(L); addFunctionsToTable(L, type); } void FunctionRegistry::addFunctionsToTable(lua_State* L, const std::string& type) { - foreach(const Registration& registration, registrations) { + for (const auto& registration : registrations) { if (registration.type == type) { lua_pushcclosure(L, registration.function, 0); if (!registration.helpDescription.empty()) { Lua::registerHelp(L, -1, registration.helpDescription, registration.helpParameters, registration.helpOptions); } else { Lua::registerExtraHelp(L, -1, registration.type + "." + registration.name); } lua_setfield(L, -2, registration.name.c_str()); } } } diff --git a/Sluift/Lua/Value.cpp b/Sluift/Lua/Value.cpp index 70fbb89..96d954c 100644 --- a/Sluift/Lua/Value.cpp +++ b/Sluift/Lua/Value.cpp @@ -1,68 +1,66 @@ /* * Copyright (c) 2011-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/Lua/Value.h> #include <boost/numeric/conversion/cast.hpp> #include <boost/variant/apply_visitor.hpp> extern "C" { #include <lualib.h> } -#include <Swiften/Base/foreach.h> - using namespace Swift; using namespace Swift::Lua; namespace { struct PushVisitor : public boost::static_visitor<> { PushVisitor(lua_State* state) : state(state) { } void operator()(const Nil&) const { lua_pushnil(state); } void operator()(const bool& b) const { lua_pushboolean(state, b); } void operator()(const int& i) const { lua_pushnumber(state, i); } void operator()(const std::string& s) const { lua_pushstring(state, s.c_str()); } void operator()(const std::vector<Value>& values) const { lua_createtable(state, boost::numeric_cast<int>(values.size()), 0); for(size_t i = 0; i < values.size(); ++i) { boost::apply_visitor(PushVisitor(state), values[i]); lua_rawseti(state, -2, boost::numeric_cast<int>(i + 1)); } } void operator()(const std::map<std::string, std::shared_ptr<Value> >& table) const { lua_createtable(state, 0, boost::numeric_cast<int>(table.size())); - for(const auto& i : table) { + for (const auto& i : table) { boost::apply_visitor(PushVisitor(state), *i.second); lua_setfield(state, -2, i.first.c_str()); } } lua_State* state; }; } namespace Swift { namespace Lua { void pushValue(lua_State* state, const Value& value) { boost::apply_visitor(PushVisitor(state), value); } }} diff --git a/Sluift/LuaElementConvertors.cpp b/Sluift/LuaElementConvertors.cpp index 38926e9..aac4d93 100644 --- a/Sluift/LuaElementConvertors.cpp +++ b/Sluift/LuaElementConvertors.cpp @@ -1,42 +1,40 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/LuaElementConvertors.h> #include <memory> -#include <Swiften/Base/foreach.h> - #include <Sluift/ElementConvertors/BodyConvertor.h> #include <Sluift/ElementConvertors/CarbonsReceivedConvertor.h> #include <Sluift/ElementConvertors/CarbonsSentConvertor.h> #include <Sluift/ElementConvertors/CommandConvertor.h> #include <Sluift/ElementConvertors/DOMElementConvertor.h> #include <Sluift/ElementConvertors/DefaultElementConvertor.h> #include <Sluift/ElementConvertors/DelayConvertor.h> #include <Sluift/ElementConvertors/DiscoInfoConvertor.h> #include <Sluift/ElementConvertors/DiscoItemsConvertor.h> #include <Sluift/ElementConvertors/FormConvertor.h> #include <Sluift/ElementConvertors/ForwardedConvertor.h> #include <Sluift/ElementConvertors/IQConvertor.h> #include <Sluift/ElementConvertors/MAMFinConvertor.h> #include <Sluift/ElementConvertors/MAMQueryConvertor.h> #include <Sluift/ElementConvertors/MAMResultConvertor.h> #include <Sluift/ElementConvertors/MessageConvertor.h> #include <Sluift/ElementConvertors/PresenceConvertor.h> #include <Sluift/ElementConvertors/PubSubEventConvertor.h> #include <Sluift/ElementConvertors/RawXMLElementConvertor.h> #include <Sluift/ElementConvertors/ResultSetConvertor.h> #include <Sluift/ElementConvertors/SoftwareVersionConvertor.h> #include <Sluift/ElementConvertors/StatusConvertor.h> #include <Sluift/ElementConvertors/StatusShowConvertor.h> #include <Sluift/ElementConvertors/SubjectConvertor.h> #include <Sluift/ElementConvertors/VCardConvertor.h> #include <Sluift/ElementConvertors/VCardUpdateConvertor.h> #include <Sluift/Lua/Exception.h> #include <Sluift/Lua/LuaUtils.h> #include <Sluift/LuaElementConvertor.h> @@ -68,74 +66,74 @@ LuaElementConvertors::LuaElementConvertors() { convertors.push_back(std::make_shared<CarbonsReceivedConvertor>(this)); convertors.push_back(std::make_shared<CarbonsSentConvertor>(this)); convertors.push_back(std::make_shared<DOMElementConvertor>()); convertors.push_back(std::make_shared<RawXMLElementConvertor>()); convertors.push_back(std::make_shared<DefaultElementConvertor>()); } LuaElementConvertors::~LuaElementConvertors() { } #include <Sluift/ElementConvertors/ElementConvertors.ipp> std::shared_ptr<Element> 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<Element> LuaElementConvertors::convertFromLuaUntyped(lua_State* L, int index, const std::string& type) { index = Lua::absoluteOffset(L, index); - foreach (std::shared_ptr<LuaElementConvertor> convertor, convertors) { + for (auto&& convertor : convertors) { if (std::shared_ptr<Element> result = convertor->convertFromLua(L, index, type)) { return result; } } return std::shared_ptr<Element>(); } int LuaElementConvertors::convertToLua(lua_State* L, std::shared_ptr<Element> payload) { if (boost::optional<std::string> 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<Element> payload) { if (doConvertToLuaUntyped(L, payload)) { return 1; } return 0; } boost::optional<std::string> LuaElementConvertors::doConvertToLuaUntyped( lua_State* L, std::shared_ptr<Element> payload) { if (!payload) { return LuaElementConvertor::NO_RESULT; } - foreach (std::shared_ptr<LuaElementConvertor> convertor, convertors) { + for (auto&& convertor : convertors) { if (boost::optional<std::string> type = convertor->convertToLua(L, payload)) { return *type; } } return LuaElementConvertor::NO_RESULT; } diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 6376e9d..186effc 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -1,42 +1,41 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <boost/assign/list_of.hpp> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #include <Swiften/Base/IDGenerator.h> -#include <Swiften/Base/foreach.h> #include <Swiften/Disco/ClientDiscoManager.h> #include <Swiften/Elements/DiscoInfo.h> #include <Swiften/Elements/Message.h> #include <Swiften/Elements/Presence.h> #include <Swiften/Elements/RawXMLPayload.h> #include <Swiften/Elements/RosterItemPayload.h> #include <Swiften/Elements/RosterPayload.h> #include <Swiften/Elements/SoftwareVersion.h> #include <Swiften/JID/JID.h> #include <Swiften/Presence/PresenceSender.h> #include <Swiften/Presence/SubscriptionManager.h> #include <Swiften/Queries/GenericRequest.h> #include <Swiften/Queries/IQRouter.h> #include <Swiften/Queries/Requests/GetSoftwareVersionRequest.h> #include <Swiften/Roster/SetRosterRequest.h> #include <Swiften/Roster/XMPPRoster.h> #include <Swiften/Roster/XMPPRosterItem.h> #include <Swiften/TLS/PKCS12Certificate.h> #include <Sluift/ElementConvertors/IQConvertor.h> #include <Sluift/ElementConvertors/MessageConvertor.h> #include <Sluift/ElementConvertors/PresenceConvertor.h> #include <Sluift/ElementConvertors/StanzaConvertor.h> #include <Sluift/ElementConvertors/StatusShowConvertor.h> #include <Sluift/Lua/Check.h> #include <Sluift/Lua/Exception.h> #include <Sluift/Lua/FunctionRegistration.h> #include <Sluift/Lua/LuaUtils.h> #include <Sluift/Lua/Value.h> #include <Sluift/SluiftClient.h> @@ -158,61 +157,61 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( SLUIFT_LUA_FUNCTION_WITH_HELP( Client, set_version, "Sets the published version of this client.", "self", "name the name of the client software\n" "version the version identifier of this client\n" "os the OS this client is running on\n" ) { Sluift::globals.eventLoop.runOnce(); SluiftClient* client = getClient(L); if (std::shared_ptr<SoftwareVersion> version = std::dynamic_pointer_cast<SoftwareVersion>(Sluift::globals.elementConvertor.convertFromLuaUntyped(L, 2, "software_version"))) { client->setSoftwareVersion(version->getName(), version->getVersion(), version->getOS()); } return 0; } SLUIFT_LUA_FUNCTION_WITH_HELP( Client, get_contacts, "Returns a table of all the contacts in the contact list.", "self\n", "" ) { Sluift::globals.eventLoop.runOnce(); SluiftClient* client = getClient(L); Lua::Table contactsTable; - foreach(const XMPPRosterItem& item, client->getRoster(getGlobalTimeout(L))) { + for (const auto& item : client->getRoster(getGlobalTimeout(L))) { std::string subscription; switch(item.getSubscription()) { case RosterItemPayload::None: subscription = "none"; break; case RosterItemPayload::To: subscription = "to"; break; case RosterItemPayload::From: subscription = "from"; break; case RosterItemPayload::Both: subscription = "both"; break; case RosterItemPayload::Remove: subscription = "remove"; break; } Lua::Table itemTable = boost::assign::map_list_of ("jid", std::make_shared<Lua::Value>(item.getJID().toString())) ("name", std::make_shared<Lua::Value>(item.getName())) ("subscription", std::make_shared<Lua::Value>(subscription)) ("groups", std::make_shared<Lua::Value>(std::vector<Lua::Value>(item.getGroups().begin(), item.getGroups().end()))); contactsTable[item.getJID().toString()] = std::make_shared<Lua::Value>(itemTable); } pushValue(L, contactsTable); Lua::registerTableToString(L, -1); return 1; } SLUIFT_LUA_FUNCTION_WITH_HELP( Client, send_message, "Send a message.", "self\n" "to the JID to send the message to\n" "body the body of the message. Can alternatively be specified using the `body` option\n", "to the JID to send the message to\n" "body the body of the message\n" "subject the subject of the MUC room to set\n" diff --git a/Sluift/component.cpp b/Sluift/component.cpp index 9c2bc3a..df96d43 100644 --- a/Sluift/component.cpp +++ b/Sluift/component.cpp @@ -1,42 +1,41 @@ /* * Copyright (c) 2014-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <boost/assign/list_of.hpp> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #include <Swiften/Base/IDGenerator.h> -#include <Swiften/Base/foreach.h> #include <Swiften/Elements/DiscoInfo.h> #include <Swiften/Elements/MAMQuery.h> #include <Swiften/Elements/Message.h> #include <Swiften/Elements/Presence.h> #include <Swiften/Elements/RawXMLPayload.h> #include <Swiften/Elements/RosterItemPayload.h> #include <Swiften/Elements/RosterPayload.h> #include <Swiften/Elements/SoftwareVersion.h> #include <Swiften/JID/JID.h> #include <Swiften/Presence/PresenceSender.h> #include <Swiften/Presence/SubscriptionManager.h> #include <Swiften/Queries/GenericRequest.h> #include <Swiften/Queries/IQRouter.h> #include <Swiften/Queries/Requests/GetSoftwareVersionRequest.h> #include <Swiften/Roster/SetRosterRequest.h> #include <Swiften/Roster/XMPPRoster.h> #include <Swiften/Roster/XMPPRosterItem.h> #include <Sluift/ElementConvertors/IQConvertor.h> #include <Sluift/ElementConvertors/MessageConvertor.h> #include <Sluift/ElementConvertors/PresenceConvertor.h> #include <Sluift/ElementConvertors/StanzaConvertor.h> #include <Sluift/ElementConvertors/StatusShowConvertor.h> #include <Sluift/Lua/Check.h> #include <Sluift/Lua/Exception.h> #include <Sluift/Lua/FunctionRegistration.h> #include <Sluift/Lua/LuaUtils.h> #include <Sluift/Lua/Value.h> #include <Sluift/SluiftComponent.h> #include <Sluift/globals.h> diff --git a/Sluift/main.cpp b/Sluift/main.cpp index 05c7179..65d6d60 100644 --- a/Sluift/main.cpp +++ b/Sluift/main.cpp @@ -1,48 +1,51 @@ /* * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ +#include <iostream> #include <string> #include <vector> -#include <iostream> -#include <lua.hpp> -#include <Swiften/Base/foreach.h> -#include <Swiften/Base/Platform.h> + +#include <boost/assign/list_of.hpp> +#include <boost/numeric/conversion/cast.hpp> +#include <boost/program_options.hpp> #include <boost/program_options/options_description.hpp> #include <boost/program_options/variables_map.hpp> -#include <boost/program_options.hpp> #include <boost/version.hpp> -#include <boost/numeric/conversion/cast.hpp> -#include <boost/assign/list_of.hpp> + +#include <lua.hpp> + +#include <Swiften/Base/Platform.h> + #include <Sluift/globals.h> #include <Sluift/Console.h> #include <Sluift/StandardTerminal.h> #include <Sluift/sluift.h> #include <Sluift/Lua/LuaUtils.h> #ifdef HAVE_EDITLINE #include <Sluift/EditlineTerminal.h> #endif #include <Sluift/Version.h> using namespace Swift; #ifdef SWIFTEN_PLATFORM_WINDOWS #define EXIT_KEY "Z" #else #define EXIT_KEY "D" #endif static const std::string SLUIFT_WELCOME_STRING( "== Sluift XMPP Console (" SLUIFT_VERSION_STRING ")\nPress Ctrl-" EXIT_KEY " to exit. Type help() for help."); static const luaL_Reg defaultLibraries[] = { {"", luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_TABLIBNAME, luaopen_table}, {LUA_IOLIBNAME, luaopen_io}, {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, @@ -60,61 +63,61 @@ static void checkResult(lua_State* L, int result) { throw std::runtime_error(errorMessage ? errorMessage : "Unknown error"); } } static void initialize(lua_State* L) { lua_gc(L, LUA_GCSTOP, 0); for (const luaL_Reg* lib = defaultLibraries; lib->func; lib++) { #if LUA_VERSION_NUM >= 502 luaL_requiref(L, lib->name, lib->func, 1); lua_pop(L, 1); #else lua_pushcfunction(L, lib->func); lua_pushstring(L, lib->name); lua_call(L, 1, 0); #endif } lua_gc(L, LUA_GCRESTART, 0); } static void runScript(lua_State* L, const std::string& script, const std::vector<std::string>& scriptArguments) { // Create arguments table lua_createtable(L, boost::numeric_cast<int>(scriptArguments.size()), 0); for (size_t i = 0; i < scriptArguments.size(); ++i) { lua_pushstring(L, scriptArguments[i].c_str()); lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); } lua_setglobal(L, "arg"); // Load file checkResult(L, luaL_loadfile(L, script.c_str())); - foreach (const std::string& scriptArgument, scriptArguments) { + for (const auto& scriptArgument : scriptArguments) { lua_pushstring(L, scriptArgument.c_str()); } checkResult(L, Console::call(L, boost::numeric_cast<int>(scriptArguments.size()), false)); } int main(int argc, char* argv[]) { // Parse program options boost::program_options::options_description visibleOptions("Options"); visibleOptions.add_options() ("help,h", "Display this help message") ("version,v", "Display version information") ("interactive,i", "Enter interactive mode after executing script") ; boost::program_options::options_description hiddenOptions("Hidden Options"); hiddenOptions.add_options() ("script", boost::program_options::value< std::string >(), "Script to be executed") ("script-arguments", boost::program_options::value< std::vector<std::string> >(), "Script arguments") ; boost::program_options::options_description options("All Options"); options.add(visibleOptions).add(hiddenOptions); boost::program_options::positional_options_description positional_options; positional_options.add("script", 1).add("script-arguments", -1); boost::program_options::variables_map arguments; try { boost::program_options::store( boost::program_options::command_line_parser(argc, argv) .options(options) .positional(positional_options).run(), arguments); @@ -136,54 +139,54 @@ int main(int argc, char* argv[]) { if (arguments.count("help")) { std::cout << visibleOptions << "\n"; return 0; } else if (arguments.count("version")) { std::cout << SLUIFT_VERSION_STRING; return 0; } lua_State* L = luaL_newstate(); initialize(L); try { // Run script if (arguments.count("script")) { std::vector<std::string> scriptArguments; if (arguments.count("script-arguments")) { scriptArguments = arguments["script-arguments"].as< std::vector<std::string> >(); } runScript(L, arguments["script"].as<std::string>(), scriptArguments); } // Run console if (arguments.count("interactive") || arguments.count("script") == 0) { // Set up signal handler signal(SIGINT, handleInterruptSignal); // Import some useful functions into the global namespace lua_getglobal(L, "sluift"); std::vector<std::string> globalImports = boost::assign::list_of ("help")("with"); - foreach (const std::string& globalImport, globalImports) { + for (const auto& globalImport : globalImports) { lua_getfield(L, -1, globalImport.c_str()); lua_setglobal(L, globalImport.c_str()); } lua_pop(L, 1); std::cout << SLUIFT_WELCOME_STRING << std::endl; #ifdef HAVE_EDITLINE EditlineTerminal& terminal = EditlineTerminal::getInstance(); #else StandardTerminal terminal; #endif Console console(L, &terminal); console.run(); } } catch (const std::exception& e) { std::cerr << e.what() << std::endl; lua_close(L); return -1; } lua_close(L); return 0; } diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index d92f0db..561f404 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -1,49 +1,48 @@ /* * Copyright (c) 2011-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/sluift.h> #include <string> #include <boost/assign/list_of.hpp> #include <boost/bind.hpp> #include <boost/filesystem.hpp> #include <boost/numeric/conversion/cast.hpp> #include <lua.hpp> #include <Swiften/Base/IDGenerator.h> -#include <Swiften/Base/foreach.h> #include <Swiften/Base/sleep.h> #include <Swiften/Crypto/CryptoProvider.h> #include <Swiften/Crypto/PlatformCryptoProvider.h> #include <Swiften/IDN/IDNConverter.h> #include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> #include <Swiften/Serializer/PayloadSerializer.h> #include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> #include <Swiften/StringCodecs/Base64.h> #include <Swiften/StringCodecs/Hexify.h> #include <Swiften/TLS/Certificate.h> #include <Swiften/TLS/CertificateFactory.h> #include <Sluift/ITunesInterface.h> #include <Sluift/Lua/Check.h> #include <Sluift/Lua/Debug.h> #include <Sluift/Lua/Exception.h> #include <Sluift/Lua/FunctionRegistration.h> #include <Sluift/Lua/LuaUtils.h> #include <Sluift/LuaElementConvertor.h> #include <Sluift/SluiftClient.h> #include <Sluift/SluiftComponent.h> #include <Sluift/Watchdog.h> #include <Sluift/globals.h> using namespace Swift; namespace Swift { namespace Sluift { SluiftGlobals globals; } @@ -434,69 +433,69 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { // Load core lib code if (luaL_loadbuffer(L, core_lua, core_lua_size, "core.lua") != 0) { lua_error(L); } lua_pushvalue(L, -2); lua_call(L, 1, 1); Sluift::globals.coreLibIndex = luaL_ref(L, LUA_REGISTRYINDEX); // Register functions Lua::FunctionRegistry::getInstance().addFunctionsToTable(L, "Sluift"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "JID"); lua_setfield(L, -2, "jid"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "Base64"); lua_setfield(L, -2, "base64"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "IDN"); lua_setfield(L, -2, "idn"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "Crypto"); lua_setfield(L, -2, "crypto"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "FS"); lua_setfield(L, -2, "fs"); #ifdef HAVE_ITUNES Lua::FunctionRegistry::getInstance().createFunctionTable(L, "iTunes"); lua_setfield(L, -2, "itunes"); #endif // Register convenience functions lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); std::vector<std::string> coreLibExports = boost::assign::list_of ("tprint")("disco")("help")("get_help")("copy")("with")("read_file")("create_form"); - foreach (const std::string& coreLibExport, coreLibExports) { + for (const auto& coreLibExport : coreLibExports) { lua_getfield(L, -1, coreLibExport.c_str()); lua_setfield(L, -3, coreLibExport.c_str()); } lua_pop(L, 1); // Load client metatable lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); std::vector<std::string> tables = boost::assign::list_of("Client"); - foreach(const std::string& table, tables) { + for (const auto& table : tables) { lua_getfield(L, -1, table.c_str()); Lua::FunctionRegistry::getInstance().addFunctionsToTable(L, table); lua_pop(L, 1); } lua_pop(L, 1); // Load component metatable lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); std::vector<std::string> comp_tables = boost::assign::list_of("Component"); - foreach(const std::string& table, comp_tables) { + for (const auto& table : comp_tables) { lua_getfield(L, -1, table.c_str()); Lua::FunctionRegistry::getInstance().addFunctionsToTable(L, table); lua_pop(L, 1); } lua_pop(L, 1); // Register documentation for all elements - foreach (std::shared_ptr<LuaElementConvertor> convertor, Sluift::globals.elementConvertor.getConvertors()) { + for (auto&& convertor : Sluift::globals.elementConvertor.getConvertors()) { boost::optional<LuaElementConvertor::Documentation> documentation = convertor->getDocumentation(); if (documentation) { Lua::registerClassHelp(L, documentation->className, documentation->description); } } // Register global documentation Lua::registerExtraHelp(L, -1, "sluift"); return 1; } |