1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// *****************************************************************************
/*!
  \file      src/Base/PrintUtil.hpp
  \copyright 2012-2015 J. Bakosi,
             2016-2018 Los Alamos National Security, LLC.,
             2019-2021 Triad National Security, LLC.,
             2022-2025 J. Bakosi
             All rights reserved. See the LICENSE file for details.
  \brief     String conversion utilities
  \details   Various string conversion utilities.
*/
// *****************************************************************************
#pragma once

#include <ostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <map><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "Has.hpp"

namespace tk {

//! Operator << for writing an enum class to an output stream
//! \param[in] os Output stream into to write to
//! \param[in] e Value of enum-class type to write to stream
//! \return Updated output stream for chain-use of the operator
template< typename Enum, typename Ch, typename Tr,
          typename std::enable_if_t< std::is_enum_v<Enum>, int > = 0 >
inline std::basic_ostream< Ch, Tr >&
operator<< ( std::basic_ostream< Ch, Tr >& os, const Enum& e ) {
  os << static_cast< unsigned int >( e );
  return os;
}

//! Operator << for writing a std::vector to an output stream
//! \param[in] os Output stream to write to
//! \param[in] v Vector to write to stream
//! \return Updated output stream for chain-use of the operator
template< class T, typename Ch, typename Tr >
inline std::basic_ostream< Ch, Tr >&
operator<< ( std::basic_ostream< Ch, Tr >& os, const std::vector< T >& v ) {
  os << std::boolalpha;
  os << "{ ";
  for (const auto& p : v) os << p << ' ';
  os << '}';
  return os;
}

//! Operator << for writing an std::map to an output stream
//! \param[in] os Output stream to write to
//! \param[in] m Map to write to stream
//! \return Updated output stream for chain-use of the operator
template< typename Ch, typename Tr,
          class Key, class Value, class Compare = std::less< Key > >
inline std::basic_ostream< Ch, Tr >&
operator<< ( std::basic_ostream< Ch, Tr >& os,
             const std::map< Key, Value, Compare >& m )
{
  if constexpr( tk::HasTypedef_i_am_tagged_tuple_v< Value > )
    for (const auto& [k,v] : m) os << '(' << k << ") = { " << v << "} ";
  else
    for (const auto& [k,v] : m) os << '(' << k << ") " << v << ' ';
  return os;
}

//! Operator << for adding (concatenating) T to a std::basic_string for lvalues.
//! \param[in] lhs Output std::basic_string into which e is written
//! \param[in] e Value of arbitrary type to write to string
//! \return Updated string
template< typename T, typename Ch, typename Tr >
std::basic_string< Ch, Tr >
operator<< ( std::basic_string< Ch, Tr >& lhs, const T& e ) {
  std::stringstream ss;
  ss << lhs << e;
  lhs = ss.str();
  return lhs;
}

//! Operator << for adding (concatenating) T to a std::basic_string for rvalues.
//! \param[in] lhs Output std::basic_string into which e is written
//! \param[in] e Value of arbitrary type to write to string
//! \return Updated string
template< typename T, typename Ch, typename Tr >
std::basic_string< Ch, Tr >
operator<< ( std::basic_string< Ch, Tr >&& lhs, const T& e ) {
  return lhs << e;
}

} // tk::