CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DetectorDescription/Base/interface/Store.h

Go to the documentation of this file.
00001 #ifndef DDI_Store_h
00002 #define DDI_Store_h
00003 
00004 #include <map>
00005 /* #include <iostream> */
00006 #include <DetectorDescription/Base/interface/rep_type.h>
00007 #include <FWCore/MessageLogger/interface/MessageLogger.h>
00008 //;
00009 //FIXME: Store : implement readOnly-behaviour ..
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    //typedef I* pimpl_type;
00043    typedef K key_type;
00044    typedef rep_type<name_type, pimpl_type> Rep_type;  
00045    // direct way - no comfortable dereferencing of a pointer to rep_type
00046    //typedef typename std::pair<name_type,pimpl_type> rep_type;
00047   /*
00048    struct rep_type {
00049      rep_type(const name_type & n, pimpl_type p) : first(n), second(p) { }
00050      key_type first;
00051      pimpl_type second;
00052    };
00053   */ 
00054    //typedef typename Rep_type<N,I> rep_type;
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    // empty shell or fetch from registry
00070    prep_type create(const name_type &);
00071    
00072    // full new object or replace existing with new one
00073    prep_type create(const name_type &, pimpl_type);
00074    
00075    // anonymous object, not stored
00076 /*    prep_type create(pimpl_type); */
00077    
00078    // clear all objects
00079    void clear();
00080 
00081    // swap moves the registry from this guy to another of the same type
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 //   rep_type as nested type inside Store, currently gives
00100 //   internal compiler error on gcc-2.95.2, but works on SUNWspro62Apr02Sun
00101 //    
00102 // template<class N, class I, class K>
00103 // Store<N,I,K>::rep_type::rep_type(const name_type & n, pimpl_type p)
00104 //  : std::pair(n,p)
00105 // { }
00106 
00107 /* template<class N, class I, class K> */
00108 /* void */
00109 /* Store<N,I,K>::clear() */
00110 /* { */
00111 /*   typename registry_type::iterator it = reg_.begin(); */
00112 /*   for (; it != reg_.end(); ++it) { */
00113 /*     delete it->second->second; */
00114 /*     delete it->second; */
00115 /*   }   */
00116 /*   reg_.clear(); */
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      // ELSE     
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    // ELSE     
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      //delete result.first->second->swap(p);
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 //     std::cout << "deleting " << it->first << std::endl;
00162      delete it->second->second; it->second->second = 0;
00163      delete it->second; it->second = 0;
00164    }
00165  } 
00166  
00167 /*  template<class N, class I, class K> */
00168 /*  typename Store<N,I,K>::prep_type  */
00169 /*  Store<N,I,K>::create(typename Store<N,I,K>::pimpl_type p) */
00170 /*  {                                      */
00171 /*    if (readOnly_) throw cms::Exception("DetectorDescriptionStore")<<" Store has been locked.  Illegal attempt to add " << name_type() << " to a global store.";  */
00172 /*     return new Rep_type(name_type(),p); */
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 } // namespace DDI
00196 
00197 #endif