Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
p6refcounted.h
Go to the documentation of this file.
1 
9 #ifndef P6REFCOUNTED_H__
10 #define P6REFCOUNTED_H__ 1
11 
12 namespace P6R {
13 
23  interface p6IRefCounted
24  {
27  };
28 
29  template<typename I> class p6RefCountedBase
30  {
31  protected:
33  : m_pRaw(NULL)
34  { }
35 
36  p6RefCountedBase(I *pRawPtr = NULL)
37  : m_pRaw(pRawPtr)
38  {
39  if(m_pRaw) { m_pRaw->addref(); }
40  }
41 
42  void release()
43  {
44  if(m_pRaw) {
45  P6R::p6IRefCounted* pTmp = m_pRaw;
46  m_pRaw = NULL;
47  pTmp->release();
48  }
49  }
50 
51  private:
52  //I** operator &() { return reinterpret_cast<I**>(&m_pRaw); }
53 
54  public:
55  typedef I element_type;
56 
58 
59  operator I*() const { return static_cast<I*>(m_pRaw); }
60  I& operator*() const { P6ASSERT(NULL != m_pRaw); return *static_cast<I*>(m_pRaw); }
61  I* operator->() const { P6ASSERT(NULL != m_pRaw); return static_cast<I*>(m_pRaw); }
62  P6BOOL operator==(I* pI) const { return (pI == m_pRaw) ? P6TRUE:P6FALSE; }
63  P6BOOL operator!=(I* pI) const { return (pI != m_pRaw) ? P6TRUE:P6FALSE; }
64  P6BOOL operator<(I* pI) const { return (m_pRaw < pI) ? P6TRUE:P6FALSE; }
65  P6BOOL operator!() const { return (NULL == m_pRaw) ? P6TRUE:P6FALSE; }
66 
67 
68  I** addressof()
69  {
70  return reinterpret_cast<I**>(&m_pRaw);
71  }
72 
74  {
75  release();
76  return reinterpret_cast<I**>(&m_pRaw);
77  }
78 
80  inline void swap(I** pRaw)
81  {
82  I *tmp = *pRaw;
83  *pRaw = reinterpret_cast<I*>(m_pRaw);
84  m_pRaw = tmp;
85  }
86 
88  inline void swap(p6RefCountedBase<I> &rhs)
89  {
90  I *tmp = rhs.m_pRaw;
91  rhs.m_pRaw = m_pRaw;
92  m_pRaw = tmp;
93  }
94 
95  I* get() const
96  {
97  return reinterpret_cast<I*>(m_pRaw);
98  }
99 
105  void attach(I* pIface)
106  {
107  release();
108  m_pRaw = pIface;
109  }
110 
117  I* detach()
118  {
119  I* p = m_pRaw;
120  m_pRaw = NULL;
121  return p;
122  }
123 
132  void detach(I** ppIface)
133  {
134  *ppIface = NULL;
135  swap(ppIface);
136  }
137 
138  protected:
139  I *m_pRaw;
140  //P6R::p6IRefCounted *m_pRaw;
141 
142  };
143 
144 
145  template<typename I> class p6RefCounted : public p6RefCountedBase<I>
146  {
147  public:
148  typedef I element_type;
153  explicit p6RefCounted(I* ip = 0) : p6RefCountedBase<I>(ip)
154  { }
155 
160  { }
161 
164  {
165  if(*this != ip) {
166  p6RefCounted<I> tmp(ip);
167  swap(tmp);
168  }
169  return *this;
170  }
171 
174  {
175  if(*this!=cp) {
176  p6RefCounted<I> tmp(cp);
177  swap(tmp);
178  }
179  return *this;
180  }
181  };
182 
183 } // namespace
184 
185 #endif
186 
187 
#define P6TRUE
Definition: p6types.h:102
P6R::P6ERR(* P6ASSERT)(const P6R::P6WCHAR *pszExpr, const P6R::P6CHAR *pszFile, P6R::P6UINT32 nLine)
Definition: p6dllapi.h:57
bool P6BOOL
Boolean type.
Definition: p6types.h:101
P6BOOL operator!() const
Definition: p6refcounted.h:65
I & operator*() const
Definition: p6refcounted.h:60
P6BOOL operator==(I *pI) const
Definition: p6refcounted.h:62
void attach(I *pIface)
Take ownership of a new interface without addref().
Definition: p6refcounted.h:105
void swap(I **pRaw)
Exchange ownership.
Definition: p6refcounted.h:80
I * operator->() const
Definition: p6refcounted.h:61
p6RefCounted(I *ip=0)
Construct from a raw pointer of the correct type Must be derrived from p6ICom.
Definition: p6refcounted.h:153
int P6INT32
Definition: p6types.h:41
void swap(p6RefCountedBase< I > &rhs)
Exchange ownership.
Definition: p6refcounted.h:88
I * detach()
Detach the owned inteface and return a dumb pointer without release().
Definition: p6refcounted.h:117
This is a non-COM based interface that is used (along with the P6_REFCOUNTED macro) for reference cou...
Definition: p6refcounted.h:23
p6RefCounted(const p6RefCounted< I > &rSI)
Copy.
Definition: p6refcounted.h:159
#define P6COMMETHOD_(type)
Definition: p6types.h:873
p6RefCounted< I > & operator=(I *ip)
Assign from a dumb pointer of the correct type.
Definition: p6refcounted.h:163
virtual P6R::P6INT32 release()=0
p6RefCounted< I > & operator=(const p6RefCounted< I > &cp)
Assign from a smart pointer of the correct type.
Definition: p6refcounted.h:173
P6BOOL operator<(I *pI) const
Definition: p6refcounted.h:64
virtual P6R::P6INT32 addref()=0
P6BOOL operator!=(I *pI) const
Definition: p6refcounted.h:63
p6RefCountedBase(I *pRawPtr=NULL)
Definition: p6refcounted.h:36
#define P6FALSE
Definition: p6types.h:103
void detach(I **ppIface)
Detach the owned interface without release().
Definition: p6refcounted.h:132