rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
array_mapper.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_ARRAY_MAPPER_H__
29 #define __RTTR_ARRAY_MAPPER_H__
30 
32 
33 #include <cstddef>
34 #include <vector>
35 #include <list>
36 #include <array>
37 
38 namespace rttr
39 {
40 namespace detail
41 {
43 
46 template <typename T>
47 struct array_mapper
48 {
49  using no_array_type = int;
50  using raw_type = T;
51  using sub_type = T;
52 };
53 
55 
56 template <typename T, std::size_t N>
57 struct array_mapper<T[N]>
58 {
59  using raw_type = typename array_mapper<T>::raw_type;
60  using sub_type = T;
61 
62  static bool is_dynamic()
63  {
64  return false;
65  }
66 
67  static std::size_t get_size(const T (&)[N])
68  {
69  return N;
70  }
71 
72  static bool set_size(T (&)[N], std::size_t)
73  {
74  return false;
75  }
76 
77  static const T& get_value(const T (& arr)[N], std::size_t index)
78  {
79  return arr[index];
80  }
81 
82  static T& get_value(T (& arr)[N], std::size_t index)
83  {
84  return arr[index];
85  }
86 
87  static bool insert_value(T (&)[N], const T&, std::size_t)
88  {
89  return false;
90  }
91 
92  static bool remove_value(T (&)[N], std::size_t)
93  {
94  return false;
95  }
96 
97 };
98 
100 
101 template <typename T, std::size_t N>
102 struct array_mapper<std::array<T, N> >
103 {
104  using raw_type = typename array_mapper<T>::raw_type;
105  using sub_type = T;
106 
107  static bool is_dynamic()
108  {
109  return false;
110  }
111 
112  static std::size_t get_size(const std::array<T, N>&)
113  {
114  return N;
115  }
116 
117  static bool set_size(std::array<T, N>&, std::size_t)
118  {
119  return false;
120  }
121 
122  static const T& get_value(const std::array<T, N>& arr, std::size_t index)
123  {
124  return arr[index];
125  }
126 
127  static T& get_value(std::array<T, N>& arr, std::size_t index)
128  {
129  return arr[index];
130  }
131 
132  static bool insert_value(std::array<T, N>&, const T&, std::size_t)
133  {
134  return false;
135  }
136 
137  static bool remove_value(std::array<T, N>&, std::size_t)
138  {
139  return false;
140  }
141 };
142 
144 
145  template <typename T>
146  struct array_mapper<std::vector<T> >
147  {
148  using raw_type = typename array_mapper<T>::raw_type;
149  using sub_type = T;
150 
151  static bool is_dynamic()
152  {
153  return true;
154  }
155 
156  static std::size_t get_size(const std::vector<T>& arr)
157  {
158  return arr.size();
159  }
160 
161  static bool set_size(std::vector<T>& arr, std::size_t new_size)
162  {
163  arr.resize(new_size);
164  return true;
165  }
166 
167  static const T& get_value(const std::vector<T>& arr, std::size_t index)
168  {
169  return arr[index];
170  }
171 
172  static T& get_value(std::vector<T>& arr, std::size_t index)
173  {
174  return arr[index];
175  }
176 
177  static bool insert_value(std::vector<T>& arr, const T& value, std::size_t index)
178  {
179  arr.insert(arr.begin() + index, value);
180  return true;
181  }
182 
183  static bool remove_value(std::vector<T>& arr, std::size_t index)
184  {
185  arr.erase(arr.begin() + index);
186  return true;
187  }
188 };
189 
191 
192  template <typename T>
193  struct array_mapper<std::list<T> >
194  {
195  using raw_type = typename array_mapper<T>::raw_type;
196  using sub_type = T;
197 
198  static bool is_dynamic()
199  {
200  return true;
201  }
202 
203  static std::size_t get_size(const std::list<T>& arr)
204  {
205  return arr.size();
206  }
207 
208  static bool set_size(std::list<T>& arr, std::size_t new_size)
209  {
210  arr.resize(new_size);
211  return true;
212  }
213 
214  static const T& get_value(const std::list<T>& arr, std::size_t index)
215  {
216  typename std::list<T>::const_iterator it = arr.begin();
217  std::advance(it, index);
218  return *it;
219  }
220 
221  static T& get_value(std::list<T>& arr, std::size_t index)
222  {
223  typename std::list<T>::iterator it = arr.begin();
224  std::advance(it, index);
225  return *it;
226  }
227 
228  static bool insert_value(std::list<T>& arr, const T& value, std::size_t index)
229  {
230  typename std::list<T>::iterator it = arr.begin();
231  std::advance(it, index);
232  arr.insert(it, value);
233  return true;
234  }
235 
236  static bool remove_value(std::list<T>& arr, std::size_t index)
237  {
238  typename std::list<T>::iterator it = arr.begin();
239  std::advance(it, index);
240  arr.erase(it);
241  return true;
242  }
243 };
244 
245 } // end namespace detail
246 } // end namespace rttr
247 
248 #endif // __RTTR_ARRAY_MAPPER_H__