Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:07:18

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 /** @file GaudiKernel/SerializeSTL.h
0012  *
0013  * Provide serialization function (output only) for some common STL classes
0014  * (vectors, lists, pairs, maps) plus GaudiUtils::Map and GaudiUtils::HashMap.
0015  *
0016  * To use the serializer provided by this file, one should add
0017  * "using namespace GaudiUtils" (possibly inside the function (scope) calling "<<").
0018  *
0019  * @author Marco Clemencic
0020  * (adapted from the code found in LHCbKernel, original author unknown)
0021  */
0022 #ifndef GAUDIKERNEL_SERIALIZESTL_H_
0023 #define GAUDIKERNEL_SERIALIZESTL_H_
0024 
0025 #include "GaudiKernel/HashMap.h"
0026 #include "GaudiKernel/Map.h"
0027 #include <array>
0028 #include <list>
0029 #include <map>
0030 #include <ostream>
0031 #include <utility>
0032 #include <vector>
0033 
0034 namespace GaudiUtils {
0035   /// Serialize an std::pair in a python like format. E.g. "(1, 2)".
0036   template <class T1, class T2>
0037   std::ostream& operator<<( std::ostream& s, const std::pair<T1, T2>& p );
0038 
0039   /// Serialize an std::vector in a python like format. E.g. "[1, 2, 3]".
0040   template <class T, class ALLOC>
0041   std::ostream& operator<<( std::ostream& s, const std::vector<T, ALLOC>& v );
0042 
0043   /// Serialize an std::array in a python like format. E.g. "[1, 2, 3]".
0044   template <class T, std::size_t N>
0045   std::ostream& operator<<( std::ostream& s, const std::array<T, N>& v );
0046 
0047   /// Serialize an std::list in a python like format. E.g. "[1, 2, 3]".
0048   template <class T, class ALLOC>
0049   std::ostream& operator<<( std::ostream& s, const std::list<T, ALLOC>& l );
0050 
0051   /// Serialize an std::map in a python like format. E.g. "{a: 1, b: 2}".
0052   template <class T1, class T2, class COMP, class ALLOC>
0053   std::ostream& operator<<( std::ostream& s, const std::map<T1, T2, COMP, ALLOC>& m );
0054 
0055   /// Serialize a GaudiUtils::Map in a python like format. E.g. "{a: 1, b: 2}".
0056   template <class K, class T, class M>
0057   std::ostream& operator<<( std::ostream& s, const GaudiUtils::Map<K, T, M>& m );
0058 
0059   /// Serialize a GaudiUtils::HashMap in a python like format. E.g. "{a: 1, b: 2}".
0060   template <class K, class T, class H, class M>
0061   std::ostream& operator<<( std::ostream& s, const GaudiUtils::HashMap<K, T, H, M>& m );
0062 
0063   namespace details {
0064 
0065     struct IdentityOutputter {
0066       template <typename T>
0067       std::ostream& operator()( std::ostream& os, T&& t ) const {
0068         return os << std::forward<T>( t );
0069       }
0070     };
0071 
0072     template <typename Stream, typename Iterator, typename Separator, typename OutputElement = IdentityOutputter>
0073     Stream& ostream_joiner( Stream& os, Iterator first, Iterator last, Separator sep,
0074                             OutputElement output = OutputElement{} ) {
0075       if ( first != last ) output( os, *first++ );
0076       while ( first != last ) output( os << sep, *first++ );
0077       return os;
0078     }
0079 
0080     template <typename Stream, typename Container, typename Separator, typename OutputElement = IdentityOutputter>
0081     Stream& ostream_joiner( Stream& os, const Container& c, Separator sep, OutputElement output = OutputElement{} ) {
0082       using std::begin, std::end;
0083       return ostream_joiner( os, begin( c ), end( c ), sep, output );
0084     }
0085   } // namespace details
0086 
0087   template <class T1, class T2>
0088   std::ostream& operator<<( std::ostream& s, const std::pair<T1, T2>& p ) {
0089     return s << '(' << p.first << ", " << p.second << ')';
0090   }
0091 
0092   template <class T, class ALLOC>
0093   std::ostream& operator<<( std::ostream& s, const std::vector<T, ALLOC>& v ) {
0094     return details::ostream_joiner( s << '[', v, ", " ) << ']';
0095   }
0096 
0097   template <class T, std::size_t N>
0098   std::ostream& operator<<( std::ostream& s, const std::array<T, N>& v ) {
0099     return details::ostream_joiner( s << '[', v, ", " ) << ']';
0100   }
0101 
0102   template <class T, class ALLOC>
0103   std::ostream& operator<<( std::ostream& s, const std::list<T, ALLOC>& l ) {
0104     return details::ostream_joiner( s << '[', l, ", " ) << ']';
0105   }
0106 
0107   template <class T1, class T2, class COMP, class ALLOC>
0108   std::ostream& operator<<( std::ostream& s, const std::map<T1, T2, COMP, ALLOC>& m ) {
0109     return details::ostream_joiner( s << "{", m, ", ",
0110                                     []( std::ostream& os, const std::pair<const T1, T2>& p ) -> std::ostream& {
0111                                       return os << p.first << ": " << p.second;
0112                                     } )
0113            << "}";
0114   }
0115 
0116   template <class K, class T, class M>
0117   std::ostream& operator<<( std::ostream& s, const GaudiUtils::Map<K, T, M>& m ) {
0118     // Serialize the internal map.
0119     return s << static_cast<const M&>( m );
0120   }
0121 
0122   /// This implementation is not efficient, but very simple. Anyway a print-out
0123   /// of a hash map is not something that we do every second.
0124   template <class K, class T, class H, class M>
0125   std::ostream& operator<<( std::ostream& s, const GaudiUtils::HashMap<K, T, H, M>& m ) {
0126     // Copy the hash map into a map to have it ordered by key.
0127     return s << GaudiUtils::Map<K, T>( m.begin(), m.end() );
0128   }
0129 
0130 } // namespace GaudiUtils
0131 
0132 #endif /*GAUDIKERNEL_SERIALIZESTL_H_*/