CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/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 <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().fullname(); }
00093     
00094 
00095   const typename DDI::rep_traits<N,C>::reference rep() const 
00096     { return *(prep_->second); }
00097   
00098   typename DDI::rep_traits<N,C>::reference rep() 
00099     { return *(prep_->second); }
00100     
00101   const typename DDI::rep_traits<N,C>::reference val() const
00102     { if (!isValid()) throw DDException(std::string("undefined: ") + std::string(name())); 
00103       return rep();
00104     };  
00105 
00106   const typename DDI::rep_traits<N,C>::reference val() 
00107     { if (!isValid()) throw DDException(std::string("undefined: ") + std::string(name())); 
00108       return rep();
00109     };  
00110   
00111   bool operator==(const DDBase & b) const { return prep_ == b.prep_; }
00112   // true, if registered or defined
00113   operator bool() const { return prep_ ? prep_->second : false; }
00114   
00115   bool operator<(const DDBase & b) const { return prep_ < b.prep_; }
00116   bool operator>(const DDBase & b) const { return prep_ > b.prep_; }
00117   
00118   // (name*,true) if defined
00119   // (name*,false) if registered but not defined
00120   // (0,false) if not there at all
00121   def_type isDefined() const 
00122    {
00123      return prep_ ?
00124                     std::make_pair(&(prep_->name()), bool(prep_->second))
00125                   :
00126                     std::make_pair((const N *)0,false);  
00127    } 
00128    
00130   bool isValid() const
00131   {
00132      return prep_ ? bool(prep_->second)
00133                   : false;
00134   } 
00135   
00136 protected: 
00137   prep_type prep_; 
00138 private:
00139   //bool operator==(const DDBase &);
00140   //bool operator<(const DDBase &) const ;  
00141 };
00142 
00143 #endif