RSC  0.10.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ContainerIO.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, 2012 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 <string>
30 #include <utility>
31 #include <vector>
32 #include <list>
33 #include <set>
34 #include <map>
35 #include <valarray>
36 #include <deque>
37 
38 #include <algorithm>
39 #include <iterator>
40 
41 #include <iostream>
42 
43 #include "rsc/rscexports.h"
44 
45 namespace std {
46 namespace detail {
47 
48 // style helpers for pairs
49 RSC_EXPORT void pair_style_delete(ios_base::event event_, ios_base& stream,
50  int index);
51 
52 struct RSC_EXPORT pair_style {
53  string open_brace;
54  string separator;
55  string close_brace;
56 
57  static const int stream_storage;
58 
59  pair_style(const string& open_brace = "(", const string& separator = ", ",
60  const string& close_brace = ")");
61 };
62 
63 template<typename T>
65  T value;
66 };
67 
68 
69 // style helpers for container types
70 RSC_EXPORT void container_style_delete(ios_base::event event_, ios_base& stream,
71  int index);
72 
73 struct RSC_EXPORT container_style {
74  string separator;
77 
78  static const int stream_storage;
79 
80  container_style(const string& separator = ", ",
81  const string& first_separator = "",
82  const string& last_separator = "");
83 };
84 
85 template<typename T>
87  T value;
88 };
89 
90 }
91 
92 template<typename Ch, typename Tr, typename T>
93 basic_ostream<Ch, Tr>&
94 operator<<(basic_ostream<Ch, Tr>& stream,
95  const detail::set_pair_style<T>& style) {
96  // Delete old style, if any, and install new style.
97  if (stream.pword(detail::pair_style::stream_storage))
98  delete reinterpret_cast<detail::pair_style*> (stream.pword(
100  else
101  stream.register_callback(&detail::pair_style_delete, 0);
102 
103  stream.pword(detail::pair_style::stream_storage) = new T(style.value);
104 
105  // Return the modified stream.
106  return stream;
107 }
108 
109 RSC_EXPORT extern const detail::set_pair_style<detail::pair_style> pair_default;
110 RSC_EXPORT extern const detail::set_pair_style<detail::pair_style>
111  pair_whitespace;
112 
113 template<typename Ch, typename Tr, typename T>
114 basic_ostream<Ch, Tr>&
115 operator<<(basic_ostream<Ch, Tr>& stream,
116  const detail::set_container_style<T>& style) {
117  // Delete old style, if any, and install new style.
118  if (stream.pword(detail::container_style::stream_storage))
119  delete reinterpret_cast<detail::container_style*> (stream.pword(
121  else
122  stream.register_callback(&detail::container_style_delete, 0);
123 
124  stream.pword(detail::container_style::stream_storage) = new T(style.value);
125 
126  // Return the modified stream.
127  return stream;
128 }
129 
130 RSC_EXPORT extern const detail::set_container_style<detail::container_style> container_singleline;
131 RSC_EXPORT extern const detail::set_container_style<detail::container_style> container_multiline;
132 
133 template<typename Ch, typename Tr, typename R, typename S>
134 basic_ostream<Ch, Tr>&
135 operator<<(basic_ostream<Ch, Tr>& stream, const pair<R, S>& pair) {
136  // Try to retrieve the installed style implementation. Create one,
137  // if none is installed.
138  if (!stream.pword(detail::pair_style::stream_storage))
139  stream << pair_default;
140 
141  detail::pair_style& style =
142  *reinterpret_cast<detail::pair_style*> (stream.pword(
144 
145  stream << style.open_brace << pair.first << style.separator << pair.second
146  << style.close_brace;
147  return stream;
148 }
149 
150 template<typename Ch, typename Tr, typename T>
151 basic_ostream<Ch, Tr>&
152 operator<<(basic_ostream<Ch, Tr>& stream, const vector<T>& container) {
153  typedef vector<T> container_type;
154  typedef typename container_type::value_type value_type;
155 
156  if (!stream.pword(detail::container_style::stream_storage)) {
157  stream << container_singleline;
158  }
159 
160  detail::container_style& style =
161  *reinterpret_cast<detail::container_style*>(stream.pword(
163 
164  stream << "#(";
165  if (container.size() >= 1) {
166  stream << style.first_separator;
167  copy(container.begin(), container.end() - 1,
168  ostream_iterator<value_type> (stream, style.separator.c_str()));
169  stream << container.back();
170  stream << style.last_separator;
171  }
172  stream << ")";
173  return stream;
174 }
175 
176 template<typename Ch, typename Tr, typename T>
177 basic_ostream<Ch, Tr>&
178 operator<<(basic_ostream<Ch, Tr>& stream, const deque<T>& container) {
179  typedef vector<T> container_type;
180  typedef typename container_type::value_type value_type;
181 
182  if (!stream.pword(detail::container_style::stream_storage)) {
183  stream << container_singleline;
184  }
185 
186  detail::container_style& style =
187  *reinterpret_cast<detail::container_style*>(stream.pword(
189 
190  stream << "d(";
191  if (container.size() >= 1) {
192  stream << style.first_separator;
193  copy(container.begin(), container.end() - 1,
194  ostream_iterator<value_type> (stream, style.separator.c_str()));
195  stream << container.back();
196  stream << style.last_separator;
197  }
198  stream << ")";
199  return stream;
200 }
201 
202 template<typename Ch, typename Tr, typename T>
203 basic_ostream<Ch, Tr>&
204 operator<<(basic_ostream<Ch, Tr>& stream, const list<T>& container) {
205  typedef list<T> container_type;
206  typedef typename container_type::value_type value_type;
207 
208  if (!stream.pword(detail::container_style::stream_storage)) {
209  stream << container_singleline;
210  }
211 
212  detail::container_style& style =
213  *reinterpret_cast<detail::container_style*>(stream.pword(
215 
216  stream << "[";
217  if (container.size() >= 1) {
218  stream << style.first_separator;
219  copy(container.begin(), --container.end(),
220  ostream_iterator<value_type> (stream, style.separator.c_str()));
221  stream << container.back();
222  stream << style.last_separator;
223  }
224  stream << "]";
225  return stream;
226 }
227 
228 template<typename Ch, typename Tr, typename T>
229 basic_ostream<Ch, Tr>&
230 operator<<(basic_ostream<Ch, Tr>& stream, const set<T>& container) {
231  typedef set<T> container_type;
232  typedef typename container_type::value_type value_type;
233 
234  if (!stream.pword(detail::container_style::stream_storage)) {
235  stream << container_singleline;
236  }
237 
238  detail::container_style& style =
239  *reinterpret_cast<detail::container_style*>(stream.pword(
241 
242  stream << "{";
243  if (container.size() >= 1) {
244  stream << style.first_separator;
245  copy(++container.begin(), container.end(),
246  ostream_iterator<value_type> (stream, style.separator.c_str()));
247  stream << *container.begin();
248  stream << style.last_separator;
249  }
250  stream << "}";
251  return stream;
252 }
253 
254 template<typename Ch, typename Tr, typename R, typename S>
255 basic_ostream<Ch, Tr>&
256 operator<<(basic_ostream<Ch, Tr>& stream, const map<R, S>& container) {
257  typedef map<R, S> container_type;
258 
259  if (!stream.pword(detail::container_style::stream_storage)) {
260  stream << container_singleline;
261  }
262 
263  detail::container_style& style =
264  *reinterpret_cast<detail::container_style*>(stream.pword(
266 
267  stream << "{";
268  stream << style.first_separator;
269  for (typename container_type::const_iterator it = container.begin(); it
270  != container.end();) {
271  stream << *it;
272  if (++it != container.end())
273  stream << style.separator;
274  }
275  stream << style.last_separator;
276  stream << "}";
277  return stream;
278 }
279 
280 template<typename Ch, typename Tr, typename R, typename S>
281 basic_ostream<Ch, Tr>&
282 operator<<(basic_ostream<Ch, Tr>& stream, const multimap<R, S>& container) {
283  typedef multimap<R, S> container_type;
284 
285  if (!stream.pword(detail::container_style::stream_storage)) {
286  stream << container_singleline;
287  }
288 
289  detail::container_style& style =
290  *reinterpret_cast<detail::container_style*>(stream.pword(
292 
293  stream << "{";
294  stream << style.first_separator;
295  for (typename container_type::const_iterator it = container.begin(); it
296  != container.end();) {
297  stream << *it;
298  if (++it != container.end())
299  stream << style.separator;
300  }
301  stream << style.last_separator;
302  stream << "}";
303  return stream;
304 }
305 
306 template<typename Ch, typename Tr, typename T>
307 basic_ostream<Ch, Tr>&
308 operator<<(basic_ostream<Ch, Tr>& stream, const valarray<T>& container) {
309  typedef valarray<T> container_type;
310  typedef typename container_type::value_type value_type;
311 
312  if (!stream.pword(detail::container_style::stream_storage)) {
313  stream << container_singleline;
314  }
315 
316  detail::container_style& style =
317  *reinterpret_cast<detail::container_style*>(stream.pword(
319 
320  stream << "(";
321  stream << style.first_separator;
322  for (unsigned int i = 0; i != container.size(); ++i) {
323  stream << container[i];
324  if (i != container.size() - 1)
325  stream << style.separator;
326  }
327  stream << style.last_separator;
328  stream << ")";
329  return stream;
330 }
331 
332 /* TODO(jmoringe, 2012-11-12): unfinished
333 template<typename Ch, typename Tr, typename T>
334 basic_ostream<Ch, Tr>&
335 operator<<(basic_ostream<Ch, Tr>& stream, const slice_array<T>& container) {
336  typedef slice_array<T> container_type;
337  typedef typename container_type::value_type value_type;
338 
339  stream << "s(";
340  for (unsigned int i = 0; i != container.size(); ++i) {
341  stream << container[i];
342  if (i != container.size() - 1)
343  stream << ", ";
344  }
345  stream << ")";
346  return stream;
347 }*/
348 
349 }
void container_style_delete(ios_base::event event_, ios_base &stream, int)
Definition: ContainerIO.cpp:47
void pair_style_delete(ios_base::event event_, ios_base &stream, int)
Definition: ContainerIO.cpp:32
static const int stream_storage
Definition: ContainerIO.h:57
static const int stream_storage
Definition: ContainerIO.h:78