blob: 336e73b7537fa9388e56434718be16cadfa3ad41 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_SCOPED_PTR_H_
2#define AOS_SCOPED_PTR_H_
brians343bc112013-02-10 01:53:46 +00003
John Park33858a32018-09-28 23:05:48 -07004#include "aos/macros.h"
brians343bc112013-02-10 01:53:46 +00005
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
John Park33858a32018-09-28 23:05:48 -070043#endif // AOS_SCOPED_PTR_H_