1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 **************************************************************************/
19 #ifndef REALM_IMPL_DESTROY_GUARD_HPP
20 #define REALM_IMPL_DESTROY_GUARD_HPP
22 #include <realm/util/features.h>
23 #include <realm/array.hpp>
29 /// Calls `ptr->destroy()` if the guarded pointer (`ptr`) is not null
30 /// when the guard is destroyed. For arrays (`T` = `Array`) this means
31 /// that the array is destroyed in a shallow fashion. See
32 /// `DeepArrayDestroyGuard` for an alternative.
36 DestroyGuard() noexcept;
38 DestroyGuard(T*) noexcept;
40 ~DestroyGuard() noexcept;
42 // Default implementations of copy/assign can trigger multiple destructions
43 DestroyGuard(const DestroyGuard&) = delete;
44 DestroyGuard& operator=(const DestroyGuard&) = delete;
46 void reset(T*) noexcept;
48 T* get() const noexcept;
50 T* release() noexcept;
56 using ShallowArrayDestroyGuard = DestroyGuard<Array>;
59 /// Calls `ptr->destroy_deep()` if the guarded Array pointer (`ptr`)
60 /// is not null when the guard is destroyed.
61 class DeepArrayDestroyGuard {
63 DeepArrayDestroyGuard() noexcept;
65 DeepArrayDestroyGuard(Array*) noexcept;
67 ~DeepArrayDestroyGuard() noexcept;
69 // Default implementations of copy/assign can trigger multiple destructions
70 DeepArrayDestroyGuard(const DeepArrayDestroyGuard&) = delete;
71 DeepArrayDestroyGuard& operator=(const DeepArrayDestroyGuard&) = delete;
73 void reset(Array*) noexcept;
75 Array* get() const noexcept;
77 Array* release() noexcept;
84 /// Calls `Array::destroy_deep(ref, alloc)` if the guarded 'ref'
85 /// (`ref`) is not zero when the guard is destroyed.
86 class DeepArrayRefDestroyGuard {
88 DeepArrayRefDestroyGuard(Allocator&) noexcept;
90 DeepArrayRefDestroyGuard(ref_type, Allocator&) noexcept;
92 ~DeepArrayRefDestroyGuard() noexcept;
94 // Default implementations of copy/assign can trigger multiple destructions
95 DeepArrayRefDestroyGuard(const DeepArrayRefDestroyGuard&) = delete;
96 DeepArrayRefDestroyGuard& operator=(const DeepArrayRefDestroyGuard&) = delete;
98 void reset(ref_type) noexcept;
100 ref_type get() const noexcept;
102 ref_type release() noexcept;
115 inline DestroyGuard<T>::DestroyGuard() noexcept
121 inline DestroyGuard<T>::DestroyGuard(T* ptr) noexcept
127 inline DestroyGuard<T>::~DestroyGuard() noexcept
134 inline void DestroyGuard<T>::reset(T* ptr) noexcept
142 inline T* DestroyGuard<T>::get() const noexcept
148 inline T* DestroyGuard<T>::release() noexcept
156 // DeepArrayDestroyGuard
158 inline DeepArrayDestroyGuard::DeepArrayDestroyGuard() noexcept
163 inline DeepArrayDestroyGuard::DeepArrayDestroyGuard(Array* ptr) noexcept
168 inline DeepArrayDestroyGuard::~DeepArrayDestroyGuard() noexcept
171 m_ptr->destroy_deep();
174 inline void DeepArrayDestroyGuard::reset(Array* ptr) noexcept
177 m_ptr->destroy_deep();
181 inline Array* DeepArrayDestroyGuard::get() const noexcept
186 inline Array* DeepArrayDestroyGuard::release() noexcept
194 // DeepArrayRefDestroyGuard
196 inline DeepArrayRefDestroyGuard::DeepArrayRefDestroyGuard(Allocator& alloc) noexcept
202 inline DeepArrayRefDestroyGuard::DeepArrayRefDestroyGuard(ref_type ref, Allocator& alloc) noexcept
208 inline DeepArrayRefDestroyGuard::~DeepArrayRefDestroyGuard() noexcept
211 Array::destroy_deep(m_ref, m_alloc);
214 inline void DeepArrayRefDestroyGuard::reset(ref_type ref) noexcept
217 Array::destroy_deep(m_ref, m_alloc);
221 inline ref_type DeepArrayRefDestroyGuard::get() const noexcept
226 inline ref_type DeepArrayRefDestroyGuard::release() noexcept
228 ref_type ref = m_ref;
237 #endif // REALM_IMPL_DESTROY_GUARD_HPP