00001 //# STLIO.h: text output IO for any STL-like container 00002 //# Copyright (C) 2011 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: STLIO.h 21331 2013-03-26 15:08:06Z gervandiepen $ 00027 00028 #ifndef CASA_STLIO_H 00029 #define CASA_STLIO_H 00030 00031 //# Includes 00032 #include <casacore/casa/aips.h> 00033 #include <casacore/casa/iostream.h> 00034 #include <casacore/casa/Logging/LogIO.h> 00035 #include <vector> 00036 #include <set> 00037 #include <list> 00038 #include <map> 00039 00040 namespace casacore { //# NAMESPACE CASACORE - BEGIN 00041 00042 //# Forward Declarations 00043 class AipsIO; 00044 00045 // <summary> 00046 // Input/output operators for STL-like containers. 00047 // </summary> 00048 00049 // <use visibility=export> 00050 00051 // <reviewed reviewer="Paul Shannon" date="1995/02/21" tests="" demos=""> 00052 // </reviewed> 00053 00054 // <prerequisite> 00055 // <li> STL container concept 00056 // </prerequisite> 00057 00058 // <synopsis> 00059 // The function <src>showContainer</src> makes it possible to show 00060 // any STL-like container (having forward iterators) on an ostream. 00061 // This include casacore classes like Array, IPosition, and Block, but 00062 // also STL classes like vector. The separator, prefix, and postfix 00063 // can be defined at will (they default to , [ ]). 00064 // 00065 // The function <src>showDataIter</src> is similar to 00066 // <src>showContainer</src>, but uses iterators directly. 00067 // </synopsis> 00068 00069 // <example> 00070 // <srcblock> 00071 // IPosition shape (3,10,10,3); 00072 // showContainer (cout, shape); 00073 // </srcblock> 00074 // 00075 // <motivation> 00076 // Effortless input/output is clearly a big win. 00077 // </motivation> 00078 // 00079 // <group name="Container IO"> 00080 00081 // Write out an ascii representation of any container using the 00082 // given begin and end iterator. 00083 // An arbitrary separator, prefix, and postfix can be given. 00084 // E.g. for separator ', ' the output looks like [1, 2, 3]. 00085 template<class ITER> void showDataIter (ostream&, 00086 ITER begin, const ITER& end, 00087 const char* separator=",", 00088 const char* prefix="[", 00089 const char* postfix="]"); 00090 00091 // Write out an ascii representation of any container having a 00092 // forward iterator. 00093 // Note that a multi-dimensional Array object is linearized. 00094 // An arbitrary separator, prefix, and postfix can be given. 00095 // E.g. for separator ', ' the output looks like [1, 2, 3]. 00096 template<class CONTAINER> void showContainer (ostream& os, const CONTAINER& c, 00097 const char* separator=",", 00098 const char* prefix="[", 00099 const char* postfix="]") 00100 { showDataIter (os, c.begin(), c.end(), separator, prefix, postfix); } 00101 00102 // Write a std::pair. 00103 template <typename T, typename U> 00104 inline ostream& operator<< (ostream& os, const std::pair<T,U>& p) 00105 { 00106 os << '<' << p.first << ',' << p.second << '>'; 00107 return os; 00108 } 00109 00110 // Write the contents of a vector enclosed in square brackets, using a comma 00111 // as separator. 00112 template<typename T> 00113 inline ostream& operator<<(ostream& os, const std::vector<T>& v) 00114 { 00115 showContainer (os, v, ",", "[", "]"); 00116 return os; 00117 } 00118 00119 // Write the contents of a set enclosed in square brackets, using a comma 00120 // as separator. 00121 template<typename T> 00122 inline ostream& operator<<(ostream& os, const std::set<T>& v) 00123 { 00124 showContainer (os, v, ",", "[", "]"); 00125 return os; 00126 } 00127 00128 // Write the contents of a list enclosed in square brackets, using a comma 00129 // as separator. 00130 template<typename T> 00131 inline ostream& operator<<(ostream& os, const std::list<T>& v) 00132 { 00133 showContainer (os, v, ",", "[", "]"); 00134 return os; 00135 } 00136 00137 // Print the contents of a map enclosed in braces, using a comma 00138 // as separator. 00139 template<typename T, typename U> 00140 inline ostream& operator<<(ostream& os, const std::map<T,U>& m) 00141 { 00142 showContainer (os, m, ", ", "{", "}"); 00143 return os; 00144 } 00145 00146 // Print the contents of a container on LogIO. 00147 // <group> 00148 template<typename T> 00149 inline LogIO& operator<<(LogIO &os, const std::vector<T> &a) 00150 { os.output() << a; return os; } 00151 template<typename T> 00152 inline LogIO& operator<<(LogIO &os, const std::set<T> &a) 00153 { os.output() << a; return os; } 00154 template<typename T> 00155 inline LogIO& operator<<(LogIO &os, const std::list<T> &a) 00156 { os.output() << a; return os; } 00157 template<typename T, typename U> 00158 inline LogIO& operator<<(LogIO& os, const std::map<T,U>& a) 00159 { os.output() << a; return os; } 00160 // </group> 00161 00162 // Read or write the contents of an STL vector from/to AipsIO. 00163 // The container is written in the same way as Block, 00164 // thus can be read back that way and vice-versa. 00165 // <group> 00166 template<typename T> 00167 AipsIO& operator>> (AipsIO& ios, std::vector<T>&); 00168 template<typename T> 00169 AipsIO& operator<< (AipsIO& ios, const std::vector<T>&); 00170 // </group> 00171 00172 } //# NAMESPACE CASACORE - END 00173 00174 #ifndef CASACORE_NO_AUTO_TEMPLATES 00175 #include <casacore/casa/BasicSL/STLIO.tcc> 00176 #endif //# CASACORE_NO_AUTO_TEMPLATES 00177 #endif