casacore
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>
34 #include <casacore/casa/Logging/LogIO.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  // <summary>
46  // Input/output operators for STL-like containers.
47  // </summary>
48 
49  // <use visibility=export>
50 
51  // <reviewed reviewer="Paul Shannon" date="1995/02/21" tests="" demos="">
52  // </reviewed>
53 
54  // <prerequisite>
55  // <li> STL container concept
56  // </prerequisite>
57 
58  // <synopsis>
59  // The function <src>showContainer</src> makes it possible to show
60  // any STL-like container (having forward iterators) on an ostream.
61  // This include casacore classes like Array, IPosition, and Block, but
62  // also STL classes like vector. The separator, prefix, and postfix
63  // can be defined at will (they default to , [ ]).
64  //
65  // The function <src>showDataIter</src> is similar to
66  // <src>showContainer</src>, but uses iterators directly.
67  // </synopsis>
68 
69  // <example>
70  // <srcblock>
71  // IPosition shape (3,10,10,3);
72  // showContainer (cout, shape);
73  // </srcblock>
74  //
75  // <motivation>
76  // Effortless input/output is clearly a big win.
77  // </motivation>
78  //
79  // <group name="Container IO">
80 
81  // Write out an ascii representation of any container using the
82  // given begin and end iterator.
83  // An arbitrary separator, prefix, and postfix can be given.
84  // E.g. for separator ', ' the output looks like [1, 2, 3].
85  template<class ITER> void showDataIter (ostream&,
86  ITER begin, const ITER& end,
87  const char* separator=",",
88  const char* prefix="[",
89  const char* postfix="]");
90 
91  // Write out an ascii representation of any container having a
92  // forward iterator.
93  // Note that a multi-dimensional Array object is linearized.
94  // An arbitrary separator, prefix, and postfix can be given.
95  // E.g. for separator ', ' the output looks like [1, 2, 3].
96  template<class CONTAINER> void showContainer (ostream& os, const CONTAINER& c,
97  const char* separator=",",
98  const char* prefix="[",
99  const char* postfix="]")
100  { showDataIter (os, c.begin(), c.end(), separator, prefix, postfix); }
101 
102  // Write a std::pair.
103  template <typename T, typename U>
104  inline ostream& operator<< (ostream& os, const std::pair<T,U>& p)
105  {
106  os << '<' << p.first << ',' << p.second << '>';
107  return os;
108  }
109 
110  // Write the contents of a vector enclosed in square brackets, using a comma
111  // as separator.
112  template<typename T>
113  inline ostream& operator<<(ostream& os, const std::vector<T>& v)
114  {
115  showContainer (os, v, ",", "[", "]");
116  return os;
117  }
118 
119  // Write the contents of a set enclosed in square brackets, using a comma
120  // as separator.
121  template<typename T>
122  inline ostream& operator<<(ostream& os, const std::set<T>& v)
123  {
124  showContainer (os, v, ",", "[", "]");
125  return os;
126  }
127 
128  // Write the contents of a list enclosed in square brackets, using a comma
129  // as separator.
130  template<typename T>
131  inline ostream& operator<<(ostream& os, const std::list<T>& v)
132  {
133  showContainer (os, v, ",", "[", "]");
134  return os;
135  }
136 
137  // Print the contents of a map enclosed in braces, using a comma
138  // as separator.
139  template<typename T, typename U>
140  inline ostream& operator<<(ostream& os, const std::map<T,U>& m)
141  {
142  showContainer (os, m, ", ", "{", "}");
143  return os;
144  }
145 
146  // Print the contents of a container on LogIO.
147  // <group>
148  template<typename T>
149  inline LogIO& operator<<(LogIO &os, const std::vector<T> &a)
150  { os.output() << a; return os; }
151  template<typename T>
152  inline LogIO& operator<<(LogIO &os, const std::set<T> &a)
153  { os.output() << a; return os; }
154  template<typename T>
155  inline LogIO& operator<<(LogIO &os, const std::list<T> &a)
156  { os.output() << a; return os; }
157  template<typename T, typename U>
158  inline LogIO& operator<<(LogIO& os, const std::map<T,U>& a)
159  { os.output() << a; return os; }
160  // </group>
161 
162  // Read or write the contents of an STL vector from/to AipsIO.
163  // The container is written in the same way as Block,
164  // thus can be read back that way and vice-versa.
165  // <group>
166  template<typename T>
167  AipsIO& operator>> (AipsIO& ios, std::vector<T>&);
168  template<typename T>
169  AipsIO& operator<< (AipsIO& ios, const std::vector<T>&);
170  // </group>
171 
172 } //# NAMESPACE CASACORE - END
173 
174 #ifndef CASACORE_NO_AUTO_TEMPLATES
175 #include <casacore/casa/BasicSL/STLIO.tcc>
176 #endif //# CASACORE_NO_AUTO_TEMPLATES
177 #endif
AipsIO & operator>>(AipsIO &os, Record &rec)
Definition: Record.h:465
this file contains all the compiler specific defines
Definition: mainpage.dox:28