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/exception.h"
13 #include "fhiclcpp/fwd.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 typedef struct sqlite3 sqlite3;
19 typedef struct sqlite3_stmt sqlite3_stmt;
20 #ifdef __cplusplus
21 }
22 #endif
23 
24 #include <mutex>
25 #include <unordered_map>
26 
27 namespace fhicl {
28 
29  class ParameterSetRegistry;
30 
31  namespace detail {
32  class HashParameterSetID;
33  void throwOnSQLiteFailure(int rc, char* msg = nullptr);
34  void throwOnSQLiteFailure(sqlite3* db, char* msg = nullptr);
35  }
36 }
37 
39 public:
40  size_t operator()(ParameterSetID const& id) const;
41 
42 private:
43  std::hash<std::string> hash_;
44 };
45 
47 public:
48  ParameterSetRegistry(ParameterSet const&) = delete;
50  ParameterSetRegistry& operator=(ParameterSet const&) = delete;
51  ParameterSetRegistry& operator=(ParameterSet&&) = delete;
53 
54  // Typedefs.
55  using collection_type = std::
56  unordered_map<ParameterSetID, ParameterSet, detail::HashParameterSetID>;
57  using key_type = typename collection_type::key_type;
58  using mapped_type = typename collection_type::mapped_type;
59  using value_type = typename collection_type::value_type;
60  using size_type = typename collection_type::size_type;
62 
63  // DB interaction.
64  static void importFrom(sqlite3* db);
65  static void exportTo(sqlite3* db);
66  static void stageIn();
67 
68  // Observers.
69  static bool empty();
70  static size_type size();
71 
72  // Put:
73  // 1. A single ParameterSet.
74  static ParameterSetID const& put(ParameterSet const& ps);
75  // 2. A range of iterator to ParameterSet.
76  template <class FwdIt>
77  static std::enable_if_t<
78  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type,
79  mapped_type>>
80  put(FwdIt begin, FwdIt end);
81  // 3. A range of iterator to pair<ParameterSetID, ParameterSet>. For
82  // each pair, first == second.id() is a prerequisite.
83  template <class FwdIt>
84  static std::enable_if_t<
85  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type,
86  value_type>>
87  put(FwdIt begin, FwdIt end);
88  // 4. A collection_type. For each value_type, first == second.id() is
89  // a prerequisite.
90  static void put(collection_type const& c);
91 
92  // Accessors.
93  static collection_type const& get() noexcept;
94  static ParameterSet const& get(ParameterSetID const& id);
95  static bool get(ParameterSetID const& id, ParameterSet& ps);
96  static bool has(ParameterSetID const& id);
97 
98 private:
100  static ParameterSetRegistry& instance_();
101  const_iterator find_(ParameterSetID const& id);
102 
104  sqlite3_stmt* stmt_{nullptr};
105  collection_type registry_{};
106  static std::recursive_mutex mutex_;
107 };
108 
109 inline bool
111 {
112  std::lock_guard sentry{mutex_};
113  return instance_().registry_.empty();
114 }
115 
116 inline auto
118 {
119  std::lock_guard sentry{mutex_};
120  return instance_().registry_.size();
121 }
122 
123 // 1.
124 inline auto
126  -> ParameterSetID const&
127 {
128  std::lock_guard sentry{mutex_};
129  return instance_().registry_.emplace(ps.id(), ps).first->first;
130 }
131 
132 // 2.
133 template <class FwdIt>
134 inline auto
135 fhicl::ParameterSetRegistry::put(FwdIt b, FwdIt const e) -> std::enable_if_t<
136  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type, mapped_type>>
137 {
138  // No lock here -- it will be acquired by 3.
139  for (; b != e; ++b) {
140  (void)put(*b);
141  }
142 }
143 
144 // 3.
145 template <class FwdIt>
146 inline auto
147 fhicl::ParameterSetRegistry::put(FwdIt const b, FwdIt const e)
148  -> std::enable_if_t<
149  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type,
150  value_type>>
151 {
152  std::lock_guard sentry{mutex_};
153  instance_().registry_.insert(b, e);
154 }
155 
156 // 4.
157 inline void
159 {
160  // No lock here -- it will be acquired by 3.
161  put(c.cbegin(), c.cend());
162 }
163 
164 inline auto
166 {
167  std::lock_guard sentry{mutex_};
168  return instance_().registry_;
169 }
170 
171 inline auto
173  -> ParameterSet const&
174 {
175  std::lock_guard sentry{mutex_};
176  auto it = instance_().find_(id);
177  if (it == instance_().registry_.cend()) {
178  throw exception(error::cant_find, "Can't find ParameterSet")
179  << "with ID " << id.to_string() << " in the registry.";
180  }
181  return it->second;
182 }
183 
184 inline bool
186 {
187  std::lock_guard sentry{mutex_};
188  bool result{false};
189  auto it = instance_().find_(id);
190  if (it != instance_().registry_.cend()) {
191  ps = it->second;
192  result = true;
193  }
194  return result;
195 }
196 
197 inline bool
199 {
200  std::lock_guard sentry{mutex_};
201  auto const& reg = instance_().registry_;
202  return reg.find(id) != reg.cend();
203 }
204 
205 inline auto
207 {
208  static ParameterSetRegistry s_registry;
209  return s_registry;
210 }
211 
212 inline size_t
214 {
215  return hash_(id.to_string());
216 }
217 
218 #endif /* fhiclcpp_ParameterSetRegistry_h */
219 
220 // Local Variables:
221 // mode: c++
222 // End:
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
typename collection_type::size_type size_type
static ParameterSetID const & put(ParameterSet const &ps)
static QCString result
void msg(const char *fmt,...)
Definition: message.cpp:107
static collection_type const & get() noexcept
struct sqlite3_stmt sqlite3_stmt
intermediate_table::const_iterator const_iterator
std::string to_string(Protection p)
Definition: Protection.cc:4
typename collection_type::mapped_type mapped_type
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
static const_iterator cend()
const double e
size_t operator()(ParameterSetID const &id) const
static constexpr double ps
Definition: Units.h:99
static bool has(ParameterSetID const &id)
static std::recursive_mutex mutex_
struct sqlite3 sqlite3
typename collection_type::const_iterator const_iterator
static bool * b
Definition: config.cpp:1043
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
typename collection_type::value_type value_type
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
typename collection_type::key_type key_type
static ParameterSetRegistry & instance_()
std::unordered_map< ParameterSetID, ParameterSet, detail::HashParameterSetID > collection_type