| The Gaudi Framework v32r1 (f65d50dc)

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 <unordered_set>
19 #include <vector>
20 // ============================================================================
21 // GaudiKernel
22 // ============================================================================
23 #include "GaudiKernel/HashMap.h"
24 #include "GaudiKernel/Map.h"
25 #include "GaudiKernel/SerializeSTL.h"
26 #include "GaudiKernel/VectorMap.h"
27 // ============================================================================
37 // ============================================================================
38 namespace Gaudi {
39 // ==========================================================================
40 namespace Utils {
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( ITERATOR first, // begin of the sequence
63 ITERATOR last, // end of the sequence
64 std::ostream& s, // the stream
65 const std::string& open, // opening
66 const std::string& close, // closing
67 const std::string& delim ); // delimiter
68 // ========================================================================
75 inline std::ostream& toStream( const std::string& obj, std::ostream& s ) {
76 auto c = ( std::string::npos == obj.find( '\'' ) ? '\'' : '\"' );
77 return s << c << obj << c;
78 }
83 inline std::ostream& toStream( const bool obj, std::ostream& s ) { return s << ( obj ? "True" : "False" ); }
88 inline std::ostream& toStream( const float obj, std::ostream& s, const int prec = 6 ) {
89 const int p = s.precision();
90 return s << std::setprecision( prec ) << obj << std::setprecision( p );
91 }
96 inline std::ostream& toStream( const double obj, std::ostream& s, const int prec = 8 ) {
97 const int p = s.precision();
98 return s << std::setprecision( prec ) << obj << std::setprecision( p );
99 }
104 inline std::ostream& toStream( const long double obj, std::ostream& s, const int prec = 10 ) {
105 const int p = s.precision();
106 return s << std::setprecision( prec ) << obj << std::setprecision( p );
107 }
108 // ========================================================================
116 template <class KTYPE, class VTYPE>
117 inline std::ostream& toStream( const std::pair<KTYPE, VTYPE>& obj, std::ostream& s ) {
118 return toStream( obj.second, toStream( obj.first, s << "( " ) << " , " ) << " )";
119 }
120 // ========================================================================
127 template <class TYPE, class ALLOCATOR>
128 inline std::ostream& toStream( const std::vector<TYPE, ALLOCATOR>& obj, std::ostream& s ) {
129 return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
130 }
131 // ========================================================================
138 template <class TYPE, class ALLOCATOR>
139 inline std::ostream& toStream( const std::list<TYPE, ALLOCATOR>& obj, std::ostream& s ) {
140 return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
141 }
142 // ========================================================================
149 template <class TYPE, class CMP, class ALLOCATOR>
150 inline std::ostream& toStream( const std::set<TYPE, CMP, ALLOCATOR>& obj, std::ostream& s ) {
151 return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
152 }
153 // ========================================================================
157 template <class TYPE, class HASH, class CMP, class ALLOCATOR>
158 inline std::ostream& toStream( const std::unordered_set<TYPE, HASH, CMP, ALLOCATOR>& obj, std::ostream& s ) {
159 return toStream( obj.begin(), obj.end(), s, "[", "]", " , " );
160 }
161 // ========================================================================
169 template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
170 inline std::ostream& toStream( const std::map<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s ) {
171 using GaudiUtils::details::ostream_joiner;
172 return ostream_joiner( s << "{ ", obj, " , ",
173 []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
174 return toStream( i.second, toStream( i.first, os ) << " : " );
175 } )
176 << " }";
177 }
178 // ========================================================================
187 template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
188 inline std::ostream& toStream( const GaudiUtils::VectorMap<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s ) {
189 using GaudiUtils::details::ostream_joiner;
190 return ostream_joiner( s << "{ ", obj, " , ",
191 []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
192 return toStream( i.second, toStream( i.first, os ) << " : " );
193 } )
194 << " }";
195 }
196 // ========================================================================
205 template <class KTYPE, class VTYPE, class MAP>
206 inline std::ostream& toStream( const GaudiUtils::Map<KTYPE, VTYPE, MAP>& obj, std::ostream& s ) {
207 using GaudiUtils::details::ostream_joiner;
208 return ostream_joiner( s << "{ ", obj, " , ",
209 []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
210 return toStream( i.second, toStream( i.first, os ) << " : " );
211 } )
212 << " }";
213 }
214 // ========================================================================
223 template <class KTYPE, class VTYPE, class HASH, class MAP>
224 inline std::ostream& toStream( const GaudiUtils::HashMap<KTYPE, VTYPE, HASH, MAP>& obj, std::ostream& s ) {
225 // Copy the hash map into a map to have it ordered by key.
226 return toStream( GaudiUtils::Map<KTYPE, VTYPE>{obj.begin(), obj.end()}, s );
227 }
228 // ========================================================================
233 template <class TYPE, unsigned int N>
234 std::ostream& toStream( const TYPE ( &obj )[N], std::ostream& s ) {
235 return toStream( obj, obj + N, s, "( ", " )", " , " );
236 }
237 // ========================================================================
242 template <class TYPE, std::size_t N>
243 std::ostream& toStream( const std::array<TYPE, N>& obj, std::ostream& s ) {
244 return toStream( begin( obj ), end( obj ), s, "( ", " )", " , " );
245 }
246 // ========================================================================
251 template <unsigned int N>
252 std::ostream& toStream( const char ( &obj )[N], std::ostream& s ) {
253 return toStream( std::string( obj, obj + N ), s );
254 }
255 // ========================================================================
260 inline std::ostream& toStream( const char* obj, std::ostream& s ) { return toStream( std::string( obj ), s ); }
261 // ========================================================================
267 template <class TYPE>
268 inline std::ostream& toStream( const TYPE& obj, std::ostream& s ) {
269 return s << obj;
270 }
271 // ========================================================================
283 template <class ITERATOR>
284 inline std::ostream& toStream( ITERATOR first, // begin of the sequence
285 ITERATOR last, // end of the sequence
286 std::ostream& s, // the stream
287 const std::string& open, // opening
288 const std::string& close, // closing
289 const std::string& delim ) // delimiter
290 {
291 using ref_t = typename std::iterator_traits<ITERATOR>::reference;
292 using GaudiUtils::details::ostream_joiner;
293 return ostream_joiner( s << open, first, last, delim,
294 []( std::ostream& os, ref_t i ) -> std::ostream& { return toStream( i, os ); } )
295 << close;
296 }
297 // ========================================================================
298 // helper function to print a tuple of any size
299 template <class Tuple, std::size_t N>
300 struct TuplePrinter {
301 static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
302 TuplePrinter<Tuple, N - 1>::toStream( t, s ) << " , ";
303 return Gaudi::Utils::toStream( std::get<N - 1>( t ), s );
304 }
305 };
306
307 template <class Tuple>
308 struct TuplePrinter<Tuple, 1> {
309 static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
310 return Gaudi::Utils::toStream( std::get<0>( t ), s );
311 }
312 };
313
320 template <typename... Args>
321 inline std::ostream& toStream( const std::tuple<Args...>& tuple, std::ostream& s ) {
322 return TuplePrinter<decltype( tuple ), sizeof...( Args )>::toStream( tuple, s << " ( " ) << " ) ";
323 }
324
325 // ========================================================================
333 template <class TYPE>
334 inline std::string toString( const TYPE& obj ) {
335 std::ostringstream s;
336 std::ios::fmtflags orig_flags = s.flags();
337 s.setf( std::ios::showpoint ); // to display correctly floats
338 toStream( obj, s );
339 s.flags( orig_flags );
340 return s.str();
341 }
342 // ========================================================================
343 } // namespace Utils
344 // ==========================================================================
345 } // end of namespace Gaudi
346 // ============================================================================
347 // The END
348 // ============================================================================
349 #endif
GaudiUtils::VectorMap
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition: VectorMap.h:102
GaudiUtils::Map
Extension of the STL map.
Definition: Map.h:81
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:284
Gaudi::Utils::TuplePrinter< Tuple, 1 >::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:309
gaudirun.c
c
Definition: gaudirun.py:487
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:123
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:334
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,...
std::string
STL class.
IOTest.N
int N
Definition: IOTest.py:99
GaudiUtils::Map::end
iterator end()
Definition: Map.h:130
VectorMap.h
std::ostringstream
STL class.
std::iterator_traits
std::unordered_set
STL class.
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:37
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:129
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:300
GaudiUtils::HashMap
Common class providing an architecture-independent hash map.
Definition: HashMap.h:73
gaudirun.s
string s
Definition: gaudirun.py:316
std::array
STL class.
Gaudi::Utils::TuplePrinter::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:301
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:120
std::setprecision
T setprecision(T... args)
std::ostream
STL class.
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
GaudiPython.GaudiAlgs.Tuple
Tuple
Definition: GaudiAlgs.py:1157