Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:19

0001 #ifndef GENERS_IOPTR_HH_
0002 #define GENERS_IOPTR_HH_
0003 
0004 #include "Alignment/Geners/interface/IOException.hh"
0005 #include <string>
0006 
0007 // "Silent" pointers for the I/O system
0008 namespace gs {
0009   template <typename T>
0010   class IOProxy;
0011 
0012   template <typename T>
0013   class IOPtr {
0014     template <typename T2>
0015     friend class IOProxy;
0016 
0017   public:
0018     typedef T element_type;
0019 
0020     inline IOPtr() : ptr_(0) {}
0021     inline IOPtr(T *ptr) : ptr_(ptr) {}
0022     IOPtr(const IOProxy<T> &p);
0023 
0024     IOPtr &operator=(const IOProxy<T> &p);
0025     inline IOPtr &operator=(const IOPtr &p) {
0026       ptr_ = p.ptr_;
0027       return *this;
0028     }
0029 
0030     // Typical pointer operations
0031     inline T *get() const { return ptr_; }
0032     inline T *operator->() const {
0033       if (!ptr_)
0034         throw gs::IOInvalidArgument("In gs::IOPtr::operator->: attempt to dereference null pointer");
0035       return ptr_;
0036     }
0037     inline T &operator*() const {
0038       if (!ptr_)
0039         throw gs::IOInvalidArgument("In gs::IOPtr::operator*: attempt to dereference null pointer");
0040       return *ptr_;
0041     }
0042     inline IOPtr &operator++() {
0043       ++ptr_;
0044       return *this;
0045     }
0046     inline void operator++(int) { ++ptr_; }
0047     inline IOPtr &operator--() {
0048       --ptr_;
0049       return *this;
0050     }
0051     inline void operator--(int) { --ptr_; }
0052 
0053     // Implicit conversion to "bool"
0054     inline operator bool() const { return !(ptr_ == 0); }
0055 
0056     // Names are ignored during comparisons
0057     inline bool operator==(const IOPtr &r) const { return ptr_ == r.ptr_; }
0058     inline bool operator!=(const IOPtr &r) const { return ptr_ != r.ptr_; }
0059     bool operator==(const IOProxy<T> &r) const;
0060     bool operator!=(const IOProxy<T> &r) const;
0061 
0062     // Should be able to work as with bare pointer inside the I/O code
0063     inline T *&getIOReference() { return ptr_; }
0064     inline T *const &getIOReference() const {
0065       if (!ptr_)
0066         throw gs::IOInvalidArgument(
0067             "In gs::IOPtr::getIOReference: unusable "
0068             "const reference to null pointer");
0069       return ptr_;
0070     }
0071 
0072   private:
0073     T *ptr_;
0074   };
0075 
0076   template <typename T>
0077   class IOProxy {
0078     template <typename T2>
0079     friend class IOPtr;
0080 
0081   public:
0082     typedef T element_type;
0083 
0084     inline IOProxy() : ptr_(0) {}
0085     inline IOProxy(T *ptr) : ptr_(ptr) {}
0086     inline IOProxy(T *ptr, const char *varname) : ptr_(ptr), name_(varname ? varname : "") {}
0087     inline IOProxy(T *ptr, const std::string &varname) : ptr_(ptr), name_(varname) {}
0088     inline IOProxy(const IOPtr<T> &p) : ptr_(p.ptr_) {}
0089     inline IOProxy(const IOPtr<T> &p, const char *varname) : ptr_(p.ptr_), name_(varname ? varname : "") {}
0090     inline IOProxy(const IOPtr<T> &p, const std::string &varname) : ptr_(p.ptr_), name_(varname) {}
0091 
0092     inline IOProxy &operator=(const IOProxy &p) {
0093       ptr_ = p.ptr_;
0094       name_ = p.name_;
0095       return *this;
0096     }
0097     inline IOProxy &operator=(const IOPtr<T> &p) {
0098       ptr_ = p.ptr_;
0099       name_ = "";
0100       return *this;
0101     }
0102 
0103     // Get name
0104     inline const std::string &name() const { return name_; }
0105 
0106     // Set name
0107     inline IOProxy &setName(const char *varname) {
0108       name_ = varname ? varname : "";
0109       return *this;
0110     }
0111     inline IOProxy &setName(const std::string &varname) {
0112       name_ = varname;
0113       return *this;
0114     }
0115 
0116     // Typical pointer operations
0117     inline T *get() const { return ptr_; }
0118     inline T *operator->() const {
0119       if (!ptr_)
0120         throw gs::IOInvalidArgument("In gs::IOProxy::operator->: attempt to dereference null pointer");
0121       return ptr_;
0122     }
0123     inline T &operator*() const {
0124       if (!ptr_)
0125         throw gs::IOInvalidArgument("In gs::IOProxy::operator*: attempt to dereference null pointer");
0126       return *ptr_;
0127     }
0128     inline IOProxy &operator++() {
0129       ++ptr_;
0130       return *this;
0131     }
0132     inline void operator++(int) { ++ptr_; }
0133     inline IOProxy &operator--() {
0134       --ptr_;
0135       return *this;
0136     }
0137     inline void operator--(int) { --ptr_; }
0138 
0139     // Implicit conversion to "bool"
0140     inline operator bool() const { return !(ptr_ == 0); }
0141 
0142     // Names are ignored during comparisons
0143     inline bool operator==(const IOProxy &r) const { return ptr_ == r.ptr_; }
0144     inline bool operator!=(const IOProxy &r) const { return ptr_ != r.ptr_; }
0145     inline bool operator==(const IOPtr<T> &r) const { return ptr_ == r.ptr_; }
0146     inline bool operator!=(const IOPtr<T> &r) const { return ptr_ != r.ptr_; }
0147 
0148     // Should be able to work as with bare pointer inside the I/O code
0149     inline T *&getIOReference() { return ptr_; }
0150     inline T *const &getIOReference() const {
0151       if (!ptr_)
0152         throw gs::IOInvalidArgument(
0153             "In gs::IOProxy::getIOReference: unusable "
0154             "const reference to null pointer");
0155       return ptr_;
0156     }
0157 
0158   private:
0159     T *ptr_;
0160     std::string name_;
0161   };
0162 
0163   // Convenience function for making IOPtr objects
0164   template <typename T>
0165   inline IOPtr<T> make_IOPtr(T &obj) {
0166     return IOPtr<T>(&obj);
0167   }
0168 
0169   // In the user code, the following function can be usually wrapped
0170   // as follows:
0171   //
0172   // #define io_proxy(obj) gs::make_IOProxy( obj , #obj )
0173   //
0174   template <typename T>
0175   inline IOProxy<T> make_IOProxy(T &obj, const char *name) {
0176     return IOProxy<T>(&obj, name);
0177   }
0178 }  // namespace gs
0179 
0180 // IOPtr methods which could not be defined earlier
0181 namespace gs {
0182   template <typename T>
0183   inline IOPtr<T>::IOPtr(const IOProxy<T> &p) : ptr_(p.ptr_) {}
0184 
0185   template <typename T>
0186   inline IOPtr<T> &IOPtr<T>::operator=(const IOProxy<T> &p) {
0187     ptr_ = p.ptr_;
0188     return *this;
0189   }
0190 
0191   template <typename T>
0192   inline bool IOPtr<T>::operator==(const IOProxy<T> &r) const {
0193     return ptr_ == r.ptr_;
0194   }
0195 
0196   template <typename T>
0197   inline bool IOPtr<T>::operator!=(const IOProxy<T> &r) const {
0198     return ptr_ != r.ptr_;
0199   }
0200 }  // namespace gs
0201 
0202 #endif  // GENERS_IOPTR_HH_