Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 #pragma once
12 
13 #include <GaudiKernel/HashMap.h>
14 #include <GaudiKernel/Map.h>
16 #include <GaudiKernel/VectorMap.h>
17 #include <array>
18 #include <iomanip>
19 #include <iostream>
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <sstream>
24 #include <string>
25 #include <unordered_set>
26 #include <vector>
27 
37 namespace Gaudi {
38  namespace Utils {
44  template <class TYPE>
45  std::ostream& toStream( const TYPE& obj, std::ostream& s );
57  template <class ITERATOR>
58  inline std::ostream& toStream( ITERATOR first, // begin of the sequence
59  ITERATOR last, // end of the sequence
60  std::ostream& s, // the stream
61  const std::string& open, // opening
62  const std::string& close, // closing
63  const std::string& delim ); // delimiter
70  inline std::ostream& toStream( const std::string& obj, std::ostream& s ) { return s << std::quoted( obj, '\'' ); }
75  inline std::ostream& toStream( const bool obj, std::ostream& s ) { return s << ( obj ? "True" : "False" ); }
80  inline std::ostream& toStream( const float obj, std::ostream& s, const int prec = 6 ) {
81  const int p = static_cast<int>( s.precision() );
82  return s << std::setprecision( prec ) << obj << std::setprecision( p );
83  }
88  inline std::ostream& toStream( const double obj, std::ostream& s, const int prec = 8 ) {
89  const int p = static_cast<int>( s.precision() );
90  return s << std::setprecision( prec ) << obj << std::setprecision( p );
91  }
96  inline std::ostream& toStream( const long double obj, std::ostream& s, const int prec = 10 ) {
97  const int p = static_cast<int>( s.precision() );
98  return s << std::setprecision( prec ) << obj << std::setprecision( p );
99  }
107  template <class KTYPE, class VTYPE>
108  inline std::ostream& toStream( const std::pair<KTYPE, VTYPE>& obj, std::ostream& s ) {
109  return toStream( obj.second, toStream( obj.first, s << "( " ) << " , " ) << " )";
110  }
111 
112  template <typename... Args>
113  inline std::ostream& toStream( const std::tuple<Args...>& tuple, std::ostream& s );
114 
121  template <class TYPE, class ALLOCATOR>
122  inline std::ostream& toStream( const std::vector<TYPE, ALLOCATOR>& obj, std::ostream& s ) {
123  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
124  }
131  template <class TYPE, class ALLOCATOR>
132  inline std::ostream& toStream( const std::list<TYPE, ALLOCATOR>& obj, std::ostream& s ) {
133  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
134  }
141  template <class TYPE, class CMP, class ALLOCATOR>
142  inline std::ostream& toStream( const std::set<TYPE, CMP, ALLOCATOR>& obj, std::ostream& s ) {
143  return toStream( obj.begin(), obj.end(), s, "[ ", " ]", " , " );
144  }
148  template <class TYPE, class HASH, class CMP, class ALLOCATOR>
149  inline std::ostream& toStream( const std::unordered_set<TYPE, HASH, CMP, ALLOCATOR>& obj, std::ostream& s ) {
150  auto ordered = std::set( obj.begin(), obj.end() ); // ensure reproducible printout
151  return obj.empty() ? s << "set()" : toStream( ordered.begin(), ordered.end(), s, "{ ", " }", " , " );
152  }
160  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
161  inline std::ostream& toStream( const std::map<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s ) {
163  return ostream_joiner( s << "{ ", obj, " , ",
164  []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
165  return toStream( i.second, toStream( i.first, os ) << " : " );
166  } )
167  << " }";
168  }
177  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
178  inline std::ostream& toStream( const GaudiUtils::VectorMap<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s ) {
180  return ostream_joiner( s << "{ ", obj, " , ",
181  []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
182  return toStream( i.second, toStream( i.first, os ) << " : " );
183  } )
184  << " }";
185  }
194  template <class KTYPE, class VTYPE, class MAP>
195  inline std::ostream& toStream( const GaudiUtils::Map<KTYPE, VTYPE, MAP>& obj, std::ostream& s ) {
197  return ostream_joiner( s << "{ ", obj, " , ",
198  []( std::ostream& os, const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
199  return toStream( i.second, toStream( i.first, os ) << " : " );
200  } )
201  << " }";
202  }
211  template <class KTYPE, class VTYPE, class HASH, class MAP>
212  inline std::ostream& toStream( const GaudiUtils::HashMap<KTYPE, VTYPE, HASH, MAP>& obj, std::ostream& s ) {
213  // Copy the hash map into a map to have it ordered by key.
214  return toStream( GaudiUtils::Map<KTYPE, VTYPE>{ obj.begin(), obj.end() }, s );
215  }
220  template <class TYPE, unsigned int N>
221  std::ostream& toStream( const TYPE ( &obj )[N], std::ostream& s ) {
222  if constexpr ( N == 1 ) {
223  return toStream( obj[0], s << "( " ) << " , )";
224  } else {
225  return toStream( obj, obj + N, s, "( ", " )", " , " );
226  }
227  }
232  template <class TYPE, std::size_t N>
233  std::ostream& toStream( const std::array<TYPE, N>& obj, std::ostream& s ) {
234  if constexpr ( N == 1 ) {
235  return toStream( obj[0], s << "( " ) << " , )";
236  } else {
237  return toStream( begin( obj ), end( obj ), s, "( ", " )", " , " );
238  }
239  }
244  template <unsigned int N>
245  std::ostream& toStream( const char ( &obj )[N], std::ostream& s ) {
246  return toStream( std::string( obj, obj + N ), s );
247  }
252  inline std::ostream& toStream( const char* obj, std::ostream& s ) { return toStream( std::string( obj ), s ); }
258  template <class TYPE>
259  inline std::ostream& toStream( const TYPE& obj, std::ostream& s ) {
260  return s << obj;
261  }
262  // helper function to print a tuple of any size
263  template <class Tuple, std::size_t N>
264  struct TuplePrinter {
265  static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
267  return Gaudi::Utils::toStream( std::get<N - 1>( t ), s );
268  }
269  };
270 
271  template <class Tuple>
272  struct TuplePrinter<Tuple, 1> {
273  static std::ostream& toStream( const Tuple& t, std::ostream& s ) {
274  return Gaudi::Utils::toStream( std::get<0>( t ), s );
275  }
276  };
277 
284  template <typename... Args>
285  inline std::ostream& toStream( const std::tuple<Args...>& tuple, std::ostream& s ) {
286  auto& out = TuplePrinter<decltype( tuple ), sizeof...( Args )>::toStream( tuple, s << " ( " );
287  if constexpr ( std::tuple_size_v<std::tuple<Args...>> == 1 ) { // this is a special case in Python
288  out << " ,";
289  }
290  return out << " ) ";
291  }
303  template <class ITERATOR>
304  inline std::ostream& toStream( ITERATOR first, // begin of the sequence
305  ITERATOR last, // end of the sequence
306  std::ostream& s, // the stream
307  const std::string& open, // opening
308  const std::string& close, // closing
309  const std::string& delim ) // delimiter
310  {
311  using ref_t = typename std::iterator_traits<ITERATOR>::reference;
313  return ostream_joiner( s << open, first, last, delim,
314  []( std::ostream& os, ref_t i ) -> std::ostream& { return toStream( i, os ); } )
315  << close;
316  }
317 
325  template <class TYPE>
326  inline std::string toString( const TYPE& obj ) {
327  std::ostringstream s;
328  std::ios::fmtflags orig_flags = s.flags();
329  s.setf( std::ios::showpoint ); // to display correctly floats
330  toStream( obj, s );
331  s.flags( orig_flags );
332  return s.str();
333  }
334  } // namespace Utils
335 } // namespace Gaudi
IOTest.N
N
Definition: IOTest.py:112
Gaudi::Utils::TuplePrinter
Definition: ToStream.h:264
gaudirun.s
string s
Definition: gaudirun.py:346
GaudiUtils::VectorMap
Definition: VectorMap.h:100
GaudiUtils::Map< K, T, std::unordered_map< K, T, Hash< K > > >::begin
iterator begin()
Definition: Map.h:121
Gaudi::Utils::TuplePrinter< Tuple, 1 >::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:273
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:135
GaudiUtils::Map
Definition: Map.h:82
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:138
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:122
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:326
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:304
fixtures.reference
Generator[dict, None, None] reference(request, Optional[Path] reference_path)
Definition: fixtures.py:211
SerializeSTL.h
Map.h
GaudiUtils::HashMap
Definition: HashMap.h:80
Gaudi::Utils::TuplePrinter::toStream
static std::ostream & toStream(const Tuple &t, std::ostream &s)
Definition: ToStream.h:265
GaudiUtils::details::ostream_joiner
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:86
Gaudi::Functional::details::out
OptOut && out
Definition: details.h:179