LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
ParameterSetRegistry.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_ParameterSetRegistry_h
2 #define fhiclcpp_ParameterSetRegistry_h
3 
4 // ======================================================================
5 //
6 // ParameterSetRegistry
7 //
8 // ======================================================================
9 
10 #include "fhiclcpp/ParameterSet.h"
12 #include "fhiclcpp/fwd.h"
13 
14 #include "sqlite3.h"
15 
16 #include <mutex>
17 #include <unordered_map>
18 
19 namespace fhicl {
20 
21  class ParameterSetRegistry;
22 
23  namespace detail {
24  class HashParameterSetID;
25  void throwOnSQLiteFailure(sqlite3* db, char* msg = nullptr);
26  }
27 }
28 
30 public:
31  size_t operator()(ParameterSetID const& id) const;
32 
33 private:
34  std::hash<std::string> hash_;
35 };
36 
38 public:
39  ParameterSetRegistry(ParameterSet const&) = delete;
41  ParameterSetRegistry& operator=(ParameterSet const&) = delete;
42  ParameterSetRegistry& operator=(ParameterSet&&) = delete;
44 
45  // Typedefs.
46  using collection_type = std::
47  unordered_map<ParameterSetID, ParameterSet, detail::HashParameterSetID>;
48  using key_type = typename collection_type::key_type;
49  using mapped_type = typename collection_type::mapped_type;
50  using value_type = typename collection_type::value_type;
51  using size_type = typename collection_type::size_type;
53 
54  // DB interaction.
55  static void importFrom(sqlite3* db);
56  static void exportTo(sqlite3* db);
57  static void stageIn();
58 
59  // Observers.
60  static bool empty();
61  static size_type size();
62 
63  // Put:
64  // 1. A single ParameterSet.
65  static ParameterSetID const& put(ParameterSet const& ps);
66  // 2. A range of iterator to ParameterSet.
67  template <class FwdIt>
68  static std::enable_if_t<
69  std::is_same<typename std::iterator_traits<FwdIt>::value_type,
71  put(FwdIt begin, FwdIt end);
72  // 3. A range of iterator to pair<ParameterSetID, ParameterSet>. For
73  // each pair, first == second.id() is a prerequisite.
74  template <class FwdIt>
75  static std::enable_if_t<
76  std::is_same<typename std::iterator_traits<FwdIt>::value_type,
78  put(FwdIt begin, FwdIt end);
79  // 4. A collection_type. For each value_type, first == second.id() is
80  // a prerequisite.
81  static void put(collection_type const& c);
82 
83  // Accessors.
84  static collection_type const& get() noexcept;
85  static ParameterSet const& get(ParameterSetID const& id);
86  static bool get(ParameterSetID const& id, ParameterSet& ps);
87  static bool has(ParameterSetID const& id);
88 
89 private:
91  static ParameterSetRegistry& instance_();
92  const_iterator find_(ParameterSetID const& id);
93 
94  sqlite3* primaryDB_;
95  sqlite3_stmt* stmt_{nullptr};
96  collection_type registry_{};
97  static std::recursive_mutex mutex_;
98 };
99 
100 inline bool
102 {
103  std::lock_guard<decltype(mutex_)> lock{mutex_};
104  return instance_().registry_.empty();
105 }
106 
107 inline auto
109 {
110  std::lock_guard<decltype(mutex_)> lock{mutex_};
111  return instance_().registry_.size();
112 }
113 
114 // 1.
115 inline auto
117  -> ParameterSetID const&
118 {
119  std::lock_guard<decltype(mutex_)> lock{mutex_};
120  return instance_().registry_.emplace(ps.id(), ps).first->first;
121 }
122 
123 // 2.
124 template <class FwdIt>
125 inline auto
126 fhicl::ParameterSetRegistry::put(FwdIt b, FwdIt const e) -> std::enable_if_t<
127  std::is_same<typename std::iterator_traits<FwdIt>::value_type,
129 {
130  // No lock here -- it will be acquired by 3.
131  for (; b != e; ++b) {
132  (void)put(*b);
133  }
134 }
135 
136 // 3.
137 template <class FwdIt>
138 inline auto
139 fhicl::ParameterSetRegistry::put(FwdIt const b, FwdIt const e)
140  -> std::enable_if_t<
141  std::is_same<typename std::iterator_traits<FwdIt>::value_type,
143 {
144  std::lock_guard<decltype(mutex_)> lock{mutex_};
145  instance_().registry_.insert(b, e);
146 }
147 
148 // 4.
149 inline void
151 {
152  // No lock here -- it will be acquired by 3.
153  put(c.cbegin(), c.cend());
154 }
155 
156 inline auto
158 {
159  std::lock_guard<decltype(mutex_)> lock{mutex_};
160  return instance_().registry_;
161 }
162 
163 inline auto
165  -> ParameterSet const&
166 {
167  std::lock_guard<decltype(mutex_)> lock{mutex_};
168  auto it = instance_().find_(id);
169  if (it == instance_().registry_.cend()) {
170  throw exception(error::cant_find, "Can't find ParameterSet")
171  << "with ID " << id.to_string() << " in the registry.";
172  }
173  return it->second;
174 }
175 
176 inline bool
178 {
179  std::lock_guard<decltype(mutex_)> lock{mutex_};
180  bool result{false};
181  auto it = instance_().find_(id);
182  if (it != instance_().registry_.cend()) {
183  ps = it->second;
184  result = true;
185  }
186  return result;
187 }
188 
189 inline bool
191 {
192  std::lock_guard<decltype(mutex_)> lock{mutex_};
193  auto const& reg = instance_().registry_;
194  return reg.find(id) != reg.cend();
195 }
196 
197 inline auto
199 {
200  static ParameterSetRegistry s_registry;
201  return s_registry;
202 }
203 
204 inline size_t
206 {
207  return hash_(id.to_string());
208 }
209 
210 #endif /* fhiclcpp_ParameterSetRegistry_h */
211 
212 // Local Variables:
213 // mode: c++
214 // End:
typename collection_type::size_type size_type
static ParameterSetID const & put(ParameterSet const &ps)
static collection_type const & get() noexcept
std::string to_string(Protection p)
Definition: Protection.cc:4
typename collection_type::mapped_type mapped_type
intermediate_table::const_iterator const_iterator
parameter set interface
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
size_t operator()(ParameterSetID const &id) const
static bool has(ParameterSetID const &id)
static std::recursive_mutex mutex_
std::string value(boost::any const &)
typename collection_type::const_iterator const_iterator
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
typename collection_type::value_type value_type
Float_t e
Definition: plot.C:34
void throwOnSQLiteFailure(sqlite3 *db, char *msg=nullptr)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
typename collection_type::key_type key_type
static ParameterSetRegistry & instance_()
std::unordered_map< ParameterSetID, ParameterSet, detail::HashParameterSetID > collection_type