RSC  0.7.17
 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 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 RSC_EXPORT void pair_style_delete(ios_base::event event_, ios_base& stream,
49  int index);
50 
51 struct RSC_EXPORT pair_style {
52  string open_brace;
53  string separator;
54  string close_brace;
55 
56  static const int stream_storage;
57 
58  pair_style(const string& open_brace = "(", const string& separator = ", ",
59  const string& close_brace = ")");
60 };
61 
62 template<typename T>
64  T value;
65 };
66 
67 }
68 
69 template<typename Ch, typename Tr, typename T>
70 basic_ostream<Ch, Tr>&
71 operator<<(basic_ostream<Ch, Tr>& stream,
72  const detail::set_pair_style<T>& style) {
73  // Delete old style, if any, and install new style.
74  if (stream.pword(detail::pair_style::stream_storage))
75  delete reinterpret_cast<detail::pair_style*> (stream.pword(
77  else
78  stream.register_callback(&detail::pair_style_delete, 0);
79 
80  stream.pword(detail::pair_style::stream_storage) = new T(style.value);
81 
82  // Return the modified stream.
83  return stream;
84 }
85 
86 RSC_EXPORT extern const detail::set_pair_style<detail::pair_style> pair_default;
87 RSC_EXPORT extern const detail::set_pair_style<detail::pair_style>
88  pair_whitespace;
89 
90 template<typename Ch, typename Tr, typename R, typename S>
91 basic_ostream<Ch, Tr>&
92 operator<<(basic_ostream<Ch, Tr>& stream, const pair<R, S>& pair) {
93  // Try to retrieve the installed style implementation. Create one,
94  // if none is installed.
95  if (!stream.pword(detail::pair_style::stream_storage))
96  stream << pair_default;
97 
98  detail::pair_style& style =
99  *reinterpret_cast<detail::pair_style*> (stream.pword(
101 
102  stream << style.open_brace << pair.first << style.separator << pair.second
103  << style.close_brace;
104  return stream;
105 }
106 
107 template<typename Ch, typename Tr, typename T>
108 basic_ostream<Ch, Tr>&
109 operator<<(basic_ostream<Ch, Tr>& stream, const vector<T>& container) {
110  typedef vector<T> container_type;
111  typedef typename container_type::value_type value_type;
112 
113  stream << "#(";
114  if (container.size() >= 1) {
115  copy(container.begin(), container.end() - 1,
116  ostream_iterator<value_type> (stream, ", "));
117  stream << container.back();
118  }
119  stream << ")";
120  return stream;
121 }
122 
123 template<typename Ch, typename Tr, typename T>
124 basic_ostream<Ch, Tr>&
125 operator<<(basic_ostream<Ch, Tr>& stream, const deque<T>& container) {
126  typedef vector<T> container_type;
127  typedef typename container_type::value_type value_type;
128 
129  stream << "d(";
130  if (container.size() >= 1) {
131  copy(container.begin(), container.end() - 1,
132  ostream_iterator<value_type> (stream, ", "));
133  stream << container.back();
134  }
135  stream << ")";
136  return stream;
137 }
138 
139 template<typename Ch, typename Tr, typename T>
140 basic_ostream<Ch, Tr>&
141 operator<<(basic_ostream<Ch, Tr>& stream, const list<T>& container) {
142  typedef list<T> container_type;
143  typedef typename container_type::value_type value_type;
144 
145  stream << "[";
146  if (container.size() >= 1) {
147  copy(container.begin(), --container.end(),
148  ostream_iterator<value_type> (stream, ", "));
149  stream << container.back();
150  }
151  stream << "]";
152  return stream;
153 }
154 
155 template<typename Ch, typename Tr, typename T>
156 basic_ostream<Ch, Tr>&
157 operator<<(basic_ostream<Ch, Tr>& stream, const set<T>& container) {
158  typedef set<T> container_type;
159  typedef typename container_type::value_type value_type;
160 
161  stream << "{";
162  if (container.size() >= 1) {
163  copy(++container.begin(), container.end(),
164  ostream_iterator<value_type> (stream, ", "));
165  stream << *container.begin();
166  }
167  stream << "}";
168  return stream;
169 }
170 
171 template<typename Ch, typename Tr, typename R, typename S>
172 basic_ostream<Ch, Tr>&
173 operator<<(basic_ostream<Ch, Tr>& stream, const map<R, S>& container) {
174  typedef map<R, S> container_type;
175 
176  stream << "{";
177  for (typename container_type::const_iterator it = container.begin(); it
178  != container.end();) {
179  stream << it->first << ": " << it->second;
180  if (++it != container.end())
181  stream << "\n";
182  }
183  stream << "}";
184  return stream;
185 }
186 
187 template<typename Ch, typename Tr, typename R, typename S>
188 basic_ostream<Ch, Tr>&
189 operator<<(basic_ostream<Ch, Tr>& stream, const multimap<R, S>& container) {
190  typedef multimap<R, S> container_type;
191 
192  stream << "{";
193  for (typename container_type::const_iterator it = container.begin(); it
194  != container.end();) {
195  stream << it->first << ": " << it->second;
196  if (++it != container.end())
197  stream << "\n";
198  }
199  stream << "}";
200  return stream;
201 }
202 
203 template<typename Ch, typename Tr, typename T>
204 basic_ostream<Ch, Tr>&
205 operator<<(basic_ostream<Ch, Tr>& stream, const valarray<T>& container) {
206  typedef valarray<T> container_type;
207  typedef typename container_type::value_type value_type;
208 
209  stream << "(";
210  for (unsigned int i = 0; i != container.size(); ++i) {
211  stream << container[i];
212  if (i != container.size() - 1)
213  stream << ", ";
214  }
215  stream << ")";
216  return stream;
217 }
218 
219 template<typename Ch, typename Tr, typename T>
220 basic_ostream<Ch, Tr>&
221 operator<<(basic_ostream<Ch, Tr>& stream, const slice_array<T>& container) {
222  typedef slice_array<T> container_type;
223  typedef typename container_type::value_type value_type;
224 
225  stream << "s(";
226  /* for (unsigned int i = 0; i != container.size(); ++i) {
227  stream << container[i];
228  if (i != container.size() - 1)
229  stream << ", ";
230  }*/
231  stream << ")";
232  return stream;
233 }
234 
235 }
void pair_style_delete(ios_base::event event_, ios_base &stream, int)
Definition: ContainerIO.cpp:32
static const int stream_storage
Definition: ContainerIO.h:56