LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
OptionalTable.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_types_OptionalTable_h
2 #define fhiclcpp_types_OptionalTable_h
3 
5 #include "fhiclcpp/type_traits.h"
8 #include "fhiclcpp/types/Name.h"
15 
16 #include <memory>
17 #include <set>
18 #include <string>
19 
20 namespace fhicl {
21 
22  //========================================================
23  template <typename T>
24  class OptionalTable final : public detail::TableBase,
25  private detail::RegisterIfTableMember {
26  public:
32 
33  //=====================================================
34  // User-friendly
35  // ... c'tors
36  explicit OptionalTable(Name&& name);
37  explicit OptionalTable(Name&& name, Comment&& comment);
38  explicit OptionalTable(Name&& name,
39  Comment&& comment,
40  std::function<bool()> maybeUse);
41  OptionalTable(ParameterSet const& pset,
42  std::set<std::string> const& keysToIgnore);
43 
44  // ... Accessors
45  bool
46  operator()(T& value) const
47  {
48  if (has_value_) {
49  value = *value_;
50  return true;
51  }
52  return false;
53  }
54 
55  bool
56  hasValue() const
57  {
58  return has_value_;
59  }
60 
61  ParameterSet const&
62  get_PSet() const
63  {
64  return pset_;
65  }
66 
67  void validate_ParameterSet(ParameterSet const& pset,
68  std::set<std::string> const& keysToIgnore = {});
69 
71  std::ostream& os,
72  std::string const& tab = std::string(3, ' ')) const;
73 
74  //=====================================================
75  // Expert-only
76  using value_type = T;
77 
78  OptionalTable();
79 
80  private:
81  using members_t = std::vector<cet::exempt_ptr<ParameterBase>>;
82 
83  std::shared_ptr<T> value_{std::make_shared<T>()};
84  bool has_value_{false};
88 
89  members_t const&
90  get_members() const override
91  {
92  return members_;
93  }
94  void do_set_value(fhicl::ParameterSet const& pset,
95  bool const trimParents) override;
96  };
97 
98  template <typename T>
99  inline std::ostream&
100  operator<<(std::ostream& os, OptionalTable<T> const& t)
101  {
102  std::ostringstream config;
103  t.print_allowed_configuration(config);
104  return os << config.str();
105  }
106 }
107 
109 
110 #endif /* fhiclcpp_types_OptionalTable_h */
111 
112 // Local variables:
113 // mode: c++
114 // End:
#define NO_DELEGATED_PARAMETERS
std::shared_ptr< T > value_
Definition: OptionalTable.h:83
#define NO_NESTED_FHICL_TYPES_IN_TABLE
static TableMemberRegistry & instance()
members_t const & get_members() const override
Definition: OptionalTable.h:90
parameter set interface
#define NO_STD_CONTAINERS
std::vector< cet::exempt_ptr< ParameterBase >> members_t
Definition: OptionalTable.h:81
void print_allowed_configuration(std::ostream &os, std::string const &tab=std::string(3, ' ')) const
#define NO_NESTED_TABLE_FRAGMENTS
std::vector< base_ptr > release_members()
bool operator()(T &value) const
Definition: OptionalTable.h:46
std::string value(boost::any const &)
ParameterSet pset_
Definition: OptionalTable.h:85
std::string name() const
Definition: ParameterBase.h:49
bool hasValue() const
Definition: OptionalTable.h:56
void validate_ParameterSet(ParameterSet const &pset, std::set< std::string > const &keysToIgnore={})
void do_set_value(fhicl::ParameterSet const &pset, bool const trimParents) override
ParameterSet const & get_PSet() const
Definition: OptionalTable.h:62
std::string comment() const
Definition: ParameterBase.h:54
art::ClosingCriteria::art::TFileService::Config value_type
Definition: OptionalTable.h:76