summaryrefslogtreecommitdiff
path: root/src/bindings/eina_cxx/eina_range_types.hh
blob: 844e2ef4cc9240aa090c0fb6acbf234e2c9eafac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#ifndef EINA_RANGE_TYPES_HH_
#define EINA_RANGE_TYPES_HH_

/**
 * @addtogroup Eina_Cxx_Content_Access_Group
 *
 * @{
 */

namespace efl { namespace eina {

/**
 * @defgroup Eina_Cxx_Range_Group Range
 * @ingroup Eina_Cxx_Content_Access_Group
 *
 * @{
 */

/**
 * @brief Range implementation for immutable collections.
 */
template <typename T, typename Traits>
struct _const_range_template
{
  typedef typename Traits::template const_iterator<T>::type const_iterator; /**< Type for constant iterator to the range. */
  typedef typename Traits::template iterator<T>::type iterator; /**< Type for iterator to the range. */
  typedef T value_type; /**< The type of each element. */
  typedef T& reference; /**< Type for a reference to an element. */
  typedef T const& const_reference; /**< Type for a constant reference to an element. */
  typedef T* pointer; /**< Type for a pointer to an element. */
  typedef T const* const_pointer; /**< Type for a constant pointer to an element. */
  typedef std::reverse_iterator<iterator> reverse_iterator; /**< Type for reverse iterator to the range. */
  typedef std::reverse_iterator<const_iterator> const_reverse_iterator; /**< Type for constant reverse iterator to the range. */
  typedef std::size_t size_type; /**< Type for size information. */
  typedef std::ptrdiff_t difference_type; /**< Type to represent the distance between two iterators. */

  typedef typename Traits::template const_native_handle<T>::type native_handle_type; /**< Type for the native handle of the container. */
  typedef _const_range_template<T, Traits> _self_type; /**< Type of the range itself.  */

  /**
   * @brief Creates a singular range object
   */
  _const_range_template()
  {}

  /**
   * @brief Creates a range object wrapping the given native container handle.
   */
  _const_range_template(native_handle_type handle)
    : _handle(handle) {}

  /**
   * @brief Get a constant handle for the native Eina container.
   * @return Constant handle for the native Eina container.
   */
  native_handle_type native_handle() const { return _handle; }

  /**
   * @brief Get a constant reference to the last element.
   * @return Constant reference to the last element of the range.
   */
  value_type const& back() const
  {
    return Traits::template back<value_type>(_handle);
  }

  /**
   * @brief Get a constant reference to the first element.
   * @return Constant reference to the first element of the range.
   */
  value_type const& front() const
  {
    return Traits::template front<value_type>(_handle);
  }

  /**
   * @brief Get a constant iterator pointing to the first element of the range.
   * @return Constant iterator to the initial position of the range.
   *
   * This member function returns a constant iterator pointing to the
   * first element of the range. If the range contains no elements the
   * returned iterator is the same as the one returned by @ref end() const.
   */
  const_iterator begin() const
  {
    return cbegin();
  }

  /**
   * @brief Get a constant iterator to the position following the last element of the range.
   * @return Constant iterator to the final position of the range.
   *
   * This member function returns a constant iterator to the position
   * following the last element in the range. If the range contains no
   * elements the returned iterator is the same as the one returned by
   * @ref begin() const.
   *
   * @note Note that attempting to access this position causes undefined
   * behavior.
   */
  const_iterator end() const
  {
    return cend();
  }

  /**
   * @brief Get a constant reverse iterator pointing to the reverse begin of the range.
   * @return Constant reverse iterator pointing to the reverse begin of the range.
   *
   * This member function works like @ref rbegin() const but is granted
   * to return a constant reverse iterator even for a range to a mutable
   * collection.
   */
  const_reverse_iterator crbegin() const
  {
    return const_reverse_iterator(Traits::template rbegin<value_type>(_handle));
  }

  /**
   * @brief Get a constant reverse iterator pointing to the reverse end of the range.
   * @return Constant reverse iterator pointing to the reverse end of the range.
   *
   * This member function works like @ref rend() const but is granted to
   * return a constant reverse iterator even for range to a mutable
   * collection.
   */
  const_reverse_iterator crend() const
  {
    return const_reverse_iterator(Traits::template rend<value_type>(_handle));
  }

  /**
   * @brief Get a constant iterator pointing to the first element of the range.
   * @return Constant iterator to the initial position of the range.
   *
   * This member function works like @ref begin() const but is granted
   * to return a constant iterator even for a range to a mutable
   * collection.
   */
  const_iterator cbegin() const
  {
    return Traits::template cbegin<value_type>(_handle);
  }

  /**
   * @brief Get a constant iterator to the position following the last element of the range.
   * @return Constant iterator to the final position of the range.
   *
   * This member function works like @ref end() const  but is granted to
   * return a constant iterator even for a range to a mutable collection.
   */
  const_iterator cend() const
  {
    return Traits::template cend<value_type>(_handle);
  }

  /**
   * @brief Get a constant reverse iterator pointing to the reverse begin of the range.
   * @return Constant reverse iterator pointing to the reverse begin of the range.
   *
   * This member function returns a constant reverse iterator pointing
   * to the last element of the range. If the range is empty the
   * returned reverse iterator is the same as the one returned by
   * @ref rend().
   */
  const_reverse_iterator rbegin()
  {
    return crbegin();
  }

  /**
   * @brief Get a constant reverse iterator pointing to the reverse end of the range.
   * @return Constant reverse iterator pointing to the reverse end of the range.
   *
   * This member function returns a constant reverse iterator pointing
   * to the position before the first element of the range. If the range
   * is empty the returned iterator is the same as the one returned by
   * @ref rbegin().
   *
   * @note Note that attempting to access this position causes undefined
   * behavior.
   */
  const_reverse_iterator rend()
  {
    return crend();
  }

  /**
   * @brief Check if the range does not contain any elements.
   * @return @c true if there is no elements in the range, @c false otherwise.
   *
   * This member function returns @c true if the range does not contain
   * any elements, otherwise it returns @c false.
   */
  bool empty() const
  {
    return Traits::template empty<value_type>(_handle);
  }

  /**
   * @brief Get the number of elements in the range.
   * @return Number of elements in the range.
   *
   * This member function returns the current number of elements in the
   * range.
   */
  size_type size() const
  {
    return Traits::template size<value_type>(_handle);
  }

  /**
   * @brief Swap content with another range of the same type.
   * @param other Another range of the same type.
   */
  void swap(_self_type& other)
  {
    std::swap(_handle, other._handle);
  }
protected:
  /**
   * @internal
   */
  native_handle_type _handle;
};

/**
 * @brief Swap content between two @ref _const_range_template.
 * @param lhs First @c _const_range_template object.
 * @param rhs Second @c _const_range_template object.
 */
template <typename T, typename Traits>
void swap(_const_range_template<T, Traits>& lhs, _const_range_template<T, Traits>& rhs)
{
  lhs.swap(rhs);
}

/**
 * @brief Range implementation for mutable collections.
 */
template <typename T, typename Traits>
struct _mutable_range_template : _const_range_template<T, Traits>
{
  typedef T value_type; /**< The type of each element. */
  typedef typename Traits::template iterator<T>::type iterator; /**< Type for a iterator to the range. */
  typedef std::reverse_iterator<iterator> reverse_iterator; /**< Type for constant reverse iterator to the range. */
  typedef typename Traits::template native_handle<T>::type native_handle_type; /**< Type for the native handle of the container. */
  typedef _const_range_template<T, Traits> _base_type;  /**< Type for the base class. */

  /**
   * @brief Creates a range object wrapping the given native container handle.
   */
  _mutable_range_template(native_handle_type handle)
    : _base_type(handle) {}

  /**
   * @brief Get a constant handle for the native Eina container.
   * @return Constant handle for the native Eina container.
   */
  native_handle_type native_handle() const
  {
    return Traits::template native_handle_from_const<T>(_base_type::native_handle());
  }

  /**
   * @brief Get a reference to the last element.
   * @return Reference to the last element of the range.
   */
  value_type& back() const
  {
    return Traits::template back<value_type>(native_handle());
  }

  /**
   * @brief Get a reference to the first element.
   * @return Reference to the first element of the range.
   */
  value_type& front() const
  {
    return Traits::template front<value_type>(native_handle());
  }

  /**
   * @brief Get an iterator pointing to the first element of the range.
   * @return Iterator to the initial position of the range.
   *
   * This member function returns an iterator pointing to the first
   * element of the range. If the range contains no elements the
   * returned iterator is the same as the one returned by @ref end() const.
   */
  iterator begin() const
  {
    return Traits::template begin<value_type>(native_handle());
  }

  /**
   * @brief Get an iterator to the position following the last element of the range.
   * @return Iterator to the final position of the range.
   *
   * This member function returns an iterator to the position following
   * the last element in the range. If the range contains no elements
   * the returned iterator is the same as the one returned by
   * @ref begin() const.
   *
   * @note Note that attempting to access this position causes undefined
   * behavior.
   */
  iterator end() const
  {
    return Traits::template end<value_type>(native_handle());
  }

  /**
   * @brief Get a reverse iterator pointing to the reverse begin of the range.
   * @return Reverse iterator pointing to the reverse begin of the range.
   *
   * This member function returns a reverse iterator pointing to the
   * last element of the range. If the range is empty the returned
   * reverse iterator is the same as the one returned by @ref rend().
   */
  reverse_iterator rbegin() const
  {
    return Traits::template rbegin<value_type>(native_handle());
  }

  /**
   * @brief Get a reverse iterator pointing to the reverse end of the range.
   * @return Reverse iterator pointing to the reverse end of the range.
   *
   * This member function returns a reverse iterator pointing to the
   * position before the first element of the range. If the range is
   * empty the returned iterator is the same as the one returned by
   * @ref rbegin().
   *
   * @note Note that attempting to access this position causes undefined
   * behavior.
   */
  reverse_iterator rend() const
  {
    return Traits::template rend<value_type>(native_handle());
  }
protected:
  /**
   * @internal
   */
  using _base_type::_handle;
};

/**
 * Range class.
 *
 * Provide objects for accessing and/or modifying elements inside a
 * container without modifying the container itself.
 */
template <typename T, typename Traits>
struct _range_template : private std::conditional
  <std::is_const<T>::value
   , _const_range_template<typename std::remove_const<T>::type, Traits>
   , _mutable_range_template<T, Traits> >::type
{
  typedef std::integral_constant<bool, !std::is_const<T>::value> is_mutable; /**< Type that specifies if the elements can be modified. */
  typedef typename std::remove_const<T>::type value_type; /**< The type of each element. */
  typedef typename std::conditional<is_mutable::value, _mutable_range_template<value_type, Traits>
                                    , _const_range_template<value_type, Traits> >::type _base_type; /**< Type for the base class. */
  typedef typename _base_type::native_handle_type native_handle_type; /**< Type for the native handle of the container. */

  typedef value_type& reference; /**< Type for a reference to an element. */
  typedef value_type const& const_reference; /**< Type for a constant reference to an element. */
  typedef value_type* pointer; /**< Type for a pointer to an element. */
  typedef value_type const* const_pointer; /**< Type for a constant pointer to an element. */
  typedef typename Traits::template const_iterator<T>::type const_iterator; /**< Type for constant iterator to the range. */
  typedef typename _base_type::const_reverse_iterator const_reverse_iterator; /**< Type for constant reverse iterator to the range. */
  typedef typename Traits::template iterator<T>::type iterator; /**< Type for iterator to the range. */
  typedef typename _base_type::reverse_iterator reverse_iterator; /**< Type for reverse iterator to the range. */
  typedef typename _base_type::size_type size_type; /**< Type for size information. */
  typedef typename _base_type::difference_type difference_type; /**< Type to represent the distance between two iterators. */

  /**
   * @brief Creates a singular range object
   */
  _range_template()
  {}

  /**
   * @brief Creates a range object wrapping the given native container handle.
   */
  _range_template(native_handle_type handle)
    : _base_type(handle)
  {}

  using _base_type::native_handle;
  using _base_type::back;
  using _base_type::front;
  using _base_type::begin;
  using _base_type::end;
  using _base_type::rbegin;
  using _base_type::rend;
  using _base_type::cbegin;
  using _base_type::cend;
  using _base_type::crbegin;
  using _base_type::crend;
  using _base_type::empty;
  using _base_type::size;
  using _base_type::swap;
protected:
  using _base_type::_handle;
};

/**
 * @}
 */

} }

/**
 * @}
 */

#endif