00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef __GNUG__
00029 #pragma interface
00030 #endif
00031
00032 #ifndef _mpqc_src_lib_chemistry_qc_mbptr12_registry_h
00033 #define _mpqc_src_lib_chemistry_qc_mbptr12_registry_h
00034
00035 #include <map>
00036 #include <stdexcept>
00037 #include <util/group/thread.h>
00038
00039 namespace sc {
00040
00041 namespace detail {
00044 template <typename T>
00045 class SingletonCreationPolicy {
00046 protected:
00047 static Ref<T> instance() {
00048 return instance_;
00049 }
00050 static Ref<T> instance(StateIn& si) {
00051 if (!instance_restored_) {
00052 instance_ = new T(si);
00053 instance_restored_ = true;
00054 }
00055 return instance_;
00056 }
00057 static void save_instance(const Ref<T>& instance, StateOut& so) {
00058 if (!instance_saved_) {
00059 instance_->save_data_state(so);
00060 instance_saved_ = true;
00061 }
00062 }
00063 static Ref<T> instance_;
00064 static bool instance_restored_;
00065 static bool instance_saved_;
00066 };
00067
00068 template <typename T> Ref<T> SingletonCreationPolicy<T>::instance_(new T);
00069 template <typename T> bool SingletonCreationPolicy<T>::instance_restored_ = false;
00070 template <typename T> bool SingletonCreationPolicy<T>::instance_saved_ = false;
00071
00073 template<typename T>
00074 class NonsingletonCreationPolicy {
00075 protected:
00076 static Ref<T> instance() {
00077 return new T;
00078 }
00079 static Ref<T> instance(StateIn& si) {
00080 return new T(si);
00081 }
00082 static void save_instance(const Ref<T>& instance, StateOut& so) {
00083 instance->save_data_state(so);
00084 }
00085 };
00086
00087 }
00088
00100 template <typename Key, typename Value, template <typename> class CreationPolicy,
00101 typename KeyEqual = std::equal_to<Key>,
00102 typename ValueEqual = std::equal_to<Value>
00103 >
00104 class Registry : public RefCount, public CreationPolicy< Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual> > {
00105 public:
00106 static Ref<Registry> instance();
00107 static void save_instance(const Ref<Registry>&, StateOut&);
00108 static Ref<Registry> restore_instance(StateIn&);
00109
00111 bool key_exists(const Key& key) const;
00113 bool value_exists(const Value& value) const;
00115 const Value& value(const Key& key) const;
00117 const Key& key(const Value& obj) const;
00119 void add(const Key& key,
00120 const Value& obj);
00122 void add(const std::pair<Key,Value>& keyval_pair);
00123
00124 class not_found : public std::runtime_error {
00125 public:
00126 not_found(const char* what) : std::runtime_error(what) {}
00127 };
00128
00129 private:
00130
00131 friend class CreationPolicy< Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual> >;
00132
00133
00134 Registry();
00135
00136 Registry(StateIn&);
00137
00138 void save_data_state(StateOut&);
00139
00140 typedef std::map<Key,Value> Map;
00141 typedef typename Map::const_iterator const_iterator;
00142 Map map_;
00143
00144
00145 const_iterator find_by_key(const Key& key) const;
00146
00147 const_iterator find_by_value(const Value& value) const;
00148
00149
00150 Ref<ThreadLock> lock_;
00151
00153 template <typename T1, typename T2> struct SameType {
00154 static const bool result = false;
00155 };
00156 template <typename T> struct SameType<T,T> {
00157 static const bool result = true;
00158 };
00159
00160 };
00161
00162 }
00163
00164 #include <chemistry/qc/mbptr12/registry.timpl.h>
00165
00166 #endif // end of header guard
00167
00168
00169
00170
00171
00172