Go to the documentation of this file.00001 #ifndef DDI_Store_h
00002 #define DDI_Store_h
00003
00004 #include <map>
00005
00006 #include <DetectorDescription/Base/interface/rep_type.h>
00007 #include <FWCore/MessageLogger/interface/MessageLogger.h>
00008
00009
00010 namespace DDI {
00011
00036 template <class N, class I, class K=I>
00037 class Store
00038 {
00039 public:
00040 typedef N name_type;
00041 typedef I pimpl_type;
00042
00043 typedef K key_type;
00044 typedef rep_type<name_type, pimpl_type> Rep_type;
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 typedef Rep_type* prep_type;
00057
00058 typedef std::map<name_type,prep_type> registry_type;
00059 typedef typename registry_type::iterator iterator;
00060 typedef typename registry_type::const_iterator const_iterator;
00061 typedef typename registry_type::size_type size_type;
00062
00063 iterator begin() { return reg_.begin(); }
00064 const_iterator begin() const { return reg_.begin(); }
00065 iterator end() { return reg_.end(); }
00066 const_iterator end() const { return reg_.end(); }
00067 size_type size() const { return reg_.size(); }
00068
00069
00070 prep_type create(const name_type &);
00071
00072
00073 prep_type create(const name_type &, pimpl_type);
00074
00075
00076
00077
00078
00079 void clear();
00080
00081
00082 void swap ( Store& );
00083
00084 bool isDefined(const name_type & n ) const;
00085 void setReadOnly(bool b) { readOnly_ = b; }
00086 bool readOnly() const { return readOnly_; }
00087
00088 Store() : readOnly_(false) { }
00089 ~Store();
00090
00091 protected:
00092 registry_type reg_;
00093 Store(const Store &);
00094 Store & operator=(const Store &);
00095 bool readOnly_;
00096 };
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 template<class N, class I, class K>
00120 typename Store<N,I,K>::prep_type
00121 Store<N,I,K>::create(const name_type & n)
00122 {
00123 prep_type tmp = 0;
00124 std::pair<typename registry_type::iterator,bool> result
00125 = reg_.insert(std::make_pair(n,tmp));
00126 if (result.second) {
00127 if (readOnly_) throw cms::Exception("DetectorDescriptionStore")<<" Store has been locked. Illegal attempt to add " << n << " to a global store.";
00128
00129 result.first->second = new Rep_type(n,(I)0);
00130 }
00131 return result.first->second;
00132 }
00133
00134
00135 template<class N, class I, class K>
00136 typename Store<N,I,K>::prep_type
00137 Store<N,I,K>::create(const name_type & n,
00138 pimpl_type p)
00139 {
00140 if (readOnly_) throw cms::Exception("DetectorDescriptionStore")<<" Store has been locked. Illegal attempt to add " << n << " to a global store.";
00141
00142 prep_type tmp = 0;
00143 std::pair<typename registry_type::iterator,bool> result
00144 = reg_.insert(std::make_pair(n,tmp));
00145 if (!result.second) {
00146 delete result.first->second->second;
00147 result.first->second->second = p;
00148
00149 }
00150 else {
00151 result.first->second = new Rep_type(n,p);
00152 }
00153 return result.first->second;
00154 }
00155
00156 template<class N, class I, class K>
00157 Store<N,I,K>::~Store()
00158 {
00159 typename registry_type::iterator it = reg_.begin();
00160 for(; it != reg_.end(); ++it) {
00161
00162 delete it->second->second; it->second->second = 0;
00163 delete it->second; it->second = 0;
00164 }
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 template<class N, class I, class K>
00176 bool Store<N,I,K>::isDefined(const name_type & n ) const
00177 {
00178 if (readOnly_) edm::LogWarning("DetectorDescriptionStore") << " Store is locked and most likely empty. isDefined will be false.";
00179 typename registry_type::const_iterator it = reg_.find(n);
00180 bool result(false);
00181 if (it != reg_.end()) {
00182 if (it->second->second) {
00183 result=true;
00184 }
00185 }
00186 return result;
00187 }
00188
00189 template<class N, class I, class K>
00190 void Store<N, I, K>::swap ( Store<N, I, K>& storeToSwap ) {
00191 reg_.swap(storeToSwap.reg_);
00192 storeToSwap.readOnly_ = readOnly_;
00193 }
00194
00195 }
00196
00197 #endif