casacore
Loading...
Searching...
No Matches
STLIO.h
Go to the documentation of this file.
1//# STLIO.h: text output IO for any STL-like container
2//# Copyright (C) 2011
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_STLIO_H
27#define CASA_STLIO_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/iostream.h>
32#include <casacore/casa/Logging/LogIO.h>
33#include <vector>
34#include <set>
35#include <list>
36#include <map>
37
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40 //# Forward Declarations
41 class AipsIO;
42
43 template <typename T, typename U>
44 inline ostream& operator<< (ostream& os, const std::pair<T,U>& p);
45
46 template<typename T>
47 inline ostream& operator<<(ostream& os, const std::vector<T>& v);
48
49 template<typename T>
50 inline ostream& operator<<(ostream& os, const std::set<T>& v);
51
52 template<typename T>
53 inline ostream& operator<<(ostream& os, const std::list<T>& v);
54
55 template<typename T, typename U>
56 inline ostream& operator<<(ostream& os, const std::map<T,U>& m);
57
58 // <summary>
59 // Input/output operators for STL-like containers.
60 // </summary>
61
62 // <use visibility=export>
63
64 // <reviewed reviewer="Paul Shannon" date="1995/02/21" tests="" demos="">
65 // </reviewed>
66
67 // <prerequisite>
68 // <li> STL container concept
69 // </prerequisite>
70
71 // <synopsis>
72 // The function <src>showContainer</src> makes it possible to show
73 // any STL-like container (having forward iterators) on an ostream.
74 // This include casacore classes like Array, IPosition, and Block, but
75 // also STL classes like vector. The separator, prefix, and postfix
76 // can be defined at will (they default to , [ ]).
77 //
78 // The function <src>showDataIter</src> is similar to
79 // <src>showContainer</src>, but uses iterators directly.
80 // </synopsis>
81
82 // <example>
83 // <srcblock>
84 // IPosition shape (3,10,10,3);
85 // showContainer (cout, shape);
86 // </srcblock>
87 //
88 // <motivation>
89 // Effortless input/output is clearly a big win.
90 // </motivation>
91 //
92 // <group name="Container IO">
94 // Write out an ascii representation of any container using the
95 // given begin and end iterator.
96 // An arbitrary separator, prefix, and postfix can be given.
97 // E.g. for separator ', ' the output looks like [1, 2, 3].
98 template<class ITER> void showDataIter (ostream&,
99 ITER begin, const ITER& end,
100 const char* separator=",",
101 const char* prefix="[",
102 const char* postfix="]");
103
104 // Write out an ascii representation of any container having a
105 // forward iterator.
106 // Note that a multi-dimensional Array object is linearized.
107 // An arbitrary separator, prefix, and postfix can be given.
108 // E.g. for separator ', ' the output looks like [1, 2, 3].
109 template<class CONTAINER> void showContainer (ostream& os, const CONTAINER& c,
110 const char* separator=",",
111 const char* prefix="[",
112 const char* postfix="]")
113 { showDataIter (os, c.begin(), c.end(), separator, prefix, postfix); }
114
115 // Write a std::pair.
116 template <typename T, typename U>
117 inline ostream& operator<< (ostream& os, const std::pair<T,U>& p)
118 {
119 os << '<' << p.first << ',' << p.second << '>';
120 return os;
121 }
122
123 // Write the contents of a vector enclosed in square brackets, using a comma
124 // as separator.
125 template<typename T>
126 inline ostream& operator<<(ostream& os, const std::vector<T>& v)
127 {
128 showContainer (os, v, ",", "[", "]");
129 return os;
130 }
131
132 // Write the contents of a set enclosed in square brackets, using a comma
133 // as separator.
134 template<typename T>
135 inline ostream& operator<<(ostream& os, const std::set<T>& v)
136 {
137 showContainer (os, v, ",", "[", "]");
138 return os;
139 }
140
141 // Write the contents of a list enclosed in square brackets, using a comma
142 // as separator.
143 template<typename T>
144 inline ostream& operator<<(ostream& os, const std::list<T>& v)
145 {
146 showContainer (os, v, ",", "[", "]");
147 return os;
148 }
149
150 // Print the contents of a map enclosed in braces, using a comma
151 // as separator.
152 template<typename T, typename U>
153 inline ostream& operator<<(ostream& os, const std::map<T,U>& m)
154 {
155 showContainer (os, m, ", ", "{", "}");
156 return os;
157 }
158
159 // Print the contents of a container on LogIO.
160 // <group>
161 template<typename T>
162 inline LogIO& operator<<(LogIO &os, const std::vector<T> &a)
163 { os.output() << a; return os; }
164 template<typename T>
165 inline LogIO& operator<<(LogIO &os, const std::set<T> &a)
166 { os.output() << a; return os; }
167 template<typename T>
168 inline LogIO& operator<<(LogIO &os, const std::list<T> &a)
169 { os.output() << a; return os; }
170 template<typename T, typename U>
171 inline LogIO& operator<<(LogIO& os, const std::map<T,U>& a)
172 { os.output() << a; return os; }
173 // </group>
174
175 // Read or write the contents of an STL vector from/to AipsIO.
176 // The container is written in the same way as Block,
177 // thus can be read back that way and vice-versa.
178 // <group>
179 template<typename T>
180 AipsIO& operator>> (AipsIO& ios, std::vector<T>&);
181 template<typename T>
182 AipsIO& operator<< (AipsIO& ios, const std::vector<T>&);
183 // </group>
184
185 // Read and write the contents of a map object from/to AipsIO.
186 // It is done in the same way as the old SimpleOrderedMap class, so
187 // persistent SimpleOrderedMap objects in CTDS can be read as std::map
188 // and vice-versa.
189 template<typename K, typename V>
190 AipsIO& operator>> (AipsIO& ios, std::map<K,V>&);
191 template<typename K, typename V>
192 AipsIO& operator<< (AipsIO& ios, const std::map<K,V>&);
193 // </group>
194
195} //# NAMESPACE CASACORE - END
196
197#ifndef CASACORE_NO_AUTO_TEMPLATES
198#include <casacore/casa/BasicSL/STLIO.tcc>
199#endif //# CASACORE_NO_AUTO_TEMPLATES
200#endif
ostream & output()
Acumulate output in this ostream.
this file contains all the compiler specific defines
Definition mainpage.dox:28
AipsIO & operator>>(AipsIO &os, Record &rec)
Definition Record.h:462
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
LogIO & operator<<(LogIO &os, const std::list< T > &a)
Definition STLIO.h:168
LogIO & operator<<(LogIO &os, const std::vector< T > &a)
Print the contents of a container on LogIO.
Definition STLIO.h:162
ostream & operator<<(ostream &os, const std::list< T > &v)
Write the contents of a list enclosed in square brackets, using a comma as separator.
Definition STLIO.h:144
LogIO & operator<<(LogIO &os, const std::set< T > &a)
Definition STLIO.h:165
ostream & operator<<(ostream &os, const std::pair< T, U > &p)
Write a std::pair.
Definition STLIO.h:117
ostream & operator<<(ostream &os, const std::set< T > &v)
Write the contents of a set enclosed in square brackets, using a comma as separator.
Definition STLIO.h:135
AipsIO & operator>>(AipsIO &ios, std::vector< T > &)
Read or write the contents of an STL vector from/to AipsIO.
ostream & operator<<(ostream &os, const std::map< T, U > &m)
Print the contents of a map enclosed in braces, using a comma as separator.
Definition STLIO.h:153
LogIO & operator<<(LogIO &os, const std::map< T, U > &a)
Definition STLIO.h:171
void showContainer(ostream &os, const CONTAINER &c, const char *separator=",", const char *prefix="[", const char *postfix="]")
Write out an ascii representation of any container having a forward iterator.
Definition STLIO.h:109
void showDataIter(ostream &, ITER begin, const ITER &end, const char *separator=",", const char *prefix="[", const char *postfix="]")
Write out an ascii representation of any container using the given begin and end iterator.
ostream & operator<<(ostream &os, const std::vector< T > &v)
Write the contents of a vector enclosed in square brackets, using a comma as separator.
Definition STLIO.h:126