The Gaudi Framework
master (da3d77e1)
ToStream.h
Go to the documentation of this file.
1
/***********************************************************************************\
2
* (c) Copyright 1998-2025 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
// ============================================================================
12
#ifndef GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H
13
#define GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H 1
14
// ============================================================================
15
// Include files
16
// ============================================================================
17
// STD & STL
18
// ============================================================================
19
#include <array>
20
#include <iomanip>
21
#include <iostream>
22
#include <list>
23
#include <map>
24
#include <set>
25
#include <sstream>
26
#include <string>
27
#include <unordered_set>
28
#include <vector>
29
// ============================================================================
30
// GaudiKernel
31
// ============================================================================
32
#include <
GaudiKernel/HashMap.h
>
33
#include <
GaudiKernel/Map.h
>
34
#include <
GaudiKernel/SerializeSTL.h
>
35
#include <
GaudiKernel/VectorMap.h
>
36
// ============================================================================
46
// ============================================================================
47
namespace
Gaudi
{
48
// ==========================================================================
49
namespace
Utils {
50
// ========================================================================
56
template
<
class
TYPE>
57
std::ostream
&
toStream
(
const
TYPE& obj,
std::ostream
&
s
);
58
// ========================================================================
70
template
<
class
ITERATOR>
71
inline
std::ostream
&
toStream
( ITERATOR first,
// begin of the sequence
72
ITERATOR last,
// end of the sequence
73
std::ostream
&
s
,
// the stream
74
const
std::string
& open,
// opening
75
const
std::string
& close,
// closing
76
const
std::string
& delim );
// delimiter
77
// ========================================================================
84
inline
std::ostream
&
toStream
(
const
std::string
& obj,
std::ostream
&
s
) {
return
s
<< std::quoted( obj,
'\''
); }
89
inline
std::ostream
&
toStream
(
const
bool
obj,
std::ostream
&
s
) {
return
s
<< ( obj ?
"True"
:
"False"
); }
94
inline
std::ostream
&
toStream
(
const
float
obj,
std::ostream
&
s
,
const
int
prec = 6 ) {
95
const
int
p =
static_cast<
int
>
(
s
.precision() );
96
return
s
<<
std::setprecision
( prec ) << obj <<
std::setprecision
( p );
97
}
102
inline
std::ostream
&
toStream
(
const
double
obj,
std::ostream
&
s
,
const
int
prec = 8 ) {
103
const
int
p =
static_cast<
int
>
(
s
.precision() );
104
return
s
<<
std::setprecision
( prec ) << obj <<
std::setprecision
( p );
105
}
110
inline
std::ostream
&
toStream
(
const
long
double
obj,
std::ostream
&
s
,
const
int
prec = 10 ) {
111
const
int
p =
static_cast<
int
>
(
s
.precision() );
112
return
s
<<
std::setprecision
( prec ) << obj <<
std::setprecision
( p );
113
}
114
// ========================================================================
122
template
<
class
KTYPE,
class
VTYPE>
123
inline
std::ostream
&
toStream
(
const
std::pair<KTYPE, VTYPE>
& obj,
std::ostream
&
s
) {
124
return
toStream
( obj.second,
toStream
( obj.first,
s
<<
"( "
) <<
" , "
) <<
" )"
;
125
}
126
127
template
<
typename
... Args>
128
inline
std::ostream
&
toStream
(
const
std::tuple<Args...>
& tuple,
std::ostream
&
s
);
129
130
// ========================================================================
137
template
<
class
TYPE,
class
ALLOCATOR>
138
inline
std::ostream
&
toStream
(
const
std::vector<TYPE, ALLOCATOR>
& obj,
std::ostream
&
s
) {
139
return
toStream
( obj.
begin
(), obj.
end
(),
s
,
"[ "
,
" ]"
,
" , "
);
140
}
141
// ========================================================================
148
template
<
class
TYPE,
class
ALLOCATOR>
149
inline
std::ostream
&
toStream
(
const
std::list<TYPE, ALLOCATOR>
& obj,
std::ostream
&
s
) {
150
return
toStream
( obj.
begin
(), obj.
end
(),
s
,
"[ "
,
" ]"
,
" , "
);
151
}
152
// ========================================================================
159
template
<
class
TYPE,
class
CMP,
class
ALLOCATOR>
160
inline
std::ostream
&
toStream
(
const
std::set<TYPE, CMP, ALLOCATOR>
& obj,
std::ostream
&
s
) {
161
return
toStream
( obj.
begin
(), obj.
end
(),
s
,
"[ "
,
" ]"
,
" , "
);
162
}
163
// ========================================================================
167
template
<
class
TYPE,
class
HASH,
class
CMP,
class
ALLOCATOR>
168
inline
std::ostream
&
toStream
(
const
std::unordered_set<TYPE, HASH, CMP, ALLOCATOR>
& obj,
std::ostream
&
s
) {
169
auto
ordered =
std::set
( obj.
begin
(), obj.
end
() );
// ensure reproducible printout
170
return
obj.
empty
() ?
s
<<
"set()"
:
toStream
( ordered.begin(), ordered.end(),
s
,
"{ "
,
" }"
,
" , "
);
171
}
172
// ========================================================================
180
template
<
class
KTYPE,
class
VTYPE,
class
CMP,
class
ALLOCATOR>
181
inline
std::ostream
&
toStream
(
const
std::map<KTYPE, VTYPE, CMP, ALLOCATOR>
& obj,
std::ostream
&
s
) {
182
using
GaudiUtils::details::ostream_joiner
;
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
// ========================================================================
198
template
<
class
KTYPE,
class
VTYPE,
class
CMP,
class
ALLOCATOR>
199
inline
std::ostream
&
toStream
(
const
GaudiUtils::VectorMap<KTYPE, VTYPE, CMP, ALLOCATOR>
& obj,
std::ostream
&
s
) {
200
using
GaudiUtils::details::ostream_joiner
;
201
return
ostream_joiner
(
s
<<
"{ "
, obj,
" , "
,
202
[](
std::ostream
& os,
const
std::pair<const KTYPE, VTYPE>
& i ) ->
std::ostream
& {
203
return
toStream
( i.second,
toStream
( i.first, os ) <<
" : "
);
204
} )
205
<<
" }"
;
206
}
207
// ========================================================================
216
template
<
class
KTYPE,
class
VTYPE,
class
MAP>
217
inline
std::ostream
&
toStream
(
const
GaudiUtils::Map<KTYPE, VTYPE, MAP>
& obj,
std::ostream
&
s
) {
218
using
GaudiUtils::details::ostream_joiner
;
219
return
ostream_joiner
(
s
<<
"{ "
, obj,
" , "
,
220
[](
std::ostream
& os,
const
std::pair<const KTYPE, VTYPE>
& i ) ->
std::ostream
& {
221
return
toStream
( i.second,
toStream
( i.first, os ) <<
" : "
);
222
} )
223
<<
" }"
;
224
}
225
// ========================================================================
234
template
<
class
KTYPE,
class
VTYPE,
class
HASH,
class
MAP>
235
inline
std::ostream
&
toStream
(
const
GaudiUtils::HashMap<KTYPE, VTYPE, HASH, MAP>
& obj,
std::ostream
&
s
) {
236
// Copy the hash map into a map to have it ordered by key.
237
return
toStream
(
GaudiUtils::Map<KTYPE, VTYPE>
{ obj.
begin
(), obj.
end
() },
s
);
238
}
239
// ========================================================================
244
template
<
class
TYPE,
unsigned
int
N>
245
std::ostream
&
toStream
(
const
TYPE ( &obj )[
N
],
std::ostream
&
s
) {
246
if
constexpr (
N
== 1 ) {
247
return
toStream
( obj[0],
s
<<
"( "
) <<
" , )"
;
248
}
else
{
249
return
toStream
( obj, obj +
N
,
s
,
"( "
,
" )"
,
" , "
);
250
}
251
}
252
// ========================================================================
257
template
<
class
TYPE, std::
size_t
N>
258
std::ostream
&
toStream
(
const
std::array<TYPE, N>
& obj,
std::ostream
&
s
) {
259
if
constexpr (
N
== 1 ) {
260
return
toStream
( obj[0],
s
<<
"( "
) <<
" , )"
;
261
}
else
{
262
return
toStream
(
begin
( obj ),
end
( obj ),
s
,
"( "
,
" )"
,
" , "
);
263
}
264
}
265
// ========================================================================
270
template
<
unsigned
int
N>
271
std::ostream
&
toStream
(
const
char
( &obj )[
N
],
std::ostream
&
s
) {
272
return
toStream
(
std::string
( obj, obj +
N
),
s
);
273
}
274
// ========================================================================
279
inline
std::ostream
&
toStream
(
const
char
* obj,
std::ostream
&
s
) {
return
toStream
(
std::string
( obj ),
s
); }
280
// ========================================================================
286
template
<
class
TYPE>
287
inline
std::ostream
&
toStream
(
const
TYPE& obj,
std::ostream
&
s
) {
288
return
s
<< obj;
289
}
290
// ========================================================================
291
// helper function to print a tuple of any size
292
template
<
class
Tuple, std::
size_t
N>
293
struct
TuplePrinter
{
294
static
std::ostream
&
toStream
(
const
Tuple&
t
,
std::ostream
&
s
) {
295
TuplePrinter<Tuple, N - 1>::toStream
(
t
,
s
) <<
" , "
;
296
return
Gaudi::Utils::toStream
( std::get<N - 1>(
t
),
s
);
297
}
298
};
299
300
template
<
class
Tuple>
301
struct
TuplePrinter
<Tuple, 1> {
302
static
std::ostream
&
toStream
(
const
Tuple&
t
,
std::ostream
&
s
) {
303
return
Gaudi::Utils::toStream
( std::get<0>(
t
),
s
);
304
}
305
};
306
313
template
<
typename
... Args>
314
inline
std::ostream
&
toStream
(
const
std::tuple<Args...>
& tuple,
std::ostream
&
s
) {
315
auto
&
out
=
TuplePrinter
<decltype( tuple ),
sizeof
...( Args )>::
toStream
( tuple,
s
<<
" ( "
);
316
if
constexpr ( std::tuple_size_v<
std::tuple<Args...>
> == 1 ) {
// this is a special case in Python
317
out
<<
" ,"
;
318
}
319
return
out
<<
" ) "
;
320
}
321
// ========================================================================
333
template
<
class
ITERATOR>
334
inline
std::ostream
&
toStream
( ITERATOR first,
// begin of the sequence
335
ITERATOR last,
// end of the sequence
336
std::ostream
&
s
,
// the stream
337
const
std::string
& open,
// opening
338
const
std::string
& close,
// closing
339
const
std::string
& delim )
// delimiter
340
{
341
using
ref_t =
typename
std::iterator_traits<ITERATOR>::reference
;
342
using
GaudiUtils::details::ostream_joiner
;
343
return
ostream_joiner
(
s
<< open, first, last, delim,
344
[](
std::ostream
& os, ref_t i ) ->
std::ostream
& {
return
toStream
( i, os ); } )
345
<< close;
346
}
347
348
// ========================================================================
356
template
<
class
TYPE>
357
inline
std::string
toString
(
const
TYPE& obj ) {
358
std::ostringstream
s
;
359
std::ios::fmtflags orig_flags =
s
.flags();
360
s
.setf( std::ios::showpoint );
// to display correctly floats
361
toStream
( obj,
s
);
362
s
.flags( orig_flags );
363
return
s
.str();
364
}
365
// ========================================================================
366
}
// namespace Utils
367
// ==========================================================================
368
}
// end of namespace Gaudi
369
// ============================================================================
370
// The END
371
// ============================================================================
372
#endif
std::setprecision
T setprecision(T... args)
std::string
STL class.
IOTest.N
N
Definition:
IOTest.py:112
std::list
STL class.
std::unordered_set
STL class.
std::pair
Gaudi::Utils::TuplePrinter
Definition:
ToStream.h:293
gaudirun.s
string s
Definition:
gaudirun.py:346
std::vector
STL class.
GaudiUtils::VectorMap
Definition:
VectorMap.h:112
std::tuple
GaudiUtils::Map< K, T, std::unordered_map< K, T, Hash< K > > >::begin
iterator begin()
Definition:
Map.h:139
Gaudi::Utils::TuplePrinter< Tuple, 1 >::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition:
ToStream.h:302
HashMap.h
bug_34121.t
t
Definition:
bug_34121.py:31
VectorMap.h
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition:
AttribStringParser.h:136
std::ostream
STL class.
std::iterator_traits
std::array
STL class.
GaudiUtils::Map
Definition:
Map.h:91
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition:
AttribStringParser.h:139
std::map
STL class.
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition:
__init__.py:1
GaudiUtils::Map< K, T, std::unordered_map< K, T, Hash< K > > >::end
iterator end()
Definition:
Map.h:140
std::ostringstream
STL class.
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition:
ToStream.h:357
std::vector::begin
T begin(T... args)
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:334
std::unordered_set::empty
T empty(T... args)
SerializeSTL.h
std::vector::end
T end(T... args)
Map.h
GaudiUtils::HashMap
Definition:
HashMap.h:83
Gaudi::Utils::TuplePrinter::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition:
ToStream.h:294
std::set
STL class.
GaudiUtils::details::ostream_joiner
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition:
SerializeSTL.h:87
PrepareBase.out
out
Definition:
PrepareBase.py:20
GaudiKernel
include
GaudiKernel
ToStream.h
Generated on Mon Feb 24 2025 11:10:45 for The Gaudi Framework by
1.8.18