Last active
December 18, 2015 05:58
-
-
Save RedBeard0531/5736292 to your computer and use it in GitHub Desktop.
A dumb non-owning pointer type that implicitly converts from any smart or dumb pointer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** A generic non-owning pointer type for function arguments. | |
| * It will convert from any pointer type except auto_ptr. | |
| * Semantics are the same as passing the pointer returned from get() | |
| * const ptr<T> => T * const | |
| * ptr<const T> => T const * or const T* | |
| */ | |
| template <typename T> | |
| struct ptr { | |
| ptr() : _p(NULL) {} | |
| // convert to ptr<T> | |
| ptr(T* p) : _p(p) {} // needed for NULL | |
| template<typename U> ptr(U* p) : _p(p) {} | |
| template<typename U> ptr(const ptr<U>& p) : _p(p) {} | |
| template<typename U> ptr(const boost::shared_ptr<U>& p) : _p(p.get()) {} | |
| template<typename U> ptr(const boost::scoped_ptr<U>& p) : _p(p.get()) {} | |
| //template<typename U> ptr(const auto_ptr<U>& p) : _p(p.get()) {} // not supported | |
| // assign to ptr<T> | |
| ptr& operator= (T* p) { _p = p; return *this; } // needed for NULL | |
| template<typename U> ptr& operator= (U* p) { _p = p; return *this; } | |
| template<typename U> ptr& operator= (const ptr<U>& p) { _p = p; return *this; } | |
| template<typename U> ptr& operator= (const boost::shared_ptr<U>& p) { _p = p.get(); return *this; } | |
| template<typename U> ptr& operator= (const boost::scoped_ptr<U>& p) { _p = p.get(); return *this; } | |
| //template<typename U> ptr& operator= (const auto_ptr<U>& p) { _p = p.get(); return *this; } // not supported | |
| // use | |
| T* operator->() const { return _p; } | |
| T& operator*() const { return *_p; } | |
| // convert from ptr<T> | |
| operator T* () const { return _p; } | |
| private: | |
| T* _p; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment