registry.h
Go to the documentation of this file.
1 #ifndef cetlib_registry_h
2 #define cetlib_registry_h
3 
4 // ======================================================================
5 //
6 // registry<K,V>: A singleton std::map<K,V>
7 //
8 // ======================================================================
9 
10 #include "cetlib_except/exception.h"
11 
12 #include <map>
13 #include <utility>
14 
15 namespace cet {
16  template <class K, class V>
17  class registry;
18 }
19 
20 // ======================================================================
21 
22 template <class K, class V>
23 class cet::registry {
24  // non-instantiable (and non-copyable, just in case):
25  registry() = delete;
26  registry(registry const&) = delete;
27  void operator=(registry const&) = delete;
28 
29  // shorthand:
30  typedef std::map<K const, V> reg_t;
31  typedef typename reg_t::const_iterator iter_t;
32 
33 public:
34  typedef iter_t const_iterator;
35 
36  static bool
38  {
39  return the_registry_().empty();
40  }
41  static typename reg_t::size_type
42  size()
43  {
44  return the_registry_().size();
45  }
46 
47  static const_iterator
49  {
50  return the_registry_().begin();
51  }
52  static const_iterator
53  end()
54  {
55  return the_registry_().end();
56  }
57 
58  static void put(K const& key, V const& value);
59 
60  static V const& get(K const& key);
61  static bool get(K const& key, V& value) throw();
62 
63 private:
64  // encapsulated singleton:
65  static reg_t&
67  {
68  static reg_t the_registry;
69  return the_registry;
70  }
71 
72 }; // registry<>
73 
74 // ----------------------------------------------------------------------
75 
76 template <class K, class V>
77 void
79 {
80  the_registry_().insert(std::make_pair(key, value));
81 }
82 
83 template <class K, class V>
84 V const&
86 {
87  iter_t it = the_registry_().find(key);
88  if (it == the_registry_().end())
89  throw cet::exception("cet::registry")
90  << "Key \"" << key << "\" not found in registry";
91  return it->second;
92 }
93 
94 template <class K, class V>
95 bool
96 cet::registry<K, V>::get(K const& key, V& value) throw() try {
97  value = get(key);
98  return true;
99 }
100 catch (cet::exception const&) {
101  return false;
102 }
103 
104 // ======================================================================
105 
106 #endif /* cetlib_registry_h */
107 
108 // Local Variables:
109 // mode: c++
110 // End:
static void put(K const &key, V const &value)
Definition: registry.h:78
static reg_t & the_registry_()
Definition: registry.h:66
intermediate_table::const_iterator const_iterator
std::map< K const, V > reg_t
Definition: registry.h:30
static reg_t::size_type size()
Definition: registry.h:42
def key(type, name=None)
Definition: graph.py:13
void operator=(registry const &)=delete
registry()=delete
static const_iterator begin()
Definition: registry.h:48
static bool empty()
Definition: registry.h:37
static const_iterator end()
Definition: registry.h:53
static V const & get(K const &key)
Definition: registry.h:85
reg_t::const_iterator iter_t
Definition: registry.h:31
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
iter_t const_iterator
Definition: registry.h:34