LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 
9 namespace fhicl {
10  namespace detail {
11  namespace yes_defaults {
12 
13  //=================================================================
14  // std::string
15  //
16  // When printing out strings, it is best to surround them with
17  // \"...\" so that there will be no ambiguity for the user.
18  template <typename T>
19  struct maybe_quotes {
20  maybe_quotes(T const& t) : value(t) {}
21  T const& value;
22  };
23 
24  template <>
25  struct maybe_quotes<std::string> {
26  maybe_quotes(std::string const& t) : value{"\"" + t + "\""} {}
27  std::string value;
28  };
29 
30  inline std::ostream&
31  operator<<(std::ostream& os, maybe_quotes<bool>&& mq)
32  {
33  return os << std::boolalpha << mq.value;
34  }
35 
36  template <typename T>
38  operator<<(std::ostream& os, maybe_quotes<T>&& mq)
39  {
40  return os << mq.value;
41  }
42 
43  template <typename T>
45  operator<<(std::ostream& os, maybe_quotes<T>&& mq)
46  {
47  return os << std::showpoint << mq.value;
48  }
49  }
50  }
51 } // fhicl::detail::yes_defaults
52 
53 //====================================================================================
54 // =
55 //====================================================================================
56 
57 namespace fhicl {
58  namespace detail {
59  namespace no_defaults {
60 
61  std::string stripped_typename(std::string const& fullName);
62 
63  inline std::string
64  padded_string(std::string const& tnToPad)
65  {
66  return std::string("<") + tnToPad + std::string(">");
67  }
68 
69  inline std::string
70  presented_string(std::string const& fullName)
71  {
72  return padded_string(stripped_typename(fullName));
73  }
74 
75  template <typename T>
76  struct expected_types {
78  {
79  std::string const strippedTypename =
80  stripped_typename(cet::demangle_symbol(typeid(T).name()));
81  value = padded_string(strippedTypename);
82  }
83  std::string value;
84  };
85 
86  // std::string is an expected_type but may be demangled to full
87  // typename when inline namespaces are used (libc++, gcc5)
88  // Specialize expected_types for this type
89  template <>
90  struct expected_types<std::string> {
91  expected_types() : value("std::string")
92  {
94  }
95  std::string value;
96  };
97 
98  template <typename T>
99  std::ostream&
100  operator<<(std::ostream& os, expected_types<T>&& et)
101  {
102  return os << et.value;
103  }
104  }
105  }
106 } // fhicl::detail::no_defaults
107 
108 #endif /* fhiclcpp_types_detail_ostream_helpers_h */
109 
110 // Local variables:
111 // mode : c++
112 // 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)
parameter set interface
std::string value(boost::any const &)