CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/DetectorDescription/Core/interface/DDBase.h

Go to the documentation of this file.
00001 #ifndef DDCore_DDBase_h
00002 #define DDCore_DDBase_h
00003 
00004 #include <utility>
00005 #include <string>
00006 #include "DetectorDescription/Base/interface/Singleton.h"
00007 #include "DetectorDescription/Base/interface/Store.h"
00008 #include "DetectorDescription/Base/interface/rep_type.h"
00009 
00013 template <class N, class C>
00014 class DDBase
00015 {
00016 public:
00017   template <class D>
00018   class iterator
00019   {
00020   public:
00022     typedef D value_type;
00023 
00024     explicit iterator( const typename DDI::Store<N,C>::iterator & it) : it_(it) { }
00025         
00026     iterator() : it_(StoreT::instance().begin()) {  }
00027   
00028     value_type& operator*() const {
00029       d_.prep_ = it_->second;
00030       return d_;
00031     }
00032   
00033     value_type* operator->() const {
00034       d_.prep_ = it_->second;
00035       return &d_;
00036     }
00037   
00038     bool operator==(const iterator & i) {
00039       return i.it_ == it_;
00040     }
00041     
00042     bool operator!=(const iterator & i) {
00043       return i.it_ != it_;
00044     }
00045     
00046     bool operator<(const iterator & i) {
00047       return it_ < i.it_;
00048     }
00049     
00050     bool operator>(const iterator & i) {
00051       return it_ > i.it_;
00052     }
00053     
00054     void operator++() {
00055       ++it_;
00056     }
00057     
00058     void end() const {
00059       it_ = StoreT::instance().end();
00060     }  
00061         
00062      
00063   private:
00064     mutable typename DDI::Store<N,C>::iterator it_;
00065     mutable D d_;
00066   };
00067    
00068 public:
00069   static typename DDI::Store<N,C>::iterator end()   { return StoreT::instance().end(); }
00070   static typename DDI::Store<N,C>::iterator begin() { return StoreT::instance().begin(); }
00071   // dangerous stuff!!
00072   static void clear() { StoreT::instance().clear(); }
00073   static size_t size() { return StoreT::instance().size(); }
00074   typedef DDI::Singleton<DDI::Store<N,C> > StoreT;
00075   typedef C pimpl_type;
00076   typedef DDI::rep_type<N,pimpl_type>* prep_type;
00077   typedef std::pair<const N*,bool> def_type;
00078   
00079   DDBase() : prep_(0) { }
00080   virtual ~DDBase() { /*never do this here: if (prep_) delete prep_;*/ }
00081       
00082   const N & name() const { return prep_->name(); }
00083   
00084   const N & ddname() const { return prep_->name(); }
00085   
00086   std::string toString() const { return prep_->name().fullname(); }
00087     
00088 
00089   const typename DDI::rep_traits<N,C>::reference rep() const 
00090     { return *(prep_->second); }
00091   
00092   typename DDI::rep_traits<N,C>::reference rep() 
00093     { return *(prep_->second); }
00094     
00095   const typename DDI::rep_traits<N,C>::reference val() const
00096    { if (!isValid()) throw cms::Exception("DDException") << "undefined: " << name(); 
00097       return rep();
00098     };  
00099 
00100   const typename DDI::rep_traits<N,C>::reference val() 
00101    { if (!isValid()) throw cms::Exception("DDException") << "undefined: " << name(); 
00102       return rep();
00103     };  
00104   
00105   bool operator==(const DDBase & b) const { return prep_ == b.prep_; }
00106   // true, if registered or defined
00107   operator bool() const { return isValid(); }
00108   
00109   bool operator<(const DDBase & b) const { return prep_ < b.prep_; }
00110   bool operator>(const DDBase & b) const { return prep_ > b.prep_; }
00111   
00112   // (name*,true) if defined
00113   // (name*,false) if registered but not defined
00114   // (0,false) if not there at all
00115   def_type isDefined() const 
00116     {
00117       return prep_ ?
00118         std::make_pair(&(prep_->name()), bool(prep_->second))
00119         :
00120         std::make_pair((const N *)0,false);  
00121     } 
00122    
00124   bool isValid() const
00125     {
00126       return prep_ ? bool(prep_->second)
00127         : false;
00128     } 
00129   
00130 protected: 
00131   prep_type prep_; 
00132 private:
00133   //bool operator==(const DDBase &);
00134   //bool operator<(const DDBase &) const ;  
00135 };
00136 
00137 #endif