locked_reference.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mutex>
5 
6 namespace BT
7 {
15 template <typename T>
16 class LockedPtr
17 {
18 public:
19  LockedPtr() = default;
20 
21  LockedPtr(T* obj, std::mutex* obj_mutex) : ref_(obj), mutex_(obj_mutex)
22  {
23  mutex_->lock();
24  }
25 
27  {
28  if(mutex_)
29  {
30  mutex_->unlock();
31  }
32  }
33 
34  LockedPtr(LockedPtr const&) = delete;
35  LockedPtr& operator=(LockedPtr const&) = delete;
36 
38  {
39  std::swap(ref_, other.ref_);
40  std::swap(mutex_, other.mutex_);
41  }
42 
44  {
45  std::swap(ref_, other.ref_);
46  std::swap(mutex_, other.mutex_);
47  }
48 
49  operator bool() const
50  {
51  return ref_ != nullptr;
52  }
53 
54  void lock()
55  {
56  if(mutex_)
57  {
58  mutex_->lock();
59  }
60  }
61 
62  void unlock()
63  {
64  if(mutex_)
65  {
66  mutex_->unlock();
67  }
68  }
69 
70  const T* get() const
71  {
72  return ref_;
73  }
74 
75  const T* operator->() const
76  {
77  return ref_;
78  }
79 
81  {
82  return ref_;
83  }
84 
85  template <typename OtherT>
86  void assign(const OtherT& other)
87  {
88  if(ref_ == nullptr)
89  {
90  throw std::runtime_error("Empty LockedPtr reference");
91  }
92  else if constexpr(std::is_same_v<T, OtherT>)
93  {
94  *ref_ = other;
95  }
96  else if constexpr(std::is_same_v<BT::Any, OtherT>)
97  {
98  other->copyInto(*ref_);
99  }
100  else
101  {
102  *ref_ = T(other);
103  }
104  }
105 
106 private:
107  T* ref_ = nullptr;
108  std::mutex* mutex_ = nullptr;
109 };
110 
111 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::LockedPtr::LockedPtr
LockedPtr(T *obj, std::mutex *obj_mutex)
Definition: locked_reference.hpp:21
BT::LockedPtr::~LockedPtr
~LockedPtr()
Definition: locked_reference.hpp:26
BT::LockedPtr::assign
void assign(const OtherT &other)
Definition: locked_reference.hpp:86
BT::LockedPtr::operator->
const T * operator->() const
Definition: locked_reference.hpp:75
BT::LockedPtr::mutex_
std::mutex * mutex_
Definition: locked_reference.hpp:108
BT::LockedPtr::unlock
void unlock()
Definition: locked_reference.hpp:62
mutex
static pthread_mutex_t mutex
Definition: minitrace.cpp:77
BT::LockedPtr
The LockedPtr class is used to share a pointer to an object and a mutex that protects the read/write ...
Definition: locked_reference.hpp:16
BT::LockedPtr::operator=
LockedPtr & operator=(LockedPtr const &)=delete
BT::LockedPtr::LockedPtr
LockedPtr()=default
safe_any.hpp
BT::LockedPtr::LockedPtr
LockedPtr(LockedPtr &&other)
Definition: locked_reference.hpp:37
BT::LockedPtr::operator->
T * operator->()
Definition: locked_reference.hpp:80
BT::LockedPtr::lock
void lock()
Definition: locked_reference.hpp:54
BT::LockedPtr::ref_
T * ref_
Definition: locked_reference.hpp:107
std::swap
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.hpp:24537
BT::LockedPtr::get
const T * get() const
Definition: locked_reference.hpp:70
BT::LockedPtr::operator=
LockedPtr & operator=(LockedPtr &&other)
Definition: locked_reference.hpp:43


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07