43 #include <Ultralight/Defines.h> 55 virtual void AddRef()
const = 0;
56 virtual void Release()
const = 0;
57 virtual int ref_count()
const = 0;
62 inline void adopted(
const void*) { }
64 template<
typename T>
class Ref;
83 template<
typename T>
class Ref {
107 : instance_(other.instance_)
117 : instance_(other.
ptr())
127 : instance_(other.
ptr())
136 : instance_(&other.LeakRef())
146 : instance_(&other.LeakRef())
151 Ref& operator=(T&
object)
155 instance_->Release();
161 Ref& operator=(
const Ref& other)
164 other.instance_->AddRef();
165 instance_->Release();
166 instance_ = other.instance_;
172 Ref& operator=(
const Ref<U>& other)
175 other.instance_->AddRef();
176 instance_->Release();
177 instance_ = other.instance_;
182 Ref& operator=(
Ref&& reference)
185 instance_->Release();
186 instance_ = &reference.LeakRef();
191 template<
typename U>
Ref& operator=(Ref<U>&& reference)
194 instance_->Release();
195 instance_ = &reference.LeakRef();
200 const T* operator->()
const { assert(instance_);
return instance_; }
201 T* operator->() { assert(instance_);
return instance_; }
206 const T*
ptr()
const { assert(instance_);
return instance_; }
211 T*
ptr() { assert(instance_);
return instance_; }
216 const T&
get()
const { assert(instance_);
return *instance_; }
221 T&
get() { assert(instance_);
return *instance_; }
223 operator T&() { assert(instance_);
return *instance_; }
224 operator const T&()
const { assert(instance_);
return *instance_; }
226 template<
typename U> Ref<T> Replace(Ref<U>&&);
231 T* result = std::move(instance_);
232 instance_ = std::forward<T*>(
nullptr);
236 friend inline bool operator==(
const Ref& a,
const Ref& b) {
237 return a.instance_ == b.instance_;
240 friend inline bool operator!=(
const Ref& a,
const Ref& b) {
241 return a.instance_ != b.instance_;
244 friend inline bool operator<(
const Ref& a,
const Ref& b) {
245 return a.instance_ < b.instance_;
249 friend Ref AdoptRef<T>(T&);
250 template<
typename U>
friend class RefPtr;
252 enum AdoptTag { Adopt };
253 Ref(T&
object, AdoptTag)
261 template<
typename T>
template<
typename U> Ref<T> Ref<T>::Replace(Ref<U>&& reference)
263 auto oldReference =
AdoptRef(*instance_);
264 instance_ = &reference.LeakRef();
313 : instance_(other.instance_)
324 : instance_(other.instance_)
334 : instance_(other.LeakRef())
343 : instance_(other.LeakRef())
352 : instance_(other.instance_)
369 T* old_value = std::move(instance_);
370 instance_ = std::forward<T*>(
nullptr);
372 old_value->Release();
378 T*
get()
const {
return instance_; }
381 T* result = std::move(instance_);
382 instance_ = std::forward<T*>(
nullptr);
386 T& operator*()
const { assert(instance_);
return *instance_; }
387 T* operator->()
const {
return instance_; }
389 bool operator!()
const {
return !instance_; }
392 typedef T* (
RefPtr::*UnspecifiedBoolType);
393 operator UnspecifiedBoolType()
const {
return instance_ ? &RefPtr::instance_ :
nullptr; }
397 RefPtr& operator=(std::nullptr_t);
398 template<
typename U>
RefPtr& operator=(
const RefPtr<U>&);
400 template<
typename U>
RefPtr& operator=(RefPtr<U>&&);
401 template<
typename U>
RefPtr& operator=(Ref<U>&&);
403 friend inline bool operator==(
const RefPtr& a,
const RefPtr& b) {
404 return a.instance_ == b.instance_;
407 friend inline bool operator!=(
const RefPtr& a,
const RefPtr& b) {
408 return a.instance_ != b.instance_;
411 friend inline bool operator<(
const RefPtr& a,
const RefPtr& b) {
412 return a.instance_ < b.instance_;
424 : instance_(&reference.LeakRef())
438 RefPtr<T>& RefPtr<T>::operator=(
const RefPtr<U>& other)
446 RefPtr<T>& RefPtr<T>::operator=(T*
object)
454 RefPtr<T>& RefPtr<T>::operator=(std::nullptr_t)
456 T* old_instance = std::move(instance_);
457 instance_ = std::forward<T*>(
nullptr);
459 old_instance->Release();
464 RefPtr<T>& RefPtr<T>::operator=(RefPtr&& other)
466 RefPtr ptr = std::move(other);
473 RefPtr<T>& RefPtr<T>::operator=(RefPtr<U>&& other)
475 RefPtr ptr = std::move(other);
482 RefPtr<T>& RefPtr<T>::operator=(Ref<U>&& other)
484 RefPtr ptr = std::move(other);
490 void RefPtr<T>::Swap(RefPtr& other)
492 std::swap(instance_, other.instance_);
496 void Swap(RefPtr<T>& a, RefPtr<T>& b)
501 template<
typename T,
typename U>
502 bool operator==(
const RefPtr<T>& a,
const RefPtr<U>& b)
504 return a.get() == b.get();
507 template<
typename T,
typename U>
508 bool operator==(
const RefPtr<T>& a, U* b)
513 template<
typename T,
typename U>
514 bool operator==(T* a,
const RefPtr<U>& b)
519 template<
typename T,
typename U>
520 bool operator!=(
const RefPtr<T>& a,
const RefPtr<U>& b)
522 return a.get() != b.get();
525 template<
typename T,
typename U>
526 bool operator!=(
const RefPtr<T>& a, U* b)
531 template<
typename T,
typename U>
532 bool operator!=(T* a,
const RefPtr<U>& b)
RefPtr(const Ref< U > &other)
Construct from a Ref.
Ref(Ref< U > &other)
Copy constructor with internal type conversion.
RefPtr(std::nullptr_t)
Construct a NULL ref-pointer.
RefPtr(const RefPtr &other)
Copy constructor.
RefPtr(RefPtr &&other)
Move constructor.
A non-nullable smart pointer.
A nullable smart pointer.
This is a set of common JavaScriptCore Helper functions to simplify sample code.
~RefPtr()
Destroy RefPtr (wll decrement ref-count by one)
Interface for all ref-counted objects that will be managed using the Ref<> and RefPtr<> smart pointer...
RefPtr(RefPtr< U > &&other)
Move constructor.
~Ref()
Destroy Ref (wll decrement ref-count by one)
Ref(const Ref &other)
Copy constructor.
RefPtr(T *other)
Construct from a pointer.
const T * ptr() const
Get a pointer to wrapped object.
RefPtr()
Construct a NULL ref-pointer.
Ref< T > AdoptRef(T &)
Helper for wrapping new objects with the Ref smart pointer.
T * get() const
Get a pointer to wrapped object.
T & get()
Get a reference to wrapped object.
Ref(Ref< U > &&other)
Move constructor.
const T & get() const
Get a reference to wrapped object.
Ref(T &object)
Construct Ref from a reference.
Ref(const Ref< U > &other)
Copy constructor with internal type conversion.
RefPtr(const RefPtr< U > &other)
Copy constructor with internal type conversion.
Ref(Ref &&other)
Move constructor.
T * ptr()
Get a pointer to wrapped object.