OGLplus (0.52.0) a C++ wrapper for OpenGL

list_init.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_OPT_LIST_INIT_HPP
14 #define OGLPLUS_OPT_LIST_INIT_HPP
15 
16 #include <type_traits>
17 #include <utility>
18 #include <array>
19 #include <vector>
20 
21 namespace oglplus {
22 namespace aux {
23 
24 template <typename T, std::size_t I>
25 class ListInitializerBase
26 {
27 private:
28  T _value;
29  const ListInitializerBase<T, I-1>* _prev;
30 
31  friend class ListInitializerBase<T, I+1>;
32 protected:
33  template <typename RandomAccessContainer>
34  void _init(RandomAccessContainer& dest) const
35  {
36  _prev->_init(dest);
37  dest[I] = _value;
38  }
39 
40  template <typename BackInsertionContainer>
41  void _push_back(BackInsertionContainer& dest) const
42  {
43  _prev->_push_back(dest);
44  dest.push_back(_value);
45  }
46 
47  ListInitializerBase(T value, const ListInitializerBase<T, I-1>* prev)
48  : _value(value)
49  , _prev(prev)
50  { }
51 };
52 
53 template <typename T>
54 class ListInitializerBase<T, 0>
55 {
56 private:
57  T _value;
58 
59  friend class ListInitializerBase<T, 1>;
60 
61 protected:
62  template <typename RandomAccessContainer>
63  void _init(RandomAccessContainer& dest) const
64  {
65  dest[0] = _value;
66  }
67 
68  template <typename BackInsertionContainer>
69  void _push_back(BackInsertionContainer& dest) const
70  {
71  dest.push_back(_value);
72  }
73 
74  ListInitializerBase(T value)
75  : _value(value)
76  { }
77 };
78 
79 template <typename T, std::size_t I>
80 class ListInitializer
81  : public ListInitializerBase<T, I>
82 {
83 private:
84  typedef ListInitializerBase<T, I> _base;
85 
86  friend class ListInitializer<T, I-1>;
87 
88  template <typename X>
89  static decltype(X(), std::true_type()) _has_def_ctr(X*);
90 
91  static std::false_type _has_def_ctr(...);
92 
93  static std::array<T, I+1> _result_of_get(std::true_type);
94 
95  static std::vector<T> _result_of_get(std::false_type);
96 
97  typedef decltype(_result_of_get(_has_def_ctr((T*)0))) ResultOfGet;
98 
99  template <typename X>
100  void _do_get(std::array<X, I+1>& result) const
101  {
102  this->_init(result);
103  }
104 
105  void _do_get(std::vector<T>& result) const
106  {
107  this->_push_back(result);
108  }
109 
110  template <typename StdContainer>
111  StdContainer _do_get_as(StdContainer*, std::true_type) const
112  {
113  return StdContainer(Get());
114  }
115 
116  template <typename StdContainer>
117  StdContainer _do_get_as(StdContainer*, std::false_type) const
118  {
119  auto tmp = Get();
120  return StdContainer(tmp.begin(), tmp.end());
121  }
122 
123  // non copyable
124  ListInitializer(const ListInitializer&);
125 
126  ListInitializer(T value, const ListInitializer<T, I-1>* prev)
127  : _base(value, prev)
128  { }
129 public:
130  ListInitializer(T value)
131  : _base(value)
132  { }
133 
134  ListInitializer(ListInitializer&& temp)
135  : _base(temp)
136  { }
137 
138  ListInitializer<T, I+1> operator()(T value) const
139  {
140  return ListInitializer<T, I+1>(value, this);
141  }
142 
143  ResultOfGet Get(void) const
144  {
145  ResultOfGet result;
146  this->_do_get(result);
147  return result;
148  }
149 
150  template <typename StdContainer>
151  StdContainer As(void) const
152  {
153  return this->_do_get_as(
154  (StdContainer*)nullptr,
155  typename std::is_convertible<
156  ResultOfGet,
157  StdContainer
158  >::type()
159  );
160  }
161 
162  std::vector<T> AsVector(void) const
163  {
164  return As<std::vector<T>>();
165  }
166 };
167 
168 } // namespace aux
169 
171 
190 template <typename T>
191 class ListOf
192  : public aux::ListInitializer<T, 0>
193 {
194 public:
195  ListOf(T value)
196  : aux::ListInitializer<T, 0>(value)
197  { }
198 };
199 
200 
202 
220 template <typename T>
221 inline aux::ListInitializer<T, 0> List(T value)
222 {
223  return aux::ListInitializer<T, 0>(value);
224 }
225 
226 } // namespace oglplus
227 
228 #endif // include guard
Helper class template that can be used for static container initialization.
Definition: list_init.hpp:191
aux::ListInitializer< T, 0 > List(T value)
Helper function template that can be used for static container initialization.
Definition: list_init.hpp:221

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).