RSC  0.17.1
ContainerProxy.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2010, 2011 Jan Moringen
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <boost/noncopyable.hpp>
30 #include <boost/iterator/transform_iterator.hpp>
31 
32 #include "NoSuchKey.h"
33 #include "Accessors.h"
34 #include "detail/ForceConst.h"
35 #include "../runtime/TypeStringTools.h"
36 
37 namespace rsc {
38 namespace patterns {
39 
49 template<typename Container, typename Accessor = pass_through>
50 class ContainerProxy: private boost::noncopyable {
51 public:
52  typedef typename Container::size_type size_type;
53 
54  typedef typename Container::value_type value_type;
55  typedef typename Container::pointer pointer;
56 
57  typedef typename Container::const_reference const_reference;
58  typedef typename Container::reference reference;
59 
60  typedef boost::transform_iterator<Accessor,
61  typename Container::const_iterator, typename detail::force_const<
62  typename Accessor::result_type>::type> const_iterator;
63 
64  typedef boost::transform_iterator<Accessor, typename Container::iterator,
65  typename Accessor::result_type> iterator;
66 
67  typedef typename iterator::difference_type difference_type;
68 
73  ContainerProxy(Container& container);
74 
80  template<typename P1> // TODO
81  ContainerProxy(Container& container, P1 p1);
82 
89  template<typename P1, typename P2> // TODO
90  ContainerProxy(Container& container, P1 p1, P2 p2);
91 
92  const_iterator
93  begin() const throw ();
94 
95  iterator
96  begin() throw ();
97 
98  const_iterator
99  end() const throw ();
100 
101  iterator
102  end() throw ();
103 
104  size_type
105  size() const throw ();
106 
107  bool
108  empty() const throw ();
109 protected:
110  typedef Container container_type;
111 
112  typedef Accessor accessor_type;
113 
114  container_type& container;
115 
116  accessor_type accessor;
117 };
118 
119 template<typename Container>
120 class ContainerProxy<Container, pass_through> {
121 public:
122  typedef typename Container::size_type size_type;
123 
124  typedef typename Container::value_type value_type;
125  typedef typename Container::pointer pointer;
126 
127  typedef typename Container::const_iterator const_iterator;
128  typedef typename Container::iterator iterator;
129 
130  typedef typename Container::const_reference const_reference;
131  typedef typename Container::reference reference;
132 
133  typedef typename Container::difference_type difference_type;
134 
135  ContainerProxy(Container& container);
136 
137  inline const_iterator
138  begin() const throw ();
139 
140  inline iterator
141  begin() throw ();
142 
143  inline const_iterator
144  end() const throw ();
145 
146  inline iterator
147  end() throw ();
148 
149  inline size_type
150  size() const throw ();
151 
152  inline bool
153  empty() const throw ();
154 protected:
155  typedef Container container_type;
156 
157  typedef pass_through accessor_type;
158 
159  container_type& container;
160 };
161 
162 // ContainerProxy implementation
163 
164 template<typename Container, typename Accessor>
165 ContainerProxy<Container, Accessor>::ContainerProxy(Container& container) :
166  container(container) {
167 }
168 
169 template<typename Container, typename Accessor>
171  Container, Accessor>::begin() const throw () {
172  return const_iterator(this->container.begin(), this->accessor);
173 }
174 
175 template<typename Container, typename Accessor>
177  Container, Accessor>::begin() throw () {
178  return iterator(this->container.begin(), this->accessor);
179 }
180 
181 template<typename Container, typename Accessor>
183  Container, Accessor>::end() const throw () {
184  return const_iterator(this->container.end(), this->accessor);
185 }
186 
187 template<typename Container, typename Accessor>
189  Container, Accessor>::end() throw () {
190  return iterator(this->container.end(), this->accessor);
191 }
192 
193 template<typename Container, typename Accessor>
195  Container, Accessor>::size() const throw () {
196  return this->container.size();
197 }
198 
199 template<typename Container, typename Accessor>
201  return this->container.empty();
202 }
203 
204 // ContainerProxy<Container, pass_through> implementation
205 
206 template<typename Container>
208  container(container) {
209 }
210 
211 template<typename Container>
213  Container, pass_through>::begin() const throw () {
214  return container.begin();
215 }
216 
217 template<typename Container>
219  Container, pass_through>::begin() throw () {
220  return container.begin();
221 }
222 
223 template<typename Container>
225  Container, pass_through>::end() const throw () {
226  return container.end();
227 }
228 
229 template<typename Container>
231  Container, pass_through>::end() throw () {
232  return container.end();
233 }
234 
235 template<typename Container>
237  Container, pass_through>::size() const throw () {
238  return container.size();
239 }
240 
241 template<typename Container>
243  return container.empty();
244 }
245 
246 }
247 }
Container::size_type size_type
Container::value_type value_type
boost::transform_iterator< Accessor, typename Container::iterator, typename Accessor::result_type > iterator
Container::pointer pointer
const_iterator begin() const
Container::reference reference
const_iterator end() const
iterator::difference_type difference_type
boost::transform_iterator< Accessor, typename Container::const_iterator, typename detail::force_const< typename Accessor::result_type >::type > const_iterator
ContainerProxy(Container &container)
Container::const_reference const_reference