Agora C++ API Reference for All Platforms
AgoraRefCountedObject.h
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 
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:
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 
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:
108  RefCountedObject& operator=(const RefCountedObject&);
109 
110  protected:
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>
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::RefCountedObject::Release
virtual agora::RefCountReleaseStatus Release() const
Definition: AgoraRefCountedObject.h:85
agora::AtomicOps::AcquireLoad
static int AcquireLoad(volatile const int *i)
Definition: AgoraAtomicOps.h:33
agora::RefCountedObject::RefCountedObject
RefCountedObject(P0 &&p0, P1 &&p1, Args &&... args)
Definition: AgoraRefCountedObject.h:76
agora::agora_refptr
Definition: AgoraRefPtr.h:44
agora::AtomicOps::Decrement
static int Decrement(volatile int *i)
Definition: AgoraAtomicOps.h:30
agora::RefCountedObject::HasOneRef
virtual bool HasOneRef() const
Definition: AgoraRefCountedObject.h:101
agora::RefCountedObject::ref_count_
agora::RefCounter ref_count_
Definition: AgoraRefCountedObject.h:111
agora::RefCountedObject::RefCountedObject
RefCountedObject()
Definition: AgoraRefCountedObject.h:65
agora::RefCountedObject::AddRef
virtual void AddRef() const
Definition: AgoraRefCountedObject.h:83
agora::RefCountedObject
Definition: AgoraRefCountedObject.h:63
agora
Definition: AgoraAtomicOps.h:21
agora::AtomicOps::Increment
static int Increment(volatile int *i)
Definition: AgoraAtomicOps.h:27
agora::RefCountedObject::RefCountedObject
RefCountedObject(const P0 &p0)
Definition: AgoraRefCountedObject.h:71
agora::RefCountedObject::RefCountedObject
RefCountedObject(P0 &&p0)
Definition: AgoraRefCountedObject.h:69
agora::make_refptr
agora_refptr< T > make_refptr(types &&... args)
Definition: AgoraRefCountedObject.h:116
agora::RefCounter::IncRef
void IncRef()
Definition: AgoraRefCountedObject.h:26
agora::RefCounter::HasOneRef
bool HasOneRef() const
Definition: AgoraRefCountedObject.h:46
std
Definition: AgoraOptional.h:881
agora::RefCountedObject::~RefCountedObject
virtual ~RefCountedObject()
Definition: AgoraRefCountedObject.h:104
agora::RefCounter
Definition: AgoraRefCountedObject.h:22
agora::RefCountReleaseStatus
OPTIONAL_ENUM_CLASS RefCountReleaseStatus
Definition: AgoraRefPtr.h:25
agora::RefCounter::DecRef
agora::RefCountReleaseStatus DecRef()
Definition: AgoraRefCountedObject.h:32
agora::RefCounter::RefCounter
RefCounter(int ref_count)
Definition: AgoraRefCountedObject.h:24