brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #ifndef AOS_COMMON_SCOPED_PTR_H_ |
| 2 | #define AOS_COMMON_SCOPED_PTR_H_ |
| 3 | |
| 4 | #include "aos/common/macros.h" |
| 5 | |
| 6 | namespace aos { |
| 7 | |
| 8 | // A simple scoped_ptr implementation that works under both linux and vxworks. |
| 9 | template<typename T> |
| 10 | class scoped_ptr { |
| 11 | public: |
| 12 | typedef T element_type; |
| 13 | |
| 14 | explicit scoped_ptr(T *p = NULL) : p_(p) {} |
| 15 | ~scoped_ptr() { |
| 16 | delete p_; |
| 17 | } |
| 18 | |
| 19 | T &operator*() const { return *p_; } |
| 20 | T *operator->() const { return p_; } |
| 21 | T *get() const { return p_; } |
| 22 | |
| 23 | operator bool() const { return p_ != NULL; } |
| 24 | |
| 25 | void swap(scoped_ptr<T> &other) { |
| 26 | T *temp = other.p_; |
| 27 | other.p_ = p_; |
| 28 | p_ = other.p_; |
| 29 | } |
| 30 | void reset(T *p = NULL) { |
| 31 | if (p_ != NULL) delete p_; |
| 32 | p_ = p; |
| 33 | } |
| 34 | |
| 35 | private: |
| 36 | T *p_; |
| 37 | |
| 38 | DISALLOW_COPY_AND_ASSIGN(scoped_ptr<T>); |
| 39 | }; |
| 40 | |
| 41 | } // namespace aos |
| 42 | |
| 43 | #endif // AOS_COMMON_SCOPED_PTR_H_ |