c++17: Use fold expression with std::apply
authorSangwan Kwon <sangwan.kwon@samsung.com>
Fri, 31 Jan 2020 04:41:06 +0000 (13:41 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Fri, 31 Jan 2020 06:27:26 +0000 (15:27 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/query-builder/database.hpp
src/vist/query-builder/table.hpp
src/vist/query-builder/tuple-helper.hpp [deleted file]

index 57dbac6..7cdf8ba 100644 (file)
@@ -20,7 +20,6 @@
 #include "condition.hpp"
 #include "crud.hpp"
 #include "expression.hpp"
-#include "tuple-helper.hpp"
 #include "util.hpp"
 
 #include <algorithm>
@@ -118,7 +117,7 @@ std::vector<std::string> Database<Tables...>::getTableNames(Cs&& ...columns) con
 {
        std::set<std::string> names;
 
-       auto closure = [this, &names](const auto& type) {
+       auto predicate = [this, &names](const auto& type) {
                Column anonymous("anonymous", type);
                using TableType = typename decltype(anonymous)::Table;
                auto name = this->getTableName(TableType());
@@ -126,8 +125,11 @@ std::vector<std::string> Database<Tables...>::getTableNames(Cs&& ...columns) con
                                names.emplace(name);
        };
 
-       auto tuple = std::tuple(columns...);
-       tuple_helper::for_each(tuple, closure);
+       auto closure = [&predicate](const auto&... iter) {
+               (predicate(iter), ...);
+       };
+
+       std::apply(closure, std::tuple(columns...));
 
        return std::vector<std::string>(names.begin(), names.end());
 }
@@ -137,14 +139,17 @@ template<typename... Cs>
 std::vector<std::string> Database<Tables...>::getColumnNames(Cs&& ...columns) const noexcept
 {
        std::vector<std::string> names;
-       auto closure = [this, &names](const auto& iter) {
+       auto predicate = [this, &names](const auto& iter) {
                auto name = this->getColumnName(iter);
                if (!name.empty())
                        names.emplace_back(name);
        };
 
-       auto tuple = std::tuple(columns...);
-       tuple_helper::for_each(tuple, closure);
+       auto closure = [&predicate](const auto&... iter) {
+               (predicate(iter), ...);
+       };
+
+       std::apply(closure, std::tuple(columns...));
 
        return names;
 }
@@ -154,12 +159,16 @@ template<typename Table>
 std::string Database<Tables...>::getTableName(Table&& table) const noexcept
 {
        std::string name;
-       auto predicate = [&name, &table](const auto& iter) {
-               if (iter.compare(table))
-                       name = iter.name;
+       auto predicate = [&name, &table](const auto& type) {
+               if (type.compare(table))
+                       name = type.name;
        };
 
-       tuple_helper::for_each(this->tables, predicate);
+       auto closure = [&predicate](const auto&... iter) {
+               (predicate(iter), ...);
+       };
+
+       std::apply(closure, this->tables);
 
        return name;
 }
@@ -180,7 +189,11 @@ std::string Database<Tables...>::getColumnName(ColumnType&& column) const noexce
                }
        };
 
-       tuple_helper::for_each(this->tables, predicate);
+       auto closure = [&predicate](const auto&... iter) {
+               (predicate(iter), ...);
+       };
+
+       std::apply(closure, this->tables);
 
        return name;
 }
index c50e05c..92d2a31 100644 (file)
@@ -18,7 +18,6 @@
 
 #include "column.hpp"
 #include "crud.hpp"
-#include "tuple-helper.hpp"
 #include "type.hpp"
 #include "util.hpp"
 
@@ -85,14 +84,17 @@ template<typename... Cs>
 std::vector<std::string> Table<Columns...>::getColumnNames(Cs&& ...columns) const noexcept
 {
        std::vector<std::string> names;
-       auto closure = [this, &names](auto type) {
+       auto predicate = [this, &names](const auto& type) {
                auto name = this->getColumnName(type);
                if (!name.empty())
                        names.emplace_back(name);
        };
 
-       auto tuple = std::tuple(columns...);
-       tuple_helper::for_each(tuple, closure);
+       auto closure = [&predicate](const auto&... iter) {
+               (predicate(iter), ...);
+       };
+
+       std::apply(closure, std::tuple(columns...));
 
        return names;
 }
@@ -108,11 +110,11 @@ template<typename... Columns>
 std::vector<std::string> Table<Columns...>::getColumnNames(void) const noexcept
 {
        std::vector<std::string> names;
-       auto closure = [&names](const auto& iter) {
-               names.push_back(iter.name);
+       auto closure = [&names](const auto&... iter) {
+               (names.push_back(iter.name), ...);
        };
 
-       tuple_helper::for_each(this->columns, closure);
+       std::apply(closure, this->columns);
 
        return names;
 }
@@ -127,7 +129,11 @@ std::string Table<Columns...>::getColumnName(const Column& column) const noexcep
                        name = iter.name;
        };
 
-       tuple_helper::for_each(this->columns, predicate);
+       auto closure = [&predicate](const auto&... iter) {
+               (predicate(iter), ...);
+       };
+
+       std::apply(closure, this->columns);
 
        return name;
 }
diff --git a/src/vist/query-builder/tuple-helper.hpp b/src/vist/query-builder/tuple-helper.hpp
deleted file mode 100644 (file)
index 23e2558..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- *  Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-
-#pragma once
-
-#include <tuple>
-
-namespace vist {
-namespace tsqb {
-namespace tuple_helper {
-namespace internal {
-
-template<int n, typename T, typename C>
-class Iterator {
-public:
-       Iterator(const T& tuple, C&& closure) {
-               Iterator<n - 1, T, C> iter(tuple, std::forward<C>(closure));
-               closure(std::get<n-1>(tuple));
-       }
-};
-
-template<typename T, typename C>
-class Iterator<0, T, C> {
-public:
-       Iterator(const T&, C&&) {}
-};
-
-} // namespace internal
-
-template<typename T, typename C>
-void for_each(const T& tuple, C&& closure)
-{
-       using Iter = internal::Iterator<std::tuple_size<T>::value, T, C>;
-       Iter iter(tuple, std::forward<C>(closure));
-}
-
-} // namspace tuple-hepler
-} // namspace tsqb
-} // namspace vist