registry_via_id.h
Go to the documentation of this file.
1 #ifndef cetlib_registry_via_id_h
2 #define cetlib_registry_via_id_h
3 
4 // ======================================================================
5 //
6 // registry_via_id<K,V>: A singleton std::map<K,V> requiring that V::id()
7 // exists and yields values of type K
8 //
9 // ======================================================================
10 
11 #include "cetlib_except/exception.h"
12 
13 #include <iterator>
14 #include <map>
15 #include <type_traits>
16 
17 namespace cet {
18  template <class K, class V>
20 
21  namespace detail {
22  template <class K, class V, K (V::*)() const = &V::id>
23  struct must_have_id {
24  typedef K type;
25  };
26  }
27 }
28 
29 // ======================================================================
30 
31 template <class K, class V>
33  // non-instantiable (and non-copyable, just in case):
34  registry_via_id() = delete;
35  registry_via_id(registry_via_id const&) = delete;
36  void operator=(registry_via_id const&) = delete;
37 
38 public:
39  using collection_type = std::map<K const, V>;
40  using key_type = typename collection_type::key_type;
41  using mapped_type = typename collection_type::mapped_type;
42  using value_type = typename collection_type::value_type;
43  using size_type = typename collection_type::size_type;
45 
46  // observers:
47  static bool
49  {
50  return the_registry_().empty();
51  }
52  static size_type
53  size()
54  {
55  return the_registry_().size();
56  }
57 
58  // iterators:
59  static const_iterator
61  {
62  return the_registry_().begin();
63  }
64  static const_iterator
65  end()
66  {
67  return the_registry_().end();
68  }
69  static const_iterator
71  {
72  return the_registry_().cbegin();
73  }
74  static const_iterator
75  cend()
76  {
77  return the_registry_().cend();
78  }
79 
80  // mutators:
81  // A single V;
82  static typename detail::must_have_id<K, V>::type put(V const& value);
83  // A range of iterator to V.
84  template <class FwdIt>
85  static std::enable_if_t<
86  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type,
87  mapped_type>>
88  put(FwdIt begin, FwdIt end);
89  // A range of iterator to std::pair<K, V>. For each pair, first ==
90  // second.id() is a prerequisite.
91  template <class FwdIt>
92  static std::enable_if_t<
93  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type,
94  value_type>>
95  put(FwdIt begin, FwdIt end);
96  // A collection_type. For each value_type, first == second.id() is a
97  // prerequisite.
98  static void put(collection_type const& c);
99 
100  // accessors:
101  static collection_type const&
102  get() noexcept
103  {
104  return the_registry_();
105  }
106  static V const& get(K const& key);
107  static bool get(K const& key, V& value) noexcept;
108 
109 private:
110  // encapsulated singleton:
111  static collection_type&
113  {
114  static collection_type the_registry;
115  return the_registry;
116  }
117 
118 }; // registry_via_id<>
119 
120 // ----------------------------------------------------------------------
121 // put() overloads:
122 
123 template <class K, class V>
126 {
127  K id = value.id();
128  the_registry_().emplace(id, value);
129  return id;
130 }
131 
132 template <class K, class V>
133 template <class FwdIt>
134 inline auto
135 cet::registry_via_id<K, V>::put(FwdIt b, FwdIt e) -> std::enable_if_t<
136  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type, mapped_type>>
137 {
138  for (; b != e; ++b)
139  put(*b);
140 }
141 
142 template <class K, class V>
143 template <class FwdIt>
144 inline auto
145 cet::registry_via_id<K, V>::put(FwdIt b, FwdIt e) -> std::enable_if_t<
146  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type, value_type>>
147 {
148  the_registry_().insert(b, e);
149 }
150 
151 template <class K, class V>
152 inline void
154 {
155  put(c.cbegin(), c.cend());
156 }
157 
158 // ----------------------------------------------------------------------
159 // get() overloads:
160 
161 template <class K, class V>
162 V const&
164 {
165  const_iterator it = the_registry_().find(key);
166  if (it == the_registry_().end())
167  throw cet::exception("cet::registry_via_id")
168  << "Key \"" << key << "\" not found in registry";
169  return it->second;
170 }
171 
172 template <class K, class V>
173 bool
175 {
176  bool result;
177  const_iterator it = the_registry_().find(key);
178  if (it == the_registry_().end()) {
179  result = false;
180  } else {
181  value = it->second;
182  result = true;
183  }
184  return result;
185 }
186 
187 #endif /* cetlib_registry_via_id_h */
188 
189 // Local Variables:
190 // mode: c++
191 // End:
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
typename collection_type::const_iterator const_iterator
typename collection_type::mapped_type mapped_type
static QCString result
static const_iterator begin()
intermediate_table::const_iterator const_iterator
int find(char c, int index=0, bool cs=TRUE) const
Definition: qcstring.cpp:41
static const_iterator end()
static collection_type & the_registry_()
typename collection_type::key_type key_type
typename collection_type::value_type value_type
static const_iterator cend()
const double e
typename collection_type::size_type size_type
def key(type, name=None)
Definition: graph.py:13
std::map< K const, V > collection_type
static collection_type const & get() noexcept
static size_type size()
static detail::must_have_id< K, V >::type put(V const &value)
static bool * b
Definition: config.cpp:1043
static const_iterator cbegin()
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33