Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:58

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   const N& name() const { return prep_->name(); }
0059 
0060   const N& ddname() const { return prep_->name(); }
0061 
0062   std::string toString() const { return prep_->name().fullname(); }
0063 
0064   const typename DDI::rep_traits<N, C>::reference rep() const { return *(prep_->second); }
0065 
0066   typename DDI::rep_traits<N, C>::reference rep() { return *(prep_->second); }
0067 
0068   const typename DDI::rep_traits<N, C>::reference val() const {
0069     if (!isValid())
0070       throw cms::Exception("DDException") << "undefined: " << name();
0071     return rep();
0072   }
0073 
0074   const typename DDI::rep_traits<N, C>::reference val() {
0075     if (!isValid())
0076       throw cms::Exception("DDException") << "undefined: " << name();
0077     return rep();
0078   }
0079 
0080   bool operator==(const DDBase& b) const { return prep_ == b.prep_; }
0081   // true, if registered or defined
0082   operator bool() const { return isValid(); }
0083   bool operator<(const DDBase& b) const { return prep_ < b.prep_; }
0084   bool operator>(const DDBase& b) const { return prep_ > b.prep_; }
0085 
0086   // ( name*, true ) if defined
0087   // ( name*, false ) if registered but not defined
0088   // ( 0, false ) if not there at all
0089   def_type isDefined() const {
0090     return prep_ ? std::make_pair(&(prep_->name()), bool(prep_->second)) : std::make_pair((const N*)nullptr, false);
0091   }
0092 
0093   //! true, if the wrapped pointer is valid
0094   bool isValid() const { return prep_ ? bool(prep_->second) : false; }
0095   void create(const N& name, C vals) { prep_ = StoreT::instance().create(name, std::move(vals)); }
0096   void create(const N& name) { prep_ = StoreT::instance().create(name); }
0097 
0098 private:
0099   DDI::rep_type<N, C>* prep_;
0100 };
0101 
0102 #endif