Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
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
18
19namespace roc {
20namespace 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.
38template <class T, class AllocationPolicy>
39class RefCounted : public NonCopyable<RefCounted<T, AllocationPolicy> >,
40 protected AllocationPolicy {
41public:
42 //! Initialize.
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
74private:
75 RefCountedImpl impl_;
76};
77
78} // namespace core
79} // namespace roc
80
81#endif // ROC_CORE_REF_COUNTED_H_
Allocation policies.
Base class for non-copyable objects.
Definition noncopyable.h:23
Implementation class for reference counter.
int incref() const
Increment reference counter.
int decref() const
Decrement reference counter.
int getref() const
Get reference counter.
Base class for object with reference counter.
Definition ref_counted.h:40
int getref() const
Get reference counter.
Definition ref_counted.h:53
void incref() const
Increment reference counter.
Definition ref_counted.h:58
RefCounted(const AllocationPolicy &policy)
Initialize.
Definition ref_counted.h:48
RefCounted()
Initialize.
Definition ref_counted.h:43
void decref() const
Decrement reference counter.
Definition ref_counted.h:65
Shared ownership intrusive pointer.
Definition shared_ptr.h:32
Root namespace.
Non-copyable object.
Implementation class for reference counter.