-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
#include "Tuple.h" | ||
|
||
/// note: for this header you need fmt library (not included as library dependency) | ||
#include <fmt/format.h> | ||
|
||
namespace tuple19 { | ||
|
||
/// use values of the tuple as format arguments | ||
/// note: you still have to craft the proper format string! | ||
/// usage: vformat(formatStr, tuple19::make_format_args(argsTuple)); | ||
template<class... Ts> constexpr auto make_format_args(Tuple<Ts...> const& args) { | ||
return [&]<size_t... Is>(std::index_sequence<Is...> const&) { | ||
return fmt::make_format_args(args.template at<Is>()...); | ||
} | ||
(std::make_index_sequence<sizeof...(Ts)>{}); | ||
} | ||
|
||
} // namespace tuple19 | ||
|
||
template<class... Ts, class Char> struct fmt::formatter<tuple19::Tuple<Ts...>, Char> { | ||
constexpr auto parse(fmt::basic_format_parse_context<Char>& ctx) { return ctx.begin(); } | ||
|
||
template<typename FormatContext> auto format(tuple19::Tuple<Ts...> const& v, FormatContext& ctx) { | ||
if constexpr (sizeof...(Ts) == 0) { | ||
return fmt::format_to(ctx.out(), "Tuple<>"); | ||
} | ||
else { | ||
[&]<size_t... Is>(std::index_sequence<0, Is...> const&) { | ||
fmt::format_to(ctx.out(), "{}", v.template at<0>()); | ||
(fmt::format_to(ctx.out(), ", {}", v.template at<Is>()), ...); | ||
} | ||
(std::make_index_sequence<sizeof...(Ts)>{}); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
#include "Tuple.h" | ||
|
||
namespace tuple19 { | ||
|
||
/// returns a tuple with all values of a and b | ||
/// note: types don't have to be uniqe | ||
template<class... As, class... Bs> | ||
auto joinTuples(Tuple<As...> const& a, Tuple<Bs...> const& b) -> Tuple<As..., Bs...> { | ||
return [&]<std::size_t... AIs, size_t... BIs>( | ||
std::index_sequence<AIs...> const&, | ||
std::index_sequence<BIs...> const&) | ||
->Tuple<As..., Bs...> { | ||
return {a.template at<AIs>()..., b.template at<BIs>()...}; | ||
} | ||
(std::make_index_sequence<sizeof...(As)>{}, std::make_index_sequence<sizeof...(Bs)>{}); | ||
} | ||
|
||
template<class... As, class... Bs> auto joinTuples(Tuple<As...>&& a, Tuple<Bs...>&& b) -> Tuple<As..., Bs...> { | ||
return [&]<std::size_t... AIs, size_t... BIs>( | ||
std::index_sequence<AIs...> const&, | ||
std::index_sequence<BIs...> const&) | ||
->Tuple<As..., Bs...> { | ||
return {std::move(a.template amendAt<AIs>())..., std::move(b.template amendAt<BIs>())...}; | ||
} | ||
(std::make_index_sequence<sizeof...(As)>{}, std::make_index_sequence<sizeof...(Bs)>{}); | ||
} | ||
|
||
} // namespace tuple19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "Tuple.h" | ||
|
||
#include "Tuple.join.h" | ||
#include "Tuple.ostream.h" | ||
|
||
#include <gtest/gtest.h> | ||
#include <string> | ||
|
||
using namespace tuple19; | ||
|
||
TEST(TupleJoin, constants) { | ||
using meta19::_index; | ||
using meta19::type; | ||
|
||
using A = Tuple<char, int, double>; | ||
auto a = A{'x', 23, 4.2}; | ||
|
||
using B = Tuple<std::string, float, int>; | ||
auto b = B{"!", 4.2f, 42}; | ||
|
||
using C = Tuple<char, int, double, std::string, float, int>; | ||
auto c = C{'x', 23, 4.2, "!", 4.2f, 42}; | ||
|
||
EXPECT_EQ(joinTuples(a, b), c); | ||
} | ||
|
||
TEST(TupleJoin, move) { | ||
// | ||
EXPECT_EQ(joinTuples(Tuple{'a', 12}, Tuple{4.2, 4.2f, 23}), (Tuple{'a', 12, 4.2, 4.2f, 23})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ Application { | |
|
||
files: [ | ||
"Tuple.bind.test.cpp", | ||
"Tuple.join.test.cpp", | ||
"Tuple.test.cpp", | ||
] | ||
} |