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 #include <map>
12 #include <stdexcept>
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
set< int >::iterator it
static reg_t & the_registry_()
Definition: registry.h:66
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
std::pair< Spectrum *, CheatDecomp * > make_pair(SpectrumLoaderBase &loader_data, SpectrumLoaderBase &loader_mc, HistAxis *axis, Cut *cut, const SystShifts &shift, const Var &wei)
Definition: DataMCLoad.C:336
std::map< K const, V > reg_t
Definition: registry.h:30
Double_t K
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Definition: typedefs.hpp:11
static reg_t::size_type size()
Definition: registry.h:42
const XML_Char int const XML_Char * value
Definition: expat.h:331
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
iter_t const_iterator
Definition: registry.h:34