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