The Gaudi Framework  v29r0 (ff2e7097)
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 <vector>
19 // ============================================================================
20 // GaudiKernel
21 // ============================================================================
22 #include "GaudiKernel/HashMap.h"
23 #include "GaudiKernel/Map.h"
25 #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( ITERATOR first, // begin of the sequence
64  ITERATOR last, // end of the sequence
65  std::ostream& s, // the stream
66  const std::string& open, // opening
67  const std::string& close, // closing
68  const std::string& delim ); // delimiter
69  // ========================================================================
77  {
78  auto c = ( std::string::npos == obj.find( '\'' ) ? '\'' : '\"' );
79  return s << c << obj << c;
80  }
85  inline std::ostream& toStream( const bool obj, std::ostream& s ) { return s << ( obj ? "True" : "False" ); }
90  inline std::ostream& toStream( const float obj, std::ostream& s, const int prec = 6 )
91  {
92  const int p = s.precision();
93  return s << std::setprecision( prec ) << obj << std::setprecision( p );
94  }
99  inline std::ostream& toStream( const double obj, std::ostream& s, const int prec = 8 )
100  {
101  const int p = s.precision();
102  return s << std::setprecision( prec ) << obj << std::setprecision( p );
103  }
108  inline std::ostream& toStream( const long double obj, std::ostream& s, const int prec = 10 )
109  {
110  const int p = s.precision();
111  return s << std::setprecision( prec ) << obj << std::setprecision( p );
112  }
113  // ========================================================================
121  template <class KTYPE, class VTYPE>
123  {
124  return toStream( obj.second, toStream( obj.first, s << "( " ) << " , " ) << " )";
125  }
126  // ========================================================================
133  template <class TYPE, class ALLOCATOR>
135  {
136  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
137  }
138  // ========================================================================
145  template <class TYPE, class ALLOCATOR>
147  {
148  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
149  }
150  // ========================================================================
157  template <class TYPE, class CMP, class ALLOCATOR>
159  {
160  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
161  }
162  // ========================================================================
170  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
172  {
174  return ostream_joiner( s << "{ ", obj, " , ",
176  return toStream( i.second, toStream( i.first, os ) << " : " );
177  } )
178  << " }";
179  }
180  // ========================================================================
189  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
191  {
193  return ostream_joiner( s << "{ ", obj, " , ",
195  return toStream( i.second, toStream( i.first, os ) << " : " );
196  } )
197  << " }";
198  }
199  // ========================================================================
208  template <class KTYPE, class VTYPE, class MAP>
210  {
212  return ostream_joiner( s << "{ ", obj, " , ",
214  return toStream( i.second, toStream( i.first, s ) << " : " );
215  } )
216  << " }";
217  }
218  // ========================================================================
227  template <class KTYPE, class VTYPE, class HASH, class MAP>
229  {
230  // Copy the hash map into a map to have it ordered by key.
231  return toStream( GaudiUtils::Map<KTYPE, VTYPE>{obj.begin(), obj.end()}, s );
232  }
233  // ========================================================================
238  template <class TYPE, unsigned int N>
239  std::ostream& toStream( const TYPE ( &obj )[N], std::ostream& s )
240  {
241  return toStream( obj, obj + N, s, "( ", " )", " , " );
242  }
243  // ========================================================================
248  template <class TYPE, std::size_t N>
250  {
251  return toStream( begin( obj ), end( obj ), s, "( ", " )", " , " );
252  }
253  // ========================================================================
258  template <unsigned int N>
259  std::ostream& toStream( const char ( &obj )[N], std::ostream& s )
260  {
261  return toStream( std::string( obj, obj + N ), s );
262  }
263  // ========================================================================
268  inline std::ostream& toStream( const char* obj, std::ostream& s ) { return toStream( std::string( obj ), s ); }
269  // ========================================================================
275  template <class TYPE>
276  inline std::ostream& toStream( const TYPE& obj, std::ostream& s )
277  {
278  return s << obj;
279  }
280  // ========================================================================
292  template <class ITERATOR>
293  inline std::ostream& toStream( ITERATOR first, // begin of the sequence
294  ITERATOR last, // end of the sequence
295  std::ostream& s, // the stream
296  const std::string& open, // opening
297  const std::string& close, // closing
298  const std::string& delim ) // delimiter
299  {
300  using ref_t = typename std::iterator_traits<ITERATOR>::reference;
302  return ostream_joiner( s << open, first, last, delim,
303  []( std::ostream& os, ref_t i ) -> std::ostream& { return toStream( i, os ); } )
304  << close;
305  }
306  // ========================================================================
307  // helper function to print a tuple of any size
308  template <class Tuple, std::size_t N>
309  struct TuplePrinter {
310  static std::ostream& toStream( const Tuple& t, std::ostream& s )
311  {
312  TuplePrinter<Tuple, N - 1>::toStream( t, s ) << " , ";
313  return Gaudi::Utils::toStream( std::get<N - 1>( t ), s );
314  }
315  };
316 
317  template <class Tuple>
318  struct TuplePrinter<Tuple, 1> {
319  static std::ostream& toStream( const Tuple& t, std::ostream& s )
320  {
321  return Gaudi::Utils::toStream( std::get<0>( t ), s );
322  }
323  };
324 
331  template <typename... Args>
333  {
334  return TuplePrinter<decltype( tuple ), sizeof...( Args )>::toStream( tuple, s << " ( " ) << " ) ";
335  }
336 
337  // ========================================================================
345  template <class TYPE>
346  inline std::string toString( const TYPE& obj )
347  {
349  std::ios::fmtflags orig_flags = s.flags();
350  s.setf( std::ios::showpoint ); // to display correctly floats
351  toStream( obj, s );
352  s.flags( orig_flags );
353  return s.str();
354  }
355  // ========================================================================
356  } // end of namespace Gaudi::Utils
357  // ==========================================================================
358 } // end of namespace Gaudi
359 // ============================================================================
360 // The END
361 // ============================================================================
362 #endif
363 // ============================================================================
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:293
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:319
AttribStringParser::Iterator end(const AttribStringParser &)
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:346
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:310
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