casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: aips2-request@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 //# $Id: STLIO.h 21331 2013-03-26 15:08:06Z gervandiepen $
27 
28 #ifndef CASA_STLIO_H
29 #define CASA_STLIO_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/iostream.h>
35 #include <vector>
36 #include <set>
37 #include <list>
38 #include <map>
39 
40 namespace casacore { //# NAMESPACE CASACORE - BEGIN
41 
42  //# Forward Declarations
43  class AipsIO;
44 
45  template <typename T, typename U>
46  inline ostream& operator<< (ostream& os, const std::pair<T,U>& p);
47 
48  template<typename T>
49  inline ostream& operator<<(ostream& os, const std::vector<T>& v);
50 
51  template<typename T>
52  inline ostream& operator<<(ostream& os, const std::set<T>& v);
53 
54  template<typename T>
55  inline ostream& operator<<(ostream& os, const std::list<T>& v);
56 
57  template<typename T, typename U>
58  inline ostream& operator<<(ostream& os, const std::map<T,U>& m);
59 
60  // <summary>
61  // Input/output operators for STL-like containers.
62  // </summary>
63 
64  // <use visibility=export>
65 
66  // <reviewed reviewer="Paul Shannon" date="1995/02/21" tests="" demos="">
67  // </reviewed>
68 
69  // <prerequisite>
70  // <li> STL container concept
71  // </prerequisite>
72 
73  // <synopsis>
74  // The function <src>showContainer</src> makes it possible to show
75  // any STL-like container (having forward iterators) on an ostream.
76  // This include casacore classes like Array, IPosition, and Block, but
77  // also STL classes like vector. The separator, prefix, and postfix
78  // can be defined at will (they default to , [ ]).
79  //
80  // The function <src>showDataIter</src> is similar to
81  // <src>showContainer</src>, but uses iterators directly.
82  // </synopsis>
83 
84  // <example>
85  // <srcblock>
86  // IPosition shape (3,10,10,3);
87  // showContainer (cout, shape);
88  // </srcblock>
89  //
90  // <motivation>
91  // Effortless input/output is clearly a big win.
92  // </motivation>
93  //
94  // <group name="Container IO">
95 
96  // Write out an ascii representation of any container using the
97  // given begin and end iterator.
98  // An arbitrary separator, prefix, and postfix can be given.
99  // E.g. for separator ', ' the output looks like [1, 2, 3].
100  template<class ITER> void showDataIter (ostream&,
101  ITER begin, const ITER& end,
102  const char* separator=",",
103  const char* prefix="[",
104  const char* postfix="]");
105 
106  // Write out an ascii representation of any container having a
107  // forward iterator.
108  // Note that a multi-dimensional Array object is linearized.
109  // An arbitrary separator, prefix, and postfix can be given.
110  // E.g. for separator ', ' the output looks like [1, 2, 3].
111  template<class CONTAINER> void showContainer (ostream& os, const CONTAINER& c,
112  const char* separator=",",
113  const char* prefix="[",
114  const char* postfix="]")
115  { showDataIter (os, c.begin(), c.end(), separator, prefix, postfix); }
116 
117  // Write a std::pair.
118  template <typename T, typename U>
119  inline ostream& operator<< (ostream& os, const std::pair<T,U>& p)
120  {
121  os << '<' << p.first << ',' << p.second << '>';
122  return os;
123  }
124 
125  // Write the contents of a vector enclosed in square brackets, using a comma
126  // as separator.
127  template<typename T>
128  inline ostream& operator<<(ostream& os, const std::vector<T>& v)
129  {
130  showContainer (os, v, ",", "[", "]");
131  return os;
132  }
133 
134  // Write the contents of a set enclosed in square brackets, using a comma
135  // as separator.
136  template<typename T>
137  inline ostream& operator<<(ostream& os, const std::set<T>& v)
138  {
139  showContainer (os, v, ",", "[", "]");
140  return os;
141  }
142 
143  // Write the contents of a list enclosed in square brackets, using a comma
144  // as separator.
145  template<typename T>
146  inline ostream& operator<<(ostream& os, const std::list<T>& v)
147  {
148  showContainer (os, v, ",", "[", "]");
149  return os;
150  }
151 
152  // Print the contents of a map enclosed in braces, using a comma
153  // as separator.
154  template<typename T, typename U>
155  inline ostream& operator<<(ostream& os, const std::map<T,U>& m)
156  {
157  showContainer (os, m, ", ", "{", "}");
158  return os;
159  }
160 
161  // Print the contents of a container on LogIO.
162  // <group>
163  template<typename T>
164  inline LogIO& operator<<(LogIO &os, const std::vector<T> &a)
165  { os.output() << a; return os; }
166  template<typename T>
167  inline LogIO& operator<<(LogIO &os, const std::set<T> &a)
168  { os.output() << a; return os; }
169  template<typename T>
170  inline LogIO& operator<<(LogIO &os, const std::list<T> &a)
171  { os.output() << a; return os; }
172  template<typename T, typename U>
173  inline LogIO& operator<<(LogIO& os, const std::map<T,U>& a)
174  { os.output() << a; return os; }
175  // </group>
176 
177  // Read or write the contents of an STL vector from/to AipsIO.
178  // The container is written in the same way as Block,
179  // thus can be read back that way and vice-versa.
180  // <group>
181  template<typename T>
182  AipsIO& operator>> (AipsIO& ios, std::vector<T>&);
183  template<typename T>
184  AipsIO& operator<< (AipsIO& ios, const std::vector<T>&);
185  // </group>
186 
187  // Read and write the contents of a map object from/to AipsIO.
188  // It is done in the same way as the old SimpleOrderedMap class, so
189  // persistent SimpleOrderedMap objects in CTDS can be read as std::map
190  // and vice-versa.
191  template<typename K, typename V>
192  AipsIO& operator>> (AipsIO& ios, std::map<K,V>&);
193  template<typename K, typename V>
194  AipsIO& operator<< (AipsIO& ios, const std::map<K,V>&);
195  // </group>
196 
197 } //# NAMESPACE CASACORE - END
198 
199 #ifndef CASACORE_NO_AUTO_TEMPLATES
200 #include <casacore/casa/BasicSL/STLIO.tcc>
201 #endif //# CASACORE_NO_AUTO_TEMPLATES
202 #endif
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:111
AipsIO is the object persistency mechanism of Casacore.
Definition: AipsIO.h:168
ostream & operator>>(ostream &is, MVTime &meas)
ostream-like interface to creating log messages.
Definition: LogIO.h:167
const_iterator end() const
const_iterator begin() const
ostream & output()
Acumulate output in this ostream.
const Double c
Fundamental physical constants (SI units):
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42