iceoryx_hoofs  2.0.2
unique_ptr.hpp
1 // Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 
18 #ifndef IOX_HOOFS_CXX_UNIQUE_PTR_HPP
19 #define IOX_HOOFS_CXX_UNIQUE_PTR_HPP
20 
21 #include "iceoryx_hoofs/cxx/function_ref.hpp"
22 
23 namespace iox
24 {
25 namespace cxx
26 {
35 template <typename T>
37 {
38  public:
39  unique_ptr() = delete;
40 
44  unique_ptr(function_ref<void(T*)>&& deleter) noexcept;
45 
54  unique_ptr(T* const ptr, function_ref<void(T*)>&& deleter) noexcept;
55 
56  unique_ptr(const unique_ptr& other) = delete;
57  unique_ptr& operator=(const unique_ptr&) = delete;
58  unique_ptr(unique_ptr&& rhs) noexcept;
59  unique_ptr& operator=(unique_ptr&& rhs) noexcept;
60 
64  ~unique_ptr() noexcept;
65 
66 
67  unique_ptr<T>& operator=(std::nullptr_t) noexcept;
68 
73  T* operator->() noexcept;
74 
79  const T* operator->() const noexcept;
80 
84  explicit operator bool() const noexcept;
85 
91  T* get() noexcept;
92 
98  const T* get() const noexcept;
99 
104  T* release() noexcept;
105 
111  void reset(T* const ptr = nullptr) noexcept;
112 
117  void swap(unique_ptr& other) noexcept;
118 
119  private:
120  T* m_ptr = nullptr;
121  function_ref<void(T* const)> m_deleter;
122 };
123 
124 } // namespace cxx
125 } // namespace iox
126 
127 #include "iceoryx_hoofs/internal/cxx/unique_ptr.inl"
128 
129 #endif // IOX_HOOFS_CXX_UNIQUE_PTR_HPP
Definition: function_ref.hpp:34
The unique_ptr class is a heap-less unique ptr implementation, unlike the STL.
Definition: unique_ptr.hpp:37
unique_ptr(T *const ptr, function_ref< void(T *)> &&deleter) noexcept
unique_ptr Creates a unique pointer that takes ownership of an object.
unique_ptr(function_ref< void(T *)> &&deleter) noexcept
unique_ptr Creates an empty unique ptr that owns nothing. Can be passed ownership later via reset.
~unique_ptr() noexcept
T * get() noexcept
get Retrieve the underlying raw pointer.
void swap(unique_ptr &other) noexcept
swap Swaps object ownership with another unique_ptr (incl. deleters)
T * release() noexcept
release Release ownership of the underlying pointer.
void reset(T *const ptr=nullptr) noexcept
reset Reset the unique pointer to take ownership of the given pointer.
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29