tuple: add tuple iteration helpers

This commit is contained in:
Danny Robson 2015-04-20 17:16:18 +10:00
parent 29af32106b
commit fa8387252e
4 changed files with 160 additions and 0 deletions

View File

@ -176,6 +176,8 @@ UTIL_FILES = \
tap.ipp \ tap.ipp \
time.cpp \ time.cpp \
time.hpp \ time.hpp \
tuple.cpp \
tuple.hpp \
types.hpp \ types.hpp \
types/bits.hpp \ types/bits.hpp \
types/casts.hpp \ types/casts.hpp \
@ -284,6 +286,7 @@ TEST_BIN = \
test/sha2 \ test/sha2 \
test/signal \ test/signal \
test/stringid \ test/stringid \
test/tuple \
test/uri \ test/uri \
test/vector \ test/vector \
test/version test/version

38
test/tuple.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "tuple.hpp"
#include "tap.hpp"
#include <typeindex>
int
main ()
{
util::TAP::logger tap;
{
auto tuple = std::make_tuple (1,2,3,4);
std::vector<int> expected {{ 1, 2, 3, 4 }};
std::vector<int> actual;
util::for_each ([&actual] (auto i) { actual.push_back (i); }, tuple);
tap.expect_eq (actual, expected, "value iteration");
}
{
auto tuple = std::make_tuple (1u, 1, 1.f, 1.);
std::vector<std::type_index> expected {
std::type_index (typeid (1u)),
std::type_index (typeid (1)),
std::type_index (typeid (1.f)),
std::type_index (typeid (1.))
};
std::vector<std::type_index> actual;
util::for_type<decltype(tuple)> ([&actual] (auto i) {
actual.push_back (typeid (typename decltype(i)::type));
});
tap.expect_eq (actual, expected, "type iteration");
}
}

19
tuple.cpp Normal file
View File

@ -0,0 +1,19 @@
/*
* 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.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "tuple.hpp"
// Make sure _someone_ includes the header for syntax checking

100
tuple.hpp Normal file
View File

@ -0,0 +1,100 @@
/*
* 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.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_TUPLE_HPP
#define __UTIL_TUPLE_HPP
#include "types.hpp"
#include <tuple>
#include <type_traits>
namespace util {
///////////////////////////////////////////////////////////////////////////
/// call a provided object with type_tag<T> for each type in a tuple
template <
typename T,
typename F,
size_t S = 0
>
typename std::enable_if<
S == std::tuple_size<T>::value,
void
>::type
for_type (F)
{ }
//-------------------------------------------------------------------------
template<
typename T,
typename F,
size_t S = 0
>
typename std::enable_if<
S < std::tuple_size<T>::value,
void
>::type
for_type (F f)
{
using E = typename std::tuple_element<S,T>::type;
f (type_tag<E> {});
for_type<T,F,S+1> (f);
}
//-------------------------------------------------------------------------
template <typename T, typename F>
auto for_type (F f, T t)
{ return for_type<decltype(t)> (f); }
///////////////////////////////////////////////////////////////////////////
/// call a provided object with each value in a tuple
template <
size_t S = 0,
typename F,
typename ...T
>
typename std::enable_if<
S == sizeof...(T),
void
>::type
for_each (F, const std::tuple<T...>&)
{ }
//-------------------------------------------------------------------------
template<
size_t S = 0,
typename F,
typename ...T
>
typename std::enable_if<
S < sizeof...(T),
void
>::type
for_each (F f, const std::tuple<T...> &t)
{
f (std::get<S> (t));
for_each<S+1,F,T...> (f, t);
}
}
#endif