blob: e4df78ede0dabb1b5084460dc7b7b6fa6366d0a8 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_COMMON_SCOPED_PTR_H_
2#define AOS_COMMON_SCOPED_PTR_H_
3
4#include "aos/common/macros.h"
5
6namespace aos {
7
8// A simple scoped_ptr implementation that works under both linux and vxworks.
9template<typename T>
10class 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_