AVPKit
RefPointer.h
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #ifndef REFPOINTER_H_
21 #define REFPOINTER_H_
22 
23 #ifdef VS_DEBUG
24 #include <assert.h>
25 #endif
26 #include <com/avpkit/ferry/RefCounted.h>
27 namespace com { namespace avpkit { namespace ferry
28  {
29 
46  template <class T> class RefPointer
47  {
48  public:
52  RefPointer() : mPtr(0)
53  {
54  }
55 
67  RefPointer(T* ptr) : mPtr(ptr)
68  {
69  }
70 
80  RefPointer(const RefPointer& rPtr) : mPtr(rPtr.mPtr)
81  {
82  VS_REF_ACQUIRE(mPtr);
83  }
90  RefPointer(RefPointer& rPtr) : mPtr(rPtr.mPtr)
91  {
92  VS_REF_ACQUIRE(mPtr);
93  }
94 
103  {
104  reset(static_cast<T*>(rPtr.mPtr));
105  VS_REF_ACQUIRE(mPtr);
106  return *this;
107  }
108 
117  {
118  reset(static_cast<T*>(rPtr.mPtr));
119  VS_REF_ACQUIRE(mPtr);
120  return *this;
121  }
122 
139  {
140  reset(ptr);
141  return *this;
142  }
143 
153  T* operator->() const
154  {
155  // we don't protect against a null defer here.
156  return static_cast<T*>(mPtr);
157  }
158 
159 
173  operator bool()
174  {
175  return mPtr!=0;
176  }
177 
181  virtual ~RefPointer()
182  {
183  #ifdef VS_DEBUG
184  {
185  if (mPtr)
186  {
187  // This check should never fail in a multi-thread
188  // environment, even though the underlying
189  // objects ref can change.
190  // If the object is bad, this should cause
191  // a READ error in memory tools
192  int refCount = mPtr->getCurrentRefCount();
193  assert(refCount >= 1);
194  }
195  }
196  #endif // VS_DEBUG
197  VS_REF_RELEASE(mPtr);
198  }
199 
206  T* get()
207  {
208  VS_REF_ACQUIRE(mPtr);
209  return static_cast<T*>(mPtr);
210  }
211 
226  T* value()
227  {
228  return static_cast<T*>(mPtr);
229  }
230 
237  void reset(T* ptr=0, bool acquire=false)
238  {
239  VS_REF_RELEASE(mPtr);
240  mPtr = ptr;
241  if (acquire)
242  VS_REF_ACQUIRE(mPtr);
243  }
244  private:
245  // And the underlying thing we manage.
246  // Must derive from RefCounted; We used to keep
247  // a RefCounted here, but it's much nicer to
248  // a debugger if you keep the actual type here.
249  T *mPtr;
250  };
251 
252  }}}
253 
254 #endif /*REFPOINTER_H_*/
This class is only useful from C++.
Definition: RefPointer.h:47
T * operator->() const
Allow people to def-ref the RefPointer() without RefCounted::acquire() the managed pointer.
Definition: RefPointer.h:153
RefPointer(T *ptr)
Construct a RefPointer from a RefCounted object, and take over management of the ref-count this point...
Definition: RefPointer.h:67
T * get()
Call RefCounted::acquire() on the managed pointer and return it.
Definition: RefPointer.h:206
RefPointer & operator=(T *ptr)
Assignment from a RefCounted* where we take over ownership of the refcount.
Definition: RefPointer.h:138
T * value()
Return the managed pointer without calling RefCounted::acquire() on it.
Definition: RefPointer.h:226
RefPointer(const RefPointer &rPtr)
Copy another RefPointer object.
Definition: RefPointer.h:80
RefPointer(RefPointer &rPtr)
Copy and acquire.
Definition: RefPointer.h:90
RefPointer()
Default constructor; sets the managed pointer to 0.
Definition: RefPointer.h:52
RefPointer & operator=(RefPointer &rPtr)
Assignment operator to copy another ref pointer.
Definition: RefPointer.h:116
virtual ~RefPointer()
Always release the ref-count of our underlying pointer.
Definition: RefPointer.h:181
RefPointer & operator=(const RefPointer &rPtr)
Assignment operator to copy another ref pointer.
Definition: RefPointer.h:102
void reset(T *ptr=0, bool acquire=false)
Reset the managed pointer, calling RefCounted::release() on the previously managed pointer first.
Definition: RefPointer.h:237
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...