The Gaudi Framework  v30r3 (a5ef0a68)
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 <array>
10 #include <iomanip>
11 #include <iostream>
12 #include <list>
13 #include <map>
14 #include <set>
15 #include <sstream>
16 #include <string>
17 #include <type_traits>
18 #include <unordered_set>
19 #include <vector>
20 // ============================================================================
21 // GaudiKernel
22 // ============================================================================
23 #include "GaudiKernel/HashMap.h"
24 #include "GaudiKernel/Map.h"
26 #include "GaudiKernel/VectorMap.h"
27 // ============================================================================
37 // ============================================================================
38 namespace Gaudi
39 {
40  // ==========================================================================
41  namespace Utils
42  {
43  // ========================================================================
49  template <class TYPE>
50  std::ostream& toStream( const TYPE& obj, std::ostream& s );
51  // ========================================================================
63  template <class ITERATOR>
64  inline std::ostream& toStream( 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  // ========================================================================
78  {
79  auto c = ( std::string::npos == obj.find( '\'' ) ? '\'' : '\"' );
80  return s << c << obj << c;
81  }
86  inline std::ostream& toStream( const bool obj, std::ostream& s ) { return s << ( obj ? "True" : "False" ); }
91  inline std::ostream& toStream( const float obj, std::ostream& s, const int prec = 6 )
92  {
93  const int p = s.precision();
94  return s << std::setprecision( prec ) << obj << std::setprecision( p );
95  }
100  inline std::ostream& toStream( const double obj, std::ostream& s, const int prec = 8 )
101  {
102  const int p = s.precision();
103  return s << std::setprecision( prec ) << obj << std::setprecision( p );
104  }
109  inline std::ostream& toStream( const long double obj, std::ostream& s, const int prec = 10 )
110  {
111  const int p = s.precision();
112  return s << std::setprecision( prec ) << obj << std::setprecision( p );
113  }
114  // ========================================================================
122  template <class KTYPE, class VTYPE>
124  {
125  return toStream( obj.second, toStream( obj.first, s << "( " ) << " , " ) << " )";
126  }
127  // ========================================================================
134  template <class TYPE, class ALLOCATOR>
136  {
137  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
138  }
139  // ========================================================================
146  template <class TYPE, class ALLOCATOR>
148  {
149  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
150  }
151  // ========================================================================
158  template <class TYPE, class CMP, class ALLOCATOR>
160  {
161  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
162  }
163  // ========================================================================
167  template <class TYPE, class HASH, class CMP, class ALLOCATOR>
169  {
170  return toStream( obj.begin(), obj.end(), s, "[", "]", " , " );
171  }
172  // ========================================================================
180  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
182  {
184  return ostream_joiner( s << "{ ", obj, " , ",
186  return toStream( i.second, toStream( i.first, os ) << " : " );
187  } )
188  << " }";
189  }
190  // ========================================================================
199  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
201  {
203  return ostream_joiner( s << "{ ", obj, " , ",
205  return toStream( i.second, toStream( i.first, os ) << " : " );
206  } )
207  << " }";
208  }
209  // ========================================================================
218  template <class KTYPE, class VTYPE, class MAP>
220  {
222  return ostream_joiner( s << "{ ", obj, " , ",
224  return toStream( i.second, toStream( i.first, s ) << " : " );
225  } )
226  << " }";
227  }
228  // ========================================================================
237  template <class KTYPE, class VTYPE, class HASH, class MAP>
239  {
240  // Copy the hash map into a map to have it ordered by key.
241  return toStream( GaudiUtils::Map<KTYPE, VTYPE>{obj.begin(), obj.end()}, s );
242  }
243  // ========================================================================
248  template <class TYPE, unsigned int N>
249  std::ostream& toStream( const TYPE ( &obj )[N], std::ostream& s )
250  {
251  return toStream( obj, obj + N, s, "( ", " )", " , " );
252  }
253  // ========================================================================
258  template <class TYPE, std::size_t N>
260  {
261  return toStream( begin( obj ), end( obj ), s, "( ", " )", " , " );
262  }
263  // ========================================================================
268  template <unsigned int N>
269  std::ostream& toStream( const char ( &obj )[N], std::ostream& s )
270  {
271  return toStream( std::string( obj, obj + N ), s );
272  }
273  // ========================================================================
278  inline std::ostream& toStream( const char* obj, std::ostream& s ) { return toStream( std::string( obj ), s ); }
279  // ========================================================================
285  template <class TYPE>
286  inline std::ostream& toStream( const TYPE& obj, std::ostream& s )
287  {
288  return s << obj;
289  }
290  // ========================================================================
302  template <class ITERATOR>
303  inline std::ostream& toStream( ITERATOR first, // begin of the sequence
304  ITERATOR last, // end of the sequence
305  std::ostream& s, // the stream
306  const std::string& open, // opening
307  const std::string& close, // closing
308  const std::string& delim ) // delimiter
309  {
310  using ref_t = typename std::iterator_traits<ITERATOR>::reference;
312  return ostream_joiner( s << open, first, last, delim,
313  []( std::ostream& os, ref_t i ) -> std::ostream& { return toStream( i, os ); } )
314  << close;
315  }
316  // ========================================================================
317  // helper function to print a tuple of any size
318  template <class Tuple, std::size_t N>
319  struct TuplePrinter {
320  static std::ostream& toStream( const Tuple& t, std::ostream& s )
321  {
322  TuplePrinter<Tuple, N - 1>::toStream( t, s ) << " , ";
323  return Gaudi::Utils::toStream( std::get<N - 1>( t ), s );
324  }
325  };
326 
327  template <class Tuple>
328  struct TuplePrinter<Tuple, 1> {
329  static std::ostream& toStream( const Tuple& t, std::ostream& s )
330  {
331  return Gaudi::Utils::toStream( std::get<0>( t ), s );
332  }
333  };
334 
341  template <typename... Args>
343  {
344  return TuplePrinter<decltype( tuple ), sizeof...( Args )>::toStream( tuple, s << " ( " ) << " ) ";
345  }
346 
347  // ========================================================================
355  template <class TYPE>
356  inline std::string toString( const TYPE& obj )
357  {
359  std::ios::fmtflags orig_flags = s.flags();
360  s.setf( std::ios::showpoint ); // to display correctly floats
361  toStream( obj, s );
362  s.flags( orig_flags );
363  return s.str();
364  }
365  // ========================================================================
366  } // end of namespace Gaudi::Utils
367  // ==========================================================================
368 } // end of namespace Gaudi
369 // ============================================================================
370 // The END
371 // ============================================================================
372 #endif
373 // ============================================================================
T setf(T...args)
A bit modified version of &#39;Loki::AssocVector&#39; associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:103
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:303
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:329
AttribStringParser::Iterator end(const AttribStringParser &)
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:356
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:101
iterator end()
Definition: Map.h:134
STL class.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:40
iterator begin()
Definition: Map.h:133
T find(T...args)
STL class.
STL class.
T begin(T...args)
Common class providing an architecture-independent hash map.
Definition: HashMap.h:74
string s
Definition: gaudirun.py:253
STL class.
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:320
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