The Gaudi Framework  v33r0 (d5ea422b)
ToStream.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ============================================================================
12 #ifndef GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H
13 #define GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H 1
14 // ============================================================================
15 // Include files
16 // ============================================================================
17 // STD & STL
18 // ============================================================================
19 #include <array>
20 #include <iomanip>
21 #include <iostream>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <sstream>
26 #include <string>
27 #include <type_traits>
28 #include <unordered_set>
29 #include <vector>
30 // ============================================================================
31 // GaudiKernel
32 // ============================================================================
33 #include "GaudiKernel/HashMap.h"
34 #include "GaudiKernel/Map.h"
36 #include "GaudiKernel/VectorMap.h"
37 // ============================================================================
47 // ============================================================================
48 namespace Gaudi {
49  // ==========================================================================
50  namespace Utils {
51  // ========================================================================
57  template <class TYPE>
58  std::ostream& toStream( const TYPE& obj, std::ostream& s );
59  // ========================================================================
71  template <class ITERATOR>
72  inline std::ostream& toStream( ITERATOR first, // begin of the sequence
73  ITERATOR last, // end of the sequence
74  std::ostream& s, // the stream
75  const std::string& open, // opening
76  const std::string& close, // closing
77  const std::string& delim ); // delimiter
78  // ========================================================================
85  inline std::ostream& toStream( const std::string& obj, std::ostream& s ) { return s << std::quoted( obj, '\'' ); }
90  inline std::ostream& toStream( const bool obj, std::ostream& s ) { return s << ( obj ? "True" : "False" ); }
95  inline std::ostream& toStream( const float obj, std::ostream& s, const int prec = 6 ) {
96  const int p = s.precision();
97  return s << std::setprecision( prec ) << obj << std::setprecision( p );
98  }
103  inline std::ostream& toStream( const double obj, std::ostream& s, const int prec = 8 ) {
104  const int p = s.precision();
105  return s << std::setprecision( prec ) << obj << std::setprecision( p );
106  }
111  inline std::ostream& toStream( const long double obj, std::ostream& s, const int prec = 10 ) {
112  const int p = s.precision();
113  return s << std::setprecision( prec ) << obj << std::setprecision( p );
114  }
115  // ========================================================================
123  template <class KTYPE, class VTYPE>
125  return toStream( obj.second, toStream( obj.first, s << "( " ) << " , " ) << " )";
126  }
127  // ========================================================================
134  template <class TYPE, class ALLOCATOR>
136  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
137  }
138  // ========================================================================
145  template <class TYPE, class ALLOCATOR>
147  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
148  }
149  // ========================================================================
156  template <class TYPE, class CMP, class ALLOCATOR>
158  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
159  }
160  // ========================================================================
164  template <class TYPE, class HASH, class CMP, class ALLOCATOR>
166  return toStream( obj.begin(), obj.end(), s, "[", "]", " , " );
167  }
168  // ========================================================================
176  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
179  return ostream_joiner( s << "{ ", obj, " , ",
181  return toStream( i.second, toStream( i.first, os ) << " : " );
182  } )
183  << " }";
184  }
185  // ========================================================================
194  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
197  return ostream_joiner( s << "{ ", obj, " , ",
199  return toStream( i.second, toStream( i.first, os ) << " : " );
200  } )
201  << " }";
202  }
203  // ========================================================================
212  template <class KTYPE, class VTYPE, class MAP>
215  return ostream_joiner( s << "{ ", obj, " , ",
217  return toStream( i.second, toStream( i.first, os ) << " : " );
218  } )
219  << " }";
220  }
221  // ========================================================================
230  template <class KTYPE, class VTYPE, class HASH, class MAP>
232  // Copy the hash map into a map to have it ordered by key.
233  return toStream( GaudiUtils::Map<KTYPE, VTYPE>{obj.begin(), obj.end()}, s );
234  }
235  // ========================================================================
240  template <class TYPE, unsigned int N>
241  std::ostream& toStream( const TYPE ( &obj )[N], std::ostream& s ) {
242  return toStream( obj, obj + N, s, "( ", " )", " , " );
243  }
244  // ========================================================================
249  template <class TYPE, std::size_t N>
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  return toStream( std::string( obj, obj + N ), s );
261  }
262  // ========================================================================
267  inline std::ostream& toStream( const char* obj, std::ostream& s ) { return toStream( std::string( obj ), s ); }
268  // ========================================================================
274  template <class TYPE>
275  inline std::ostream& toStream( const TYPE& obj, std::ostream& s ) {
276  return s << obj;
277  }
278  // ========================================================================
290  template <class ITERATOR>
291  inline std::ostream& toStream( ITERATOR first, // begin of the sequence
292  ITERATOR last, // end of the sequence
293  std::ostream& s, // the stream
294  const std::string& open, // opening
295  const std::string& close, // closing
296  const std::string& delim ) // delimiter
297  {
298  using ref_t = typename std::iterator_traits<ITERATOR>::reference;
300  return ostream_joiner( s << open, first, last, delim,
301  []( std::ostream& os, ref_t i ) -> std::ostream& { return toStream( i, os ); } )
302  << close;
303  }
304  // ========================================================================
305  // helper function to print a tuple of any size
306  template <class Tuple, std::size_t N>
307  struct TuplePrinter {
308  static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
310  return Gaudi::Utils::toStream( std::get<N - 1>( t ), s );
311  }
312  };
313 
314  template <class Tuple>
315  struct TuplePrinter<Tuple, 1> {
316  static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
317  return Gaudi::Utils::toStream( std::get<0>( t ), s );
318  }
319  };
320 
327  template <typename... Args>
329  return TuplePrinter<decltype( tuple ), sizeof...( Args )>::toStream( tuple, s << " ( " ) << " ) ";
330  }
331 
332  // ========================================================================
340  template <class TYPE>
341  inline std::string toString( const TYPE& obj ) {
343  std::ios::fmtflags orig_flags = s.flags();
344  s.setf( std::ios::showpoint ); // to display correctly floats
345  toStream( obj, s );
346  s.flags( orig_flags );
347  return s.str();
348  }
349  // ========================================================================
350  } // namespace Utils
351  // ==========================================================================
352 } // end of namespace Gaudi
353 // ============================================================================
354 // The END
355 // ============================================================================
356 #endif
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:112
Extension of the STL map.
Definition: Map.h:91
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:291
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:316
AttribStringParser::Iterator end(const AttribStringParser &)
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:341
T end(T... args)
STL class.
Provide serialization function (output only) for some common STL classes (vectors,...
STL class.
int N
Definition: IOTest.py:110
iterator end()
Definition: Map.h:140
STL class.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:47
iterator begin()
Definition: Map.h:139
STL class.
STL class.
T begin(T... args)
Common class providing an architecture-independent hash map.
Definition: HashMap.h:83
string s
Definition: gaudirun.py:328
STL class.
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:308
AttribStringParser::Iterator begin(const AttribStringParser &parser)
T setprecision(T... args)
STL class.
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1