OGLplus (0.52.0) a C++ wrapper for OpenGL

group.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_OBJECT_GROUP_1405011014_HPP
14 #define OGLPLUS_OBJECT_GROUP_1405011014_HPP
15 
16 #include <oglplus/object/seq_tpl.hpp>
17 #include <oglplus/config/compiler.hpp>
18 #include <array>
19 #include <vector>
20 #include <cassert>
21 
22 namespace oglplus {
23 
24 template <typename ObjName>
25 class Group;
26 
28 template <typename ObjTag>
29 class Group<ObjectName<ObjTag>>
30 {
31 private:
32  typedef typename ObjTag::NameType NameT;
33  std::vector<NameT> _names;
34 public:
36  Group(void) { }
37 
39  {
40  _names.push_back(GetName(a));
41  _names.push_back(GetName(b));
42  }
43 
44  Group(const Group& that)
45  : _names(that._names)
46  { }
47 
48  Group(Group&& temp)
49  : _names(std::move(temp._names))
50  { }
51 
52 #if !OGLPLUS_NO_INITIALIZER_LISTS
53  Group(std::initializer_list<ObjectName<ObjTag>> names)
55  {
56  _names.reserve(names.size());
57 
58  auto i = names.begin();
59  while(i != names.end())
60  {
61  _names.push_back(GetName(*i));
62  ++i;
63  }
64  }
65 #endif
66 
68  template <typename StdRange>
69  Group(const StdRange& range)
70  {
71  using std::begin;
72  using std::end;
73  using std::distance;
74 
75  auto b = begin(range);
76  auto e = end(range);
77 
78  _names.reserve(distance(b, e));
79 
80  auto i = b;
81  while(i != e)
82  {
83  _names.push_back(GetName(*i));
84  ++i;
85  }
86  }
87 
89  Group& Add(ObjectName<ObjTag> name)
90  {
91  _names.push_back(GetName(name));
92  return *this;
93  }
94 
95 
96  Sequence<ObjectName<ObjTag>> seq(void) const
97  {
99  _names.data(),
100  _names.size()
101  );
102  }
103 
105 
108  operator Sequence<ObjectName<ObjTag>> (void) const
109  {
110  return seq();
111  }
112 };
113 
114 template <typename ObjTag>
115 inline Group<ObjectName<ObjTag>>
116 operator , (ObjectName<ObjTag> a, ObjectName<ObjTag> b)
117 {
118  return Group<ObjectName<ObjTag>>(a, b);
119 }
120 
121 template <typename ObjTag>
122 inline Group<ObjectName<ObjTag>>&&
123 operator , (Group<ObjectName<ObjTag>>&& g, ObjectName<ObjTag> n)
124 {
125  g.Add(n);
126  return std::move(g);
127 }
128 
129 template <typename ObjName, std::size_t N>
130 class StaticGroup;
131 
132 template <typename ObjTag, std::size_t N>
133 class StaticGroup<ObjectName<ObjTag>, N>
134 {
135 private:
136  typedef typename ObjTag::NameType NameT;
137 
138  std::array<NameT, N> _names;
139 
140 #if !OGLPLUS_NO_VARIADIC_TEMPLATES
141  void _init(std::size_t) { }
142 
143  template <typename ... Tags>
144  void _init(
145  std::size_t i,
146  ObjectName<ObjTag> name,
147  ObjectName<Tags> ... names
148  )
149  {
150  _names[i] = GetName(name);
151  _init(i+1, names...);
152  }
153 #endif
154 public:
155  StaticGroup(const ObjectName<ObjTag> (&names)[N])
156  {
157  for(std::size_t i=0; i!=N; ++i)
158  {
159  _names[i] = GetName(names[i]);
160  }
161  }
162 
163 #if !OGLPLUS_NO_VARIADIC_TEMPLATES
164  template <typename ... Tag>
165  StaticGroup(ObjectName<Tag>... names)
166  {
167  _init(0, names...);
168  }
169 #else
170  StaticGroup(
171  ObjectName<ObjTag> n0,
172  ObjectName<ObjTag> n1
173  )
174  {
175  _names[0] = GetName(n0);
176  _names[1] = GetName(n1);
177  }
178 
179  StaticGroup(
180  ObjectName<ObjTag> n0,
181  ObjectName<ObjTag> n1,
182  ObjectName<ObjTag> n2
183  )
184  {
185  _names[0] = GetName(n0);
186  _names[1] = GetName(n1);
187  _names[2] = GetName(n2);
188  }
189 #endif
190 
191  Sequence<ObjectName<ObjTag>> seq(void) const
192  {
193  return Sequence<ObjectName<ObjTag>>(
194  _names.data(),
195  _names.size()
196  );
197  }
198 
200 
203  operator Sequence<ObjectName<ObjTag>> (void) const
204  {
205  return seq();
206  }
207 };
208 
209 #if !OGLPLUS_NO_VARIADIC_TEMPLATES
210 template <typename ObjTag, typename ... ObjTags>
211 inline StaticGroup<ObjectName<ObjTag>, 1+sizeof...(ObjTags)>
212 MakeGroup(ObjectName<ObjTag> name, ObjectName<ObjTags>... names)
213 {
214  return StaticGroup<ObjectName<ObjTag>, 1+sizeof...(ObjTags)>(
215  name,
216  names...
217  );
218 }
219 #else
220 template <typename ObjTag>
221 inline StaticGroup<ObjectName<ObjTag>, 2>
222 MakeGroup(ObjectName<ObjTag> n0, ObjectName<ObjTag> n1)
223 {
224  return StaticGroup<ObjectName<ObjTag>, 2>(n0, n1);
225 }
226 
227 template <typename ObjTag>
228 inline StaticGroup<ObjectName<ObjTag>, 3>
229 MakeGroup(ObjectName<ObjTag> n0, ObjectName<ObjTag> n1, ObjectName<ObjTag> n2)
230 {
231  return StaticGroup<ObjectName<ObjTag>, 3>(n0, n1, n2);
232 }
233 #endif
234 
235 } // namespace oglplus
236 
237 #endif // include guard
Common base class for Object name sequences.
Definition: fwd.hpp:139
Group & Add(ObjectName< ObjTag > name)
Add a new name to this group.
Definition: group.hpp:89
Group(void)
Constructs an empty Group.
Definition: group.hpp:36
Group(const StdRange &range)
Constructs the Group from a std range supporting begin and end.
Definition: group.hpp:69
ObjTag::NameType GetName(ObjectName< ObjTag >)
Returns the base name assigned to named object.
Definition: name_tpl.hpp:128
A common template for "named" objects like textures, buffers, etc.
Definition: fwd.hpp:136

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).