| The Gaudi Framework v29r0 (ff2e7097)

- 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 <array>
10 #include <iomanip>
11 #include <iostream>
12 #include <list>
13 #include <map>
14 #include <set>
15 #include <sstream>
16 #include <string>
17 #include <type_traits>
18 #include <vector>
19 // ============================================================================
20 // GaudiKernel
21 // ============================================================================
22 #include "GaudiKernel/HashMap.h"
23 #include "GaudiKernel/Map.h"
24 #include "GaudiKernel/SerializeSTL.h"
25 #include "GaudiKernel/VectorMap.h"
26 // ============================================================================
36 // ============================================================================
37 namespace Gaudi
38 {
39 // ==========================================================================
40 namespace Utils
41 {
42 // ========================================================================
48 template <class TYPE>
49 std::ostream& toStream( const TYPE& obj, std::ostream& s );
50 // ========================================================================
62 template <class ITERATOR>
63 inline std::ostream& toStream( 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( const std::string& obj, std::ostream& s )
77 {
78 auto c = ( std::string::npos == obj.find( '\'' ) ? '\'' : '\"' );
79 return s << c << obj << c;
80 }
85 inline std::ostream& toStream( const bool obj, std::ostream& s ) { return s << ( obj ? "True" : "False" ); }
90 inline std::ostream& toStream( const float obj, std::ostream& s, const int prec = 6 )
91 {
92 const int p = s.precision();
93 return s << std::setprecision( prec ) << obj << std::setprecision( p );
94 }
99 inline std::ostream& toStream( const double obj, std::ostream& s, const int prec = 8 )
100 {
101 const int p = s.precision();
102 return s << std::setprecision( prec ) << obj << std::setprecision( p );
103 }
108 inline std::ostream& toStream( const long double obj, std::ostream& s, const int prec = 10 )
109 {
110 const int p = s.precision();
111 return s << std::setprecision( prec ) << obj << std::setprecision( p );
112 }
113 // ========================================================================
121 template <class KTYPE, class VTYPE>
122 inline std::ostream& toStream( const std::pair<KTYPE, VTYPE>& obj, std::ostream& s )
123 {
124 return toStream( obj.second, toStream( obj.first, s << "( " ) << " , " ) << " )";
125 }
126 // ========================================================================
133 template <class TYPE, class ALLOCATOR>
134 inline std::ostream& toStream( const std::vector<TYPE, ALLOCATOR>& obj, std::ostream& s )
135 {
136 return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
137 }
138 // ========================================================================
145 template <class TYPE, class ALLOCATOR>
146 inline std::ostream& toStream( const std::list<TYPE, ALLOCATOR>& obj, std::ostream& s )
147 {
148 return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
149 }
150 // ========================================================================
157 template <class TYPE, class CMP, class ALLOCATOR>
158 inline std::ostream& toStream( const std::set<TYPE, CMP, ALLOCATOR>& obj, std::ostream& s )
159 {
160 return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
161 }
162 // ========================================================================
170 template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
171 inline std::ostream& toStream( const std::map<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s )
172 {
173 using GaudiUtils::details::ostream_joiner;
174 return ostream_joiner( s << "{ ", obj, " , ",
175 []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
176 return toStream( i.second, toStream( i.first, os ) << " : " );
177 } )
178 << " }";
179 }
180 // ========================================================================
189 template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
190 inline std::ostream& toStream( const GaudiUtils::VectorMap<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s )
191 {
192 using GaudiUtils::details::ostream_joiner;
193 return ostream_joiner( s << "{ ", obj, " , ",
194 []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
195 return toStream( i.second, toStream( i.first, os ) << " : " );
196 } )
197 << " }";
198 }
199 // ========================================================================
208 template <class KTYPE, class VTYPE, class MAP>
209 inline std::ostream& toStream( const GaudiUtils::Map<KTYPE, VTYPE, MAP>& obj, std::ostream& s )
210 {
211 using GaudiUtils::details::ostream_joiner;
212 return ostream_joiner( s << "{ ", obj, " , ",
213 []( std::ostream& s, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
214 return toStream( i.second, toStream( i.first, s ) << " : " );
215 } )
216 << " }";
217 }
218 // ========================================================================
227 template <class KTYPE, class VTYPE, class HASH, class MAP>
228 inline std::ostream& toStream( const GaudiUtils::HashMap<KTYPE, VTYPE, HASH, MAP>& obj, std::ostream& s )
229 {
230 // Copy the hash map into a map to have it ordered by key.
231 return toStream( GaudiUtils::Map<KTYPE, VTYPE>{obj.begin(), obj.end()}, s );
232 }
233 // ========================================================================
238 template <class TYPE, unsigned int N>
239 std::ostream& toStream( const TYPE ( &obj )[N], std::ostream& s )
240 {
241 return toStream( obj, obj + N, s, "( ", " )", " , " );
242 }
243 // ========================================================================
248 template <class TYPE, std::size_t N>
249 std::ostream& toStream( const std::array<TYPE, N>& obj, std::ostream& s )
250 {
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 {
261 return toStream( std::string( obj, obj + N ), s );
262 }
263 // ========================================================================
268 inline std::ostream& toStream( const char* obj, std::ostream& s ) { return toStream( std::string( obj ), s ); }
269 // ========================================================================
275 template <class TYPE>
276 inline std::ostream& toStream( const TYPE& obj, std::ostream& s )
277 {
278 return s << obj;
279 }
280 // ========================================================================
292 template <class ITERATOR>
293 inline std::ostream& toStream( ITERATOR first, // begin of the sequence
294 ITERATOR last, // end of the sequence
295 std::ostream& s, // the stream
296 const std::string& open, // opening
297 const std::string& close, // closing
298 const std::string& delim ) // delimiter
299 {
300 using ref_t = typename std::iterator_traits<ITERATOR>::reference;
301 using GaudiUtils::details::ostream_joiner;
302 return ostream_joiner( s << open, first, last, delim,
303 []( std::ostream& os, ref_t i ) -> std::ostream& { return toStream( i, os ); } )
304 << close;
305 }
306 // ========================================================================
307 // helper function to print a tuple of any size
308 template <class Tuple, std::size_t N>
309 struct TuplePrinter {
310 static std::ostream& toStream( const Tuple& t, std::ostream& s )
311 {
312 TuplePrinter<Tuple, N - 1>::toStream( t, s ) << " , ";
313 return Gaudi::Utils::toStream( std::get<N - 1>( t ), s );
314 }
315 };
316
317 template <class Tuple>
318 struct TuplePrinter<Tuple, 1> {
319 static std::ostream& toStream( const Tuple& t, std::ostream& s )
320 {
321 return Gaudi::Utils::toStream( std::get<0>( t ), s );
322 }
323 };
324
331 template <typename... Args>
332 inline std::ostream& toStream( const std::tuple<Args...>& tuple, std::ostream& s )
333 {
334 return TuplePrinter<decltype( tuple ), sizeof...( Args )>::toStream( tuple, s << " ( " ) << " ) ";
335 }
336
337 // ========================================================================
345 template <class TYPE>
346 inline std::string toString( const TYPE& obj )
347 {
348 std::ostringstream s;
349 std::ios::fmtflags orig_flags = s.flags();
350 s.setf( std::ios::showpoint ); // to display correctly floats
351 toStream( obj, s );
352 s.flags( orig_flags );
353 return s.str();
354 }
355 // ========================================================================
356 } // end of namespace Gaudi::Utils
357 // ==========================================================================
358 } // end of namespace Gaudi
359 // ============================================================================
360 // The END
361 // ============================================================================
362 #endif
363 // ============================================================================
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:103
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:293
Gaudi::Utils::TuplePrinter< Tuple, 1 >::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:319
gaudirun.c
c
Definition: gaudirun.py:407
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:133
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:346
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:101
GaudiUtils::Map::end
iterator end()
Definition: Map.h:134
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:40
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:133
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:309
GaudiUtils::HashMap
Common class providing an architecture-independent hash map.
Definition: HashMap.h:74
gaudirun.s
string s
Definition: gaudirun.py:253
std::array
STL class.
Gaudi::Utils::TuplePrinter::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:310
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:129
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:1130