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 #ifdef __cplusplus
13 namespace P6R {
14 extern "C" {
15 #endif
16 
26  #undef INTERFACE
27  #define INTERFACE p6IRefCounted
29  {
32  };
33 
34 #ifdef __cplusplus
35 } // extern "C"
36 
37  template<typename I> class p6RefCountedBase
38  {
39  protected:
41  : m_pRaw(NULL)
42  { }
43 
44  p6RefCountedBase(I *pRawPtr = NULL)
45  : m_pRaw(pRawPtr)
46  {
47  if(m_pRaw) { m_pRaw->addref(); }
48  }
49 
50  void release()
51  {
52  if(m_pRaw) {
53  P6R::p6IRefCounted* pTmp = m_pRaw;
54  m_pRaw = NULL;
55  pTmp->release();
56  }
57  }
58 
59  private:
60  //I** operator &() { return reinterpret_cast<I**>(&m_pRaw); }
61 
62  public:
63  typedef I element_type;
64 
66 
67  operator I*() const { return static_cast<I*>(m_pRaw); }
68  I& operator*() const { P6ASSERT(NULL != m_pRaw); return *static_cast<I*>(m_pRaw); }
69  I* operator->() const { P6ASSERT(NULL != m_pRaw); return static_cast<I*>(m_pRaw); }
70  P6BOOL operator==(I* pI) const { return (pI == m_pRaw) ? P6TRUE:P6FALSE; }
71  P6BOOL operator!=(I* pI) const { return (pI != m_pRaw) ? P6TRUE:P6FALSE; }
72  P6BOOL operator<(I* pI) const { return (m_pRaw < pI) ? P6TRUE:P6FALSE; }
73  P6BOOL operator!() const { return (NULL == m_pRaw) ? P6TRUE:P6FALSE; }
74 
75 
76  I** addressof()
77  {
78  return reinterpret_cast<I**>(&m_pRaw);
79  }
80 
82  {
83  release();
84  return reinterpret_cast<I**>(&m_pRaw);
85  }
86 
88  inline void swap(I** pRaw)
89  {
90  I *tmp = *pRaw;
91  *pRaw = reinterpret_cast<I*>(m_pRaw);
92  m_pRaw = tmp;
93  }
94 
96  inline void swap(p6RefCountedBase<I> &rhs)
97  {
98  I *tmp = rhs.m_pRaw;
99  rhs.m_pRaw = m_pRaw;
100  m_pRaw = tmp;
101  }
102 
103  I* get() const
104  {
105  return reinterpret_cast<I*>(m_pRaw);
106  }
107 
113  void attach(I* pIface)
114  {
115  release();
116  m_pRaw = pIface;
117  }
118 
125  I* detach()
126  {
127  I* p = m_pRaw;
128  m_pRaw = NULL;
129  return p;
130  }
131 
140  void detach(I** ppIface)
141  {
142  *ppIface = NULL;
143  swap(ppIface);
144  }
145 
146  protected:
147  I *m_pRaw;
148  //P6R::p6IRefCounted *m_pRaw;
149 
150  };
151 
152 
153  template<typename I> class p6RefCounted : public p6RefCountedBase<I>
154  {
155  public:
156  typedef I element_type;
161  explicit p6RefCounted(I* ip = 0) : p6RefCountedBase<I>(ip)
162  { }
163 
168  { }
169 
172  {
173  if(*this != ip) {
174  p6RefCounted<I> tmp(ip);
175  this->swap(tmp);
176  }
177  return *this;
178  }
179 
182  {
183  if(*this!=cp) {
184  p6RefCounted<I> tmp(cp);
185  this->swap(tmp);
186  }
187  return *this;
188  }
189  };
190 
191 
192 } // namespace
193 #endif // __cplusplus
194 #endif
195 
196 
#define P6TRUE
Definition: p6types.h:134
#define P6DECLAREIF(iface)
Definition: p6defs.h:187
#define P6PURE
Definition: p6defs.h:192
unsigned char P6BOOL
Boolean type.
Definition: p6types.h:133
P6BOOL operator!() const
Definition: p6refcounted.h:73
I & operator*() const
Definition: p6refcounted.h:68
virtual P6INT32 release(void)=0
P6BOOL operator==(I *pI) const
Definition: p6refcounted.h:70
P6ERR(P6CCALL * P6ASSERT)(const P6WCHAR *pszExpr, const P6CHAR *pszFile, P6UINT32 nLine)
Definition: p6dllapi.h:58
void attach(I *pIface)
Take ownership of a new interface without addref().
Definition: p6refcounted.h:113
void swap(I **pRaw)
Exchange ownership.
Definition: p6refcounted.h:88
I * operator->() const
Definition: p6refcounted.h:69
#define P6DECLCOMMETHOD_(type, method)
Definition: p6defs.h:190
p6RefCounted(I *ip=0)
Construct from a raw pointer of the correct type Must be derrived from p6ICom.
Definition: p6refcounted.h:161
void swap(p6RefCountedBase< I > &rhs)
Exchange ownership.
Definition: p6refcounted.h:96
I * detach()
Detach the owned inteface and return a dumb pointer without release().
Definition: p6refcounted.h:125
This is a non-COM based interface that is used (along with the P6_REFCOUNTED macro) for reference cou...
Definition: p6refcounted.h:28
p6RefCounted(const p6RefCounted< I > &rSI)
Copy.
Definition: p6refcounted.h:167
p6RefCounted< I > & operator=(I *ip)
Assign from a dumb pointer of the correct type.
Definition: p6refcounted.h:171
p6RefCounted< I > & operator=(const p6RefCounted< I > &cp)
Assign from a smart pointer of the correct type.
Definition: p6refcounted.h:181
P6BOOL operator<(I *pI) const
Definition: p6refcounted.h:72
int32_t P6INT32
Definition: p6types.h:76
P6BOOL operator!=(I *pI) const
Definition: p6refcounted.h:71
#define P6THIS
Definition: p6defs.h:194
p6RefCountedBase(I *pRawPtr=NULL)
Definition: p6refcounted.h:44
#define P6FALSE
Definition: p6types.h:135
void detach(I **ppIface)
Detach the owned interface without release().
Definition: p6refcounted.h:140