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 <vector>
12 #include <map>
13 #include <set>
14 #include <list>
15 #include <string>
16 #include <sstream>
17 #include <type_traits>
18 // ============================================================================
19 // GaudiKernel
20 // ============================================================================
21 #include "GaudiKernel/Map.h"
22 #include "GaudiKernel/HashMap.h"
23 #include "GaudiKernel/VectorMap.h"
25 // ============================================================================
35 // ============================================================================
36 namespace Gaudi
37 {
38  // ==========================================================================
39  namespace Utils
40  {
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
63  ( 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  // ========================================================================
76  inline std::ostream& toStream
77  ( const std::string& obj , std::ostream& s )
78  {
79  auto c = ( std::string::npos == obj.find('\'') ? '\'' : '\"' );
80  return s << c << obj << c;
81  }
86  inline std::ostream& toStream
87  ( const bool obj , std::ostream& s )
88  { return s << ( obj ? "True" : "False" ) ; }
93  inline std::ostream& toStream
94  ( const float obj , std::ostream& s , const int prec = 6 )
95  {
96  const int p = s.precision() ;
97  return s << std::setprecision ( prec ) << obj << std::setprecision ( p ) ;
98  }
103  inline std::ostream& toStream
104  ( const double obj , std::ostream& s , const int prec = 8 )
105  {
106  const int p = s.precision() ;
107  return s << std::setprecision ( prec ) << obj << std::setprecision ( p ) ;
108  }
113  inline std::ostream& toStream
114  ( const long double obj , std::ostream& s , const int prec = 10 )
115  {
116  const int p = s.precision() ;
117  return s << std::setprecision ( prec ) << obj << std::setprecision ( p ) ;
118  }
119  // ========================================================================
127  template<class KTYPE, class VTYPE>
128  inline std::ostream& toStream
130  {
131  return toStream( obj.second,
132  toStream( obj.first, s << "( " ) << " , " )
133  << " )" ;
134  }
135  // ========================================================================
142  template<class TYPE,class ALLOCATOR>
143  inline std::ostream& toStream
145  {
146  return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
147  }
148  // ========================================================================
155  template<class TYPE,class ALLOCATOR>
156  inline std::ostream& toStream
158  {
159  return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
160  }
161  // ========================================================================
168  template<class TYPE,class CMP,class ALLOCATOR>
169  inline std::ostream& toStream
171  {
172  return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
173  }
174  // ========================================================================
182  template<class KTYPE, class VTYPE,class CMP,class ALLOCATOR>
183  inline std::ostream& toStream
185  {
187  return ostream_joiner( s << "{ ", obj, " , ",
189  -> std::ostream&
190  { return toStream( i.second, toStream( i.first, os ) << " : " ); }
191  ) << " }";
192  }
193  // ========================================================================
202  template<class KTYPE, class VTYPE,class CMP,class ALLOCATOR>
203  inline std::ostream& toStream
205  {
207  return ostream_joiner( s << "{ ", obj, " , ",
209  -> std::ostream&
210  { return toStream( i.second, toStream( i.first, os ) << " : " ); }
211  ) << " }";
212  }
213  // ========================================================================
222  template<class KTYPE, class VTYPE,class MAP>
223  inline std::ostream& toStream
225  {
227  return ostream_joiner( s << "{ ", obj, " , " ,
229  -> std::ostream&
230  { return toStream( i.second, toStream( i.first, s ) << " : " ) ; }
231  ) << " }";
232  }
233  // ========================================================================
242  template<class KTYPE, class VTYPE,class HASH,class MAP>
243  inline std::ostream& toStream
245  {
247  return ostream_joiner( s << "{ ", obj, " , ",
249  -> std::ostream&
250  { return toStream( p.second, toStream( p.first, os) << " : " ); }
251  ) << " }";
252  }
253  // ========================================================================
258  template <class TYPE, unsigned int N>
259  std::ostream& toStream ( TYPE(&obj)[N] , std::ostream& s )
260  {
261  return toStream ( obj , obj + N , s , "( " , " )" , " , " ) ;
262  }
263  // ========================================================================
268  template <class TYPE, unsigned int N>
269  std::ostream& toStream ( const TYPE(&obj)[N] , std::ostream& s )
270  {
271  return toStream ( obj , obj + N , s , "( " , " )" , " , " ) ;
272  }
273  // ========================================================================
278  template <unsigned int N>
279  std::ostream& toStream ( char (&obj)[N] , std::ostream& s )
280  { return toStream ( std::string ( obj , obj+N ) , s ) ; }
281  // ========================================================================
286  template <unsigned int N>
287  std::ostream& toStream ( const char (&obj)[N] , std::ostream& s )
288  { return toStream ( std::string ( obj , obj+N ) , s ) ; }
289  // ========================================================================
294  inline std::ostream& toStream ( const char* obj , std::ostream& s )
295  { return toStream ( std::string ( obj ) , s ) ; }
296  // ========================================================================
302  template<class TYPE>
303  inline std::ostream& toStream ( const TYPE& obj, std::ostream& s )
304  { return s << obj ; }
305  // ========================================================================
317  template <class ITERATOR>
318  inline std::ostream& toStream
319  ( ITERATOR first , // begin of the sequence
320  ITERATOR last , // end of the sequence
321  std::ostream& s , // the stream
322  const std::string& open , // opening
323  const std::string& close , // closing
324  const std::string& delim ) // delimiter
325  {
326  using ref_t = typename std::iterator_traits<ITERATOR>::reference;
328  return ostream_joiner( s << open, first, last, delim,
329  [](std::ostream& os, ref_t i ) -> std::ostream&
330  { return toStream( i, os ); } ) << close;
331  }
332  // ========================================================================
333  // helper function to print a tuple of any size
334  template<class Tuple, std::size_t N>
335  struct TuplePrinter {
336  static std::ostream& toStream(const Tuple& t, std::ostream& s)
337  {
338  TuplePrinter<Tuple, N-1>::toStream(t, s) << " , ";
339  return Gaudi::Utils::toStream(std::get<N-1>(t), s);
340  }
341  };
342 
343  template<class Tuple>
344  struct TuplePrinter<Tuple, 1>{
345  static std::ostream& toStream(const Tuple& t, std::ostream& s)
346  {
347  return Gaudi::Utils::toStream(std::get<0>(t), s);
348  }
349  };
350 
357  template<typename... Args>
359  return TuplePrinter<decltype(tuple), sizeof...(Args)>::toStream(tuple, s << " ( ")<< " ) ";
360  }
361 
362  // ========================================================================
370  template <class TYPE>
371  inline std::string toString ( const TYPE& obj )
372  {
374  std::ios::fmtflags orig_flags = s.flags();
375  s.setf(std::ios::showpoint); // to display correctly floats
376  toStream ( obj , s);
377  s.flags(orig_flags);
378  return s.str();
379  }
380  // ========================================================================
381  } // end of namespace Gaudi::Utils
382  // ==========================================================================
383 } // end of namespace Gaudi
384 // ============================================================================
385 // The END
386 // ============================================================================
387 #endif
388 // ============================================================================
T setf(T...args)
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:108
Extension of the STL map.
Definition: Map.h:82
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:345
tuple c
Definition: gaudirun.py:391
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:371
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
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:319
STL class.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:35
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
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:336
T setprecision(T...args)
list i
Definition: ana.py:128
STL class.
Helper functions to set/get the application return code.
Definition: __init__.py:1