iceoryx_doc  1.0.1
shared_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 #ifndef IOX_POSH_MEPOO_SHARED_POINTER_HPP
18 #define IOX_POSH_MEPOO_SHARED_POINTER_HPP
19 
20 #include "iceoryx_posh/internal/mepoo/shared_chunk.hpp"
21 #include "iceoryx_posh/mepoo/chunk_header.hpp"
22 #include "iceoryx_utils/design_pattern/creation.hpp"
23 
24 namespace iox
25 {
26 namespace mepoo
27 {
28 enum class SharedPointerError
29 {
30  INVALID_STATE,
31  SharedChunkIsEmpty
32 };
33 
45 template <typename T>
46 class SharedPointer : public DesignPattern::Creation<SharedPointer<T>, SharedPointerError>
47 {
48  public:
49  using CreationPattern_t = DesignPattern::Creation<SharedPointer<T>, SharedPointerError>;
50 
51  SharedPointer() = default;
52  SharedPointer(const SharedPointer&) = default;
53  SharedPointer(SharedPointer&&) = default;
54  ~SharedPointer() noexcept;
55 
56  SharedPointer& operator=(const SharedPointer&) noexcept;
57  SharedPointer& operator=(SharedPointer&&) noexcept;
58 
59  T* get() noexcept;
60  const T* get() const noexcept;
61 
62  T* operator->() noexcept;
63  const T* operator->() const noexcept;
64 
65  T& operator*() noexcept;
66  const T& operator*() const noexcept;
67 
68  explicit operator bool() const noexcept;
69 
70  friend class DesignPattern::Creation<SharedPointer<T>, SharedPointerError>;
71 
72  private:
73  template <typename... Targs>
74  SharedPointer(const SharedChunk& chunk, Targs&&... args) noexcept;
75 
76  void deleteManagedObjectIfNecessary() noexcept;
77  SharedChunk m_chunk;
78 };
79 
80 } // namespace mepoo
81 } // namespace iox
82 
83 #include "iceoryx_posh/internal/mepoo/shared_pointer.inl"
84 
85 #endif // IOX_POSH_MEPOO_SHARED_POINTER_HPP
WARNING: SharedChunk is not thread safe! Don't share SharedChunk objects between threads!...
Definition: shared_chunk.hpp:35
DesignPattern::Creation offers us a create method which forwards the arguments to the constructor....
Definition: shared_pointer.hpp:47
Definition: service_description.hpp:29