LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ostream_helpers.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_types_detail_ostream_helpers_h
2 #define fhiclcpp_types_detail_ostream_helpers_h
3 
4 #include "cetlib_except/demangle.h"
5 
6 #include <ostream>
7 #include <string>
8 #include <typeinfo>
9 
11 
12  //=================================================================
13  // std::string
14  //
15  // When printing out strings, it is best to surround them with
16  // \"...\" so that there will be no ambiguity for the user.
17  template <typename T>
18  struct maybe_quotes {
19  maybe_quotes(T const& t) : value(t) {}
20  T const& value;
21  };
22 
23  template <>
24  struct maybe_quotes<std::string> {
25  maybe_quotes(std::string const& t) : value{"\"" + t + "\""} {}
26  std::string value;
27  };
28 
29  inline std::ostream&
30  operator<<(std::ostream& os, maybe_quotes<bool>&& mq)
31  {
32  return os << std::boolalpha << mq.value;
33  }
34 
35  template <typename T>
36  inline std::ostream&
37  operator<<(std::ostream& os, maybe_quotes<T>&& mq)
38  {
39  if constexpr (std::is_floating_point_v<T>) {
40  return os << std::showpoint << mq.value;
41  } else {
42  return os << mq.value;
43  }
44  }
45 } // fhicl::detail::yes_defaults
46 
47 //====================================================================================
48 // =
49 //====================================================================================
50 
52 
53  std::string stripped_typename(std::string const& fullName);
54 
55  inline std::string
56  padded_string(std::string const& tnToPad)
57  {
58  return std::string("<") + tnToPad + std::string(">");
59  }
60 
61  inline std::string
62  presented_string(std::string const& fullName)
63  {
64  return padded_string(stripped_typename(fullName));
65  }
66 
67  template <typename T>
68  struct expected_types {
70  {
71  std::string const strippedTypename =
72  stripped_typename(cet::demangle_symbol(typeid(T).name()));
73  value = padded_string(strippedTypename);
74  }
75  std::string value;
76  };
77 
78  // std::string is an expected_type but may be demangled to full
79  // typename when inline namespaces are used (libc++, gcc5)
80  // Specialize expected_types for this type
81  template <>
82  struct expected_types<std::string> {
83  expected_types() : value("std::string")
84  {
86  }
87  std::string value;
88  };
89 
90  template <typename T>
91  std::ostream&
92  operator<<(std::ostream& os, expected_types<T>&& et)
93  {
94  return os << et.value;
95  }
96 } // fhicl::detail::no_defaults
97 
98 #endif /* fhiclcpp_types_detail_ostream_helpers_h */
99 
100 // Local variables:
101 // mode : c++
102 // End:
std::string stripped_typename(std::string const &fullName)
std::string presented_string(std::string const &fullName)
STL namespace.
std::string padded_string(std::string const &tnToPad)