00001
00002
00003
00004
00005 #ifndef GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H
00006 #define GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H 1
00007
00008
00009
00010
00011
00012 #include <iostream>
00013 #include <iomanip>
00014 #include <vector>
00015 #include <map>
00016 #include <set>
00017 #include <list>
00018 #include <string>
00019 #include <sstream>
00020
00021
00022
00023 #include "GaudiKernel/Map.h"
00024 #include "GaudiKernel/HashMap.h"
00025 #include "GaudiKernel/VectorMap.h"
00026
00036
00037 namespace Gaudi
00038 {
00039
00040 namespace Utils
00041 {
00042
00048 template<class TYPE>
00049 std::ostream& toStream ( const TYPE& obj, std::ostream& s ) ;
00050
00062 template <class ITERATOR>
00063 inline std::ostream& toStream
00064 ( ITERATOR first ,
00065 ITERATOR last ,
00066 std::ostream& s ,
00067 const std::string& open ,
00068 const std::string& close ,
00069 const std::string& delim ) ;
00070
00077 inline std::ostream& toStream
00078 ( const std::string& obj , std::ostream& s )
00079 {
00080 if ( std::string::npos == obj.find('\'') )
00081 { s << "\'" << obj << "\'" ; }
00082 else
00083 { s << "\"" << obj << "\"" ; }
00084 return s ;
00085 }
00090 inline std::ostream& toStream
00091 ( const bool obj , std::ostream& s )
00092 { return s << ( obj ? "True" : "False" ) ; }
00097 inline std::ostream& toStream
00098 ( const float obj , std::ostream& s , const int prec = 6 )
00099 {
00100 const int p = s.precision() ;
00101 return s << std::setprecision ( prec ) << obj << std::setprecision ( p ) ;
00102 }
00107 inline std::ostream& toStream
00108 ( const double obj , std::ostream& s , const int prec = 8 )
00109 {
00110 const int p = s.precision() ;
00111 return s << std::setprecision ( prec ) << obj << std::setprecision ( p ) ;
00112 }
00117 inline std::ostream& toStream
00118 ( const long double obj , std::ostream& s , const int prec = 10 )
00119 {
00120 const int p = s.precision() ;
00121 return s << std::setprecision ( prec ) << obj << std::setprecision ( p ) ;
00122 }
00123
00131 template<class KTYPE, class VTYPE>
00132 inline std::ostream& toStream
00133 ( const std::pair<KTYPE,VTYPE>& obj, std::ostream& s)
00134 {
00135 s << "( " ;
00136 toStream ( obj.first , s ) ;
00137 s << " , " ;
00138 toStream ( obj.second , s ) ;
00139 return s << " )" ;
00140 }
00141
00148 template<class TYPE,class ALLOCATOR>
00149 inline std::ostream& toStream
00150 ( const std::vector<TYPE,ALLOCATOR>& obj, std::ostream& s)
00151 {
00152 return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
00153 }
00154
00161 template<class TYPE,class ALLOCATOR>
00162 inline std::ostream& toStream
00163 ( const std::list<TYPE,ALLOCATOR>& obj, std::ostream& s)
00164 {
00165 return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
00166 }
00167
00174 template<class TYPE,class CMP,class ALLOCATOR>
00175 inline std::ostream& toStream
00176 ( const std::set<TYPE,CMP,ALLOCATOR>& obj, std::ostream& s)
00177 {
00178 return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
00179 }
00180
00188 template<class KTYPE, class VTYPE,class CMP,class ALLOCATOR>
00189 inline std::ostream& toStream
00190 ( const std::map<KTYPE,VTYPE,CMP,ALLOCATOR>& obj, std::ostream& s )
00191 {
00192 s << "{ ";
00193 for ( typename std::map<KTYPE,VTYPE,CMP,ALLOCATOR>::const_iterator cur =
00194 obj.begin() ; obj.end() != cur ; ++cur )
00195 {
00196 if ( obj.begin() != cur ) { s << " , " ; }
00197 toStream ( cur -> first , s ) ;
00198 s << " : " ;
00199 toStream ( cur -> second , s ) ;
00200 }
00201 return s << " }";
00202 }
00203
00212 template<class KTYPE, class VTYPE,class CMP,class ALLOCATOR>
00213 inline std::ostream& toStream
00214 ( const GaudiUtils::VectorMap<KTYPE,VTYPE,CMP,ALLOCATOR>& obj, std::ostream& s )
00215 {
00216 s << "{ ";
00217 for ( typename GaudiUtils::VectorMap<KTYPE,VTYPE,CMP,ALLOCATOR>::const_iterator cur = obj.begin() ;
00218 obj.end() != cur ; ++cur )
00219 {
00220 if ( obj.begin() != cur ) { s << " , " ; }
00221 toStream ( cur -> first , s ) ;
00222 s << " : " ;
00223 toStream ( cur -> second , s ) ;
00224 }
00225 return s << " }";
00226 }
00227
00236 template<class KTYPE, class VTYPE,class MAP>
00237 inline std::ostream& toStream
00238 ( const GaudiUtils::Map<KTYPE,VTYPE,MAP>& obj, std::ostream& s)
00239 {
00240 s << "{ ";
00241 for ( typename GaudiUtils::Map<KTYPE,VTYPE,MAP>::const_iterator cur = obj.begin() ;
00242 obj.end() != cur ; ++cur )
00243 {
00244 if ( obj.begin() != cur ) { s << " , " ; }
00245 toStream ( cur -> first , s ) ;
00246 s << " : " ;
00247 toStream ( cur -> second , s ) ;
00248 }
00249 return s << " }";
00250 }
00251
00260 template<class KTYPE, class VTYPE,class HASH,class MAP>
00261 inline std::ostream& toStream
00262 ( const GaudiUtils::HashMap<KTYPE,VTYPE,HASH,MAP>& obj, std::ostream& s)
00263 {
00264 s << "{ ";
00265 for ( typename GaudiUtils::HashMap<KTYPE,VTYPE,HASH,MAP>::const_iterator cur = obj.begin() ;
00266 obj.end() != cur ; ++cur )
00267 {
00268 if ( obj.begin() != cur ) { s << " , " ; }
00269 toStream ( cur -> first , s ) ;
00270 s << " : " ;
00271 toStream ( cur -> second , s ) ;
00272 }
00273 return s << " }";
00274 }
00275
00280 template <class TYPE, unsigned int N>
00281 std::ostream& toStream ( TYPE(&obj)[N] , std::ostream& s )
00282 {
00283 return toStream ( obj , obj + N , s , "( " , " )" , " , " ) ;
00284 }
00285
00290 template <class TYPE, unsigned int N>
00291 std::ostream& toStream ( const TYPE(&obj)[N] , std::ostream& s )
00292 {
00293 return toStream ( obj , obj + N , s , "( " , " )" , " , " ) ;
00294 }
00295
00300 template <unsigned int N>
00301 std::ostream& toStream ( char (&obj)[N] , std::ostream& s )
00302 { return toStream ( std::string ( obj , obj+N ) , s ) ; }
00303
00308 template <unsigned int N>
00309 std::ostream& toStream ( const char (&obj)[N] , std::ostream& s )
00310 { return toStream ( std::string ( obj , obj+N ) , s ) ; }
00311
00316 inline std::ostream& toStream ( const char* obj , std::ostream& s )
00317 { return toStream ( std::string ( obj ) , s ) ; }
00318
00324 template<class TYPE>
00325 inline std::ostream& toStream ( const TYPE& obj, std::ostream& s )
00326 { return s << obj ; }
00327
00339 template <class ITERATOR>
00340 inline std::ostream& toStream
00341 ( ITERATOR first ,
00342 ITERATOR last ,
00343 std::ostream& s ,
00344 const std::string& open ,
00345 const std::string& close ,
00346 const std::string& delim )
00347 {
00348 s << open ;
00349 for ( ITERATOR curr = first ; curr != last ; ++curr )
00350 {
00351 if ( first != curr ) { s << delim ; }
00352 toStream ( *curr , s ) ;
00353 }
00354 s << close ;
00355
00356 return s ;
00357 }
00358
00366 template <class TYPE>
00367 inline std::string toString ( const TYPE& obj )
00368 {
00369 std::ostringstream s;
00370 std::ios::fmtflags orig_flags = s.flags();
00371 s.setf(std::ios::fixed);
00372 toStream ( obj , s);
00373 s.flags(orig_flags);
00374 return s.str();
00375 }
00376
00377 }
00378
00379 }
00380
00381
00382
00383 #endif
00384
00385