Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
shared_ptr.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/shared_ptr.h
10//! @brief Shared ownership intrusive pointer.
11
12#ifndef ROC_CORE_SHARED_PTR_H_
13#define ROC_CORE_SHARED_PTR_H_
14
16#include "roc_core/panic.h"
17#include "roc_core/stddefs.h"
18
19namespace roc {
20namespace core {
21
22//! Shared ownership intrusive pointer.
23//!
24//! @tparam T defines pointee type. It may be const.
25//!
26//! @tparam OwnershipPolicy defines ownwership policy. It provides methods to
27//! increase and decrease the reference counter embedded into object.
28//!
29//! If RefCountedOwnership is used, T should inherit RefCounted. A template
30//! parameter of RefCounted defines its (de)allocation policy.
31template <class T, template <class TT> class OwnershipPolicy = RefCountedOwnership>
32class SharedPtr {
33public:
34 //! Create empty shared pointer.
35 //! @remarks
36 //! This overload is a bit faster than SharedPtr(NULL).
38 : ptr_(NULL) {
39 }
40
41 //! Create shared pointer from raw pointer.
42 //! @remarks
43 //! This overload hits SharedPtr(NULL) and SharedPtr(T).
44 SharedPtr(T* ptr)
45 : ptr_(ptr) {
46 acquire_();
47 }
48
49 //! Create shared pointer from shared pointer of the same type.
50 //! @remarks
51 //! This is a copy constructor.
52 SharedPtr(const SharedPtr& other)
53 : ptr_(other.ptr_) {
54 acquire_();
55 }
56
57 //! Create shared pointer from shared pointer of another type.
58 //! @remarks
59 //! - This overload hits SharedPtr(SharedPtr<ConvertibleToT>).
60 //! - This doesn't work as a copy constructor since it's template.
61 template <class TT>
63 : ptr_(other.get()) {
64 acquire_();
65 }
66
67 //! Destroy shared pointer.
69 release_();
70 }
71
72 //! Reset shared pointer and attach it to another object.
73 SharedPtr& operator=(const SharedPtr& other) {
74 reset(other.ptr_);
75 return *this;
76 }
77
78 //! Reset shared pointer and attach it to another object.
79 void reset(T* ptr = NULL) {
80 if (ptr != ptr_) {
81 release_();
82 ptr_ = ptr;
83 acquire_();
84 }
85 }
86
87 //! Get underlying pointer.
88 T* get() const {
89 return ptr_;
90 }
91
92 //! Get underlying pointer.
93 T* operator->() const {
94 if (ptr_ == NULL) {
95 roc_panic("shared ptr: attempting to dereference null shared pointer");
96 }
97 return ptr_;
98 }
99
100 //! Get underlying reference.
101 T& operator*() const {
102 if (ptr_ == NULL) {
103 roc_panic("shared ptr: attempting to dereference null shared pointer");
104 }
105 return *ptr_;
106 }
107
108 //! Convert to bool.
109 operator const struct unspecified_bool *() const {
110 return (const unspecified_bool*)ptr_;
111 }
112
113private:
114 void acquire_() {
115 if (ptr_ != NULL) {
116 OwnershipPolicy<T>::acquire(*ptr_);
117 }
118 }
119
120 void release_() {
121 if (ptr_ != NULL) {
122 OwnershipPolicy<T>::release(*ptr_);
123 }
124 }
125
126 T* ptr_;
127};
128
129//! Equality check.
130template <class T1, class T2>
131inline bool operator==(const SharedPtr<T1>& a, const SharedPtr<T2>& b) {
132 return (a.get() == b.get());
133}
134
135} // namespace core
136} // namespace roc
137
138#endif // ROC_CORE_SHARED_PTR_H_
Shared ownership intrusive pointer.
Definition shared_ptr.h:32
SharedPtr(const SharedPtr< TT, OwnershipPolicy > &other)
Create shared pointer from shared pointer of another type.
Definition shared_ptr.h:62
~SharedPtr()
Destroy shared pointer.
Definition shared_ptr.h:68
T * get() const
Get underlying pointer.
Definition shared_ptr.h:88
SharedPtr(const SharedPtr &other)
Create shared pointer from shared pointer of the same type.
Definition shared_ptr.h:52
void reset(T *ptr=NULL)
Reset shared pointer and attach it to another object.
Definition shared_ptr.h:79
SharedPtr & operator=(const SharedPtr &other)
Reset shared pointer and attach it to another object.
Definition shared_ptr.h:73
T * operator->() const
Get underlying pointer.
Definition shared_ptr.h:93
SharedPtr()
Create empty shared pointer.
Definition shared_ptr.h:37
SharedPtr(T *ptr)
Create shared pointer from raw pointer.
Definition shared_ptr.h:44
T & operator*() const
Get underlying reference.
Definition shared_ptr.h:101
bool operator==(const SharedPtr< T1 > &a, const SharedPtr< T2 > &b)
Equality check.
Definition shared_ptr.h:131
Root namespace.
Ownership policies.
Panic.
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition panic.h:50
Commonly used types and functions.