summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2016-11-23 07:09:39 (GMT)
committerTobias Markmann <tm@ayena.de>2016-11-23 11:30:02 (GMT)
commite405ff3561be3d3c0bd79d7d5173923a8828cf02 (patch)
tree9118ef838ebfaec1df90ec24761944b5d833774c /Sluift/ElementConvertors/FormConvertor.cpp
parent8a71b91be885652f37c5aab5e1ecf25af4599fbc (diff)
downloadswift-e405ff3561be3d3c0bd79d7d5173923a8828cf02.zip
swift-e405ff3561be3d3c0bd79d7d5173923a8828cf02.tar.bz2
Migrate remaining Swiften/Base/foreach.h use to range-based for loop
Test-Information: Build on macOS 10.12.1 and all tests pass. Change-Id: Iedaa3fa7e7672c77909fd0568bf30e9393cb87e0
Diffstat (limited to 'Sluift/ElementConvertors/FormConvertor.cpp')
-rw-r--r--Sluift/ElementConvertors/FormConvertor.cpp14
1 files changed, 6 insertions, 8 deletions
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) {