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