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
41class AipsIO;
42
43template <typename T, typename U>
44inline ostream& operator<<(ostream& os, const std::pair<T, U>& p);
45
46template <typename T>
47inline ostream& operator<<(ostream& os, const std::vector<T>& v);
48
49template <typename T>
50inline ostream& operator<<(ostream& os, const std::set<T>& v);
51
52template <typename T>
53inline ostream& operator<<(ostream& os, const std::list<T>& v);
54
55template <typename T, typename U>
56inline 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].
98template <class ITER>
99void showDataIter(ostream&, ITER begin, const ITER& end, const char* separator = ",",
100 const char* prefix = "[", const char* postfix = "]");
101
102// Write out an ascii representation of any container having a
103// forward iterator.
104// Note that a multi-dimensional Array object is linearized.
105// An arbitrary separator, prefix, and postfix can be given.
106// E.g. for separator ', ' the output looks like [1, 2, 3].
107template <class CONTAINER>
108void showContainer(ostream& os, const CONTAINER& c, const char* separator = ",",
109 const char* prefix = "[", const char* postfix = "]") {
110 showDataIter(os, c.begin(), c.end(), separator, prefix, postfix);
111}
112
113// Write a std::pair.
114template <typename T, typename U>
115inline ostream& operator<<(ostream& os, const std::pair<T, U>& p) {
116 os << '<' << p.first << ',' << p.second << '>';
117 return os;
118}
119
120// Write the contents of a vector enclosed in square brackets, using a comma
121// as separator.
122template <typename T>
123inline ostream& operator<<(ostream& os, const std::vector<T>& v) {
124 showContainer(os, v, ",", "[", "]");
125 return os;
126}
127
128// Write the contents of a set enclosed in square brackets, using a comma
129// as separator.
130template <typename T>
131inline ostream& operator<<(ostream& os, const std::set<T>& v) {
132 showContainer(os, v, ",", "[", "]");
133 return os;
134}
135
136// Write the contents of a list enclosed in square brackets, using a comma
137// as separator.
138template <typename T>
139inline ostream& operator<<(ostream& os, const std::list<T>& v) {
140 showContainer(os, v, ",", "[", "]");
141 return os;
142}
143
144// Print the contents of a map enclosed in braces, using a comma
145// as separator.
146template <typename T, typename U>
147inline ostream& operator<<(ostream& os, const std::map<T, U>& m) {
148 showContainer(os, m, ", ", "{", "}");
149 return os;
150}
151
152// Print the contents of a container on LogIO.
153// <group>
154template <typename T>
155inline LogIO& operator<<(LogIO& os, const std::vector<T>& a) {
156 os.output() << a;
157 return os;
158}
159template <typename T>
160inline LogIO& operator<<(LogIO& os, const std::set<T>& a) {
161 os.output() << a;
162 return os;
163}
164template <typename T>
165inline LogIO& operator<<(LogIO& os, const std::list<T>& a) {
166 os.output() << a;
167 return os;
168}
169template <typename T, typename U>
170inline LogIO& operator<<(LogIO& os, const std::map<T, U>& a) {
171 os.output() << a;
172 return os;
173}
174// </group>
175
176// Read or write the contents of an STL vector from/to AipsIO.
177// The container is written in the same way as Block,
178// thus can be read back that way and vice-versa.
179// <group>
180template <typename T>
181AipsIO& operator>>(AipsIO& ios, std::vector<T>&);
182template <typename T>
183AipsIO& operator<<(AipsIO& ios, const std::vector<T>&);
184// </group>
185
186// Read and write the contents of a map object from/to AipsIO.
187// It is done in the same way as the old SimpleOrderedMap class, so
188// persistent SimpleOrderedMap objects in CTDS can be read as std::map
189// and vice-versa.
190template <typename K, typename V>
191AipsIO& operator>>(AipsIO& ios, std::map<K, V>&);
192template <typename K, typename V>
193AipsIO& operator<<(AipsIO& ios, const std::map<K, V>&);
194// </group>
195
196} // namespace casacore
197
198#ifndef CASACORE_NO_AUTO_TEMPLATES
199#include <casacore/casa/BasicSL/STLIO.tcc>
200#endif // # CASACORE_NO_AUTO_TEMPLATES
201#endif
ostream & output()
Acumulate output in this ostream.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
iterator begin()
Get the begin and end iterator object for this block.
Definition Block.h:599
iterator end()
Definition Block.h:601
LogIO & operator<<(LogIO &os, const std::list< T > &a)
Definition STLIO.h:165
LogIO & operator<<(LogIO &os, const std::vector< T > &a)
Print the contents of a container on LogIO.
Definition STLIO.h:155
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:139
AipsIO & operator>>(AipsIO &ios, std::map< K, V > &)
Read and write the contents of a map object from/to AipsIO.
LogIO & operator<<(LogIO &os, const std::set< T > &a)
Definition STLIO.h:160
ostream & operator<<(ostream &os, const std::pair< T, U > &p)
Write a std::pair.
Definition STLIO.h:115
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:131
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:147
AipsIO & operator<<(AipsIO &ios, const std::map< K, V > &)
LogIO & operator<<(LogIO &os, const std::map< T, U > &a)
Definition STLIO.h:170
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:108
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.
AipsIO & operator<<(AipsIO &ios, const std::vector< T > &)
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:123