Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
SerializeSTL.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 \***********************************************************************************/
22 #pragma once
23 
24 #include <GaudiKernel/HashMap.h>
25 #include <GaudiKernel/Map.h>
26 #include <array>
27 #include <list>
28 #include <map>
29 #include <ostream>
30 #include <set>
31 #include <unordered_set>
32 #include <utility>
33 #include <vector>
34 
35 namespace GaudiUtils {
37  template <class T1, class T2>
38  std::ostream& operator<<( std::ostream& s, const std::pair<T1, T2>& p );
39 
41  template <typename... Args>
42  std::ostream& operator<<( std::ostream& s, const std::tuple<Args...>& tuple );
43 
45  template <class T, class ALLOC>
46  std::ostream& operator<<( std::ostream& s, const std::vector<T, ALLOC>& v );
47 
49  template <class T, std::size_t N>
50  std::ostream& operator<<( std::ostream& s, const std::array<T, N>& v );
51 
53  template <class T, class ALLOC>
54  std::ostream& operator<<( std::ostream& s, const std::list<T, ALLOC>& l );
55 
57  template <class T, class ALLOC>
58  std::ostream& operator<<( std::ostream& s, const std::set<T, ALLOC>& l );
59 
61  template <class T, class ALLOC>
62  std::ostream& operator<<( std::ostream& s, const std::unordered_set<T, ALLOC>& l );
63 
65  template <class T1, class T2, class COMP, class ALLOC>
66  std::ostream& operator<<( std::ostream& s, const std::map<T1, T2, COMP, ALLOC>& m );
67 
69  template <class K, class T, class M>
70  std::ostream& operator<<( std::ostream& s, const GaudiUtils::Map<K, T, M>& m );
71 
73  template <class K, class T, class H, class M>
74  std::ostream& operator<<( std::ostream& s, const GaudiUtils::HashMap<K, T, H, M>& m );
75 
76  namespace details {
77 
79  template <typename T>
80  std::ostream& operator()( std::ostream& os, T&& t ) const {
81  return os << std::forward<T>( t );
82  }
83  };
84 
85  template <typename Stream, typename Iterator, typename Separator, typename OutputElement = IdentityOutputter>
86  Stream& ostream_joiner( Stream& os, Iterator first, Iterator last, Separator sep,
87  OutputElement output = OutputElement{} ) {
88  if ( first != last ) output( os, *first++ );
89  while ( first != last ) output( os << sep, *first++ );
90  return os;
91  }
92 
93  template <typename Stream, typename Container, typename Separator, typename OutputElement = IdentityOutputter>
94  Stream& ostream_joiner( Stream& os, const Container& c, Separator sep, OutputElement output = OutputElement{} ) {
95  using std::begin, std::end;
96  return ostream_joiner( os, begin( c ), end( c ), sep, output );
97  }
98  } // namespace details
99 
100  template <class T1, class T2>
101  std::ostream& operator<<( std::ostream& s, const std::pair<T1, T2>& p ) {
102  return s << '(' << p.first << ", " << p.second << ')';
103  }
104 
105  template <typename... Args>
106  std::ostream& operator<<( std::ostream& s, const std::tuple<Args...>& tup ) {
107  return std::apply(
108  [&s]( const auto&... a ) -> decltype( auto ) {
109  unsigned n = sizeof...( a );
110  if ( n == 1 ) n = 2; // special case in python...
111  s << " (";
112  ( ( s << " " << a << ( --n == 0 ? "" : "," ) ), ... );
113  return s << " )";
114  },
115  tup );
116  }
117 
118  template <class T, class ALLOC>
119  std::ostream& operator<<( std::ostream& s, const std::vector<T, ALLOC>& v ) {
120  return details::ostream_joiner( s << '[', v, ", " ) << ']';
121  }
122 
123  template <class T, std::size_t N>
124  std::ostream& operator<<( std::ostream& s, const std::array<T, N>& v ) {
125  return details::ostream_joiner( s << '[', v, ", " ) << ']';
126  }
127 
128  template <class T, class ALLOC>
129  std::ostream& operator<<( std::ostream& s, const std::list<T, ALLOC>& l ) {
130  return details::ostream_joiner( s << '[', l, ", " ) << ']';
131  }
132 
133  template <class T, class ALLOC>
134  std::ostream& operator<<( std::ostream& s, const std::set<T, ALLOC>& l ) {
135  return details::ostream_joiner( s << '[', l, ", " ) << ']';
136  }
137 
138  template <class T, class ALLOC>
139  std::ostream& operator<<( std::ostream& s, const std::unordered_set<T, ALLOC>& l ) {
140  auto ordered = std::set( l.begin(), l.end() ); // ensure reproducible printout
141  s << ordered;
142  return s;
143  }
144 
145  template <class T1, class T2, class COMP, class ALLOC>
146  std::ostream& operator<<( std::ostream& s, const std::map<T1, T2, COMP, ALLOC>& m ) {
147  return details::ostream_joiner( s << "{", m, ", ",
148  []( std::ostream& os, const std::pair<const T1, T2>& p ) -> std::ostream& {
149  return os << p.first << ": " << p.second;
150  } )
151  << "}";
152  }
153 
154  template <class K, class T, class M>
155  std::ostream& operator<<( std::ostream& s, const GaudiUtils::Map<K, T, M>& m ) {
156  // Serialize the internal map.
157  return s << static_cast<const M&>( m );
158  }
159 
162  template <class K, class T, class H, class M>
163  std::ostream& operator<<( std::ostream& s, const GaudiUtils::HashMap<K, T, H, M>& m ) {
164  // Copy the hash map into a map to have it ordered by key.
165  return s << GaudiUtils::Map<K, T>( m.begin(), m.end() );
166  }
167 
168 } // namespace GaudiUtils
gaudirun.s
string s
Definition: gaudirun.py:346
gaudirun.c
c
Definition: gaudirun.py:525
gaudirun.output
output
Definition: gaudirun.py:521
HashMap.h
bug_34121.t
t
Definition: bug_34121.py:31
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:135
GaudiUtils::details::IdentityOutputter::operator()
std::ostream & operator()(std::ostream &os, T &&t) const
Definition: SerializeSTL.h:80
GaudiUtils::operator<<
std::ostream & operator<<(std::ostream &s, const std::pair< T1, T2 > &p)
Serialize an std::pair in a python like format. E.g. "(1, 2)".
Definition: SerializeSTL.h:101
details
Definition: AnyDataWrapper.h:19
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:107
GaudiUtils::details::IdentityOutputter
Definition: SerializeSTL.h:78
GaudiUtils::Map
Definition: Map.h:82
cpluginsvc.n
n
Definition: cpluginsvc.py:234
gaudirun.l
dictionary l
Definition: gaudirun.py:583
Properties.v
v
Definition: Properties.py:122
Map.h
GaudiUtils::HashMap
Definition: HashMap.h:80
IOTest.end
end
Definition: IOTest.py:125
GaudiUtils
Definition: Allocator.h:62
GaudiUtils::details::ostream_joiner
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:86
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18