The Gaudi Framework  master (69a68366)
Loading...
Searching...
No Matches
ToStream.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12
13#include <GaudiKernel/HashMap.h>
14#include <GaudiKernel/Map.h>
17#include <array>
18#include <format>
19#include <iomanip>
20#include <iostream>
21#include <list>
22#include <map>
23#include <set>
24#include <sstream>
25#include <string>
26#include <unordered_set>
27#include <vector>
28
38namespace Gaudi {
39 namespace Utils {
45 template <class TYPE>
46 std::ostream& toStream( const TYPE& obj, std::ostream& s );
58 template <class ITERATOR>
59 inline std::ostream& toStream( ITERATOR first, // begin of the sequence
60 ITERATOR last, // end of the sequence
61 std::ostream& s, // the stream
62 const std::string& open, // opening
63 const std::string& close, // closing
64 const std::string& delim ); // delimiter
71 inline std::ostream& toStream( const std::string& obj, std::ostream& s ) { return s << std::quoted( obj, '\'' ); }
76 inline std::ostream& toStream( const bool obj, std::ostream& s ) { return s << ( obj ? "True" : "False" ); }
81 inline std::ostream& toStream( const float obj, std::ostream& s ) { return s << std::format( "{}", obj ); }
86 inline std::ostream& toStream( const double obj, std::ostream& s ) { return s << std::format( "{}", obj ); }
91 inline std::ostream& toStream( const long double obj, std::ostream& s ) { return s << std::format( "{}", obj ); }
99 template <class KTYPE, class VTYPE>
100 inline std::ostream& toStream( const std::pair<KTYPE, VTYPE>& obj, std::ostream& s ) {
101 return toStream( obj.second, toStream( obj.first, s << "( " ) << " , " ) << " )";
102 }
103
104 template <typename... Args>
105 inline std::ostream& toStream( const std::tuple<Args...>& tuple, std::ostream& s );
106
113 template <class TYPE, class ALLOCATOR>
114 inline std::ostream& toStream( const std::vector<TYPE, ALLOCATOR>& obj, std::ostream& s ) {
115 return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
116 }
117
121 inline std::ostream& toStream( const std::vector<bool>& obj, std::ostream& s ) {
123 return ostream_joiner(
124 s << "[ ", obj, " , ",
125 []( std::ostream& os, bool b ) -> std::ostream& { return os << ( b ? "True" : "False" ); } )
126 << " ]";
127 }
128
134 template <class TYPE, class ALLOCATOR>
135 inline std::ostream& toStream( const std::list<TYPE, ALLOCATOR>& obj, std::ostream& s ) {
136 return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
137 }
138
144 template <class TYPE, class CMP, class ALLOCATOR>
145 inline std::ostream& toStream( const std::set<TYPE, CMP, ALLOCATOR>& obj, std::ostream& s ) {
146 return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
147 }
148
151 template <class TYPE, class HASH, class CMP, class ALLOCATOR>
152 inline std::ostream& toStream( const std::unordered_set<TYPE, HASH, CMP, ALLOCATOR>& obj, std::ostream& s ) {
153 auto ordered = std::set( obj.begin(), obj.end() ); // ensure reproducible printout
154 return obj.empty() ? s << "set()" : toStream( ordered.begin(), ordered.end(), s, "{ ", " }", " , " );
155 }
156
163 template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
164 inline std::ostream& toStream( const std::map<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s ) {
166 return ostream_joiner( s << "{ ", obj, " , ",
167 []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
168 return toStream( i.second, toStream( i.first, os ) << " : " );
169 } )
170 << " }";
171 }
172
180 template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
181 inline std::ostream& toStream( const GaudiUtils::VectorMap<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s ) {
183 return ostream_joiner( s << "{ ", obj, " , ",
184 []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
185 return toStream( i.second, toStream( i.first, os ) << " : " );
186 } )
187 << " }";
188 }
189
197 template <class KTYPE, class VTYPE, class MAP>
198 inline std::ostream& toStream( const GaudiUtils::Map<KTYPE, VTYPE, MAP>& obj, std::ostream& s ) {
200 return ostream_joiner( s << "{ ", obj, " , ",
201 []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
202 return toStream( i.second, toStream( i.first, os ) << " : " );
203 } )
204 << " }";
205 }
206
214 template <class KTYPE, class VTYPE, class HASH, class MAP>
215 inline std::ostream& toStream( const GaudiUtils::HashMap<KTYPE, VTYPE, HASH, MAP>& obj, std::ostream& s ) {
216 // Copy the hash map into a map to have it ordered by key.
217 return toStream( GaudiUtils::Map<KTYPE, VTYPE>{ obj.begin(), obj.end() }, s );
218 }
219
223 template <class TYPE, unsigned int N>
224 std::ostream& toStream( const TYPE ( &obj )[N], std::ostream& s ) {
225 if constexpr ( N == 1 ) {
226 return toStream( obj[0], s << "( " ) << " , )";
227 } else {
228 return toStream( obj, obj + N, s, "( ", " )", " , " );
229 }
230 }
231
235 template <class TYPE, std::size_t N>
236 std::ostream& toStream( const std::array<TYPE, N>& obj, std::ostream& s ) {
237 if constexpr ( N == 1 ) {
238 return toStream( obj[0], s << "( " ) << " , )";
239 } else {
240 return toStream( begin( obj ), end( obj ), s, "( ", " )", " , " );
241 }
242 }
243
247 template <unsigned int N>
248 std::ostream& toStream( const char ( &obj )[N], std::ostream& s ) {
249 return toStream( std::string( obj, obj + N ), s );
250 }
251
255 inline std::ostream& toStream( const char* obj, std::ostream& s ) { return toStream( std::string( obj ), s ); }
261 template <class TYPE>
262 inline std::ostream& toStream( const TYPE& obj, std::ostream& s ) {
263 return s << obj;
264 }
265 // helper function to print a tuple of any size
266 template <class Tuple, std::size_t N>
268 static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
270 return Gaudi::Utils::toStream( std::get<N - 1>( t ), s );
271 }
272 };
273
274 template <class Tuple>
275 struct TuplePrinter<Tuple, 1> {
276 static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
277 return Gaudi::Utils::toStream( std::get<0>( t ), s );
278 }
279 };
280
287 template <typename... Args>
288 inline std::ostream& toStream( const std::tuple<Args...>& tuple, std::ostream& s ) {
289 auto& out = TuplePrinter<decltype( tuple ), sizeof...( Args )>::toStream( tuple, s << " ( " );
290 if constexpr ( std::tuple_size_v<std::tuple<Args...>> == 1 ) { // this is a special case in Python
291 out << " ,";
292 }
293 return out << " ) ";
294 }
295
306 template <class ITERATOR>
307 inline std::ostream& toStream( ITERATOR first, // begin of the sequence
308 ITERATOR last, // end of the sequence
309 std::ostream& s, // the stream
310 const std::string& open, // opening
311 const std::string& close, // closing
312 const std::string& delim ) // delimiter
313 {
314 using ref_t = typename std::iterator_traits<ITERATOR>::reference;
316 return ostream_joiner( s << open, first, last, delim,
317 []( std::ostream& os, ref_t i ) -> std::ostream& { return toStream( i, os ); } )
318 << close;
319 }
320
328 template <class TYPE>
329 inline std::string toString( const TYPE& obj ) {
330 std::ostringstream s;
331 std::ios::fmtflags orig_flags = s.flags();
332 s.setf( std::ios::showpoint ); // to display correctly floats
333 toStream( obj, s );
334 s.flags( orig_flags );
335 return s.str();
336 }
337 } // namespace Utils
338} // namespace Gaudi
Provide serialization function (output only) for some common STL classes (vectors,...
Common class providing an architecture-independent hash map.
Definition HashMap.h:80
Extension of the STL map.
Definition Map.h:83
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition VectorMap.h:100
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition ToStream.h:329
AttribStringParser::Iterator begin(const AttribStringParser &parser)
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:307
AttribStringParser::Iterator end(const AttribStringParser &)
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition ToStream.h:276
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition ToStream.h:268