Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
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 <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, " , ",
208  [](std::ostream& os, const std::pair<const KTYPE,VTYPE>& i )
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  {
246  // Copy the hash map into a map to have it ordered by key.
247  return toStream(GaudiUtils::Map<KTYPE,VTYPE>{obj.begin(), obj.end()}, s);
248  }
249  // ========================================================================
254  template <class TYPE, unsigned int N>
255  std::ostream& toStream ( TYPE(&obj)[N] , std::ostream& s )
256  {
257  return toStream ( obj , obj + N , s , "( " , " )" , " , " ) ;
258  }
259  // ========================================================================
264  template <class TYPE, unsigned int N>
265  std::ostream& toStream ( const TYPE(&obj)[N] , std::ostream& s )
266  {
267  return toStream ( obj , obj + N , s , "( " , " )" , " , " ) ;
268  }
269  // ========================================================================
274  template <unsigned int N>
275  std::ostream& toStream ( char (&obj)[N] , std::ostream& s )
276  { return toStream ( std::string ( obj , obj+N ) , s ) ; }
277  // ========================================================================
282  template <unsigned int N>
283  std::ostream& toStream ( const char (&obj)[N] , std::ostream& s )
284  { return toStream ( std::string ( obj , obj+N ) , s ) ; }
285  // ========================================================================
290  inline std::ostream& toStream ( const char* obj , std::ostream& s )
291  { return toStream ( std::string ( obj ) , s ) ; }
292  // ========================================================================
298  template<class TYPE>
299  inline std::ostream& toStream ( const TYPE& obj, std::ostream& s )
300  { return s << obj ; }
301  // ========================================================================
313  template <class ITERATOR>
314  inline std::ostream& toStream
315  ( ITERATOR first , // begin of the sequence
316  ITERATOR last , // end of the sequence
317  std::ostream& s , // the stream
318  const std::string& open , // opening
319  const std::string& close , // closing
320  const std::string& delim ) // delimiter
321  {
322  using ref_t = typename std::iterator_traits<ITERATOR>::reference;
324  return ostream_joiner( s << open, first, last, delim,
325  [](std::ostream& os, ref_t i ) -> std::ostream&
326  { return toStream( i, os ); } ) << close;
327  }
328  // ========================================================================
329  // helper function to print a tuple of any size
330  template<class Tuple, std::size_t N>
331  struct TuplePrinter {
332  static std::ostream& toStream(const Tuple& t, std::ostream& s)
333  {
334  TuplePrinter<Tuple, N-1>::toStream(t, s) << " , ";
335  return Gaudi::Utils::toStream(std::get<N-1>(t), s);
336  }
337  };
338 
339  template<class Tuple>
340  struct TuplePrinter<Tuple, 1>{
341  static std::ostream& toStream(const Tuple& t, std::ostream& s)
342  {
343  return Gaudi::Utils::toStream(std::get<0>(t), s);
344  }
345  };
346 
353  template<typename... Args>
355  return TuplePrinter<decltype(tuple), sizeof...(Args)>::toStream(tuple, s << " ( ")<< " ) ";
356  }
357 
358  // ========================================================================
366  template <class TYPE>
367  inline std::string toString ( const TYPE& obj )
368  {
370  std::ios::fmtflags orig_flags = s.flags();
371  s.setf(std::ios::showpoint); // to display correctly floats
372  toStream ( obj , s);
373  s.flags(orig_flags);
374  return s.str();
375  }
376  // ========================================================================
377  } // end of namespace Gaudi::Utils
378  // ==========================================================================
379 } // end of namespace Gaudi
380 // ============================================================================
381 // The END
382 // ============================================================================
383 #endif
384 // ============================================================================
T setf(T...args)
A bit modified version of &#39;Loki::AssocVector&#39; associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:108
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:315
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:341
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:367
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
iterator end()
Definition: Map.h:132
STL class.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:35
iterator begin()
Definition: Map.h:131
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:332
T setprecision(T...args)
STL class.
Helper functions to set/get the application return code.
Definition: __init__.py:1