Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Toggle main menu visibility
Loading...
Searching...
No Matches
ref_counted.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2015 Roc Streaming authors
3
*
4
* This Source Code Form is subject to the terms of the Mozilla Public
5
* License, v. 2.0. If a copy of the MPL was not distributed with this
6
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
*/
8
9
//! @file roc_core/ref_counted.h
10
//! @brief Base class for object with reference counter.
11
12
#ifndef ROC_CORE_REF_COUNTED_H_
13
#define ROC_CORE_REF_COUNTED_H_
14
15
#include "
roc_core/allocation_policy.h
"
16
#include "
roc_core/noncopyable.h
"
17
#include "
roc_core/ref_counted_impl.h
"
18
19
namespace
roc
{
20
namespace
core
{
21
22
//! Base class for object with reference counter.
23
//!
24
//! Allows to increment and decrement reference counter. When the counter
25
//! reaches zero, the object is automatically destroyed.
26
//!
27
//! @tparam T defines the derived class.
28
//! @tparam AllocationPolicy defines destroy policy.
29
//!
30
//! When reference counter becomes zero, AllocationPolicy::destroy() is invoked
31
//! by RefCounted to destroy itself.
32
//!
33
//! Inherits AllocationPolicy to make its methods and state available in the
34
//! derived class. E.g., PoolAllocation policy holds a reference to the pool
35
//! and uses it to release the object.
36
//!
37
//! Thread-safe.
38
template
<
class
T,
class
AllocationPolicy>
39
class
RefCounted
:
public
NonCopyable<RefCounted<T, AllocationPolicy> >,
40
protected
AllocationPolicy {
41
public
:
42
//! Initialize.
43
RefCounted
()
44
: AllocationPolicy() {
45
}
46
47
//! Initialize.
48
explicit
RefCounted
(
const
AllocationPolicy& policy)
49
: AllocationPolicy(policy) {
50
}
51
52
//! Get reference counter.
53
int
getref
()
const
{
54
return
impl_.getref();
55
}
56
57
//! Increment reference counter.
58
void
incref
()
const
{
59
impl_.incref();
60
}
61
62
//! Decrement reference counter.
63
//! @remarks
64
//! Destroys itself if reference counter becomes zero.
65
void
decref
()
const
{
66
const
int
current_counter = impl_.decref();
67
68
if
(current_counter == 0) {
69
const_cast<
RefCounted
&
>
(*this).destroy(
70
static_cast<
T&
>
(
const_cast<
RefCounted
&
>
(*
this
)));
71
}
72
}
73
74
private
:
75
RefCountedImpl
impl_;
76
};
77
78
}
// namespace core
79
}
// namespace roc
80
81
#endif
// ROC_CORE_REF_COUNTED_H_
allocation_policy.h
Allocation policies.
roc::core::RefCountedImpl
Implementation class for reference counter.
Definition
ref_counted_impl.h:23
roc::core::RefCounted::getref
int getref() const
Get reference counter.
Definition
ref_counted.h:53
roc::core::RefCounted::incref
void incref() const
Increment reference counter.
Definition
ref_counted.h:58
roc::core::RefCounted::RefCounted
RefCounted(const AllocationPolicy &policy)
Initialize.
Definition
ref_counted.h:48
roc::core::RefCounted::RefCounted
RefCounted()
Initialize.
Definition
ref_counted.h:43
roc::core::RefCounted::decref
void decref() const
Decrement reference counter.
Definition
ref_counted.h:65
roc::core
General-purpose building blocks and platform abstraction layer.
roc
Root namespace.
noncopyable.h
Non-copyable object.
ref_counted_impl.h
Implementation class for reference counter.
roc_core
ref_counted.h
Generated by
1.17.0