Agora C++ API Reference for All Platforms

Loading...
Searching...
No Matches
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#ifndef __AGORA_REF_COUNTED_OBJECT_H__
11#define __AGORA_REF_COUNTED_OBJECT_H__
12#endif
13
14#if defined(__AGORA_REF_COUNTED_OBJECT_INTERNAL_H__)
15#error AgoraRefCountedObject is deprected now, its only purpose is for API compatiable.
16#endif
17
18#include "AgoraRefPtr.h"
19#include "AgoraAtomicOps.h"
20
21#ifndef OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER
22#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
23#define OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER agora::RefCountReleaseStatus::
24#else
25#define OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER
26#endif
27#endif
28namespace agora {
29
30 class RefCounter {
31 public:
32 explicit RefCounter(int ref_count) : ref_count_(ref_count) {}
33
34 void IncRef() { AtomicOps::Increment(&ref_count_); }
35
40 agora::RefCountReleaseStatus DecRef() {
41 return (AtomicOps::Decrement(&ref_count_) == 0
42 ? OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER kDroppedLastRef
43 : OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER kOtherRefsRemained);
44 }
45
54 bool HasOneRef() const { return (AtomicOps::AcquireLoad(&ref_count_) == 1); }
55
56 private:
57 RefCounter();
58
59 private:
60 volatile int ref_count_;
61};
62
69
70template <class T>
71 class RefCountedObject : public T {
72 public:
73 RefCountedObject(): ref_count_(0) {}
74
75 template <class P0>
76#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
77 explicit RefCountedObject(P0&& p0) : T(std::forward<P0>(p0)), ref_count_(0) {}
78#else
79 explicit RefCountedObject(const P0& p0) : T(p0), ref_count_(0) {}
80#endif
81
82#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
83 template <class P0, class P1, class... Args>
84 RefCountedObject(P0&& p0, P1&& p1, Args&&... args)
85 : T(std::forward<P0>(p0),
86 std::forward<P1>(p1),
87 std::forward<Args>(args)...),
88 ref_count_(0) {}
89#endif
90
91 virtual void AddRef() const { ref_count_.IncRef(); }
92
93 virtual agora::RefCountReleaseStatus Release() const {
94 const agora::RefCountReleaseStatus status = ref_count_.DecRef();
95 if (status == OPTIONAL_REFCOUNTRELEASESTATUS_SPECIFIER kDroppedLastRef) {
96 delete this;
97 }
98 return status;
99 }
100
109 virtual bool HasOneRef() const { return ref_count_.HasOneRef(); }
110
111 protected:
112 virtual ~RefCountedObject() {}
113
114 private:
115 RefCountedObject(const RefCountedObject&);
116 RefCountedObject& operator=(const RefCountedObject&);
117
118 protected:
119 mutable agora::RefCounter ref_count_;
120};
121
122#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
123template <typename T, typename... types>
124 inline agora_refptr<T> make_refptr(types&&... args) {
125 return agora_refptr<T>(new RefCountedObject<T>(std::forward<types>(args)...));
126}
127#else
128template <typename T>
129 inline agora_refptr<T> make_refptr() {
130 return agora_refptr<T>(new RefCountedObject<T>());
131}
132template <typename T, typename P0>
133 inline agora_refptr<T> make_refptr(const P0& p0) {
134 return agora_refptr<T>(new RefCountedObject<T>(p0));
135}
136#endif
137} // namespace agora
agora::AtomicOps::Increment
static int Increment(volatile int *i)
Definition AgoraAtomicOps.h:27
agora::AtomicOps::AcquireLoad
static int AcquireLoad(volatile const int *i)
Definition AgoraAtomicOps.h:33
agora::AtomicOps::Decrement
static int Decrement(volatile int *i)
Definition AgoraAtomicOps.h:30
agora::RefCountedObject
Definition AgoraRefCountedObject.h:71
agora::RefCountedObject::Release
virtual agora::RefCountReleaseStatus Release() const
Definition AgoraRefCountedObject.h:93
agora::RefCountedObject::RefCountedObject
RefCountedObject(P0 &&p0, P1 &&p1, Args &&... args)
Definition AgoraRefCountedObject.h:84
agora::RefCountedObject::HasOneRef
virtual bool HasOneRef() const
Definition AgoraRefCountedObject.h:109
agora::RefCountedObject< T, ExtProviderDeleter >::ref_count_
agora::RefCounter ref_count_
Definition AgoraRefCountedObject.h:119
agora::RefCountedObject::RefCountedObject
RefCountedObject(P0 &&p0)
Definition AgoraRefCountedObject.h:77
agora::RefCountedObject::~RefCountedObject
virtual ~RefCountedObject()
Definition AgoraRefCountedObject.h:112
agora::RefCountedObject::AddRef
virtual void AddRef() const
Definition AgoraRefCountedObject.h:91
agora::RefCountedObject::RefCountedObject
RefCountedObject(const P0 &p0)
Definition AgoraRefCountedObject.h:79
agora::RefCountedObject::RefCountedObject
RefCountedObject()
Definition AgoraRefCountedObject.h:73
agora::RefCounter
Definition AgoraRefCountedObject.h:30
agora::RefCounter::RefCounter
RefCounter(int ref_count)
Definition AgoraRefCountedObject.h:32
agora::RefCounter::IncRef
void IncRef()
Definition AgoraRefCountedObject.h:34
agora::RefCounter::HasOneRef
bool HasOneRef() const
Definition AgoraRefCountedObject.h:54
agora::RefCounter::DecRef
agora::RefCountReleaseStatus DecRef()
Definition AgoraRefCountedObject.h:40
agora::agora_refptr
Definition AgoraRefPtr.h:44
agora
Definition AgoraAtomicOps.h:21
agora::make_refptr
agora_refptr< T > make_refptr()
Definition AgoraRefCountedObject.h:129
agora::RefCountReleaseStatus
OPTIONAL_ENUM_CLASS RefCountReleaseStatus
Definition AgoraRefPtr.h:25
std
Definition AgoraOptional.h:881