123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- #pragma once
- #include "../foundation/dispatch.h"
- #include "../foundation/atomics.h"
- #include <new>
- #include "../nx/nxstring.h"
- #include "../nx/nxuri.h"
- #define REFERENCE_COUNT_AS(x) size_t Retain() { return x::Retain(); } size_t Release() { return x::Release(); }
- template <class t>
- class ReferenceCounted : public t
- {
- public:
- ReferenceCounted() { reference_count = 1; }
- protected:
- /* Dispatchable implementation */
- size_t WASABICALL Dispatchable_Retain()
- {
- return nx_atomic_inc(&reference_count);
- }
- size_t WASABICALL Dispatchable_Release()
- {
- if (!reference_count)
- return reference_count;
- size_t r = nx_atomic_dec(&reference_count);
- if (!r)
- {
- #if defined(__ARM_ARCH_7A__)
- __asm__ __volatile__ ("dmb" : : : "memory");
- #endif
- delete(this);
- }
- return r;
- }
- size_t reference_count;
- };
- template <class t>
- class ReferenceCountedBase
- {
- public:
- ReferenceCountedBase() { reference_count = 1; }
- size_t Retain()
- {
- return nx_atomic_inc(&reference_count);
- }
- size_t Release()
- {
- if (!reference_count)
- return reference_count;
- size_t r = nx_atomic_dec(&reference_count);
- if (!r)
- {
- #if defined(__ARM_ARCH_7A__)
- __asm__ __volatile__ ("dmb" : : : "memory");
- #endif
- delete static_cast<t*>(this);
- }
- return r;
- }
- size_t reference_count;
- };
- template <class t>
- class ReferenceCountedObject
- {
- public:
- ReferenceCountedObject()
- {
- ptr = new (std::nothrow) ReferenceCounted<t>;
- };
- ~ReferenceCountedObject()
- {
- if (ptr)
- ptr->Release();
- }
- operator t *()
- {
- return ptr;
- }
- t *operator ->()
- {
- return ptr;
- }
- t *ptr;
- };
- template <class t>
- class ReferenceCountedPointer
- {
- public:
- ReferenceCountedPointer()
- {
- ptr = 0;
- };
- ReferenceCountedPointer(t *new_ptr)
- {
- ptr = new_ptr;
- };
- ~ReferenceCountedPointer()
- {
- if (ptr)
- ptr->Release();
- }
- operator t *()
- {
- return ptr;
- }
- t *operator ->()
- {
- return ptr;
- }
- t **operator &()
- {
- // if there's something already in here, we need to release it first
- if (ptr)
- ptr->Release();
- ptr=0;
- return &ptr;
- }
- t *operator =(t *new_ptr)
- {
- if (ptr)
- ptr->Release();
- ptr=0;
- ptr = new_ptr;
- return ptr;
- }
- t *ptr;
- };
- class ReferenceCountedNXString
- {
- public:
- ReferenceCountedNXString()
- {
- ptr = 0;
- }
- ReferenceCountedNXString(const ReferenceCountedNXString ©)
- {
- ptr = NXStringRetain(copy.ptr);
- }
- ~ReferenceCountedNXString()
- {
- NXStringRelease(ptr);
- }
- operator nx_string_t()
- {
- return ptr;
- }
- nx_string_t *operator &()
- {
- // if there's something already in here, we need to release it first
- if (ptr)
- NXStringRelease(ptr);
- ptr=0;
- return &ptr;
- }
- nx_string_t operator =(nx_string_t new_ptr)
- {
- if (ptr)
- NXStringRelease(ptr);
-
- ptr = new_ptr;
- return ptr;
- }
-
- nx_string_t operator ->()
- {
- return ptr;
- }
- nx_string_t ptr;
- };
- class ReferenceCountedNXURI
- {
- public:
- ReferenceCountedNXURI()
- {
- ptr = 0;
- }
- ReferenceCountedNXURI(const ReferenceCountedNXURI ©)
- {
- ptr = NXURIRetain(copy.ptr);
- }
- ~ReferenceCountedNXURI()
- {
- NXURIRelease(ptr);
- }
- operator nx_uri_t()
- {
- return ptr;
- }
- nx_uri_t *operator &()
- {
- // if there's something already in here, we need to release it first
- if (ptr)
- NXURIRelease(ptr);
- ptr=0;
- return &ptr;
- }
- nx_uri_t operator ->()
- {
- return ptr;
- }
- nx_uri_t ptr;
- };
|