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
31 public:
32 explicit RefCounter(int ref_count) : ref_count_(ref_count) {}
33
34 void IncRef() { AtomicOps::Increment(&ref_count_); }
35
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>
71class RefCountedObject : public T {
72 public:
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
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:
116 RefCountedObject& operator=(const RefCountedObject&);
117
118 protected:
120};
121
122#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
123template <typename T, typename... types>
124inline 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>
132template <typename T, typename P0>
133inline agora_refptr<T> make_refptr(const P0& p0) {
134 return agora_refptr<T>(new RefCountedObject<T>(p0));
135}
136#endif
137} // namespace agora
static int Increment(volatile int *i)
Definition AgoraAtomicOps.h:27
static int AcquireLoad(volatile const int *i)
Definition AgoraAtomicOps.h:33
static int Decrement(volatile int *i)
Definition AgoraAtomicOps.h:30
Definition AgoraRefCountedObject.h:71
virtual agora::RefCountReleaseStatus Release() const
Definition AgoraRefCountedObject.h:93
RefCountedObject(P0 &&p0, P1 &&p1, Args &&... args)
Definition AgoraRefCountedObject.h:84
virtual bool HasOneRef() const
Definition AgoraRefCountedObject.h:109
agora::RefCounter ref_count_
Definition AgoraRefCountedObject.h:119
RefCountedObject(P0 &&p0)
Definition AgoraRefCountedObject.h:77
virtual ~RefCountedObject()
Definition AgoraRefCountedObject.h:112
virtual void AddRef() const
Definition AgoraRefCountedObject.h:91
RefCountedObject(const P0 &p0)
Definition AgoraRefCountedObject.h:79
RefCountedObject()
Definition AgoraRefCountedObject.h:73
Definition AgoraRefCountedObject.h:30
RefCounter(int ref_count)
Definition AgoraRefCountedObject.h:32
void IncRef()
Definition AgoraRefCountedObject.h:34
bool HasOneRef() const
Definition AgoraRefCountedObject.h:54
agora::RefCountReleaseStatus DecRef()
Definition AgoraRefCountedObject.h:40
Definition AgoraRefPtr.h:44
Definition AgoraAtomicOps.h:21
agora_refptr< T > make_refptr()
Definition AgoraRefCountedObject.h:129
OPTIONAL_ENUM_CLASS RefCountReleaseStatus
Definition AgoraRefPtr.h:25
Definition AgoraOptional.h:881