ToStream.h
Go to the documentation of this file.
1 // ============================================================================
2 #ifndef GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H
3 #define GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H 1
4 // ============================================================================
5 // Include files
6 // ============================================================================
7 // STD & STL
8 // ============================================================================
9 #include <iostream>
10 #include <iomanip>
11 #include <vector>
12 #include <map>
13 #include <set>
14 #include <list>
15 #include <string>
16 #include <sstream>
17 // ============================================================================
18 // GaudiKernel
19 // ============================================================================
20 #include "GaudiKernel/Map.h"
21 #include "GaudiKernel/HashMap.h"
22 #include "GaudiKernel/VectorMap.h"
23 // ============================================================================
33 // ============================================================================
34 namespace Gaudi
35 {
36  // ==========================================================================
37  namespace Utils
38  {
39  // ========================================================================
45  template<class TYPE>
46  std::ostream& toStream ( const TYPE& obj, std::ostream& s ) ;
47  // ========================================================================
59  template <class ITERATOR>
60  inline std::ostream& toStream
61  ( ITERATOR first , // begin of the sequence
62  ITERATOR last , // end of the sequence
63  std::ostream& s , // the stream
64  const std::string& open , // opening
65  const std::string& close , // closing
66  const std::string& delim ) ; // delimiter
67  // ========================================================================
74  inline std::ostream& toStream
75  ( const std::string& obj , std::ostream& s )
76  {
77  if ( std::string::npos == obj.find('\'') )
78  { s << "\'" << obj << "\'" ; }
79  else
80  { s << "\"" << obj << "\"" ; }
81  return s ;
82  }
87  inline std::ostream& toStream
88  ( const bool obj , std::ostream& s )
89  { return s << ( obj ? "True" : "False" ) ; }
94  inline std::ostream& toStream
95  ( const float obj , std::ostream& s , const int prec = 6 )
96  {
97  const int p = s.precision() ;
98  return s << std::setprecision ( prec ) << obj << std::setprecision ( p ) ;
99  }
104  inline std::ostream& toStream
105  ( const double obj , std::ostream& s , const int prec = 8 )
106  {
107  const int p = s.precision() ;
108  return s << std::setprecision ( prec ) << obj << std::setprecision ( p ) ;
109  }
114  inline std::ostream& toStream
115  ( const long double obj , std::ostream& s , const int prec = 10 )
116  {
117  const int p = s.precision() ;
118  return s << std::setprecision ( prec ) << obj << std::setprecision ( p ) ;
119  }
120  // ========================================================================
128  template<class KTYPE, class VTYPE>
129  inline std::ostream& toStream
130  ( const std::pair<KTYPE,VTYPE>& obj, std::ostream& s)
131  {
132  s << "( " ;
133  toStream ( obj.first , s ) ;
134  s << " , " ;
135  toStream ( obj.second , s ) ;
136  return s << " )" ;
137  }
138  // ========================================================================
145  template<class TYPE,class ALLOCATOR>
146  inline std::ostream& toStream
147  ( const std::vector<TYPE,ALLOCATOR>& obj, std::ostream& s)
148  {
149  return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
150  }
151  // ========================================================================
158  template<class TYPE,class ALLOCATOR>
159  inline std::ostream& toStream
160  ( const std::list<TYPE,ALLOCATOR>& obj, std::ostream& s)
161  {
162  return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
163  }
164  // ========================================================================
171  template<class TYPE,class CMP,class ALLOCATOR>
172  inline std::ostream& toStream
173  ( const std::set<TYPE,CMP,ALLOCATOR>& obj, std::ostream& s)
174  {
175  return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
176  }
177  // ========================================================================
185  template<class KTYPE, class VTYPE,class CMP,class ALLOCATOR>
186  inline std::ostream& toStream
187  ( const std::map<KTYPE,VTYPE,CMP,ALLOCATOR>& obj, std::ostream& s )
188  {
189  s << "{ ";
190  for ( typename std::map<KTYPE,VTYPE,CMP,ALLOCATOR>::const_iterator cur =
191  obj.begin() ; obj.end() != cur ; ++cur )
192  {
193  if ( obj.begin() != cur ) { s << " , " ; }
194  toStream ( cur -> first , s ) ;
195  s << " : " ;
196  toStream ( cur -> second , s ) ;
197  }
198  return s << " }";
199  }
200  // ========================================================================
209  template<class KTYPE, class VTYPE,class CMP,class ALLOCATOR>
210  inline std::ostream& toStream
211  ( const GaudiUtils::VectorMap<KTYPE,VTYPE,CMP,ALLOCATOR>& obj, std::ostream& s )
212  {
213  s << "{ ";
215  obj.end() != cur ; ++cur )
216  {
217  if ( obj.begin() != cur ) { s << " , " ; }
218  toStream ( cur -> first , s ) ;
219  s << " : " ;
220  toStream ( cur -> second , s ) ;
221  }
222  return s << " }";
223  }
224  // ========================================================================
233  template<class KTYPE, class VTYPE,class MAP>
234  inline std::ostream& toStream
235  ( const GaudiUtils::Map<KTYPE,VTYPE,MAP>& obj, std::ostream& s)
236  {
237  s << "{ ";
238  for ( typename GaudiUtils::Map<KTYPE,VTYPE,MAP>::const_iterator cur = obj.begin() ;
239  obj.end() != cur ; ++cur )
240  {
241  if ( obj.begin() != cur ) { s << " , " ; }
242  toStream ( cur -> first , s ) ;
243  s << " : " ;
244  toStream ( cur -> second , s ) ;
245  }
246  return s << " }";
247  }
248  // ========================================================================
257  template<class KTYPE, class VTYPE,class HASH,class MAP>
258  inline std::ostream& toStream
259  ( const GaudiUtils::HashMap<KTYPE,VTYPE,HASH,MAP>& obj, std::ostream& s)
260  {
261  s << "{ ";
263  obj.end() != cur ; ++cur )
264  {
265  if ( obj.begin() != cur ) { s << " , " ; }
266  toStream ( cur -> first , s ) ;
267  s << " : " ;
268  toStream ( cur -> second , s ) ;
269  }
270  return s << " }";
271  }
272  // ========================================================================
277  template <class TYPE, unsigned int N>
278  std::ostream& toStream ( TYPE(&obj)[N] , std::ostream& s )
279  {
280  return toStream ( obj , obj + N , s , "( " , " )" , " , " ) ;
281  }
282  // ========================================================================
287  template <class TYPE, unsigned int N>
288  std::ostream& toStream ( const TYPE(&obj)[N] , std::ostream& s )
289  {
290  return toStream ( obj , obj + N , s , "( " , " )" , " , " ) ;
291  }
292  // ========================================================================
297  template <unsigned int N>
298  std::ostream& toStream ( char (&obj)[N] , std::ostream& s )
299  { return toStream ( std::string ( obj , obj+N ) , s ) ; }
300  // ========================================================================
305  template <unsigned int N>
306  std::ostream& toStream ( const char (&obj)[N] , std::ostream& s )
307  { return toStream ( std::string ( obj , obj+N ) , s ) ; }
308  // ========================================================================
313  inline std::ostream& toStream ( const char* obj , std::ostream& s )
314  { return toStream ( std::string ( obj ) , s ) ; }
315  // ========================================================================
321  template<class TYPE>
322  inline std::ostream& toStream ( const TYPE& obj, std::ostream& s )
323  { return s << obj ; }
324  // ========================================================================
336  template <class ITERATOR>
337  inline std::ostream& toStream
338  ( ITERATOR first , // begin of the sequence
339  ITERATOR last , // end of the sequence
340  std::ostream& s , // the stream
341  const std::string& open , // opening
342  const std::string& close , // closing
343  const std::string& delim ) // delimiter
344  {
345  s << open ;
346  for ( ITERATOR curr = first ; curr != last ; ++curr )
347  {
348  if ( first != curr ) { s << delim ; }
349  toStream ( *curr , s ) ;
350  }
351  s << close ;
352  //
353  return s ;
354  }
355  // ========================================================================
356  // helper function to print a tuple of any size
357  template<class Tuple, std::size_t N>
358  struct TuplePrinter {
359  static std::ostream& toStream(const Tuple& t, std::ostream& s)
360  {
362  s << " , ";
363  Gaudi::Utils::toStream(std::get<N-1>(t), s);
364  return s;
365  }
366  };
367 
368  template<class Tuple>
369  struct TuplePrinter<Tuple, 1>{
370  static std::ostream& toStream(const Tuple& t, std::ostream& s)
371  {
372  Gaudi::Utils::toStream(std::get<0>(t), s);
373  return s;
374  }
375  };
376 
383  template<typename... Args>
384  inline std::ostream& toStream(const std::tuple<Args...>& tuple, std::ostream& s) {
385  s << " ( ";
386  TuplePrinter<decltype(tuple), sizeof...(Args)>::toStream(tuple, s);
387  s << " ) ";
388  return s;
389  }
390 
391  // ========================================================================
399  template <class TYPE>
400  inline std::string toString ( const TYPE& obj )
401  {
402  std::ostringstream s;
403  std::ios::fmtflags orig_flags = s.flags();
404  s.setf(std::ios::showpoint); // to display correctly floats
405  toStream ( obj , s);
406  s.flags(orig_flags);
407  return s.str();
408  }
409  // ========================================================================
410  } // end of namespace Gaudi::Utils
411  // ==========================================================================
412 } // end of namespace Gaudi
413 // ============================================================================
414 // The END
415 // ============================================================================
416 #endif
417 // ============================================================================
418 
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:107
Extension of the STL map.
Definition: Map.h:82
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:400
constexpr double second
iterator end() const
"end" iterator for sequential access (const-only version!)
Definition: VectorMap.h:197
int N
Definition: IOTest.py:90
std::ostream & toStream(ITERATOR first, ITERATOR last, std::ostream &s, const std::string &open, const std::string &close, const std::string &delim)
the helper function to print the sequence
Definition: ToStream.h:338
iterator end()
Definition: Map.h:132
iterator begin() const
"begin" iterator for sequential access (const-only version!)
Definition: VectorMap.h:195
map_type::const_iterator const_iterator
Definition: Map.h:100
iterator begin()
Definition: Map.h:131
_vector::const_iterator const_iterator
visible const_iterator (exported)
Definition: VectorMap.h:148
Common class providing an architecture-independent hash map.
Definition: HashMap.h:77
string s
Definition: gaudirun.py:246
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:359
Helper functions to set/get the application return code.
Definition: __init__.py:1