Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:54:54

0001 #ifndef DETECTOR_DESCRIPTION_CORE_DDBASE_H
0002 #define DETECTOR_DESCRIPTION_CORE_DDBASE_H
0003 
0004 #include <utility>
0005 #include "DetectorDescription/Core/interface/Singleton.h"
0006 #include "DetectorDescription/Core/interface/Store.h"
0007 #include "DetectorDescription/Core/interface/rep_type.h"
0008 
0009 template <class N, class C>
0010 class DDBase {
0011 public:
0012   template <class D>
0013   class iterator {
0014   public:
0015     //! C is, for example, a DDLogicalPart or a DDMaterial or a DDSolid ...
0016     using value_type = D;
0017 
0018     explicit iterator(const typename DDI::Store<N, C>::iterator it) : it_(it) {}
0019 
0020     iterator() : it_(StoreT::instance().begin()) {}
0021 
0022     value_type& operator*() const {
0023       d_.prep_ = it_->second;
0024       return d_;
0025     }
0026 
0027     value_type* operator->() const {
0028       d_.prep_ = it_->second;
0029       return &d_;
0030     }
0031 
0032     bool operator==(const iterator& i) const { return i.it_ == it_; }
0033 
0034     bool operator!=(const iterator& i) const { return i.it_ != it_; }
0035 
0036     bool operator<(const iterator& i) const { return it_ < i.it_; }
0037 
0038     bool operator>(const iterator& i) const { return it_ > i.it_; }
0039 
0040     void operator++() { ++it_; }
0041 
0042     void end() const { it_ = StoreT::instance().end(); }
0043 
0044   private:
0045     mutable typename DDI::Store<N, C>::iterator it_;
0046     mutable D d_;
0047   };
0048 
0049 public:
0050   using StoreT = DDI::Singleton<DDI::Store<N, C> >;
0051   using def_type = std::pair<const N*, bool>;
0052   static auto end() { return StoreT::instance().end(); }
0053   static auto begin() { return StoreT::instance().begin(); }
0054 
0055   DDBase() : prep_(nullptr) {}
0056   virtual ~DDBase() { /*never do this here: if (prep_) delete prep_;*/
0057   }
0058 
0059   const N& name() const { return prep_->name(); }
0060 
0061   const N& ddname() const { return prep_->name(); }
0062 
0063   std::string toString() const { return prep_->name().fullname(); }
0064 
0065   const typename DDI::rep_traits<N, C>::reference rep() const { return *(prep_->second); }
0066 
0067   typename DDI::rep_traits<N, C>::reference rep() { return *(prep_->second); }
0068 
0069   const typename DDI::rep_traits<N, C>::reference val() const {
0070     if (!isValid())
0071       throw cms::Exception("DDException") << "undefined: " << name();
0072     return rep();
0073   }
0074 
0075   const typename DDI::rep_traits<N, C>::reference val() {
0076     if (!isValid())
0077       throw cms::Exception("DDException") << "undefined: " << name();
0078     return rep();
0079   }
0080 
0081   bool operator==(const DDBase& b) const { return prep_ == b.prep_; }
0082   // true, if registered or defined
0083   operator bool() const { return isValid(); }
0084   bool operator<(const DDBase& b) const { return prep_ < b.prep_; }
0085   bool operator>(const DDBase& b) const { return prep_ > b.prep_; }
0086 
0087   // ( name*, true ) if defined
0088   // ( name*, false ) if registered but not defined
0089   // ( 0, false ) if not there at all
0090   def_type isDefined() const {
0091     return prep_ ? std::make_pair(&(prep_->name()), bool(prep_->second)) : std::make_pair((const N*)nullptr, false);
0092   }
0093 
0094   //! true, if the wrapped pointer is valid
0095   bool isValid() const { return prep_ ? bool(prep_->second) : false; }
0096   void create(const N& name, C vals) { prep_ = StoreT::instance().create(name, std::move(vals)); }
0097   void create(const N& name) { prep_ = StoreT::instance().create(name); }
0098 
0099 private:
0100   DDI::rep_type<N, C>* prep_;
0101 };
0102 
0103 #endif