| The Gaudi Framework v28r2p1 (f1a77ff4)

- 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 // 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;
323 using GaudiUtils::details::ostream_joiner;
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>
354 inline std::ostream& toStream(const std::tuple<Args...>& tuple, std::ostream& s) {
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 {
369 std::ostringstream s;
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 // ============================================================================
std::ostringstream::setf
T setf(T...args)
GaudiUtils::VectorMap
A bit modified version of &#39;Loki::AssocVector&#39; 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::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:315
Gaudi::Utils::TuplePrinter< Tuple, 1 >::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:341
gaudirun.c
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:367
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
GaudiUtils::Map::end
iterator end()
Definition: Map.h:132
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
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:131
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:331
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:332
std::setprecision
T setprecision(T...args)
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