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/iarena.h"
18#include "roc_core/optional.h"
19#include "roc_core/panic.h"
20#include "roc_core/stddefs.h"
21
22namespace roc {
23namespace core {
24
25//! Unique ownrship pointer.
26//!
27//! @tparam T defines pointee type. It may be const.
28//! @tparam AllocationPolicy defies (de)allocation policy.
29//!
30//! When ScopedPtr is destroyed or reset, it invokes AllocationPolicy::destroy()
31//! to destroy the owned object.
32template <class T, class AllocationPolicy = ArenaAllocation>
33class ScopedPtr : public NonCopyable<> {
34public:
35 //! Initialize null pointer.
37 : ptr_(NULL) {
38 }
39
40 //! Initialize from a raw pointer.
41 ScopedPtr(T* ptr, const AllocationPolicy& policy)
42 : ptr_(ptr) {
43 policy_.reset(new (policy_) AllocationPolicy(policy));
44 }
45
46 //! Destroy object.
48 reset();
49 }
50
51 //! Reset pointer to null.
52 void reset() {
53 if (ptr_ != NULL) {
54 policy_->destroy(*ptr_);
55 policy_.reset();
56 ptr_ = NULL;
57 }
58 }
59
60 //! Reset pointer to a new value.
61 void reset(T* new_ptr, const AllocationPolicy& new_policy) {
62 if (new_ptr != ptr_) {
63 reset();
64
65 ptr_ = new_ptr;
66 policy_.reset(new (policy_) AllocationPolicy(new_policy));
67 }
68 }
69
70 //! Get underlying pointer and pass ownership to the caller.
71 T* release() {
72 T* ret = ptr_;
73 if (ret == NULL) {
74 roc_panic("scoped ptr: attempting to release a null pointer");
75 }
76
77 ptr_ = NULL;
78 policy_.reset();
79
80 return ret;
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("scoped ptr: attempting to dereference a null pointer");
97 }
98 return *ptr_;
99 }
100
101 //! Convert to bool.
102 operator const struct unspecified_bool *() const {
103 return (unspecified_bool*)ptr_;
104 }
105
106private:
107 T* ptr_;
109};
110
111} // namespace core
112} // namespace roc
113
114#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:33
void reset(T *new_ptr, const AllocationPolicy &new_policy)
Reset pointer to a new value.
Definition scoped_ptr.h:61
T * operator->() const
Get underlying pointer.
Definition scoped_ptr.h:89
T * get() const
Get underlying pointer.
Definition scoped_ptr.h:84
T * release()
Get underlying pointer and pass ownership to the caller.
Definition scoped_ptr.h:71
void reset()
Reset pointer to null.
Definition scoped_ptr.h:52
T & operator*() const
Get underlying reference.
Definition scoped_ptr.h:94
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:36
Shared ownership intrusive pointer.
Definition shared_ptr.h:32
void reset(const SharedPtr &other)
Reset shared pointer and attach it to another object.
Definition shared_ptr.h:79
Memory arena interface.
Root namespace.
Non-copyable object.
Optionally constructed object.
Panic.
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition panic.h:50
Commonly used types and functions.