| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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"
24 #include "GaudiKernel/SerializeSTL.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
129 ( const std::pair<KTYPE,VTYPE>& obj, std::ostream& s)
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
144 ( const std::vector<TYPE,ALLOCATOR>& obj, std::ostream& s)
145 {
146 return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
147 }
148 // ========================================================================
155 template<class TYPE,class ALLOCATOR>
156 inline std::ostream& toStream
157 ( const std::list<TYPE,ALLOCATOR>& obj, std::ostream& s)
158 {
159 return toStream ( obj.begin() , obj.end () , s , "[ " , " ]" , " , " ) ;
160 }
161 // ========================================================================
168 template<class TYPE,class CMP,class ALLOCATOR>
169 inline std::ostream& toStream
170 ( const std::set<TYPE,CMP,ALLOCATOR>& obj, std::ostream& s)
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
184 ( const std::map<KTYPE,VTYPE,CMP,ALLOCATOR>& obj, std::ostream& s )
185 {
186 using GaudiUtils::details::ostream_joiner;
187 return ostream_joiner( s << "{ ", obj, " , ",
188 [](std::ostream& os, const std::pair<const KTYPE,VTYPE>& i)
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
204 ( const GaudiUtils::VectorMap<KTYPE,VTYPE,CMP,ALLOCATOR>& obj, std::ostream& s )
205 {
206 using GaudiUtils::details::ostream_joiner;
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
224 ( const GaudiUtils::Map<KTYPE,VTYPE,MAP>& obj, std::ostream& s)
225 {
226 using GaudiUtils::details::ostream_joiner;
227 return ostream_joiner( s << "{ ", obj, " , " ,
228 [](std::ostream& s, const std::pair<const KTYPE,VTYPE>& i )
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
244 ( const GaudiUtils::HashMap<KTYPE,VTYPE,HASH,MAP>& obj, std::ostream& s)
245 {
246 using GaudiUtils::details::ostream_joiner;
247 return ostream_joiner( s << "{ ", obj, " , ",
248 [](std::ostream& os, const std::pair<const KTYPE,VTYPE>& p)
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;
327 using GaudiUtils::details::ostream_joiner;
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>
358 inline std::ostream& toStream(const std::tuple<Args...>& tuple, std::ostream& s) {
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 {
373 std::ostringstream s;
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 // ============================================================================
std::ostringstream::setf
T setf(T...args)
GaudiUtils::VectorMap
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:108
GaudiUtils::Map
Extension of the STL map.
Definition: Map.h:82
Gaudi::Utils::TuplePrinter< Tuple, 1 >::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:345
gaudirun.c
tuple c
Definition: gaudirun.py:391
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:371
std::ostream::precision
T precision(T...args)
std::vector::end
T end(T...args)
Map.h
HashMap.h
std::map
STL class.
SerializeSTL.h
Provide serialization function (output only) for some common STL classes (vectors, lists, pairs, maps) plus GaudiUtils::Map and GaudiUtils::HashMap.
std::string
STL class.
std::ostringstream::flags
T flags(T...args)
IOTest.N
int N
Definition: IOTest.py:90
Gaudi::Utils::toStream
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
VectorMap.h
std::ostringstream
STL class.
std::ostringstream::str
T str(T...args)
std::iterator_traits
std::list
STL class.
GaudiUtils::details::ostream_joiner
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:35
std::tuple
std::pair
std::string::find
T find(T...args)
std::set
STL class.
std::vector
STL class.
std::vector::begin
T begin(T...args)
Gaudi::Utils::TuplePrinter
Definition: ToStream.h:335
GaudiUtils::HashMap
Common class providing an architecture-independent hash map.
Definition: HashMap.h:77
gaudirun.s
string s
Definition: gaudirun.py:245
Gaudi::Utils::TuplePrinter::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:336
std::setprecision
T setprecision(T...args)
ana.i
list i
Definition: ana.py:128
std::ostream
STL class.
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
GaudiPython.GaudiAlgs.Tuple
Tuple
Definition: GaudiAlgs.py:1041