stlab 2.3.0
Modern, modular C++ algorithms, data structures, and concurrency primitives
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1/*
2 Copyright 2017 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6
7/**************************************************************************************************/
8
9#ifndef STLAB_UTILITY_HPP
10#define STLAB_UTILITY_HPP
11
15
16/**************************************************************************************************/
17
18#include <stlab/config.hpp>
19
20#include <array>
21#include <cstddef> // for size_t
22#include <initializer_list> // for initializer_list
23#include <type_traits> // for remove_reference, is_same, is_const
24#include <utility> // for index_sequence, forward
25
26/**************************************************************************************************/
27
28namespace stlab {
29STLAB_VERSION_NAMESPACE_BEGIN()
30
31
35
36/**************************************************************************************************/
37
38namespace detail {
39
40/**************************************************************************************************/
41
42template <bool, class T>
43struct move_if_helper;
44
45template <class T>
46struct move_if_helper<true, T> {
47 using type = std::remove_reference_t<T>&&;
48};
49
50template <class T>
51struct move_if_helper<false, T> {
52 using type = std::remove_reference_t<T>&;
53};
54
55template <bool P, class T>
56using move_if_helper_t = typename move_if_helper<P, T>::type;
57
58/**************************************************************************************************/
59
60} // namespace detail
61
62/**************************************************************************************************/
63
64template <class Seq1, class Seq2>
66
67template <std::size_t... N1, std::size_t... N2>
68struct index_sequence_cat<std::index_sequence<N1...>, std::index_sequence<N2...>> {
69 using type = std::index_sequence<N1..., N2...>;
70};
71
72template <class Seq1, class Seq2>
73using index_sequence_cat_t = typename index_sequence_cat<Seq1, Seq2>::type;
74
75/**************************************************************************************************/
76
77template <class Seq>
79
80template <std::size_t... N>
81struct index_sequence_to_array<std::index_sequence<N...>> {
82 static constexpr std::array<std::size_t, sizeof...(N)> value{{N...}};
83};
84
85/**************************************************************************************************/
86
87template <class Seq, template <std::size_t> class F, std::size_t Index, std::size_t Count>
89
90template <class Seq,
91 template <std::size_t> class F,
92 std::size_t Index = 0,
93 std::size_t Count = Seq::size()>
94using index_sequence_transform_t = typename index_sequence_transform<Seq, F, Index, Count>::type;
95
96template <class Seq, template <std::size_t> class F, std::size_t Index, std::size_t Count>
98 using type = index_sequence_cat_t<
99 index_sequence_transform_t<Seq, F, Index, Count / 2>,
100 index_sequence_transform_t<Seq, F, Index + Count / 2, Count - Count / 2>>;
101};
102
103template <class Seq, template <std::size_t> class F, std::size_t Index>
104struct index_sequence_transform<Seq, F, Index, 0> {
105 using type = std::index_sequence<>;
106};
107
108template <class Seq, template <std::size_t> class F, std::size_t Index>
109struct index_sequence_transform<Seq, F, Index, 1> {
110 using type = typename F<index_sequence_to_array<Seq>::value[Index]>::type;
111};
112
113/**************************************************************************************************/
114
116template <bool P, class T>
117constexpr auto move_if(T&& t) noexcept -> detail::move_if_helper_t<P, T> {
118 return static_cast<detail::move_if_helper_t<P, T>>(t);
119}
120
121/**************************************************************************************************/
122
126template <class F, class... Args>
127void for_each_argument(F&& f, Args&&... args) {
128 return (void)std::initializer_list<int>{(std::forward<F>(f)(args), 0)...};
129}
130
131/**************************************************************************************************/
132
143template <typename T>
144constexpr auto copy(T&& value) noexcept(noexcept(std::decay_t<T>{static_cast<T&&>(value)}))
145 -> std::decay_t<T> {
146 static_assert(!std::is_same_v<std::decay_t<T>, T>, "explicit copy of rvalue.");
147 return std::decay_t<T>{static_cast<T&&>(value)};
148}
149
150/**************************************************************************************************/
151
153template <class T>
154constexpr auto move(T&& t) noexcept -> std::remove_reference_t<T>&& {
155 static_assert(!std::is_const_v<std::remove_reference_t<T>>,
156 "move of const type will unintentionally decay to copy");
157 return static_cast<std::remove_reference_t<T>&&>(t);
158}
159
160/**************************************************************************************************/
161
163
164STLAB_VERSION_NAMESPACE_END()
165} // namespace stlab
166
167/**************************************************************************************************/
168
169#endif
170
171/**************************************************************************************************/
constexpr auto copy(T &&value) noexcept(noexcept(std::decay_t< T >{static_cast< T && >(value)})) -> std::decay_t< T >
Returns a copy of the argument. Used to pass an lvalue to a function taking an rvalue,...
Definition utility.hpp:144
constexpr auto move_if(T &&t) noexcept -> detail::move_if_helper_t< P, T >
If P is true, casts t to an rvalue; otherwise keeps an lvalue reference.
Definition utility.hpp:117
constexpr auto move(T &&t) noexcept -> std::remove_reference_t< T > &&
A standard move implementation but with a compile-time check for const types.
Definition utility.hpp:154
void for_each_argument(F &&f, Args &&... args)
Invokes f(arg0), f(arg1), … f(argN).
Definition utility.hpp:127
Definition reverse.hpp:28
Definition utility.hpp:65
Definition utility.hpp:78
Definition utility.hpp:97