iceoryx_doc  1.0.1
atomic_relocatable_pointer.hpp
1 // Copyright (c) 2019 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_UTILS_RELOCATABLE_POINTER_ATOMIC_RELOCATABLE_POINTER_HPP
19 #define IOX_UTILS_RELOCATABLE_POINTER_ATOMIC_RELOCATABLE_POINTER_HPP
20 
21 #include <atomic>
22 #include <limits>
23 
24 namespace iox
25 {
26 namespace rp
27 {
33 template <typename T>
35 {
36  public:
37  using offset_t = std::ptrdiff_t;
38  static constexpr offset_t NULL_POINTER_OFFSET = std::numeric_limits<offset_t>::max();
39 
42  AtomicRelocatablePointer(const T* ptr = nullptr) noexcept;
43 
46  AtomicRelocatablePointer& operator=(const AtomicRelocatablePointer& other) = delete;
48  AtomicRelocatablePointer& operator=(AtomicRelocatablePointer&& other) = delete;
49 
51 
55  AtomicRelocatablePointer& operator=(const T* ptr) noexcept;
56 
59  T* operator->() const noexcept;
60 
63  T& operator*() const noexcept;
64 
67  operator T*() const noexcept;
68 
69  private:
70  std::atomic<offset_t> m_offset{NULL_POINTER_OFFSET};
71 
72  inline T* computeRawPtr() const noexcept;
73 
74  inline offset_t computeOffset(const void* ptr) const noexcept;
75 };
76 } // namespace rp
77 } // namespace iox
78 
79 #include "iceoryx_utils/internal/relocatable_pointer/atomic_relocatable_pointer.inl"
80 
81 #endif // IOX_UTILS_RELOCATABLE_POINTER_ATOMIC_RELOCATABLE_POINTER_HPP
minimalistic relocatable pointer that can be written and read atomically and can be stored safely in ...
Definition: atomic_relocatable_pointer.hpp:35
T * operator->() const noexcept
access to the underlying object in shared memory
Definition: atomic_relocatable_pointer.inl:41
AtomicRelocatablePointer(const T *ptr=nullptr) noexcept
creates an AtomicRelocatablePointer pointing to the same pointee as ptr
Definition: atomic_relocatable_pointer.inl:28
AtomicRelocatablePointer(const AtomicRelocatablePointer &)=delete
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28