[libsigc++2/variadic_bind2: 58/68] Copy in files from murrayc-tuple-utils.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libsigc++2/variadic_bind2: 58/68] Copy in files from murrayc-tuple-utils.
- Date: Tue, 1 Mar 2016 21:48:35 +0000 (UTC)
commit b6126846031f782830d927d1597dd02b2f967490
Author: Murray Cumming <murrayc murrayc com>
Date: Fri Feb 19 11:20:29 2016 +0100
Copy in files from murrayc-tuple-utils.
sigc++/tuple_cat.h | 43 +++++------
sigc++/tuple_cdr.h | 82 +++++++++++---------
sigc++/tuple_end.h | 106 +++++++++++++++----------
sigc++/tuple_start.h | 80 ++++++++++++-------
sigc++/tuple_transform_each.h | 147 ++++++++++++++++++------------------
tests/test_tuple_cat.cc | 50 +++++++++---
tests/test_tuple_cdr.cc | 60 ++++++++++++---
tests/test_tuple_end.cc | 84 ++++++++++++++-------
tests/test_tuple_for_each.cc | 136 ++++++++++++++------------------
tests/test_tuple_start.cc | 84 ++++++++++++++-------
tests/test_tuple_transform_each.cc | 149 +++++++++++++++++------------------
11 files changed, 584 insertions(+), 437 deletions(-)
---
diff --git a/sigc++/tuple_cat.h b/sigc++/tuple_cat.h
index e96f722..bb08644 100644
--- a/sigc++/tuple_cat.h
+++ b/sigc++/tuple_cat.h
@@ -14,46 +14,41 @@
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
-#ifndef _SIGC_TUPLE_UTILS_TUPLE_CAT_H_
-#define _SIGC_TUPLE_UTILS_TUPLE_CAT_H_
+#ifndef MURRAYC_TUPLE_UTILS_TUPLE_CAT_H
+#define MURRAYC_TUPLE_UTILS_TUPLE_CAT_H
#include <tuple>
-#include <utility>
#include <type_traits>
+#include <utility>
-namespace sigc
-{
+namespace tupleutils {
-namespace {
+namespace detail {
-template<typename T1, typename T2, typename Seq1, typename Seq2>
+template <typename T1, typename T2, typename Seq1, typename Seq2>
struct tuple_type_cat_impl;
-template<typename T1, typename T2, std::size_t... I1, std::size_t... I2>
-struct tuple_type_cat_impl<T1, T2, std::index_sequence<I1...>, std::index_sequence<I2...>>
-{
- using type = std::tuple<
- typename std::tuple_element<I1, T1>::type...,
+template <typename T1, typename T2, std::size_t... I1, std::size_t... I2>
+struct tuple_type_cat_impl<T1, T2, std::index_sequence<I1...>,
+ std::index_sequence<I2...>> {
+ using type = std::tuple<typename std::tuple_element<I1, T1>::type...,
typename std::tuple_element<I2, T2>::type...>;
};
-} //anonymous namespace
+} // detail namespace
/**
* Get the type of a tuple without the first item.
*/
-template<typename T1, typename T2>
+template <typename T1, typename T2>
struct tuple_type_cat
-: tuple_type_cat_impl<T1, T2,
- std::make_index_sequence<std::tuple_size<T1>::value>,
- std::make_index_sequence<std::tuple_size<T2>::value>>
-{};
-
-
-//There is no tuple_cat() here because std::tuple_cat() exists already in
-//the C++ standard library.
+ : detail::tuple_type_cat_impl<T1, T2,
+ std::make_index_sequence<std::tuple_size<T1>::value>,
+ std::make_index_sequence<std::tuple_size<T2>::value>> {};
+// There is no tuple_cat() here because std::tuple_cat() exists already in
+// the C++ standard library.
-} //namespace sigc
+} // namespace tupleutils
-#endif //_SIGC_TUPLE_UTILS_TUPLE_CAT_H_
+#endif //MURRAYC_TUPLE_UTILS_TUPLE_CAT_H
diff --git a/sigc++/tuple_cdr.h b/sigc++/tuple_cdr.h
index 3d62d1f..c266ef0 100644
--- a/sigc++/tuple_cdr.h
+++ b/sigc++/tuple_cdr.h
@@ -1,56 +1,68 @@
-#ifndef _SIGC_TUPLE_CDR_H_
-#define _SIGC_TUPLE_CDR_H_
+/* Copyright (C) 2016 Murray Cumming
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/
+ */
+
+#ifndef MURRAYC_TUPLE_UTILS_TUPLE_CDR_H
+#define MURRAYC_TUPLE_UTILS_TUPLE_CDR_H
#include <tuple>
-#include <utility>
#include <type_traits>
+#include <utility>
-namespace sigc
-{
-
-namespace {
-
-template<typename T, typename Seq>
-struct tuple_type_cdr_impl;
-
-template<typename T, std::size_t I0, std::size_t... I>
-struct tuple_type_cdr_impl<T, std::index_sequence<I0, I...>>
-{
- using type = std::tuple<typename std::tuple_element<I, T>::type...>;
-};
-
-} //anonymous namespace
+namespace tupleutils {
/**
* Get the type of a tuple without the first item.
*/
-template<typename T>
-struct tuple_type_cdr
-: tuple_type_cdr_impl<T, std::make_index_sequence<std::tuple_size<T>::value>>
-{};
+template <typename T>
+struct tuple_type_cdr; // primary template is not defined
+// Partial specialization for tuples of at least one element:
+template <typename H, typename... T>
+struct tuple_type_cdr<std::tuple<H, T...>>
+{
+ using type = std::tuple<T...>;
+};
-namespace {
+namespace detail {
-template<typename T, std::size_t I0, std::size_t... I>
-decltype(auto) tuple_cdr_impl(const T& t, std::index_sequence<I0, I...>) {
- return std::make_tuple(std::get<I>(t)...);
+template <typename T, std::size_t... I>
+decltype(auto)
+tuple_cdr_impl(T&& t, std::index_sequence<0, I...>)
+{
+ using cdr = typename tuple_type_cdr<std::decay_t<T>>::type;
+ return cdr(std::get<I>(std::forward<T>(t))...);
}
-} //anonymous namespace
+} // detail namespace
/**
* Get the a tuple without the first item.
* This is analogous to std::tuple_cat().
*/
-template<typename T>
-decltype(auto) tuple_cdr(const T& t) {
- constexpr auto size = std::tuple_size<T>::value;
- const auto seq = std::make_index_sequence<size>{};
- return tuple_cdr_impl(t, seq);
-}
+template <typename T>
+decltype(auto)
+tuple_cdr(T&& t) {
+ //We use std::decay_t<> because tuple_size is not defined for references.
+ constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
+ static_assert(size != 0, "tuple size must be non-zero");
+ using seq = std::make_index_sequence<size>;
+ return detail::tuple_cdr_impl(std::forward<T>(t), seq{});
+}
-} //namespace sigc
+} // namespace tupleutils
-#endif //_SIGC_TUPLE_CDR_H_
+#endif //MURRAYC_TUPLE_UTILS_TUPLE_CDR_H
diff --git a/sigc++/tuple_end.h b/sigc++/tuple_end.h
index 6334f57..528ae29 100644
--- a/sigc++/tuple_end.h
+++ b/sigc++/tuple_end.h
@@ -1,71 +1,93 @@
-#ifndef _SIGC_TUPLE_END_H_
-#define _SIGC_TUPLE_END_H_
+/* Copyright (C) 2016 Murray Cumming
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/
+ */
+
+#ifndef MURRAYC_TUPLE_UTILS_TUPLE_END_H
+#define MURRAYC_TUPLE_UTILS_TUPLE_END_H
-#include <sigc++/tuple_cdr.h>
+#include <tuple-utils/tuple_cdr.h>
-namespace sigc
-{
+namespace tupleutils {
-namespace {
+namespace detail {
-template<typename T, std::size_t remove_from_start>
-struct tuple_type_end_impl
-{
- using type = typename tuple_type_end_impl<typename tuple_type_cdr<T>::type, remove_from_start - 1>::type;
+template <typename T, std::size_t remove_from_start>
+struct tuple_type_end_impl {
+ using type = typename tuple_type_end_impl<typename tuple_type_cdr<std::decay_t<T>>::type,
+ remove_from_start - 1>::type;
};
-template<typename T>
-struct tuple_type_end_impl<T, 0>
-{
+template <typename T>
+struct tuple_type_end_impl<T, 0> {
using type = T;
};
-} //anonymous namespace
-
+} // detail namespace
/**
* Get the type of a tuple with the last @a len types of the original.
*/
-template<typename T, std::size_t len>
+template <typename T, std::size_t len>
struct tuple_type_end
-: tuple_type_end_impl<T, std::tuple_size<T>::value - len>
-{};
+ : detail::tuple_type_end_impl<T, std::tuple_size<T>::value - len> {};
+namespace detail {
-namespace {
+template <typename T, std::size_t remove_from_start>
+struct tuple_end_impl {
+ static decltype(auto) // typename tuple_type_end<T, size - remove_from_start>::type
+ tuple_end(T&& t) {
+ static_assert(remove_from_start > 0, "remove_from_start must be more than zero.");
+
+ using cdr = typename tuple_type_cdr<std::decay_t<T>>::type;
+ return tuple_end_impl<cdr, remove_from_start - 1>::tuple_end(
+ tuple_cdr(std::forward<T>(t)));
+ }
+};
-template<typename T, std::size_t remove_from_start>
-struct tuple_end_impl
-{
- static
- decltype(auto) //typename tuple_type_end<T, size - remove_from_start>::type
- tuple_end(const T& t) {
- return tuple_end_impl<typename tuple_type_cdr<T>::type, remove_from_start - 1>::tuple_end(tuple_cdr(t));
+template <typename T>
+struct tuple_end_impl<T, 1> {
+ static decltype(auto)
+ tuple_end(T&& t) {
+ return tuple_cdr(std::forward<T>(t));
}
};
-template<typename T>
-struct tuple_end_impl<T, 0>
-{
- static
- T
- tuple_end(const T& t) {
- return t;
+template <typename T>
+struct tuple_end_impl<T, 0> {
+ static decltype(auto)
+ tuple_end(T&& t) {
+ return std::forward<T>(t);
}
};
-} //anonymous namespace
+} // detail namespace
/**
- * Get the ttuple with the last @a len items of the original.
+ * Get the tuple with the last @a len items of the original.
*/
-template<std::size_t len, typename T>
-decltype(auto) //typename tuple_type_end<T, len>::type
-tuple_end(const T& t) {
- constexpr auto size = std::tuple_size<T>::value;
- return tuple_end_impl<T, size - len>::tuple_end(t);
+template <std::size_t len, typename T>
+decltype(auto) // typename tuple_type_end<T, len>::type
+ tuple_end(T&& t) {
+ //We use std::decay_t<> because tuple_size is not defined for references.
+ constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
+ static_assert(len <= size, "The tuple size must be less than or equal to the length.");
+ constexpr auto size_start = size - len;
+ return detail::tuple_end_impl<T, size_start>::tuple_end(std::forward<T>(t));
}
-} //namespace sigc;
+} // namespace tupleutils;
-#endif //_SIGC_TUPLE_END_H_
+#endif //MURRAYC_TUPLE_UTILS_TUPLE_END_H
diff --git a/sigc++/tuple_start.h b/sigc++/tuple_start.h
index 487db65..7fe2164 100644
--- a/sigc++/tuple_start.h
+++ b/sigc++/tuple_start.h
@@ -1,60 +1,80 @@
-#ifndef _SIGC_TUPLE_START_H_
-#define _SIGC_TUPLE_START_H_
+/* Copyright (C) 2016 Murray Cumming
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/
+ */
+
+#ifndef MURRAYC_TUPLE_UTILS_TUPLE_START_H
+#define MURRAYC_TUPLE_UTILS_TUPLE_START_H
#include <tuple>
#include <utility>
-namespace sigc
-{
+namespace tupleutils {
-namespace {
+namespace detail {
-template<typename T, typename Seq>
+template <typename T, typename Seq>
struct tuple_type_start_impl;
-template<typename T, std::size_t... I>
-struct tuple_type_start_impl<T, std::index_sequence<I...>>
-{
+template <typename T, std::size_t... I>
+struct tuple_type_start_impl<T, std::index_sequence<I...>> {
using type = std::tuple<typename std::tuple_element<I, T>::type...>;
};
-} //anonymous namespace
+} // detail namespace
/**
* Get the type of a tuple with just the first @len items.
*/
-template<typename T, std::size_t len>
+template <typename T, std::size_t len>
struct tuple_type_start
-: tuple_type_start_impl<T, std::make_index_sequence<len>>
-{};
-
+ : detail::tuple_type_start_impl<T, std::make_index_sequence<len>> {};
-namespace {
+namespace detail {
-template<typename T, typename Seq>
+template <typename T, typename Seq>
struct tuple_start_impl;
-template<typename T, std::size_t... I>
-struct tuple_start_impl<T, std::index_sequence<I...>>
-{
- static
- decltype(auto)
- tuple_start(const T& t) {
- return std::make_tuple(std::get<I>(t)...);
+template <typename T, std::size_t... I>
+struct tuple_start_impl<T, std::index_sequence<I...>> {
+ static decltype(auto)
+ tuple_start(T&& t) {
+ constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
+ constexpr auto len = sizeof...(I);
+ static_assert(len <= size, "The tuple size must be less than or equal to the length.");
+
+ using start = typename tuple_type_start<std::decay_t<T>, len>::type;
+ return start(std::get<I>(std::forward<T>(t))...);
}
};
-} //anonymous namespace
+} // detail namespace
/**
* Get the tuple with the last @a len items of the original.
*/
-template<std::size_t len, typename T>
-decltype(auto) //typename tuple_type_end<T, len>::type
-tuple_start(const T& t) {
- return tuple_start_impl<T, std::make_index_sequence<len>>::tuple_start(t);
+template <std::size_t len, typename T>
+decltype(auto) // typename tuple_type_end<T, len>::type
+ tuple_start(T&& t) {
+ //We use std::decay_t<> because tuple_size is not defined for references.
+ constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
+ static_assert(len <= size, "The tuple size must be less than or equal to the length.");
+
+ return detail::tuple_start_impl<T, std::make_index_sequence<len>>::tuple_start(
+ std::forward<T>(t));
}
-} //namespace sigc;
+} // namespace tupleutils;
-#endif //_SIGC_TUPLE_START_H_
+#endif //MURRAYC_TUPLE_UTILS_TUPLE_START_H
diff --git a/sigc++/tuple_transform_each.h b/sigc++/tuple_transform_each.h
index a55a485..7797a34 100644
--- a/sigc++/tuple_transform_each.h
+++ b/sigc++/tuple_transform_each.h
@@ -14,55 +14,56 @@
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
-#ifndef _SIGC_TUPLE_TRANSFORM_EACH_H_
-#define _SIGC_TUPLE_TRANSFORM_EACH_H_
+#ifndef MURRAYC_TUPLE_UTILS_TUPLE_TRANSFORM_EACH_H
+#define MURRAYC_TUPLE_UTILS_TUPLE_TRANSFORM_EACH_H
-#include <sigc++/tuple_cat.h>
-#include <sigc++/tuple_cdr.h>
-#include <sigc++/tuple_start.h>
-#include <sigc++/tuple_end.h>
+#include <tuple-utils/tuple_cat.h>
+#include <tuple-utils/tuple_cdr.h>
+#include <tuple-utils/tuple_end.h>
+#include <tuple-utils/tuple_start.h>
#include <type_traits>
-namespace sigc
-{
+namespace tupleutils {
-/*
-namespace {
+namespace detail {
-template<typename T, template<typename> class T_transformer, std::size_t index>
-struct tuple_type_transform_each_impl
-{
+template <typename T, template <typename> class T_transformer,
+ std::size_t index>
+struct tuple_type_transform_each_impl {
private:
using from_element_type = typename std::tuple_element<index, T>::type;
- using to_element_type =
- typename
std::result_of<decltype(&T_transformer<from_element_type>::transform)(from_element_type&)>::type;
+ using to_element_type = typename std::result_of<decltype (
+ &T_transformer<from_element_type>::transform)(from_element_type&)>::type;
using t_element_type = std::tuple<to_element_type>;
using t_type_start = typename tuple_type_start<T, index>::type;
- using t_type_end = typename tuple_type_end<T, std::tuple_size<T>::value - index - 1>::type;
+ using t_type_end =
+ typename tuple_type_end<T, std::tuple_size<T>::value - index - 1>::type;
- using t_type_with_transformed_element =
- typename tuple_type_cat<
- typename tuple_type_cat<t_type_start, t_element_type>::type,
- t_type_end>::type;
+ using t_type_with_transformed_element = typename tuple_type_cat<
+ typename tuple_type_cat<t_type_start, t_element_type>::type,
+ t_type_end>::type;
public:
- using type = typename tuple_type_transform_each_impl<t_type_with_transformed_element, T_transformer, index
- 1>::type;
+ using type =
+ typename tuple_type_transform_each_impl<t_type_with_transformed_element,
+ T_transformer, index - 1>::type;
};
-template<typename T, template<typename> class T_transformer>
-struct tuple_type_transform_each_impl<T, T_transformer, 0>
-{
+template <typename T, template <typename> class T_transformer>
+struct tuple_type_transform_each_impl<T, T_transformer, 0> {
private:
static constexpr std::size_t index = 0;
using from_element_type = typename std::tuple_element<index, T>::type;
- using to_element_type = typename
std::result_of<decltype(&T_transformer<from_element_type>::transform)(from_element_type&)>::type;
+ using to_element_type = typename std::result_of<decltype (
+ &T_transformer<from_element_type>::transform)(from_element_type&)>::type;
using t_element_type = std::tuple<to_element_type>;
- using t_type_end = typename tuple_type_end<T, std::tuple_size<T>::value - index - 1>::type;
+ using t_type_end =
+ typename tuple_type_end<T, std::tuple_size<std::decay_t<T>>::value - index - 1>::type;
using t_type_with_transformed_element =
typename tuple_type_cat<t_element_type, t_type_end>::type;
@@ -71,86 +72,84 @@ public:
using type = t_type_with_transformed_element;
};
-} //anonymous namespace
-*/
+} // detail namespace
/**
* Get a tuple with each element having the transformed value of the element
* in the original tuple.
*/
-/*
-template<typename T, template<typename> class T_transformer>
-struct tuple_type_transform_each
-{
- using type = typename tuple_type_transform_each_impl<T, T_transformer, std::tuple_size<T>::value -
1>::type;
+
+template <typename T, template <typename> class T_transformer>
+struct tuple_type_transform_each {
+ using type = typename detail::tuple_type_transform_each_impl<T, T_transformer,
+ std::tuple_size<T>::value - 1>::type;
};
-*/
-namespace {
+namespace detail {
-template<template<typename> class T_transformer, std::size_t index>
-struct tuple_transform_each_impl
-{
- //TODO: Avoid the need to pass t_original all the way into the recursion?
- template<typename T_current, typename T_original>
- static
- decltype(auto)
- tuple_transform_each(T_current& t, T_original& t_original) {
- using element_type = typename std::tuple_element<index, T_current>::type;
+template <template <typename> class T_transformer, std::size_t index>
+struct tuple_transform_each_impl {
+ // TODO: Avoid the need to pass t_original all the way into the recursion?
+ template <typename T_current, typename T_original>
+ static decltype(auto)
+ tuple_transform_each(T_current&& t, T_original& t_original) {
+ using element_type = typename std::tuple_element<index, std::decay_t<T_current>>::type;
auto& from = std::get<index>(t_original);
- const auto& element = T_transformer<element_type>::transform(from);
+ const auto element = T_transformer<element_type>::transform(from);
const auto t_element = std::make_tuple(element);
-
- const auto t_start = tuple_start<index>(t);
- constexpr auto size = std::tuple_size<T_current>::value;
+ const auto t_start = tuple_start<index>(std::forward<T_current>(t));
- //t_end's elements will be copies of the elements in t, so this method's
- //caller won't see the changes made in the subsequent call of
- //tuple_transform_each() on those copies. That's why we pass t_original
- //through too, so we can modify that directly.
- //the const version (tuple_transform_each_const()) doesn't have to worry
- //about this, though avoiding copying would be more efficient.
+ constexpr auto size = std::tuple_size<std::decay_t<T_current>>::value;
+
+ // t_end's elements will be copies of the elements in t, so this method's
+ // caller won't see the changes made in the subsequent call of
+ // tuple_transform_each() on those copies. That's why we pass t_original
+ // through too, so we can modify that directly.
+ // the const version (tuple_transform_each_const()) doesn't have to worry
+ // about this, though avoiding copying would be more efficient.
const auto t_end = tuple_end<size - index - 1>(t);
- auto t_with_transformed_element =
- std::tuple_cat(t_start, t_element, t_end);
- return tuple_transform_each_impl<T_transformer, index - 1>::tuple_transform_each(
- t_with_transformed_element, t_original);
+ auto t_with_transformed_element = std::tuple_cat(t_start, t_element, t_end);
+ return tuple_transform_each_impl<T_transformer,
+ index - 1>::tuple_transform_each(t_with_transformed_element, t_original);
}
};
-template<template<typename> class T_transformer>
-struct tuple_transform_each_impl<T_transformer, 0>
-{
- template<typename T_current, typename T_original>
- static
- decltype(auto)
- tuple_transform_each(T_current& t, T_original& t_original) {
+template <template <typename> class T_transformer>
+struct tuple_transform_each_impl<T_transformer, 0> {
+ template <typename T_current, typename T_original>
+ static decltype(auto)
+ tuple_transform_each(T_current&& t, T_original& t_original) {
constexpr std::size_t index = 0;
using element_type = typename std::tuple_element<index, T_original>::type;
- const auto& element = T_transformer<element_type>::transform(std::get<index>(t_original));
+ const auto element =
+ T_transformer<element_type>::transform(std::get<index>(t_original));
const auto tuple_element = std::make_tuple(element);
- const auto tuple_rest = tuple_cdr(t);
+ const auto tuple_rest = tuple_cdr(std::forward<T_current>(t));
return std::tuple_cat(tuple_element, tuple_rest);
}
};
-} //anonymous namespace
+} // detail namespace
/**
* Get a tuple with each element having the transformed value of the element
* in the original tuple.
*/
-template<template<typename> class T_transformer, typename T>
+template <template <typename> class T_transformer, typename T>
decltype(auto)
-tuple_transform_each(T& t) {
- constexpr auto size = std::tuple_size<std::remove_reference_t<T>>::value;
- return tuple_transform_each_impl<T_transformer, size - 1>::tuple_transform_each(t, t);
+tuple_transform_each(T&& t) {
+ //We use std::decay_t<> because tuple_size is not defined for references.
+ constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
+ static_assert(size > 0, "The tuple size must be more than zero.");
+
+ return detail::tuple_transform_each_impl<T_transformer,
+ size - 1>::tuple_transform_each(std::forward<T>(t), t);
}
-} //namespace sigc
+} // namespace tupleutils
-#endif //_SIGC_TUPLE_TRANSFORM_EACH_H_
+#endif //MURRAYC_TUPLE_UTILS_TUPLE_TRANSFORM_EACH_H
diff --git a/tests/test_tuple_cat.cc b/tests/test_tuple_cat.cc
index e5214ba..1b4e22a 100644
--- a/tests/test_tuple_cat.cc
+++ b/tests/test_tuple_cat.cc
@@ -14,19 +14,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
-<<<<<<< Updated upstream
-#include <sigc++/tuple_cat.h>
-=======
-#include <sigc/tuple_cat.h>
->>>>>>> Stashed changes
-#include <utility>
-#include <cstdlib>
#include <cassert>
+#include <cstdlib>
+#include <tuple-utils/tuple_cat.h>
+#include <utility>
+//#include <functional>
-void test_tuple_type_cat() {
+void
+test_tuple_type_cat() {
using type_tuple_is = std::tuple<int, short>;
using type_tuple_dc = std::tuple<double, char>;
- using type_tuple_cat = sigc::tuple_type_cat<type_tuple_is, type_tuple_dc>::type;
+ using type_tuple_cat =
+ tupleutils::tuple_type_cat<type_tuple_is, type_tuple_dc>::type;
using type_tuple_expected = std::tuple<int, short, double, char>;
static_assert(std::tuple_size<type_tuple_cat>::value == 4,
@@ -35,9 +34,38 @@ void test_tuple_type_cat() {
"unexpected tuple_cat()ed tuple type");
}
-int main() {
+/** We don't want to test std::tuple_cat() here,
+ * but this a demonstration that std::ref() works with std::tuple_cat().
+void
+test_tuple_cat_stdref() {
+ std::string a = "yadda1";
+ std::string b = "yaddayadda1";
+ auto t_one =
+ std::make_tuple(std::ref(a), std::ref(b));
+ int c = 2;
+ char d = 'a';
+ auto t_two =
+ std::make_tuple(std::ref(c), std::ref(d));
+ auto t_both = std::tuple_cat(t_one, t_two);
+ a = "hello";
+ b = "world";
+ c = 3;
+ d = 'b';
+
+ assert(std::get<0>(t_both) == "hello");
+ assert(std::get<1>(t_both) == "world");
+ assert(std::get<2>(t_both) == 3);
+ assert(std::get<3>(t_both) == 'b');
+}
+*/
+
+int
+main() {
test_tuple_type_cat();
- //There is no typeutils::tuple_cat() because std::tuple_cat() exists: test_tuple_cat();
+ // There is no typeutils::tuple_cat() because std::tuple_cat() exists:
+ // test_tuple_cat();
+
+ //test_tuple_cat_stdref();
return EXIT_SUCCESS;
}
diff --git a/tests/test_tuple_cdr.cc b/tests/test_tuple_cdr.cc
index 387155a..7b73876 100644
--- a/tests/test_tuple_cdr.cc
+++ b/tests/test_tuple_cdr.cc
@@ -1,13 +1,30 @@
-#include <sigc++/tuple_cdr.h>
-#include <utility>
-#include <cstdlib>
-#include <cassert>
+/* Copyright (C) 2016 Murray Cumming
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/
+ */
+#include <cassert>
+#include <cstdlib>
+#include <tuple-utils/tuple_cdr.h>
+#include <utility>
+#include <functional>
-void test_tuple_type_cdr() {
+void
+test_tuple_type_cdr() {
using type_tuple_isd = std::tuple<int, short, double>;
using type_tuple_sd = std::tuple<short, double>;
- using type_tuple_suffix = sigc::tuple_type_cdr<type_tuple_isd>::type;
+ using type_tuple_suffix = tupleutils::tuple_type_cdr<type_tuple_isd>::type;
static_assert(std::tuple_size<type_tuple_suffix>::value == 2,
"unexpected tuple_cdr()ed tuple size.");
@@ -15,10 +32,11 @@ void test_tuple_type_cdr() {
"unexpected tuple_cdr()ed tuple type");
}
-void test_tuple_cdr() {
- auto t_larger = std::make_tuple(nullptr, std::string("hello"),
- std::string("world"));
- auto t_suffix = sigc::tuple_cdr(t_larger);
+void
+test_tuple_cdr() {
+ auto t_larger =
+ std::make_tuple(nullptr, std::string("hello"), std::string("world"));
+ auto t_suffix = tupleutils::tuple_cdr(t_larger);
assert(std::get<0>(t_suffix) == "hello");
assert(std::get<1>(t_suffix) == "world");
@@ -30,9 +48,29 @@ void test_tuple_cdr() {
"unexpected cdr()ed tuple type");
}
-int main() {
+void
+test_tuple_cdr_stdref() {
+ std::string b = "yadda";
+ std::string c = "yaddayadda";
+ auto t_larger = std::make_tuple(1, std::ref(b), std::ref(c));
+
+ //std::cout << "debug: " << type(std::get<1>(t_larger)) << std::endl;
+
+ auto t_suffix = tupleutils::tuple_cdr(t_larger);
+ b = "hello";
+ c = "world";
+ //This works, but it's not what we are testing here:
+ //assert(std::get<1>(t_larger) == "hello");
+
+ assert(std::get<0>(t_suffix) == "hello");
+ assert(std::get<1>(t_suffix) == "world");
+}
+
+int
+main() {
test_tuple_type_cdr();
test_tuple_cdr();
+ test_tuple_cdr_stdref();
return EXIT_SUCCESS;
}
diff --git a/tests/test_tuple_end.cc b/tests/test_tuple_end.cc
index 411131d..f013f95 100644
--- a/tests/test_tuple_end.cc
+++ b/tests/test_tuple_end.cc
@@ -1,19 +1,29 @@
-// -*- c++ -*-
-/* Copyright 2016, The libsigc++ Development Team
- * Assigned to public domain. Use as you wish without restriction.
+/* Copyright (C) 2016 Murray Cumming
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/
*/
-
-#include <sigc++/tuple_end.h>
-#include <cstdlib>
#include <cassert>
-#include <iostream>
+#include <cstdlib>
+#include <tuple-utils/tuple_end.h>
+#include <functional>
-void test_tuple_type_end()
-{
+void
+test_tuple_type_end() {
{
using type_tuple = std::tuple<int, short, double>;
- using type_tuple_end = sigc::tuple_type_end<type_tuple, 1>::type;
+ using type_tuple_end = tupleutils::tuple_type_end<type_tuple, 1>::type;
using type_tuple_expected = std::tuple<double>;
static_assert(std::tuple_size<type_tuple_end>::value == 1,
@@ -24,7 +34,7 @@ void test_tuple_type_end()
{
using type_tuple = std::tuple<int, short, double>;
- using type_tuple_end = sigc::tuple_type_end<type_tuple, 2>::type;
+ using type_tuple_end = tupleutils::tuple_type_end<type_tuple, 2>::type;
using type_tuple_expected = std::tuple<short, double>;
static_assert(std::tuple_size<type_tuple_end>::value == 2,
@@ -35,22 +45,22 @@ void test_tuple_type_end()
{
using type_tuple = std::tuple<int, short, double>;
- using type_tuple_end = sigc::tuple_type_end<type_tuple, 3>::type;
+ using type_tuple_end = tupleutils::tuple_type_end<type_tuple, 3>::type;
using type_tuple_expected = std::tuple<int, short, double>;
static_assert(std::tuple_size<type_tuple_end>::value == 3,
"unexpected tuple_end()ed tuple size.");
static_assert(std::is_same<type_tuple_end, type_tuple_expected>::value,
- "unexpected type_tuple_end type");
+ "unexpected type_tuple_end type");
}
}
-void test_tuple_end()
-{
+void
+test_tuple_end() {
{
- auto t_original = std::make_tuple(nullptr, std::string("hello"),
- std::string("world"));
- auto t_suffix = sigc::tuple_end<3>(t_original);
+ auto t_original =
+ std::make_tuple(nullptr, std::string("hello"), std::string("world"));
+ auto t_suffix = tupleutils::tuple_end<3>(t_original);
static_assert(std::tuple_size<decltype(t_suffix)>::value == 3,
"unexpected tuple_end()ed tuple size.");
@@ -64,13 +74,13 @@ void test_tuple_end()
}
{
- auto t_original = std::make_tuple(nullptr, std::string("hello"),
- std::string("world"));
- auto t_suffix = sigc::tuple_end<2>(t_original);
+ auto t_original =
+ std::make_tuple(nullptr, std::string("hello"), std::string("world"));
+ auto t_suffix = tupleutils::tuple_end<2>(t_original);
static_assert(std::tuple_size<decltype(t_suffix)>::value == 2,
"unexpected tuple_end()ed tuple size.");
-
+
assert(std::get<0>(t_suffix) == "hello");
assert(std::get<1>(t_suffix) == "world");
@@ -80,9 +90,9 @@ void test_tuple_end()
}
{
- auto t_original = std::make_tuple(nullptr, std::string("hello"),
- std::string("world"));
- auto t_suffix = sigc::tuple_end<1>(t_original);
+ auto t_original =
+ std::make_tuple(nullptr, std::string("hello"), std::string("world"));
+ auto t_suffix = tupleutils::tuple_end<1>(t_original);
static_assert(std::tuple_size<decltype(t_suffix)>::value == 1,
"unexpected tuple_end()ed tuple size.");
@@ -95,10 +105,28 @@ void test_tuple_end()
}
}
-int main()
-{
+void
+test_tuple_end_stdref() {
+ std::string c = "yadda";
+ std::string d = "yaddayadda";
+ auto t_larger = std::make_tuple(1, 2, std::ref(c), std::ref(d));
+
+ auto t_suffix = tupleutils::tuple_end<2>(t_larger);
+ c = "hello";
+ d = "world";
+ //This works, but it's not what we are testing here:
+ //assert(std::get<0>(t_larger) == "hello");
+
+ assert(std::get<0>(t_suffix) == "hello");
+ assert(std::get<1>(t_suffix) == "world");
+}
+
+
+int
+main() {
test_tuple_type_end();
test_tuple_end();
-
+ test_tuple_end_stdref();
+
return EXIT_SUCCESS;
}
diff --git a/tests/test_tuple_for_each.cc b/tests/test_tuple_for_each.cc
index 67156ae..82ef268 100644
--- a/tests/test_tuple_for_each.cc
+++ b/tests/test_tuple_for_each.cc
@@ -14,167 +14,149 @@
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
-//#include <sigc++/tuple_for_each.h>
-#include <sigc++/tuple_for_each_const.h>
-#include <utility>
-#include <cstdlib>
#include <cassert>
+#include <cstdlib>
+#include <tuple-utils/tuple_for_each.h>
+#include <utility>
//#include <typeinfo>
#include <iostream>
template <class T_element_from>
-class for_each_simple
-{
+class for_each_simple {
public:
- static
- void
+ static void
visit(const T_element_from& from) {
std::cout << "for_each_simple(): " << std::to_string(from) << std::endl;
- }
+ }
};
-void test_tuple_for_each_same_types()
-{
+void
+test_tuple_for_each_same_types() {
{
auto t_original = std::make_tuple(1, 2, 3);
- sigc::tuple_for_each_const<for_each_simple>(t_original);
+ tupleutils::tuple_for_each<for_each_simple>(t_original);
}
{
auto t_original = std::make_tuple(1, (double)2.1f, 3);
- sigc::tuple_for_each_const<for_each_simple>(t_original);
+ tupleutils::tuple_for_each<for_each_simple>(t_original);
}
}
template <class T_element_from>
-class for_each_simple_with_extras
-{
+class for_each_simple_with_extras {
public:
- static
- void
+ static void
visit(const T_element_from& from, int extra1, const std::string& extra2) {
- std::cout << "for_each_simple_with_extras(): from=" <<
- std::to_string(from) <<
- ", extra1: " << extra1 <<
- ", extra2: " << extra2 << std::endl;
+ std::cout << "for_each_simple_with_extras(): from=" << std::to_string(from)
+ << ", extra1: " << extra1 << ", extra2: " << extra2 << std::endl;
}
};
-void test_tuple_for_each_same_types_with_extras()
-{
+void
+test_tuple_for_each_same_types_with_extras() {
{
auto t_original = std::make_tuple(1, (double)2.1f, 3);
- sigc::tuple_for_each_const<for_each_simple_with_extras>(t_original, 89, "eightynine");
+ tupleutils::tuple_for_each<for_each_simple_with_extras>(
+ t_original, 89, "eightynine");
}
}
template <class T_element_from>
-class for_each_simple_with_nonconst_extras
-{
+class for_each_simple_with_nonconst_extras {
public:
- static
- void
+ static void
visit(const T_element_from& from, int& extra) {
extra += (int)from;
}
};
-void test_tuple_for_each_same_types_with_nonconst_extras()
-{
+void
+test_tuple_for_each_same_types_with_nonconst_extras() {
{
auto t_original = std::make_tuple(1, (double)2.1f, 3);
int extra = 0;
- //TODO: avoid the need to specify the tuple type (decltype(t_original).
- // It can't be at the end (or can't it?) because we have T_extras... at the end.
- //TODO: avoid the need to specify that the int should be passed by reference?
- sigc::tuple_for_each_const<for_each_simple_with_nonconst_extras, decltype(t_original), int&>(t_original,
extra);
- //std::cout << "extra: " << extra << std::endl;
+ tupleutils::tuple_for_each<for_each_simple_with_nonconst_extras>(t_original, extra);
+ // std::cout << "extra: " << extra << std::endl;
assert(extra == 6);
}
}
-//The general template declaration.
-//We then provide specializations for each type,
-//so we can test having a different return value for each T_element_from type.
+// The general template declaration.
+// We then provide specializations for each type,
+// so we can test having a different return value for each T_element_from type.
template <class T_element_from>
class visitor_with_specializations;
-//An int will be converted to a std::string:
+// An int will be converted to a std::string:
template <>
-class visitor_with_specializations<int>
-{
+class visitor_with_specializations<int> {
public:
- static
- void
+ static void
visit(const int& from) {
- std::cout << "visitor_with_specializations::visit(): " << std::to_string(from) << std::endl;
- }
+ std::cout << "visitor_with_specializations::visit(): "
+ << std::to_string(from) << std::endl;
+ }
};
-//A double will be converted to a char:
+// A double will be converted to a char:
template <>
-class visitor_with_specializations<double>
-{
+class visitor_with_specializations<double> {
public:
- static
- void
+ static void
visit(const double& from) {
- std::cout << "visitor_with_specializations::visit(): " << std::to_string(from)[0] << std::endl;
- }
+ std::cout << "visitor_with_specializations::visit(): "
+ << std::to_string(from)[0] << std::endl;
+ }
};
-//A std::string will be converted to an int:
+// A std::string will be converted to an int:
template <>
-class visitor_with_specializations<std::string>
-{
+class visitor_with_specializations<std::string> {
public:
- static
- void
+ static void
visit(const std::string& from) {
- std::cout << "visitor_with_specializations::visit(): " << std::stoi(from) << std::endl;
- }
+ std::cout << "visitor_with_specializations::visit(): " << std::stoi(from)
+ << std::endl;
+ }
};
-void test_tuple_for_each_multiple_types()
-{
+void
+test_tuple_for_each_multiple_types() {
auto t_original = std::make_tuple(1, (double)2.1f, std::string("3"));
- sigc::tuple_for_each_const<visitor_with_specializations>(t_original);
+ tupleutils::tuple_for_each<visitor_with_specializations>(t_original);
}
-/*
template <class T_element_from>
-class for_each_nonconst
-{
+class for_each_nonconst {
public:
- static
- void
+ static void
visit(T_element_from& from) {
from *= 2;
- //Or, for instance, call a non-const method on from.
- }
+ // Or, for instance, call a non-const method on from.
+ }
};
-void test_tuple_for_each_nonconst()
-{
+void
+test_tuple_for_each_nonconst() {
auto t = std::make_tuple(1, 2, 3);
- sigc::tuple_for_each<for_each_nonconst, decltype(t)&>(t);
+ tupleutils::tuple_for_each<for_each_nonconst, decltype(t)&>(t);
std::cout << std::get<0>(t) << std::endl;
assert(std::get<0>(t) == 2);
assert(std::get<1>(t) == 4);
assert(std::get<2>(t) == 6);
}
-*/
-int main()
-{
+int
+main() {
test_tuple_for_each_same_types();
test_tuple_for_each_same_types_with_extras();
test_tuple_for_each_same_types_with_nonconst_extras();
test_tuple_for_each_multiple_types();
- //test_tuple_for_each_nonconst();
-
+ test_tuple_for_each_nonconst();
+
return EXIT_SUCCESS;
}
diff --git a/tests/test_tuple_start.cc b/tests/test_tuple_start.cc
index d7c16ed..8509d4b 100644
--- a/tests/test_tuple_start.cc
+++ b/tests/test_tuple_start.cc
@@ -1,49 +1,60 @@
-// -*- c++ -*-
-/* Copyright 2016, The libsigc++ Development Team
- * Assigned to public domain. Use as you wish without restriction.
+/* Copyright (C) 2016 Murray Cumming
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/
*/
-
-#include <sigc++/tuple_start.h>
-#include <cstdlib>
#include <cassert>
+#include <cstdlib>
+#include <tuple-utils/tuple_start.h>
+#include <functional>
-void test_tuple_type_start()
-{
+void
+test_tuple_type_start() {
{
using type_tuple = std::tuple<int, short, double>;
- using type_tuple_start = sigc::tuple_type_start<type_tuple, 1>::type;
+ using type_tuple_start = tupleutils::tuple_type_start<type_tuple, 1>::type;
using type_tuple_expected = std::tuple<int>;
static_assert(std::is_same<type_tuple_start, type_tuple_expected>::value,
- "unexpected type_tuple_start type");
+ "unexpected type_tuple_start type");
}
{
using type_tuple = std::tuple<int, short, double>;
- using type_tuple_start = sigc::tuple_type_start<type_tuple, 2>::type;
+ using type_tuple_start = tupleutils::tuple_type_start<type_tuple, 2>::type;
using type_tuple_expected = std::tuple<int, short>;
static_assert(std::is_same<type_tuple_start, type_tuple_expected>::value,
- "unexpected type_tuple_start type");
+ "unexpected type_tuple_start type");
}
{
using type_tuple = std::tuple<int, short, double>;
- using type_tuple_start = sigc::tuple_type_start<type_tuple, 3>::type;
+ using type_tuple_start = tupleutils::tuple_type_start<type_tuple, 3>::type;
using type_tuple_expected = std::tuple<int, short, double>;
static_assert(std::is_same<type_tuple_start, type_tuple_expected>::value,
- "unexpected type_tuple_start type");
+ "unexpected type_tuple_start type");
}
}
-void test_tuple_start()
-{
+void
+test_tuple_start() {
{
- auto t_original = std::make_tuple(nullptr, std::string("hello"),
- std::string("world"));
- auto t_prefix = sigc::tuple_start<3>(t_original);
+ auto t_original =
+ std::make_tuple(nullptr, std::string("hello"), std::string("world"));
+ auto t_prefix = tupleutils::tuple_start<3>(t_original);
static_assert(std::tuple_size<decltype(t_prefix)>::value == 3,
"unexpected tuple_start()ed tuple size.");
@@ -57,13 +68,13 @@ void test_tuple_start()
}
{
- auto t_original = std::make_tuple(nullptr, std::string("hello"),
- std::string("world"));
- auto t_prefix = sigc::tuple_start<2>(t_original);
+ auto t_original =
+ std::make_tuple(nullptr, std::string("hello"), std::string("world"));
+ auto t_prefix = tupleutils::tuple_start<2>(t_original);
static_assert(std::tuple_size<decltype(t_prefix)>::value == 2,
"unexpected tuple_start()ed tuple size.");
-
+
assert(std::get<0>(t_prefix) == nullptr);
assert(std::get<1>(t_prefix) == "hello");
@@ -73,9 +84,9 @@ void test_tuple_start()
}
{
- auto t_original = std::make_tuple(nullptr, std::string("hello"),
- std::string("world"));
- auto t_prefix = sigc::tuple_start<1>(t_original);
+ auto t_original =
+ std::make_tuple(nullptr, std::string("hello"), std::string("world"));
+ auto t_prefix = tupleutils::tuple_start<1>(t_original);
static_assert(std::tuple_size<decltype(t_prefix)>::value == 1,
"unexpected tuple_start()ed tuple size.");
@@ -88,8 +99,25 @@ void test_tuple_start()
}
}
-int main()
-{
+void
+test_tuple_start_stdref() {
+ std::string a = "yadda";
+ std::string b = "yaddayadda";
+ auto t_larger = std::make_tuple(std::ref(a), std::ref(b), 1);
+
+ auto t_prefix = tupleutils::tuple_start<2>(t_larger);
+ a = "hello";
+ b = "world";
+ //This works, but it's not what we are testing here:
+ //assert(std::get<0>(t_larger) == "hello");
+
+ assert(std::get<0>(t_prefix) == "hello");
+ assert(std::get<1>(t_prefix) == "world");
+}
+
+int
+main() {
test_tuple_type_start();
test_tuple_start();
+ test_tuple_start_stdref();
}
diff --git a/tests/test_tuple_transform_each.cc b/tests/test_tuple_transform_each.cc
index 5501f3a..fbe7989 100644
--- a/tests/test_tuple_transform_each.cc
+++ b/tests/test_tuple_transform_each.cc
@@ -14,43 +14,42 @@
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
-#include <sigc++/tuple_transform_each.h>
-#include <utility>
-#include <cstdlib>
#include <cassert>
-
+#include <cstdlib>
+#include <tuple-utils/tuple_transform_each.h>
+#include <utility>
template <class T_element_from>
-class transform_to_string
-{
+class transform_to_string {
public:
- static
- decltype(auto)
+ static decltype(auto)
transform(T_element_from& from) {
return std::to_string(from);
- }
+ }
};
-/*
-void test_tuple_type_transform_each_same_types()
-{
+void
+test_tuple_type_transform_each_same_types() {
using type_tuple_original = std::tuple<int, int>;
- using type_tuple_transformed = sigc::tuple_type_transform_each<
- type_tuple_original, transform_to_string>::type;
+ using type_tuple_transformed =
+ tupleutils::tuple_type_transform_each<type_tuple_original,
+ transform_to_string>::type;
using type_tuple_expected = std::tuple<std::string, std::string>;
- static_assert(std::is_same<type_tuple_transformed, type_tuple_expected>::value,
+ static_assert(
+ std::is_same<type_tuple_transformed, type_tuple_expected>::value,
"unexpected tuple_transform_each()ed tuple type");
}
-*/
-//In these tests, t_expected has elements all of the same type.
-void test_tuple_transform_each_same_types()
-{
+// In these tests, t_expected has elements all of the same type.
+void
+test_tuple_transform_each_same_types() {
{
auto t_original = std::make_tuple(1, 2, 3);
- auto t_transformed = sigc::tuple_transform_each<transform_to_string>(t_original);
- auto t_expected = std::make_tuple(std::string("1"), std::string("2"), std::string("3"));
+ auto t_transformed =
+ tupleutils::tuple_transform_each<transform_to_string>(t_original);
+ auto t_expected =
+ std::make_tuple(std::string("1"), std::string("2"), std::string("3"));
static_assert(std::tuple_size<decltype(t_transformed)>::value == 3,
"unexpected tuple_transform_each()ed tuple size.");
@@ -59,14 +58,17 @@ void test_tuple_transform_each_same_types()
assert(std::get<1>(t_transformed) == "2");
assert(std::get<2>(t_transformed) == "3");
- static_assert(std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
+ static_assert(
+ std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
"unexpected transform_each()ed tuple type");
}
{
auto t_original = std::make_tuple(1, (double)2.1f, 3);
- auto t_transformed = sigc::tuple_transform_each<transform_to_string>(t_original);
- auto t_expected = std::make_tuple(std::string("1"), std::string("2"), std::string("3"));
+ auto t_transformed =
+ tupleutils::tuple_transform_each<transform_to_string>(t_original);
+ auto t_expected =
+ std::make_tuple(std::string("1"), std::string("2"), std::string("3"));
static_assert(std::tuple_size<decltype(t_transformed)>::value == 3,
"unexpected tuple_transform_each()ed tuple size.");
@@ -75,71 +77,67 @@ void test_tuple_transform_each_same_types()
assert(std::get<1>(t_transformed).substr(0, 3) == "2.1");
assert(std::get<2>(t_transformed) == "3");
- static_assert(std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
+ static_assert(
+ std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
"unexpected transform_each()ed tuple type");
}
}
-//The general template declaration.
-//We then provide specializations for each type,
-//so we can test having a different return value for each T_element_from type.
+// The general template declaration.
+// We then provide specializations for each type,
+// so we can test having a different return value for each T_element_from type.
template <class T_element_from>
class transform_to_something;
-//An int will be converted to a std::string:
+// An int will be converted to a std::string:
template <>
-class transform_to_something<int>
-{
+class transform_to_something<int> {
public:
- static
- std::string
+ static std::string
transform(int& from) {
return std::to_string(from);
- }
+ }
};
-//A double will be converted to a char:
+// A double will be converted to a char:
template <>
-class transform_to_something<double>
-{
+class transform_to_something<double> {
public:
- static
- char
+ static char
transform(double& from) {
return std::to_string(from)[0];
- }
+ }
};
-//A std::string will be converted to an int:
+// A std::string will be converted to an int:
template <>
-class transform_to_something<std::string>
-{
+class transform_to_something<std::string> {
public:
- static
- int
+ static int
transform(std::string& from) {
return std::stoi(from);
- }
+ }
};
-/*
-void test_tuple_type_transform_each_multiple_types()
-{
+void
+test_tuple_type_transform_each_multiple_types() {
using type_tuple_original = std::tuple<int, double, std::string>;
- using type_tuple_transformed = sigc::tuple_type_transform_each<
- type_tuple_original, transform_to_something>::type;
+ using type_tuple_transformed =
+ tupleutils::tuple_type_transform_each<type_tuple_original,
+ transform_to_something>::type;
using type_tuple_expected = std::tuple<std::string, char, int>;
- static_assert(std::is_same<type_tuple_transformed, type_tuple_expected>::value,
+ static_assert(
+ std::is_same<type_tuple_transformed, type_tuple_expected>::value,
"unexpected tuple_transform_each()ed tuple type");
}
-*/
-//In these tests, t_expected has elements of different types.
-void test_tuple_transform_each_multiple_types()
-{
+// In these tests, t_expected has elements of different types.
+void
+test_tuple_transform_each_multiple_types() {
auto t_original = std::make_tuple(1, (double)2.1f, std::string("3"));
- auto t_transformed = sigc::tuple_transform_each<transform_to_something>(t_original);
+ auto t_transformed =
+ tupleutils::tuple_transform_each<transform_to_something>(t_original);
auto t_expected = std::make_tuple(std::string("1"), '2', 3);
static_assert(std::tuple_size<decltype(t_transformed)>::value == 3,
@@ -149,52 +147,49 @@ void test_tuple_transform_each_multiple_types()
assert(std::get<1>(t_transformed) == '2');
assert(std::get<2>(t_transformed) == 3);
- static_assert(std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
+ static_assert(
+ std::is_same<decltype(t_transformed), decltype(t_expected)>::value,
"unexpected transform_each()ed tuple type");
}
-
template <class T_element_from>
-class transform_each_nonconst
-{
+class transform_each_nonconst {
public:
- static
- int
+ static int
transform(T_element_from& from) {
from *= 2;
- //Or, for instance, call a non-const method on from.
+ // Or, for instance, call a non-const method on from.
return from * 10;
- }
+ }
};
-void test_tuple_transform_each_nonconst()
-{
+void
+test_tuple_transform_each_nonconst() {
auto t = std::make_tuple(1, 2, 3);
- auto t_transformed = sigc::tuple_transform_each<transform_each_nonconst>(t);
+ auto t_transformed =
+ tupleutils::tuple_transform_each<transform_each_nonconst>(t);
- //Check that t was changed (from * 2):
+ // Check that t was changed (from * 2):
assert(std::get<0>(t) == 2);
assert(std::get<1>(t) == 4);
assert(std::get<2>(t) == 6);
- //Check that t_transformed has the expected values ( from * 2 * 10):
+ // Check that t_transformed has the expected values ( from * 2 * 10):
assert(std::get<0>(t_transformed) == 20);
assert(std::get<1>(t_transformed) == 40);
assert(std::get<2>(t_transformed) == 60);
}
-
-int main()
-{
- //test_tuple_type_transform_each_same_types();
-
- //test_tuple_type_transform_each_multiple_types();
+int
+main() {
+ test_tuple_type_transform_each_same_types();
+ test_tuple_type_transform_each_multiple_types();
test_tuple_transform_each_same_types();
test_tuple_transform_each_multiple_types();
test_tuple_transform_each_nonconst();
-
+
return EXIT_SUCCESS;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]