iceoryx_doc  1.0.1
unique_ptr.hpp
1 // Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 
17 #ifndef IOX_UTILS_CXX_UNIQUE_PTR_HPP
18 #define IOX_UTILS_CXX_UNIQUE_PTR_HPP
19 
20 #include "iceoryx_utils/cxx/function_ref.hpp"
21 
22 namespace iox
23 {
24 namespace cxx
25 {
34 template <typename T>
36 {
37  public:
38  unique_ptr() = delete;
39 
43  unique_ptr(function_ref<void(T*)>&& deleter) noexcept;
44 
53  unique_ptr(T* const ptr, function_ref<void(T*)>&& deleter) noexcept;
54 
55  unique_ptr(const unique_ptr& other) = delete;
56  unique_ptr& operator=(const unique_ptr&) = delete;
57  unique_ptr(unique_ptr&& rhs) noexcept;
58  unique_ptr& operator=(unique_ptr&& rhs) noexcept;
59 
63  ~unique_ptr() noexcept;
64 
65 
66  unique_ptr<T>& operator=(std::nullptr_t) noexcept;
67 
72  T* operator->() noexcept;
73 
78  const T* operator->() const noexcept;
79 
83  explicit operator bool() const noexcept;
84 
90  T* get() noexcept;
91 
97  const T* get() const noexcept;
98 
103  T* release() noexcept;
104 
110  void reset(T* const ptr = nullptr) noexcept;
111 
116  void swap(unique_ptr& other) noexcept;
117 
118  private:
119  T* m_ptr = nullptr;
120  function_ref<void(T* const)> m_deleter;
121 };
122 
123 } // namespace cxx
124 } // namespace iox
125 
126 #include "iceoryx_utils/internal/cxx/unique_ptr.inl"
127 
128 #endif // IOX_UTILS_CXX_UNIQUE_PTR_HPP
Definition: function_ref.hpp:32
The unique_ptr class is a heap-less unique ptr implementation, unlike the STL.
Definition: unique_ptr.hpp:36
~unique_ptr() noexcept
Definition: unique_ptr.inl:62
T * get() noexcept
get Retrieve the underlying raw pointer.
Definition: unique_ptr.inl:86
void swap(unique_ptr &other) noexcept
swap Swaps object ownership with another unique_ptr (incl. deleters)
Definition: unique_ptr.inl:116
void reset(T *const ptr=nullptr) noexcept
reset Reset the unique pointer to take ownership of the given pointer.
Definition: unique_ptr.inl:106
T * release() noexcept
release Release ownership of the underlying pointer.
Definition: unique_ptr.inl:98
T * operator->() noexcept
operator -> Transparent access to the managed object.
Definition: unique_ptr.inl:68
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28