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(int rc, char* msg = nullptr);
26  void throwOnSQLiteFailure(sqlite3* db, char* msg = nullptr);
27  }
28 }
29 
31 public:
32  size_t operator()(ParameterSetID const& id) const;
33 
34 private:
35  std::hash<std::string> hash_;
36 };
37 
39 public:
40  ParameterSetRegistry(ParameterSet const&) = delete;
42  ParameterSetRegistry& operator=(ParameterSet const&) = delete;
43  ParameterSetRegistry& operator=(ParameterSet&&) = delete;
45 
46  // Typedefs.
47  using collection_type = std::
48  unordered_map<ParameterSetID, ParameterSet, detail::HashParameterSetID>;
49  using key_type = typename collection_type::key_type;
50  using mapped_type = typename collection_type::mapped_type;
51  using value_type = typename collection_type::value_type;
53  using const_iterator = typename collection_type::const_iterator;
54 
55  // DB interaction.
56  static void importFrom(sqlite3* db);
57  static void exportTo(sqlite3* db);
58  static void stageIn();
59 
60  // Observers.
61  static bool empty();
62  static size_type size();
63 
64  // Put:
65  // 1. A single ParameterSet.
66  static ParameterSetID const& put(ParameterSet const& ps);
67  // 2. A range of iterator to ParameterSet.
68  template <class FwdIt>
69  static std::enable_if_t<
70  std::is_same<typename std::iterator_traits<FwdIt>::value_type,
72  put(FwdIt begin, FwdIt end);
73  // 3. A range of iterator to pair<ParameterSetID, ParameterSet>. For
74  // each pair, first == second.id() is a prerequisite.
75  template <class FwdIt>
76  static std::enable_if_t<
77  std::is_same<typename std::iterator_traits<FwdIt>::value_type,
79  put(FwdIt begin, FwdIt end);
80  // 4. A collection_type. For each value_type, first == second.id() is
81  // a prerequisite.
82  static void put(collection_type const& c);
83 
84  // Accessors.
85  static collection_type const& get() noexcept;
86  static ParameterSet const& get(ParameterSetID const& id);
87  static bool get(ParameterSetID const& id, ParameterSet& ps);
88  static bool has(ParameterSetID const& id);
89 
90 private:
92  static ParameterSetRegistry& instance_();
93  const_iterator find_(ParameterSetID const& id);
94 
95  sqlite3* primaryDB_;
96  sqlite3_stmt* stmt_{nullptr};
97  collection_type registry_{};
98  static std::recursive_mutex mutex_;
99 };
100 
101 inline bool
103 {
104  std::lock_guard<decltype(mutex_)> lock{mutex_};
105  return instance_().registry_.empty();
106 }
107 
108 inline auto
110 {
111  std::lock_guard<decltype(mutex_)> lock{mutex_};
112  return instance_().registry_.size();
113 }
114 
115 // 1.
116 inline auto
118  -> ParameterSetID const&
119 {
120  std::lock_guard<decltype(mutex_)> lock{mutex_};
121  return instance_().registry_.emplace(ps.id(), ps).first->first;
122 }
123 
124 // 2.
125 template <class FwdIt>
126 inline auto
127 fhicl::ParameterSetRegistry::put(FwdIt b, FwdIt const e) -> std::enable_if_t<
128  std::is_same<typename std::iterator_traits<FwdIt>::value_type,
130 {
131  // No lock here -- it will be acquired by 3.
132  for (; b != e; ++b) {
133  (void)put(*b);
134  }
135 }
136 
137 // 3.
138 template <class FwdIt>
139 inline auto
140 fhicl::ParameterSetRegistry::put(FwdIt const b, FwdIt const e)
141  -> std::enable_if_t<
142  std::is_same<typename std::iterator_traits<FwdIt>::value_type,
144 {
145  std::lock_guard<decltype(mutex_)> lock{mutex_};
146  instance_().registry_.insert(b, e);
147 }
148 
149 // 4.
150 inline void
152 {
153  // No lock here -- it will be acquired by 3.
154  put(c.cbegin(), c.cend());
155 }
156 
157 inline auto
159 {
160  std::lock_guard<decltype(mutex_)> lock{mutex_};
161  return instance_().registry_;
162 }
163 
164 inline auto
166  -> ParameterSet const&
167 {
168  std::lock_guard<decltype(mutex_)> lock{mutex_};
169  auto it = instance_().find_(id);
170  if (it == instance_().registry_.cend()) {
171  throw exception(error::cant_find, "Can't find ParameterSet")
172  << "with ID " << id.to_string() << " in the registry.";
173  }
174  return it->second;
175 }
176 
177 inline bool
179 {
180  std::lock_guard<decltype(mutex_)> lock{mutex_};
181  bool result{false};
182  auto it = instance_().find_(id);
183  if (it != instance_().registry_.cend()) {
184  ps = it->second;
185  result = true;
186  }
187  return result;
188 }
189 
190 inline bool
192 {
193  std::lock_guard<decltype(mutex_)> lock{mutex_};
194  auto const& reg = instance_().registry_;
195  return reg.find(id) != reg.cend();
196 }
197 
198 inline auto
200 {
201  static ParameterSetRegistry s_registry;
202  return s_registry;
203 }
204 
205 inline size_t
207 {
208  return hash_(id.to_string());
209 }
210 
211 #endif /* fhiclcpp_ParameterSetRegistry_h */
212 
213 // Local Variables:
214 // mode: c++
215 // End:
::xsd::cxx::tree::id< char, ncname > id
Definition: Database.h:165
typename collection_type::size_type size_type
static ParameterSetID const & put(ParameterSet const &ps)
set< int >::iterator it
static collection_type const & get() noexcept
std::string to_string(Protection p)
typename collection_type::mapped_type mapped_type
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Definition: typedefs.hpp:11
const XML_Char int const XML_Char * value
Definition: expat.h:331
size_t operator()(ParameterSetID const &id) const
static bool has(ParameterSetID const &id)
static std::recursive_mutex mutex_
typename collection_type::const_iterator const_iterator
const hit & b
Definition: hits.cxx:21
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
typename collection_type::value_type value_type
Float_t e
Definition: plot.C:35
typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData
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