1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef DOM_CANVAS_TUPLE_UTILS_H
6 #define DOM_CANVAS_TUPLE_UTILS_H
16 constexpr const auto& ToConstRef1(T
& ret
) {
20 template <class Tup
, size_t... Ids
>
21 constexpr auto ToTupleOfConstRefsN(const Tup
& tup
,
22 const std::index_sequence
<Ids
...>&) {
23 return std::tie(ToConstRef1(std::get
<Ids
>(tup
))...);
26 template <class... Args
, size_t... Ids
, class Tup
= std::tuple
<Args
...>>
27 constexpr auto ToTupleOfConstRefs(const Tup
& tup
) {
28 return ToTupleOfConstRefsN(
29 tup
, std::make_index_sequence
<std::tuple_size_v
<Tup
>>());
35 template <class Tup
, class Callable
, size_t... Ids
>
36 constexpr auto MapTupleN(Tup
&& tup
, Callable
&& fn
,
37 const std::index_sequence
<Ids
...>&) {
38 return std::make_tuple(fn(std::get
<Ids
>(tup
))...);
41 /// Callable::operator()(T) cannot return void or you will get weird errors.
42 template <class... Args
, class Callable
, class Tup
= std::tuple
<Args
...>>
43 constexpr auto MapTuple(Tup
&& t
, Callable
&& fn
) {
44 using Tup2
= typename
std::remove_reference
<Tup
>::type
;
45 return MapTupleN(t
, fn
, std::make_index_sequence
<std::tuple_size_v
<Tup2
>>());
51 struct SizeofTupleArgs
;
53 // c++17 fold expressions make this easy, once we pull out the Args parameter
54 // pack by constraining the default template:
55 template <class... Args
>
56 struct SizeofTupleArgs
<std::tuple
<Args
...>>
57 : std::integral_constant
<size_t, (... + sizeof(Args
))> {};
59 static_assert(SizeofTupleArgs
<std::tuple
<size_t, char, char, char>>::value
==
62 } // namespace mozilla
64 #endif // DOM_CANVAS_TUPLE_UTILS_H