The Gaudi Framework  v28r3 (cc1cf868)
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 <array>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <list>
16 #include <string>
17 #include <sstream>
18 #include <type_traits>
19 // ============================================================================
20 // GaudiKernel
21 // ============================================================================
22 #include "GaudiKernel/Map.h"
23 #include "GaudiKernel/HashMap.h"
24 #include "GaudiKernel/VectorMap.h"
26 // ============================================================================
36 // ============================================================================
37 namespace Gaudi
38 {
39  // ==========================================================================
40  namespace Utils
41  {
42  // ========================================================================
48  template<class TYPE>
49  std::ostream& toStream ( const TYPE& obj, std::ostream& s ) ;
50  // ========================================================================
62  template <class ITERATOR>
63  inline std::ostream& toStream
64  ( ITERATOR first , // begin of the sequence
65  ITERATOR last , // end of the sequence
66  std::ostream& s , // the stream
67  const std::string& open , // opening
68  const std::string& close , // closing
69  const std::string& delim ) ; // delimiter
70  // ========================================================================
77  inline std::ostream& toStream
78  ( const std::string& obj , std::ostream& s )
79  {
80  auto c = ( std::string::npos == obj.find('\'') ? '\'' : '\"' );
81  return s << c << obj << c;
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
131  {
132  return toStream( obj.second,
133  toStream( obj.first, s << "( " ) << " , " )
134  << " )" ;
135  }
136  // ========================================================================
143  template<class TYPE,class ALLOCATOR>
144  inline std::ostream& toStream
146  {
147  return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
148  }
149  // ========================================================================
156  template<class TYPE,class ALLOCATOR>
157  inline std::ostream& toStream
159  {
160  return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
161  }
162  // ========================================================================
169  template<class TYPE,class CMP,class ALLOCATOR>
170  inline std::ostream& toStream
172  {
173  return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
174  }
175  // ========================================================================
183  template<class KTYPE, class VTYPE,class CMP,class ALLOCATOR>
184  inline std::ostream& toStream
186  {
188  return ostream_joiner( s << "{ ", obj, " , ",
190  -> std::ostream&
191  { return toStream( i.second, toStream( i.first, os ) << " : " ); }
192  ) << " }";
193  }
194  // ========================================================================
203  template<class KTYPE, class VTYPE,class CMP,class ALLOCATOR>
204  inline std::ostream& toStream
206  {
208  return ostream_joiner( s << "{ ", obj, " , ",
209  [](std::ostream& os, const std::pair<const KTYPE,VTYPE>& i )
210  -> std::ostream&
211  { return toStream( i.second, toStream( i.first, os ) << " : " ); }
212  ) << " }";
213  }
214  // ========================================================================
223  template<class KTYPE, class VTYPE,class MAP>
224  inline std::ostream& toStream
226  {
228  return ostream_joiner( s << "{ ", obj, " , " ,
230  -> std::ostream&
231  { return toStream( i.second, toStream( i.first, s ) << " : " ) ; }
232  ) << " }";
233  }
234  // ========================================================================
243  template<class KTYPE, class VTYPE,class HASH,class MAP>
244  inline std::ostream& toStream
246  {
247  // Copy the hash map into a map to have it ordered by key.
248  return toStream(GaudiUtils::Map<KTYPE,VTYPE>{obj.begin(), obj.end()}, s);
249  }
250  // ========================================================================
255  template <class TYPE, unsigned int N>
256  std::ostream& toStream ( const TYPE(&obj)[N] , std::ostream& s )
257  {
258  return toStream ( obj , obj + N , s , "( " , " )" , " , " ) ;
259  }
260  // ========================================================================
265  template <class TYPE, std::size_t N>
267  {
268  return toStream ( begin(obj) , end(obj) , s , "( " , " )" , " , " ) ;
269  }
270  // ========================================================================
275  template <unsigned int N>
276  std::ostream& toStream ( const char (&obj)[N] , std::ostream& s )
277  { return toStream ( std::string ( obj , obj+N ) , s ) ; }
278  // ========================================================================
283  inline std::ostream& toStream ( const char* obj , std::ostream& s )
284  { return toStream ( std::string ( obj ) , s ) ; }
285  // ========================================================================
291  template<class TYPE>
292  inline std::ostream& toStream ( const TYPE& obj, std::ostream& s )
293  { return s << obj ; }
294  // ========================================================================
306  template <class ITERATOR>
307  inline std::ostream& toStream
308  ( ITERATOR first , // begin of the sequence
309  ITERATOR last , // end of the sequence
310  std::ostream& s , // the stream
311  const std::string& open , // opening
312  const std::string& close , // closing
313  const std::string& delim ) // delimiter
314  {
315  using ref_t = typename std::iterator_traits<ITERATOR>::reference;
317  return ostream_joiner( s << open, first, last, delim,
318  [](std::ostream& os, ref_t i ) -> std::ostream&
319  { return toStream( i, os ); } ) << close;
320  }
321  // ========================================================================
322  // helper function to print a tuple of any size
323  template<class Tuple, std::size_t N>
324  struct TuplePrinter {
325  static std::ostream& toStream(const Tuple& t, std::ostream& s)
326  {
327  TuplePrinter<Tuple, N-1>::toStream(t, s) << " , ";
328  return Gaudi::Utils::toStream(std::get<N-1>(t), s);
329  }
330  };
331 
332  template<class Tuple>
333  struct TuplePrinter<Tuple, 1>{
334  static std::ostream& toStream(const Tuple& t, std::ostream& s)
335  {
336  return Gaudi::Utils::toStream(std::get<0>(t), s);
337  }
338  };
339 
346  template<typename... Args>
348  return TuplePrinter<decltype(tuple), sizeof...(Args)>::toStream(tuple, s << " ( ")<< " ) ";
349  }
350 
351  // ========================================================================
359  template <class TYPE>
360  inline std::string toString ( const TYPE& obj )
361  {
363  std::ios::fmtflags orig_flags = s.flags();
364  s.setf(std::ios::showpoint); // to display correctly floats
365  toStream ( obj , s);
366  s.flags(orig_flags);
367  return s.str();
368  }
369  // ========================================================================
370  } // end of namespace Gaudi::Utils
371  // ==========================================================================
372 } // end of namespace Gaudi
373 // ============================================================================
374 // The END
375 // ============================================================================
376 #endif
377 // ============================================================================
T setf(T...args)
A bit modified version of &#39;Loki::AssocVector&#39; associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:108
Extension of the STL map.
Definition: Map.h:82
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:308
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:334
AttribStringParser::Iterator end(const AttribStringParser &)
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:360
T precision(T...args)
T end(T...args)
STL class.
Provide serialization function (output only) for some common STL classes (vectors, lists, pairs, maps) plus GaudiUtils::Map and GaudiUtils::HashMap.
STL class.
T flags(T...args)
int N
Definition: IOTest.py:90
iterator end()
Definition: Map.h:132
STL class.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:35
iterator begin()
Definition: Map.h:131
T find(T...args)
STL class.
STL class.
T begin(T...args)
Common class providing an architecture-independent hash map.
Definition: HashMap.h:77
string s
Definition: gaudirun.py:245
STL class.
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:325
AttribStringParser::Iterator begin(const AttribStringParser &parser)
T setprecision(T...args)
STL class.
Helper functions to set/get the application return code.
Definition: __init__.py:1