The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
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 <array>
25#include <list>
26#include <map>
27#include <ostream>
28#include <set>
29#include <unordered_set>
30#include <utility>
31#include <vector>
32
33namespace GaudiUtils {
35 template <class T1, class T2>
36 std::ostream& operator<<( std::ostream& s, const std::pair<T1, T2>& p );
37
39 template <typename... Args>
40 std::ostream& operator<<( std::ostream& s, const std::tuple<Args...>& tuple );
41
43 template <class T, class ALLOC>
44 std::ostream& operator<<( std::ostream& s, const std::vector<T, ALLOC>& v );
45
47 template <class T, std::size_t N>
48 std::ostream& operator<<( std::ostream& s, const std::array<T, N>& v );
49
51 template <class T, class ALLOC>
52 std::ostream& operator<<( std::ostream& s, const std::list<T, ALLOC>& l );
53
55 template <class T, class ALLOC>
56 std::ostream& operator<<( std::ostream& s, const std::set<T, ALLOC>& l );
57
59 template <class T, class ALLOC>
60 std::ostream& operator<<( std::ostream& s, const std::unordered_set<T, ALLOC>& l );
61
63 template <class T1, class T2, class COMP, class ALLOC>
64 std::ostream& operator<<( std::ostream& s, const std::map<T1, T2, COMP, ALLOC>& m );
65
66 namespace details {
67
69 template <typename T>
70 std::ostream& operator()( std::ostream& os, T&& t ) const {
71 return os << std::forward<T>( t );
72 }
73 };
74
75 template <typename Stream, typename Iterator, typename Separator, typename OutputElement = IdentityOutputter>
76 Stream& ostream_joiner( Stream& os, Iterator first, Iterator last, Separator sep,
77 OutputElement output = OutputElement{} ) {
78 if ( first != last ) output( os, *first++ );
79 while ( first != last ) output( os << sep, *first++ );
80 return os;
81 }
82
83 template <typename Stream, typename Container, typename Separator, typename OutputElement = IdentityOutputter>
84 Stream& ostream_joiner( Stream& os, const Container& c, Separator sep, OutputElement output = OutputElement{} ) {
85 using std::begin, std::end;
86 return ostream_joiner( os, begin( c ), end( c ), sep, output );
87 }
88 } // namespace details
89
90 template <class T1, class T2>
91 std::ostream& operator<<( std::ostream& s, const std::pair<T1, T2>& p ) {
92 return s << '(' << p.first << ", " << p.second << ')';
93 }
94
95 template <typename... Args>
96 std::ostream& operator<<( std::ostream& s, const std::tuple<Args...>& tup ) {
97 return std::apply(
98 [&s]( const auto&... a ) -> decltype( auto ) {
99 unsigned n = sizeof...( a );
100 if ( n == 1 ) n = 2; // special case in python...
101 s << " (";
102 ( ( s << " " << a << ( --n == 0 ? "" : "," ) ), ... );
103 return s << " )";
104 },
105 tup );
106 }
107
108 template <class T, class ALLOC>
109 std::ostream& operator<<( std::ostream& s, const std::vector<T, ALLOC>& v ) {
110 return details::ostream_joiner( s << '[', v, ", " ) << ']';
111 }
112
113 template <class T, std::size_t N>
114 std::ostream& operator<<( std::ostream& s, const std::array<T, N>& v ) {
115 return details::ostream_joiner( s << '[', v, ", " ) << ']';
116 }
117
118 template <class T, class ALLOC>
119 std::ostream& operator<<( std::ostream& s, const std::list<T, ALLOC>& l ) {
120 return details::ostream_joiner( s << '[', l, ", " ) << ']';
121 }
122
123 template <class T, class ALLOC>
124 std::ostream& operator<<( std::ostream& s, const std::set<T, ALLOC>& l ) {
125 return details::ostream_joiner( s << '[', l, ", " ) << ']';
126 }
127
128 template <class T, class ALLOC>
129 std::ostream& operator<<( std::ostream& s, const std::unordered_set<T, ALLOC>& l ) {
130 auto ordered = std::set( l.begin(), l.end() ); // ensure reproducible printout
131 s << ordered;
132 return s;
133 }
134
135 template <class T1, class T2, class COMP, class ALLOC>
136 std::ostream& operator<<( std::ostream& s, const std::map<T1, T2, COMP, ALLOC>& m ) {
137 return details::ostream_joiner( s << "{", m, ", ",
138 []( std::ostream& os, const std::pair<const T1, T2>& p ) -> std::ostream& {
139 return os << p.first << ": " << p.second;
140 } )
141 << "}";
142 }
143
144} // namespace GaudiUtils
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition Iterator.h:18
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
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)".
std::ostream & operator()(std::ostream &os, T &&t) const