Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
p6scopedptr.h
Go to the documentation of this file.
1 
11 #ifndef P6SCOPEDPTR_H__
12 #define P6SCOPEDPTR_H__ 1
13 
14 #ifdef __cplusplus
15 namespace P6R {
16 
27  template<typename T> class p6ScopedPtr
28  {
29  public:
30  typedef T element_type;
31 
32  explicit p6ScopedPtr(T *ptr=0) : m_ptr(ptr)
33  {}
34 
36  {
45  // As of GCC 5.4 this is an error
46  // error: typedef ‘ensure_type_is_complete’ locally defined but not used
47  // typedef char ensure_type_is_complete[sizeof(T)];
48  delete m_ptr;
49  }
50 
51  T* operator->() const { P6ASSERT(NULL != m_ptr); return m_ptr; }
52  T& operator*() const { P6ASSERT(NULL != m_ptr); return *m_ptr; }
53  P6BOOL operator!=(T &p) const { return m_ptr != p ? P6TRUE:P6FALSE; }
54  P6BOOL operator==(T *p) const { return m_ptr == p ? P6TRUE:P6FALSE; }
55  P6BOOL operator! () const { return NULL == m_ptr ? P6TRUE:P6FALSE; }
56 
60  T* get() const
61  { return m_ptr; }
62 
66  void swap(p6ScopedPtr &b)
67  {
68  T* pTmp = b.m_ptr;
69  b.m_ptr = m_ptr;
70  m_ptr = pTmp;
71  }
72 
80  T* release()
81  {
82  T* pTmp = m_ptr;
83  m_ptr = NULL;
84  return pTmp;
85  }
86 
93  void reset(T *p=0)
94  {
95  //typedef char typemustbecomplete[sizeof(T*)];
96  if(m_ptr != p) {
97  delete m_ptr;
98  m_ptr = p;
99  }
100  }
101 
102  private:
103  T *m_ptr;
104 
105  private:
106  p6ScopedPtr(p6ScopedPtr const &);
107  p6ScopedPtr & operator=(p6ScopedPtr const &);
108  void operator==(p6ScopedPtr const&) const;
109  void operator!=(p6ScopedPtr const&) const;
110  };
111 
112 
113 } // namespace
114 #endif // __cplusplus
115 #endif
116 
#define P6TRUE
Definition: p6types.h:134
void swap(p6ScopedPtr &b)
Exchange ownership with another p6ScopedArray.
Definition: p6scopedptr.h:66
T * operator->() const
Definition: p6scopedptr.h:51
unsigned char P6BOOL
Boolean type.
Definition: p6types.h:133
void reset(T *p=0)
Destroy any existing owned object, then take ownership of the object pointed to by p argument...
Definition: p6scopedptr.h:93
T & operator*() const
Definition: p6scopedptr.h:52
P6ERR(P6CCALL * P6ASSERT)(const P6WCHAR *pszExpr, const P6CHAR *pszFile, P6UINT32 nLine)
Definition: p6dllapi.h:58
P6BOOL operator!() const
Definition: p6scopedptr.h:55
p6ScopedPtr(T *ptr=0)
Definition: p6scopedptr.h:32
P6BOOL operator!=(T &p) const
Definition: p6scopedptr.h:53
P6BOOL operator==(T *p) const
Definition: p6scopedptr.h:54
T * release()
Return the wrapped raw pointer and give up ownership.
Definition: p6scopedptr.h:80
A simple smart pointer that mimics a standard pointer to an allocated object (allocated with new) and...
Definition: p6scopedptr.h:27
#define P6FALSE
Definition: p6types.h:135