OSVR-Core
ContainerWrapper.h
Go to the documentation of this file.
1 
12 // Copyright 2015 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 #ifndef INCLUDED_ContainerWrapper_h_GUID_87C9C3C2_1A4E_4BA8_A160_96145B60CEDB
27 #define INCLUDED_ContainerWrapper_h_GUID_87C9C3C2_1A4E_4BA8_A160_96145B60CEDB
28 
29 // Internal Includes
30 #include <osvr/TypePack/T.h>
31 #include <osvr/TypePack/List.h>
32 #include <osvr/TypePack/If.h>
33 #include <osvr/TypePack/Contains.h>
34 #include <osvr/TypePack/And.h>
35 #include <osvr/TypePack/Fold.h>
36 #include <osvr/TypePack/Not.h>
37 
38 // Library/third-party includes
39 // - none
40 
41 // Standard includes
42 // - none
43 
44 namespace osvr {
45 namespace util {
49  namespace container_policies {
53  struct iterators;
57  struct const_iterators;
60  struct size;
63  struct subscript;
66  struct const_subscript;
69  struct empty;
70  } // namespace container_policies
71 
72  namespace detail {
74  template <typename Container> class ContainerWrapperBase {
75  public:
76  typedef typename Container::value_type value_type;
77  typedef typename Container::reference reference;
78  typedef typename Container::const_reference const_reference;
79  typedef typename Container::size_type size_type;
80  typedef typename Container::iterator iterator;
81  typedef typename Container::const_iterator const_iterator;
82 
83  protected:
84  Container &container() { return m_container; }
85  Container const &container() const { return m_container; }
86  Container const &ccontainer() const { return m_container; }
87 
88  private:
89  Container m_container;
90  };
91 
94  template <typename Container, typename Base>
96  public:
97  typedef typename Container::const_iterator const_iterator;
98  const_iterator begin() const { return Base::container().begin(); }
99  const_iterator cbegin() const { return Base::container().cbegin(); }
100  const_iterator end() const { return Base::container().end(); }
101  const_iterator cend() const { return Base::container().cend(); }
102  };
103 
106  template <typename Container, typename Base>
108  : public ContainerWrapperConstIterators<Container, Base> {
109  public:
110  typedef typename Container::iterator iterator;
111  iterator begin() { return Base::container().begin(); }
112  iterator end() { return Base::container().end(); }
113  };
114 
116  template <typename Container, typename Base>
117  class ContainerWrapperSize : public Base {
118  public:
119  typedef typename Container::size_type size_type;
120  size_type size() const { return Base::container().size(); }
121  };
122 
124  template <typename Container, typename Base>
125  class ContainerWrapperEmpty : public Base {
126  public:
127  bool empty() const { return Base::container().empty(); }
128  };
129 
132  template <typename Container, typename Base>
134  public:
135  typedef typename Container::size_type size_type;
136  typedef typename Container::const_reference const_reference;
137  const_reference operator[](size_type index) const {
138  return Base::container()[index];
139  }
140  };
141 
144  template <typename Container, typename Base>
146  public:
147  typedef typename Container::size_type size_type;
148  typedef typename Container::reference reference;
149  typedef typename Container::const_reference const_reference;
150  reference operator[](size_type index) {
151  return Base::container()[index];
152  }
153  const_reference operator[](size_type index) const {
154  return Base::container()[index];
155  }
156  };
157 
160  template <typename Condition,
161  template <class, class> class ContainerMixin>
162  struct Option {
163  using condition = Condition;
164  template <typename Container, typename Base>
165  using apply = ContainerMixin<Container, Base>;
166  };
167 
169  template <typename Container> struct ContainerWrapperFold {
170  template <typename Base, typename Option> struct apply {
171  using condition = typename Option::condition;
172  using type = typepack::if_<
173  condition,
175  Base>;
176  };
177  };
178 
181  template <typename Container, typename ArgList>
183  // Determine if we want iterators or const iterators
184  using wants_iterators =
186  using wants_const_iterators = typepack::and_<
188  typepack::contains<ArgList,
189  container_policies::const_iterators>>;
190 
191  // Check for subscript operator requests
192  using wants_subscript =
194  using wants_const_subscript = typepack::and_<
196  typepack::contains<ArgList,
197  container_policies::const_subscript>>;
198 
199  // Check for size requests
200  using wants_size =
202 
203  // Check for empty requests
204  using wants_empty =
206 
207  // List of conditions and their associated mixin class templates
208  using option_list = typepack::list<
215  using type =
218  };
219 
220  template <typename Container, typename... Args>
221  using ContainerWrapper_t = typepack::t_<detail::ComputeContainerWrapper<
222  Container, typepack::list<Args...>>>;
223  } // namespace detail
224 
231  template <typename Container, typename... Args>
232  using ContainerWrapper = detail::ContainerWrapper_t<Container, Args...>;
233 
234 } // namespace util
235 } // namespace osvr
236 
237 #endif // INCLUDED_ContainerWrapper_h_GUID_87C9C3C2_1A4E_4BA8_A160_96145B60CEDB
Definition: gtest_unittest.cc:5031
Alias class to use in fold when computing a container wrapper.
Definition: ContainerWrapper.h:169
Definition: RunLoopManager.h:42
Container wrapper mixin for consumers that want size.
Definition: ContainerWrapper.h:125
apply_list< quote< or_ >, transform< Haystack, detail::is_< Needle >>> contains
Determines if type Needle is in the list Haystack - is an alias for a type that inherits std::true_ty...
Definition: Contains.h:49
Container wrapper for consumers that want const iterator methods.
Definition: ContainerWrapper.h:95
Container wrapper mixin for consumers that want the array subscript operator, including also const-ar...
Definition: ContainerWrapper.h:145
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Header.
t_< detail::and_impl< Bools... >> and_
Logically and together all the integral constant-wrapped Boolean parameters, with short-circuiting...
Definition: And.h:61
Holds the result of evaluating a condition along with the mixin template to use (as an alias class) i...
Definition: ContainerWrapper.h:162
Header.
A wrapper for a template parameter pack of types.
Definition: List.h:52
typename F::template apply< Args... > apply
Apply an alias class.
Definition: Apply.h:44
Terminal base class for container wrapper functionality.
Definition: ContainerWrapper.h:74
Header.
detail::size< coerce_list< Ts... >> size
Get the size of a list (number of elements.)
Definition: Size.h:56
bool_<!Bool::value > not_
Logical not on a single boolean.
Definition: Not.h:43
Header.
Definition: newuoa.h:1888
Header.
t_< detail::if_impl< Args... >> if_
Select one type or another depending on a compile-time Boolean integral constant type.
Definition: If.h:61
Container wrapper mixin for consumers that want const array subscript operator methods.
Definition: ContainerWrapper.h:133
Header.
Container wrapper mixin for consumers that want size.
Definition: ContainerWrapper.h:117
Definition: ContainerWrapper.h:170
Main metafunction used to compute the full type of a container wrapper.
Definition: ContainerWrapper.h:182
Container wrapper for consumers that want iterator methods, including also const-iterator methods...
Definition: ContainerWrapper.h:107
typename T::type t_
A convenience alias template to extract the nested type within the supplied T.
Definition: T.h:52
detail::ContainerWrapper_t< Container, Args... > ContainerWrapper
Parent class to inherit from to get a container with some functionality exposed.
Definition: ContainerWrapper.h:232
Header.
t_< detail::fold_< List, State, Fun >> fold
Fold the list (right) with the given alias class and initial state.
Definition: Fold.h:57