utils.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by Horatiu George Todoran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
33 #ifndef UTILS_H
34 #define UTILS_H
35 
36 #include <memory>
37 #include <math.h>
38 #include <tuple>
39 #include <utility>
40 #include <functional>
41 
42 namespace tuw
43 {
44 template <typename T>
45 inline constexpr int signum(T x, std::false_type is_signed)
46 {
47  return T(0) < x;
48 }
49 
50 template <typename T>
51 inline constexpr int signum(T x, std::true_type is_signed)
52 {
53  return (T(0) < x) - (x < T(0));
54 }
55 
56 template <typename T>
57 inline constexpr int signum(T x)
58 {
59  return signum(x, std::is_signed<T>());
60 }
61 
62 #if __cplusplus <= 201103L
63 template <typename T>
64 inline T normalizeRad(T _x)
65 {
66  while (_x > +M_PI)
67  _x -= (2 * M_PI);
68  while (_x < -M_PI)
69  _x += (2 * M_PI);
70  return _x;
71 }
72 #else
73 template <typename T>
74 inline constexpr T normalizeRad(T _x)
75 {
76  _x = fmod(_x, 2 * M_PI);
77  _x = fmod(_x + 2 * M_PI, 2 * M_PI);
78  if (_x > M_PI)
79  {
80  _x -= (2 * M_PI);
81  }
82  return _x;
83 }
84 #endif
85 
86 template <typename Enumeration>
87 constexpr auto asInt(Enumeration const value) -> typename std::underlying_type<Enumeration>::type
88 {
89  return static_cast<typename std::underlying_type<Enumeration>::type>(value);
90 }
91 
92 template <std::intmax_t Num, std::intmax_t Denom = 1>
93 struct RatioEval
94 {
95  static constexpr const std::intmax_t num = Num;
96  static constexpr const std::intmax_t denom = Denom;
97  static constexpr const double val = (Num) / (const double)(Denom);
98 };
99 
100 template <std::size_t II = 0, class FuncT, typename... Tp>
101 constexpr inline typename std::enable_if<II == sizeof...(Tp), void>::type for_each_tuple(std::tuple<Tp...>&, FuncT)
102 {
103 }
104 
105 template <std::size_t II = 0, class FuncT, typename... Tp>
106  constexpr inline typename std::enable_if <
107  II<sizeof...(Tp), void>::type for_each_tuple(std::tuple<Tp...>& t, FuncT f)
108 {
109  f(std::get<II>(t));
110  for_each_tuple<II + 1, FuncT, Tp...>(t, f);
111 }
112 template <std::size_t II = 0, class FuncT, typename... Tp>
113 constexpr inline typename std::enable_if<II == sizeof...(Tp), void>::type for_each_tuple(const std::tuple<Tp...>&,
114  FuncT)
115 {
116 }
117 
118 template <std::size_t II = 0, class FuncT, typename... Tp>
119  constexpr inline typename std::enable_if <
120  II<sizeof...(Tp), void>::type for_each_tuple(const std::tuple<Tp...>& t, FuncT f)
121 {
122  f(std::get<II>(t));
123  for_each_tuple<II + 1, FuncT, Tp...>(t, f);
124 }
125 
126 template <std::size_t II = 0, class FuncT, typename... Tp>
127 constexpr inline typename std::enable_if<II == sizeof...(Tp), void>::type for_each_2_tuples(const std::tuple<Tp...>&,
128  std::tuple<Tp...>&, FuncT)
129 {
130 }
131 
132 template <std::size_t II = 0, class FuncT, typename... Tp>
133  constexpr inline typename std::enable_if <
134  II<sizeof...(Tp), void>::type for_each_2_tuples(const std::tuple<Tp...>& t1, std::tuple<Tp...>& t2, FuncT f)
135 {
136  f(std::get<II>(t1), std::get<II>(t2));
137  for_each_2_tuples<II + 1, FuncT, Tp...>(t1, t2, f);
138 }
139 
140 template <std::size_t II = 0, typename ArrayType, typename... Tp>
141 inline typename std::enable_if<II == sizeof...(Tp), void>::type store_tuple_ptr_to_array(std::tuple<Tp...>&, ArrayType&)
142 {
143 }
144 
145 template <std::size_t II = 0, typename ArrayType, typename... Tp>
146  inline typename std::enable_if <
147  II<sizeof...(Tp), void>::type store_tuple_ptr_to_array(std::tuple<Tp...>& t, ArrayType& _array)
148 {
149  _array[II] = &std::get<II>(t);
150  for_each_tuple<II + 1, ArrayType, Tp...>(t, _array);
151 }
152 
153 template <class T, class Tuple>
154 struct Get_Tuple_Index;
155 template <class T, class... Types>
156 struct Get_Tuple_Index<T, std::tuple<T, Types...>>
157 {
158  static const std::size_t value = 0;
159 };
160 template <class T, class U, class... Types>
161 struct Get_Tuple_Index<T, std::tuple<U, Types...>>
162 {
163  static const std::size_t value = 1 + Get_Tuple_Index<T, std::tuple<Types...>>::value;
164 };
165 };
166 
167 // namespace std {
168 //
169 // // helper class
170 // template<typename R, template<typename...> class Params, typename... Args, std::size_t... I>
171 // R call_helper(std::function<R(Args...)> const&func, Params<Args...> const&params, std::index_sequence<I...>)
172 // { return func(std::get<I>(params)...); }
173 //
174 // // "return func(params...)"
175 // template<typename R, template<typename...> class Params, typename... Args>
176 // R call(std::function<R(Args...)> const&func, Params<Args...> const&params)
177 // { return call_helper(func,params,std::index_sequence_for<Args...>{}); }
178 //
179 //
180 // template <typename ...Args>
181 // struct save_it_for_later
182 // {
183 // std::tuple<Args...> params;
184 // std::function<void(Args...)> func;
185 // void delayed_dispatch() { std::call(func,params); }
186 // };
187 //
188 //
189 // }
190 
191 #if __cplusplus <= 201103L
192 namespace std
194 {
195 template <typename T, typename... Args>
196 std::unique_ptr<T> make_unique(Args&&... args)
197 {
198  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
199 }
200 namespace
201 {
202 template <class T, std::size_t N, class... Args>
203 struct get_number_of_element_from_tuple_by_type_impl
204 {
205  static constexpr auto value = N;
206 };
207 
208 template <class T, std::size_t N, class... Args>
209 struct get_number_of_element_from_tuple_by_type_impl<T, N, T, Args...>
210 {
211  static constexpr auto value = N;
212 };
213 
214 template <class T, std::size_t N, class U, class... Args>
215 struct get_number_of_element_from_tuple_by_type_impl<T, N, U, Args...>
216 {
217  static constexpr auto value = get_number_of_element_from_tuple_by_type_impl<T, N + 1, Args...>::value;
218 };
219 };
220 
221 template <class T, class... Args>
222 T& get(std::tuple<Args...>& t)
223 {
224  return std::get<get_number_of_element_from_tuple_by_type_impl<T, 0, Args...>::value>(t);
225 }
226 };
227 #endif // __cplusplus <= 201103L
228 #endif // UTILS_H
static constexpr const double val
Definition: utils.h:97
Helper function needed to upgrade c++ 2011.
Definition: utils.h:193
constexpr auto asInt(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
Definition: utils.h:87
std::unique_ptr< T > make_unique(Args &&...args)
Definition: utils.h:196
constexpr std::enable_if< II==sizeof...(Tp), void >::type for_each_tuple(std::tuple< Tp... > &, FuncT)
Definition: utils.h:101
T & get(std::tuple< Args... > &t)
Definition: utils.h:222
T normalizeRad(T _x)
Definition: utils.h:64
static constexpr auto value
Definition: utils.h:205
constexpr int signum(T x, std::false_type is_signed)
Definition: utils.h:45
static constexpr const std::intmax_t denom
Definition: utils.h:96
static constexpr const std::intmax_t num
Definition: utils.h:95


tuw_control
Author(s): George Todoran
autogenerated on Mon Jun 10 2019 15:27:22