Agora RTC Objective-C API Reference Refactor

AgoraRefCountedObject.h
Go to the documentation of this file.
1
2 // Copyright (c) 2020 Agora.io. All rights reserved
3
4 // This program is confidential and proprietary to Agora.io.
5 // And may not be copied, reproduced, modified, disclosed to others, published
6 // or used, in whole or in part, without the express prior written permission
7 // of Agora.io.
8 #pragma once
9
10 #include "AgoraRefPtr.h"
11 #include "AgoraAtomicOps.h"
12
13 #ifndef OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER
14 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
15 #define OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER agora::RefCountReleaseStatus::
16 #else
17 #define OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER
18 #endif
19 #endif
20 namespace agora {
21
22 class RefCounter {
23 public:
24 explicit RefCounter(int ref_count) : ref_count_(ref_count) {}
25
26 void IncRef() { AtomicOps::Increment(&ref_count_); }
27
32 agora::RefCountReleaseStatus DecRef() {
33 return (AtomicOps::Decrement(&ref_count_) == 0
34 ? OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER kDroppedLastRef
35 : OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER kOtherRefsRemained);
36 }
37
46 bool HasOneRef() const { return (AtomicOps::AcquireLoad(&ref_count_) == 1); }
47
48 private:
49 RefCounter();
50
51 private:
52 volatile int ref_count_;
53 };
54
62 template <class T>
63 class RefCountedObject : public T {
64 public:
65 RefCountedObject(): ref_count_(0) {}
66
67 template <class P0>
68 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
69 explicit RefCountedObject(P0&& p0) : T(std::forward<P0>(p0)), ref_count_(0) {}
70 #else
71 explicit RefCountedObject(const P0& p0) : T(p0), ref_count_(0) {}
72 #endif
73
74 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
75 template <class P0, class P1, class... Args>
76 RefCountedObject(P0&& p0, P1&& p1, Args&&... args)
77 : T(std::forward<P0>(p0),
78 std::forward<P1>(p1),
79 std::forward<Args>(args)...),
80 ref_count_(0) {}
81 #endif
82
83 virtual void AddRef() const { ref_count_.IncRef(); }
84
85 virtual agora::RefCountReleaseStatus Release() const {
86 const agora::RefCountReleaseStatus status = ref_count_.DecRef();
87 if (status == OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER kDroppedLastRef) {
88 delete this;
89 }
90 return status;
91 }
92
101 virtual bool HasOneRef() const { return ref_count_.HasOneRef(); }
102
103 protected:
104 virtual ~RefCountedObject() {}
105
106 private:
107 RefCountedObject(const RefCountedObject&);
108 RefCountedObject& operator=(const RefCountedObject&);
109
110 protected:
111 mutable agora::RefCounter ref_count_;
112 };
113
114 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
115 template <typename T, typename... types>
116 inline agora_refptr<T> make_refptr(types&&... args) {
117 return agora_refptr<T>(new RefCountedObject<T>(std::forward<types>(args)...));
118 }
119 #else
120 template <typename T>
121 inline agora_refptr<T> make_refptr() {
122 return agora_refptr<T>(new RefCountedObject<T>());
123 }
124 template <typename T, typename P0>
125 inline agora_refptr<T> make_refptr(const P0& p0) {
126 return agora_refptr<T>(new RefCountedObject<T>(p0));
127 }
128 #endif
129 } // namespace agora
agora::make_refptr
agora_refptr< T > make_refptr()
Definition: AgoraRefCountedObject.h:121
agora::agora_refptr
Definition: AgoraRefPtr.h:44
agora::RefCountedObject::~RefCountedObject
virtual ~RefCountedObject()
Definition: AgoraRefCountedObject.h:104
agora::RefCountedObject::HasOneRef
virtual bool HasOneRef() const
Definition: AgoraRefCountedObject.h:101
agora::RefCountedObject
Definition: AgoraRefCountedObject.h:63
agora
Definition: AgoraAtomicOps.h:21
agora::RefCounter::HasOneRef
bool HasOneRef() const
Definition: AgoraRefCountedObject.h:46
agora::RefCounter::DecRef
agora::RefCountReleaseStatus DecRef()
Definition: AgoraRefCountedObject.h:32
OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER
#define OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER
Definition: AgoraRefCountedObject.h:17
agora::AtomicOps::AcquireLoad
static int AcquireLoad(volatile const int *i)
Definition: AgoraAtomicOps.h:52
agora::AtomicOps::Decrement
static int Decrement(volatile int *i)
Definition: AgoraAtomicOps.h:51
agora::RefCounter::IncRef
void IncRef()
Definition: AgoraRefCountedObject.h:26
agora::RefCountedObject::RefCountedObject
RefCountedObject()
Definition: AgoraRefCountedObject.h:65
agora::RefCountedObject::Release
virtual agora::RefCountReleaseStatus Release() const
Definition: AgoraRefCountedObject.h:85
agora::AtomicOps::Increment
static int Increment(volatile int *i)
Definition: AgoraAtomicOps.h:50
agora::RefCountedObject::ref_count_
agora::RefCounter ref_count_
Definition: AgoraRefCountedObject.h:111
AgoraRefPtr.h
agora::RefCountedObject::RefCountedObject
RefCountedObject(const RefCountedObject &)
agora::RefCounter::RefCounter
RefCounter(int ref_count)
Definition: AgoraRefCountedObject.h:24
agora::RefCountedObject::AddRef
virtual void AddRef() const
Definition: AgoraRefCountedObject.h:83
agora::RefCountedObject::operator=
RefCountedObject & operator=(const RefCountedObject &)
agora::RefCounter::ref_count_
volatile int ref_count_
Definition: AgoraRefCountedObject.h:52
agora::RefCountReleaseStatus
OPTIONAL_ENUM_CLASS RefCountReleaseStatus
Definition: AgoraRefPtr.h:25
agora::RefCounter
Definition: AgoraRefCountedObject.h:22
AgoraAtomicOps.h
agora::RefCounter::RefCounter
RefCounter()
agora::RefCountedObject::RefCountedObject
RefCountedObject(const P0 &p0)
Definition: AgoraRefCountedObject.h:71