Printer.h
Go to the documentation of this file.
1 #ifndef tools_Printer_h
2 #define tools_Printer_h
3 
4 #include "cetlib/exempt_ptr.h"
5 #include "cetlib_except/demangle.h"
6 #include "cetlib_except/exception.h"
8 
9 #include <iostream>
10 #include <map>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 namespace fhicl {
16  class Printer {
17  public:
18  virtual ~Printer() = default;
19  void
20  print_as_atom(fhicl::ParameterSet const& pset, std::string const& key) const
21  {
22  do_atomic_print(pset, key);
23  }
24  void
26  std::string const& key) const
27  {
28  do_sequence_print(pset, key);
29  }
30 
31  private:
32  virtual void do_atomic_print(fhicl::ParameterSet const& pset,
33  std::string const& key) const = 0;
34  virtual void do_sequence_print(fhicl::ParameterSet const& pset,
35  std::string const& key) const = 0;
36  };
37 
38  template <typename T>
39  class PrinterFor : public Printer {
40  void
42  std::string const& key) const
43  {
44  std::cout << pset.get<T>(key) << '\n';
45  }
46 
47  void
49  std::string const& key) const
50  {
51  auto const entries = pset.get<std::vector<T>>(key);
52  for (auto const& entry : entries) {
53  std::cout << entry << '\n';
54  }
55  }
56  };
57 
58  namespace detail {
59  template <typename T>
62  {
63  return cet::demangle_symbol(typeid(T).name());
64  }
65 
66  template <>
68  name_of<std::string>()
69  {
70  return "string";
71  }
72  }
73 
74  template <typename... Args>
76  public:
78  {
79  (printers_.emplace(detail::name_of<Args>(),
80  std::make_unique<PrinterFor<Args>>()),
81  ...);
82  }
83 
85  help_message() const
86  {
87  std::string result{"Supported types include:\n"};
88  for (auto const& pr : printers_) {
89  result += " '";
90  result += pr.first;
91  result += "'\n";
92  }
93  return result;
94  }
95 
96  void
98  std::string const& cpp_type,
99  std::string const& key) const
100  {
101  get_(cpp_type)->print_as_atom(pset, key);
102  }
103 
104  void
106  std::string const& cpp_type,
107  std::string const& key) const
108  {
109  get_(cpp_type)->print_as_sequence(pset, key);
110  }
111 
113  get_(std::string const& cpp_type) const
114  {
115  if (auto it = printers_.find(cpp_type); it != cend(printers_)) {
116  return it->second.get();
117  }
118  throw cet::exception{"Usage error"} << "The specified type '" << cpp_type
119  << "' is not supported.\n"
120  << help_message();
121  }
122 
123  std::map<std::string, std::unique_ptr<Printer>> printers_;
124  };
125 
126 }
127 
128 #endif /* tools_Printer_h */
129 
130 // Local Variables:
131 // mode: c++
132 // End:
static QCString name
Definition: declinfo.cpp:673
std::map< std::string, std::unique_ptr< Printer > > printers_
Definition: Printer.h:123
QList< Entry > entry
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:87
static QCString result
std::string string
Definition: nybbler.cc:12
void value_for_sequence(fhicl::ParameterSet const &pset, std::string const &cpp_type, std::string const &key) const
Definition: Printer.h:105
void print_as_sequence(fhicl::ParameterSet const &pset, std::string const &key) const
Definition: Printer.h:25
void print_as_atom(fhicl::ParameterSet const &pset, std::string const &key) const
Definition: Printer.h:20
void do_sequence_print(fhicl::ParameterSet const &pset, std::string const &key) const
Definition: Printer.h:48
std::string help_message() const
Definition: Printer.h:85
std::string name_of()
Definition: Printer.h:61
cet::exempt_ptr< Printer const > get_(std::string const &cpp_type) const
Definition: Printer.h:113
def key(type, name=None)
Definition: graph.py:13
T get(std::string const &key) const
Definition: ParameterSet.h:271
virtual ~Printer()=default
virtual void do_sequence_print(fhicl::ParameterSet const &pset, std::string const &key) const =0
virtual void do_atomic_print(fhicl::ParameterSet const &pset, std::string const &key) const =0
void do_atomic_print(fhicl::ParameterSet const &pset, std::string const &key) const
Definition: Printer.h:41
void value_for_atom(fhicl::ParameterSet const &pset, std::string const &cpp_type, std::string const &key) const
Definition: Printer.h:97
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33