rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
utility.h
Go to the documentation of this file.
1 /************************************************************************************
2 * *
3 * Copyright (c) 2014 Axel Menzel <info@axelmenzel.de> *
4 * *
5 * This file is part of RTTR (Run Time Type Reflection) *
6 * License: MIT License *
7 * *
8 * Permission is hereby granted, free of charge, to any person obtaining *
9 * a copy of this software and associated documentation files (the "Software"), *
10 * to deal in the Software without restriction, including without limitation *
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
12 * and/or sell copies of the Software, and to permit persons to whom the *
13 * Software is furnished to do so, subject to the following conditions: *
14 * *
15 * The above copyright notice and this permission notice shall be included in *
16 * all copies or substantial portions of the Software. *
17 * *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
24 * SOFTWARE. *
25 * *
26 *************************************************************************************/
27 
28 #ifndef __RTTR_UTILITY_H__
29 #define __RTTR_UTILITY_H__
30 
32 
33 namespace rttr
34 {
35 namespace detail
36 {
37 
40 // This will add the c++14 integer sequence to c++11
41 
42 template <class T, T... I> struct integer_sequence
43 {
44  template <T N> using append = integer_sequence<T, I..., N>;
45  static std::size_t size () { return sizeof...(I); }
46  using next = append<sizeof...(I)>;
47  using type = T;
48 };
49 
50 template <class T, T Index, std::size_t N>
51 struct sequence_generator
52 {
53  using type = typename sequence_generator<T, Index - 1, N - 1>::type::next;
54 };
55 
56 template <class T, T Index>
57 struct sequence_generator<T, Index, 0ul> { using type = integer_sequence<T>; };
58 
59 template <std::size_t... I>
60 using index_sequence = integer_sequence<std::size_t, I...>;
61 
62 template <class T, T N>
63 using make_integer_sequence = typename sequence_generator<T, N, N>::type;
64 
65 template <std::size_t N>
66 using make_index_sequence = make_integer_sequence<std::size_t, N>;
67 
68 template<class... T>
69 using index_sequence_for = make_index_sequence<sizeof...(T)>;
70 
74 
75 template<typename T>
76 struct remove_first_index_impl
77 {
78  using type = index_sequence<>;
79 };
80 
81 template<std::size_t First, std::size_t... I>
82 struct remove_first_index_impl<detail::index_sequence<First, I...>>
83 {
84  using type = detail::index_sequence<I...>;
85 };
86 
87 template<typename T>
88 using remove_first_index = typename remove_first_index_impl<T>::type;
89 
90 
92 
93 template<typename, typename>
94 struct concat_index_sequence { };
95 
96 template<std::size_t... Ts, std::size_t... Us>
97 struct concat_index_sequence<index_sequence<Ts...>, index_sequence<Us...>>
98 {
99  using type = index_sequence<Ts..., Us...>;
100 };
101 
102 template <class T>
103 struct remove_last_index_impl;
104 
105 template <size_t Last>
106 struct remove_last_index_impl<index_sequence<Last>>
107 {
108  using type = index_sequence<>;
109 };
110 
111 template<std::size_t First, std::size_t... I>
112 struct remove_last_index_impl<index_sequence<First, I...>>
113 {
114  using type = typename concat_index_sequence<
115  index_sequence<First>,
116  typename remove_last_index_impl<index_sequence<I...>>::type
117  >::type;
118 };
119 
120 template<typename T>
121 using remove_last_index = typename remove_last_index_impl<T>::type;
122 
123 
124 
127 // This will add the c++14 integer sequence to c++11
128 
129 
130 static RTTR_FORCE_INLINE bool check_all_true() { return true; }
131 
132 template<typename... BoolArgs>
133 static RTTR_INLINE bool check_all_true(bool arg1, BoolArgs... args) { return arg1 & check_all_true(args...); }
134 
137 // copy the content of any arbitrary array, use it like:
138 // copy_array(in, out);
139 // works with every dimension
140 
141 template<typename ElementType>
142 struct copy_array_helper_impl
143 {
144  void operator()(const ElementType &in, ElementType &out)
145  {
146  out = in;
147  }
148 };
149 
150 template<typename ElementType, std::size_t Count>
151 struct copy_array_helper_impl<ElementType[Count]> {
152  void operator()(const ElementType (&in)[Count], ElementType (&out)[Count])
153  {
154  for(std::size_t i = 0; i < Count; ++i)
155  copy_array_helper_impl<ElementType>()(in[i], out[i]);
156  }
157 };
158 
159 template<typename ElementType, std::size_t Count>
160 auto copy_array(const ElementType (&in)[Count], ElementType (&out)[Count])
161  -> ElementType (&)[Count]
162 {
163  copy_array_helper_impl<ElementType[Count]>()(in, out);
164  return out;
165 }
166 
167 } // end namespace detail
168 } // end namespace rttr
169 
170 #endif //__RTTR_UTILITY_H__
#define RTTR_INLINE
Definition: core_prerequisites.h:90
#define RTTR_FORCE_INLINE
Definition: core_prerequisites.h:91