Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
unique_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/unique_ptr.h
10 //! @brief Unique ownrship pointer.
11 
12 #ifndef ROC_CORE_UNIQUE_PTR_H_
13 #define ROC_CORE_UNIQUE_PTR_H_
14 
15 #include "roc_core/iallocator.h"
16 #include "roc_core/noncopyable.h"
17 #include "roc_core/panic.h"
18 #include "roc_core/stddefs.h"
19 
20 namespace roc {
21 namespace core {
22 
23 //! Unique ownrship pointer.
24 //!
25 //! @tparam T defines pointee type. It may be const.
26 //! @tparam Destroyer is used to destroy the object.
27 template <class T, class Destroyer = IAllocator> class UniquePtr : public NonCopyable<> {
28 public:
29  //! Initialize null pointer.
31  : ptr_(NULL)
32  , destroyer_() {
33  }
34 
35  //! Initialize from a raw pointer.
36  UniquePtr(T* ptr, Destroyer& destroyer)
37  : ptr_(ptr)
38  , destroyer_(&destroyer) {
39  }
40 
41  //! Destroy object.
43  destroy_();
44  }
45 
46  //! Reset pointer to null.
47  void reset() {
48  destroy_();
49  ptr_ = NULL;
50  destroyer_ = NULL;
51  }
52 
53  //! Reset pointer to a new value.
54  void reset(T* new_ptr, Destroyer& new_destroyer) {
55  if (new_ptr != ptr_) {
56  destroy_();
57  ptr_ = new_ptr;
58  destroyer_ = &new_destroyer;
59  }
60  }
61 
62  //! Get underlying pointer and pass ownership to the caller.
63  T* release() {
64  T* ret = ptr_;
65  if (ret == NULL) {
66  roc_panic("uniqueptr: attempting to release a null pointer");
67  }
68  ptr_ = NULL;
69  destroyer_ = NULL;
70  return ret;
71  }
72 
73  //! Get underlying pointer.
74  T* get() const {
75  return ptr_;
76  }
77 
78  //! Get underlying pointer.
79  T* operator->() const {
80  return ptr_;
81  }
82 
83  //! Get underlying reference.
84  T& operator*() const {
85  if (ptr_ == NULL) {
86  roc_panic("unique ptr: attempting to dereference a null pointer");
87  }
88  return *ptr_;
89  }
90 
91  //! Convert to bool.
92  operator const struct unspecified_bool*() const {
93  return (unspecified_bool*)ptr_;
94  }
95 
96 private:
97  void destroy_() {
98  if (ptr_) {
99  roc_panic_if(destroyer_ == NULL);
100  destroyer_->destroy(*ptr_);
101  }
102  }
103 
104  T* ptr_;
105  Destroyer* destroyer_;
106 };
107 
108 } // namespace core
109 } // namespace roc
110 
111 #endif // ROC_CORE_UNIQUE_PTR_H_
T * release()
Get underlying pointer and pass ownership to the caller.
Definition: unique_ptr.h:63
Unique ownrship pointer.
Definition: unique_ptr.h:27
#define roc_panic_if(x)
Panic if condition is true.
Definition: panic.h:26
T * operator->() const
Get underlying pointer.
Definition: unique_ptr.h:79
~UniquePtr()
Destroy object.
Definition: unique_ptr.h:42
UniquePtr(T *ptr, Destroyer &destroyer)
Initialize from a raw pointer.
Definition: unique_ptr.h:36
Root namespace.
UniquePtr()
Initialize null pointer.
Definition: unique_ptr.h:30
Commonly used types and functions.
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition: panic.h:42
Base class for non-copyable objects.
Definition: noncopyable.h:23
Panic function.
void reset()
Reset pointer to null.
Definition: unique_ptr.h:47
void reset(T *new_ptr, Destroyer &new_destroyer)
Reset pointer to a new value.
Definition: unique_ptr.h:54
Non-copyable object.
T & operator*() const
Get underlying reference.
Definition: unique_ptr.h:84
Memory allocator interface.