Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  namespace Utils {
41  // ========================================================================
47  template <class TYPE>
48  std::ostream& toStream( const TYPE& obj, std::ostream& s );
49  // ========================================================================
61  template <class ITERATOR>
62  inline std::ostream& toStream( ITERATOR first, // begin of the sequence
63  ITERATOR last, // end of the sequence
64  std::ostream& s, // the stream
65  const std::string& open, // opening
66  const std::string& close, // closing
67  const std::string& delim ); // delimiter
68  // ========================================================================
75  inline std::ostream& toStream( const std::string& obj, std::ostream& s ) {
76  auto c = ( std::string::npos == obj.find( '\'' ) ? '\'' : '\"' );
77  return s << c << obj << c;
78  }
83  inline std::ostream& toStream( const bool obj, std::ostream& s ) { return s << ( obj ? "True" : "False" ); }
88  inline std::ostream& toStream( const float obj, std::ostream& s, const int prec = 6 ) {
89  const int p = s.precision();
90  return s << std::setprecision( prec ) << obj << std::setprecision( p );
91  }
96  inline std::ostream& toStream( const double obj, std::ostream& s, const int prec = 8 ) {
97  const int p = s.precision();
98  return s << std::setprecision( prec ) << obj << std::setprecision( p );
99  }
104  inline std::ostream& toStream( const long double obj, std::ostream& s, const int prec = 10 ) {
105  const int p = s.precision();
106  return s << std::setprecision( prec ) << obj << std::setprecision( p );
107  }
108  // ========================================================================
116  template <class KTYPE, class VTYPE>
118  return toStream( obj.second, toStream( obj.first, s << "( " ) << " , " ) << " )";
119  }
120  // ========================================================================
127  template <class TYPE, class ALLOCATOR>
129  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
130  }
131  // ========================================================================
138  template <class TYPE, class ALLOCATOR>
140  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
141  }
142  // ========================================================================
149  template <class TYPE, class CMP, class ALLOCATOR>
151  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
152  }
153  // ========================================================================
157  template <class TYPE, class HASH, class CMP, class ALLOCATOR>
159  return toStream( obj.begin(), obj.end(), s, "[", "]", " , " );
160  }
161  // ========================================================================
169  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
172  return ostream_joiner( s << "{ ", obj, " , ",
174  return toStream( i.second, toStream( i.first, os ) << " : " );
175  } )
176  << " }";
177  }
178  // ========================================================================
187  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
190  return ostream_joiner( s << "{ ", obj, " , ",
192  return toStream( i.second, toStream( i.first, os ) << " : " );
193  } )
194  << " }";
195  }
196  // ========================================================================
205  template <class KTYPE, class VTYPE, class MAP>
208  return ostream_joiner( s << "{ ", obj, " , ",
210  return toStream( i.second, toStream( i.first, os ) << " : " );
211  } )
212  << " }";
213  }
214  // ========================================================================
223  template <class KTYPE, class VTYPE, class HASH, class MAP>
225  // Copy the hash map into a map to have it ordered by key.
226  return toStream( GaudiUtils::Map<KTYPE, VTYPE>{obj.begin(), obj.end()}, s );
227  }
228  // ========================================================================
233  template <class TYPE, unsigned int N>
234  std::ostream& toStream( const TYPE ( &obj )[N], std::ostream& s ) {
235  return toStream( obj, obj + N, s, "( ", " )", " , " );
236  }
237  // ========================================================================
242  template <class TYPE, std::size_t N>
244  return toStream( begin( obj ), end( obj ), s, "( ", " )", " , " );
245  }
246  // ========================================================================
251  template <unsigned int N>
252  std::ostream& toStream( const char ( &obj )[N], std::ostream& s ) {
253  return toStream( std::string( obj, obj + N ), s );
254  }
255  // ========================================================================
260  inline std::ostream& toStream( const char* obj, std::ostream& s ) { return toStream( std::string( obj ), s ); }
261  // ========================================================================
267  template <class TYPE>
268  inline std::ostream& toStream( const TYPE& obj, std::ostream& s ) {
269  return s << obj;
270  }
271  // ========================================================================
283  template <class ITERATOR>
284  inline std::ostream& toStream( ITERATOR first, // begin of the sequence
285  ITERATOR last, // end of the sequence
286  std::ostream& s, // the stream
287  const std::string& open, // opening
288  const std::string& close, // closing
289  const std::string& delim ) // delimiter
290  {
291  using ref_t = typename std::iterator_traits<ITERATOR>::reference;
293  return ostream_joiner( s << open, first, last, delim,
294  []( std::ostream& os, ref_t i ) -> std::ostream& { return toStream( i, os ); } )
295  << close;
296  }
297  // ========================================================================
298  // helper function to print a tuple of any size
299  template <class Tuple, std::size_t N>
300  struct TuplePrinter {
301  static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
302  TuplePrinter<Tuple, N - 1>::toStream( t, s ) << " , ";
303  return Gaudi::Utils::toStream( std::get<N - 1>( t ), s );
304  }
305  };
306 
307  template <class Tuple>
308  struct TuplePrinter<Tuple, 1> {
309  static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
310  return Gaudi::Utils::toStream( std::get<0>( t ), s );
311  }
312  };
313 
320  template <typename... Args>
322  return TuplePrinter<decltype( tuple ), sizeof...( Args )>::toStream( tuple, s << " ( " ) << " ) ";
323  }
324 
325  // ========================================================================
333  template <class TYPE>
334  inline std::string toString( const TYPE& obj ) {
336  std::ios::fmtflags orig_flags = s.flags();
337  s.setf( std::ios::showpoint ); // to display correctly floats
338  toStream( obj, s );
339  s.flags( orig_flags );
340  return s.str();
341  }
342  // ========================================================================
343  } // namespace Utils
344  // ==========================================================================
345 } // end of namespace Gaudi
346 // ============================================================================
347 // The END
348 // ============================================================================
349 #endif
T setf(T...args)
A bit modified version of &#39;Loki::AssocVector&#39; associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:102
Extension of the STL map.
Definition: Map.h:81
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:284
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:309
AttribStringParser::Iterator end(const AttribStringParser &)
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:334
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:99
iterator end()
Definition: Map.h:130
STL class.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:37
iterator begin()
Definition: Map.h:129
T find(T...args)
STL class.
STL class.
T begin(T...args)
Common class providing an architecture-independent hash map.
Definition: HashMap.h:73
string s
Definition: gaudirun.py:312
STL class.
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:301
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