CMS 3D CMS Logo

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

Generated on Tue Jun 9 17:31:58 2009 for CMSSW by  doxygen 1.5.4