Line data Source code
1 : // *****************************************************************************
2 : /*!
3 : \file src/Base/PrintTaggedTuple.hpp
4 : \copyright 2012-2015 J. Bakosi,
5 : 2016-2018 Los Alamos National Security, LLC.,
6 : 2019-2021 Triad National Security, LLC.,
7 : 2022-2025 J. Bakosi
8 : All rights reserved. See the LICENSE file for details.
9 : \brief Simple (unformatted, one-line) TaggedTuple printer
10 : \details Simple (unformatted, one-line) TaggedTuple printer.
11 : */
12 : // *****************************************************************************
13 : #pragma once
14 :
15 : #include <ostream>
16 :
17 : #include <brigand/algorithms/for_each.hpp>
18 :
19 : #include "NoWarning/set.hpp"
20 : #include "TaggedTuple.hpp"
21 :
22 : namespace tk {
23 :
24 : //! Function object type to print contents of a TaggedTuple
25 : //! \tparam List brigand::list of types in the tagged tuple
26 : template< class List >
27 : struct TuplePrinter {
28 : std::ostream& os;
29 : const tk::TaggedTuple< List >& tuple;
30 : //! Constructor
31 8 : TuplePrinter( std::ostream& s, const tk::TaggedTuple< List >& t ) :
32 8 : os(s), tuple(t) {}
33 : //! Function call operator templated on the type being output
34 60 : template< typename Key > void operator()( brigand::type_<Key> ) {
35 : using Tuple = tk::TaggedTuple< List >;
36 : const auto& key = Key::key();
37 60 : const auto& value = tuple.template get< Key >();
38 : if constexpr( Tuple::template is_tagged_tuple< Key >::value )
39 4 : os << key << " = { " << value << "} ";
40 : else
41 116 : os << key << " = " << value << ' ';
42 60 : }
43 : };
44 :
45 : //! Simple (unformatted, one-line) output of a TaggedTuple to output stream
46 : //! \tparam List brigand::list of types in the tagged tuple
47 : //! \param[in,out] os Output stream to output to
48 : //! \param[in] t TaggedTuple to print
49 : //! \return Output stream
50 : template< class List >
51 : inline std::ostream&
52 9 : operator<< ( std::ostream& os, const tk::TaggedTuple< List >& t ) {
53 : using keys = typename tk::TaggedTuple< List >::Keys;
54 : os << std::boolalpha;
55 9 : brigand::for_each< keys >( TuplePrinter< List >( os, t ) );
56 9 : return os;
57 : }
58 :
59 : //! Simple (unformatted, one-line) output of a TaggedTuple to output stream
60 : //! \tparam List brigand::list of types in the tagged tuple
61 : //! \param[in,out] os Output stream to output to
62 : //! \param[in] t TaggedTuple to print
63 : template< class List >
64 : inline void
65 1 : print( std::ostream& os, const tk::TaggedTuple< List >& t ) {
66 : using keys = typename tk::TaggedTuple< List >::Keys;
67 : os << std::boolalpha;
68 1 : brigand::for_each< keys >( TuplePrinter< List >( os, t ) );
69 1 : }
70 :
71 : } // tk::
|