external.hpp
Go to the documentation of this file.
1 // Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
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 #ifndef _FASTCDR_XCDR_EXTERNAL_HPP_
16 #define _FASTCDR_XCDR_EXTERNAL_HPP_
17 
18 #include <memory>
19 
20 #include "../exceptions/LockedExternalAccessException.hpp"
21 
22 namespace eprosima {
23 namespace fastcdr {
24 
28 template<class T>
29 class external
30 {
31 public:
32 
33  using type = T;
34 
36  external() = default;
37 
40  T* pointer,
41  bool locked = false) noexcept
42  : pointer_ {pointer}
43  , locked_ {locked}
44  {
45  }
46 
49  std::shared_ptr<T> pointer) noexcept
50  : pointer_ {pointer}
51  {
52  }
53 
56  const external<T>& other) noexcept
57  : locked_ {other.locked_}
58  {
59  if (locked_)
60  {
61  pointer_ = std::make_shared<T>(*other.pointer_);
62  }
63  else
64  {
65  pointer_ = other.pointer_;
66  }
67  }
68 
71  const external<T>& other)
72  {
73  if (locked_)
74  {
77  }
78 
79  if (!other.pointer_)
80  {
81  pointer_.reset();
82  }
83  else if (other.locked_)
84  {
85  if (!pointer_)
86  {
87  pointer_ = std::make_shared<T>(*other.pointer_);
88  }
89  else
90  {
91  *pointer_ = *other.pointer_;
92  }
93  }
94  else
95  {
96  pointer_ = other.pointer_;
97  }
98 
99  return *this;
100  }
101 
103  ~external() = default;
104 
106  T& operator *() noexcept
107  {
108  return *pointer_;
109  }
110 
112  const T& operator *() const noexcept
113  {
114  return *pointer_;
115  }
116 
118  T* get() noexcept
119  {
120  return pointer_.get();
121  }
122 
124  const T* get() const noexcept
125  {
126  return pointer_.get();
127  }
128 
130  std::shared_ptr<T> get_shared_ptr() noexcept
131  {
132  return pointer_;
133  }
134 
136  T* operator ->() noexcept
137  {
138  return pointer_.get();
139  }
140 
142  const T* operator ->() const noexcept
143  {
144  return pointer_.get();
145  }
146 
149  const external<T>& other) const
150  {
151  return pointer_.get() == other.pointer_.get();
152  }
153 
156  const external<T>& other) const
157  {
158  return !(*this == other);
159  }
160 
162  operator bool() const noexcept
163  {
164  return nullptr != pointer_.get();
165  }
166 
168  bool is_locked() const noexcept
169  {
170  return locked_;
171  }
172 
174  void lock() noexcept
175  {
176  locked_ = true;
177  }
178 
179 private:
180 
181  std::shared_ptr<T> pointer_;
182 
183  bool locked_ {false};
184 
185 };
186 
187 } // namespace fastcdr
188 } // namespace eprosima
189 
190 #endif //_FASTCDR_XCDR_EXTERNAL_HPP_
eprosima::fastcdr::exception::LockedExternalAccessException::LOCKED_EXTERNAL_ACCESS_MESSAGE_DEFAULT
static const Cdr_DllAPI char *const LOCKED_EXTERNAL_ACCESS_MESSAGE_DEFAULT
Default message used in the library.
Definition: LockedExternalAccessException.hpp:78
eprosima::fastcdr::external::get
T * get() noexcept
Get pointer.
Definition: external.hpp:118
eprosima::fastcdr::external::external
external(std::shared_ptr< T > pointer) noexcept
Constructor from a shared pointer.
Definition: external.hpp:48
eprosima::fastcdr::external::external
external(const external< T > &other) noexcept
Copy constructor.
Definition: external.hpp:55
eprosima::fastcdr::external::~external
~external()=default
Destructor.
eprosima::fastcdr::external::operator==
bool operator==(const external< T > &other) const
Compares they manage the same object or empty both.
Definition: external.hpp:148
eprosima::fastcdr::exception::LockedExternalAccessException
This class is thrown as an exception when accessing to set the value of a locked external.
Definition: LockedExternalAccessException.hpp:27
eprosima::fastcdr::external::operator!=
bool operator!=(const external< T > &other) const
Compares they don't manages the same object.
Definition: external.hpp:155
eprosima::fastcdr::external
This class template manages an external member, a member declared to be external to the storage of a ...
Definition: external.hpp:29
eprosima::fastcdr::external::type
T type
Definition: external.hpp:33
eprosima::fastcdr::external::operator=
external< T > & operator=(const external< T > &other)
Assignment.
Definition: external.hpp:70
eprosima::fastcdr::external::locked_
bool locked_
Definition: external.hpp:183
eprosima::fastcdr::external::lock
void lock() noexcept
Locks the managed object.
Definition: external.hpp:174
eprosima::fastcdr::external::operator*
T & operator*() noexcept
Dereference object.
Definition: external.hpp:106
eprosima::fastcdr::external::pointer_
std::shared_ptr< T > pointer_
Definition: external.hpp:181
presentation_type::pointer
@ pointer
eprosima::fastcdr::external::external
external(T *pointer, bool locked=false) noexcept
Constructor from a pointer.
Definition: external.hpp:39
eprosima::fastcdr::external::operator->
T * operator->() noexcept
Dereference object member.
Definition: external.hpp:136
eprosima
Definition: fixed_size_string.hpp:32
eprosima::fastcdr::external::is_locked
bool is_locked() const noexcept
Checks if locked.
Definition: external.hpp:168
eprosima::fastcdr::external::external
external()=default
Default constructor.
eprosima::fastcdr::external::get_shared_ptr
std::shared_ptr< T > get_shared_ptr() noexcept
Get shared pointer.
Definition: external.hpp:130
eprosima::fastcdr::external::get
const T * get() const noexcept
Get pointer.
Definition: external.hpp:124


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:44