MPQC 3.0.0-alpha
Loading...
Searching...
No Matches
registry.h
1//
2// registry.h
3//
4// Copyright (C) 2008 Edward Valeev
5//
6// Author: Edward Valeev <evaleev@vt.edu>
7// Maintainer: EV
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifndef _mpqc_src_lib_chemistry_qc_mbptr12_registry_h
29#define _mpqc_src_lib_chemistry_qc_mbptr12_registry_h
30
31#include <map>
32#include <stdexcept>
33#include <util/group/thread.h>
34#include <util/state/state.h>
35#include <util/state/statein.h>
36#include <util/state/stateout.h>
37
38namespace sc {
39
40 namespace detail {
43 template <typename T>
45 protected:
46 static Ref<T> instance() {
47 return instance_;
48 }
49 static Ref<T> instance(StateIn& si) {
50 if (!instance_restored_) {
51 instance_ = new T(si);
52 instance_restored_ = true;
53 }
54 return instance_;
55 }
56 static void save_instance(const Ref<T>& instance, StateOut& so) {
57 if (!instance_saved_) {
58 instance_->save_data_state(so);
59 instance_saved_ = true;
60 }
61 }
62 static Ref<T> instance_;
63 static bool instance_restored_;
64 static bool instance_saved_;
65 };
66
67 template <typename T> Ref<T> SingletonCreationPolicy<T>::instance_(new T);
68 template <typename T> bool SingletonCreationPolicy<T>::instance_restored_ = false;
69 template <typename T> bool SingletonCreationPolicy<T>::instance_saved_ = false;
70
72 template<typename T>
74 protected:
75 static Ref<T> instance() {
76 return new T;
77 }
78 static Ref<T> instance(StateIn& si) {
79 bool nonnull; si.get(nonnull);
80 if (nonnull)
81 return new T(si);
82 else
83 return 0;
84 }
85 static void save_instance(const Ref<T>& instance, StateOut& so) {
86 if (instance) {
87 so.put(true);
88 instance->save_data_state(so);
89 }
90 else
91 so.put(false);
92 }
93 };
94
95 } // end of namespace detail
96
108 template <typename Key, typename Value, template <typename> class CreationPolicy,
109 typename KeyEqual = std::equal_to<Key>,
110 typename ValueEqual = std::equal_to<Value>
111 >
112 class Registry : public RefCount, public CreationPolicy< Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual> > {
113 public:
114 static Ref<Registry> instance();
115 static void save_instance(const Ref<Registry>&, StateOut&);
116 static Ref<Registry> restore_instance(StateIn&);
117
119 void clear();
121 void remove(const Key& key);
123 template <typename Pred> void remove_if(const Pred& p) {
124 ThreadLockHolder lh(lock_);
125 typename Map::iterator i = map_.begin();
126 for(; i != map_.end(); ) {
127 if (p(*i)) {
128 map_.erase(i++);
129 } else {
130 ++i;
131 }
132 }
133 }
135 bool key_exists(const Key& key) const;
137 bool value_exists(const Value& value) const;
139 const Value& value(const Key& key) const;
141 const Key& key(const Value& obj) const;
143 void add(const Key& key,
144 const Value& obj);
146 void add(const std::pair<Key,Value>& keyval_pair);
147
148 void print(std::ostream& os = ExEnv::out0()) const;
149
150 class not_found : public std::logic_error {
151 public:
152 not_found(const char* what) : std::logic_error(what) {}
153 };
154
155 private:
156 // creation policy must be able to construct Registry
157 friend class CreationPolicy< Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual> >;
158
159 // access only through instance() and related methods
160 Registry();
161 // use restore_instance
163 // use save_instance
164 void save_data_state(StateOut&);
165
166 typedef std::map<Key,Value> Map;
167 typedef typename Map::const_iterator const_iterator;
168 typedef typename Map::iterator iterator;
169 Map map_;
170
171 // assumes that map is already locked
172 const_iterator find_by_key(const Key& key) const;
173 // assumes that map is already locked
174 iterator find_by_key(const Key& key);
175 // assumes that map is already locked
176 const_iterator find_by_value(const Value& value) const;
177
178 // std::map's operations are not reentrant, hence lock the map every time
179 Ref<ThreadLock> lock_;
180
182 template <typename T1, typename T2> struct SameType {
183 static const bool result = false;
184 };
185 template <typename T> struct SameType<T,T> {
186 static const bool result = true;
187 };
188
189 };
190
191} // end of namespace sc
192
193#include <util/misc/registry.timpl.h>
194
195#endif // end of header guard
196
197
198// Local Variables:
199// mode: c++
200// c-file-style: "CLJ-CONDENSED"
201// End:
static std::ostream & out0()
Return an ostream that writes from node 0.
The base class for all reference counted objects.
Definition ref.h:192
A template class that maintains references counts.
Definition ref.h:361
Definition registry.h:150
Registry wraps std::map and can be policy-configured to act as a Singleton or a regular object.
Definition registry.h:112
void remove_if(const Pred &p)
removes all objects whose keys evaluate predicate to true: p(key) == true
Definition registry.h:123
bool value_exists(const Value &value) const
value exists?
Definition registry.timpl.h:155
void remove(const Key &key)
removes the object corresponding to key
Definition registry.timpl.h:226
bool key_exists(const Key &key) const
key exists?
Definition registry.timpl.h:140
void add(const Key &key, const Value &obj)
registers this object
Definition registry.timpl.h:206
const Value & value(const Key &key) const
returns object that corresponds to this key. If key is not known, throws
Definition registry.timpl.h:187
const Key & key(const Value &obj) const
returns key that corresponds to this object. If obj is not known, throws
Definition registry.timpl.h:170
void clear()
erases all entries
Definition registry.timpl.h:82
Restores fundamental and user-defined types from images created with StateOut.
Definition statein.h:79
virtual int get(const ClassDesc **)
This restores ClassDesc's.
Serializes fundamental and user-defined types.
Definition stateout.h:71
virtual int put(const ClassDesc *)
Write out information about the given ClassDesc.
Acquire a lock on creation and release it on destruction.
Definition thread.h:56
NonsingletonCreationPolicy is used to create non-Singletons on heap.
Definition registry.h:73
SingletonCreationPolicy is used to create Singletons.
Definition registry.h:44
Contains all MPQC code up to version 3.
Definition mpqcin.h:14

Generated at Wed Sep 25 2024 02:45:31 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.12.0.