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).
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.
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.
74 reset(other.ptr_);
75 return *this;
76 }
77
78 //! Reset shared pointer and attach it to another object.
79 void reset(const SharedPtr& other) {
80 reset(other.ptr_);
81 }
82
83 //! Reset shared pointer and attach it to another object.
84 void reset(T* ptr = NULL) {
85 if (ptr != ptr_) {
86 release_();
87 ptr_ = ptr;
88 acquire_();
89 }
90 }
91
92 //! Get underlying pointer.
93 T* get() const {
94 return ptr_;
95 }
96
97 //! Get underlying pointer.
98 T* operator->() const {
99 if (ptr_ == NULL) {
100 roc_panic("shared ptr: attempt to dereference null pointer");
101 }
102 return ptr_;
103 }
104
105 //! Get underlying reference.
106 T& operator*() const {
107 if (ptr_ == NULL) {
108 roc_panic("shared ptr: attempt to dereference null pointer");
109 }
110 return *ptr_;
111 }
112
113 //! Convert to bool.
114 operator const struct unspecified_bool *() const {
115 return (const unspecified_bool*)ptr_;
116 }
117
118private:
119 void acquire_() {
120 if (ptr_ != NULL) {
122 }
123 }
124
125 void release_() {
126 if (ptr_ != NULL) {
127 OwnershipPolicy<T>::release(*ptr_);
128 }
129 }
130
131 T* ptr_;
132};
133
134//! Equality check.
135template <class T1, class T2>
136inline bool operator==(const SharedPtr<T1>& a, const SharedPtr<T2>& b) {
137 return a.get() == b.get();
138}
139
140//! Equality check.
141template <class T1, class T2>
142inline bool operator!=(const SharedPtr<T1>& a, const SharedPtr<T2>& b) {
143 return a.get() != b.get();
144}
145
146} // namespace core
147} // namespace roc
148
149#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:93
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:84
void reset(const SharedPtr &other)
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:98
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:106
bool operator!=(const SharedPtr< T1 > &a, const SharedPtr< T2 > &b)
Equality check.
Definition shared_ptr.h:142
bool operator==(const SharedPtr< T1 > &a, const SharedPtr< T2 > &b)
Equality check.
Definition shared_ptr.h:136
Root namespace.
Ownership policies.
Panic.
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition panic.h:50
Commonly used types and functions.