66#include < stdint.h>
77#include < unordered_map>
88#include < mutex>
9- #include < shared_mutex>
109#include < sstream>
1110
1211#include " behaviortree_cpp/basic_types.h"
1615
1716namespace BT
1817{
19- // / This type contains pointer to Any.
20- // / Protected with a read-only lock as long as the object is in scope
21- using AnyPtrReadLock = LockedPtrConst<Any>;
2218
23- // / This type contains pointer to Any.
24- // / Protected with a read-write lock as long as the object is in scope
25- using AnyPtrWriteLock = LockedPtr<Any>;
19+ // / This type contains a pointer to Any, protected
20+ // / with a locked mutex as long as the object is in scope
21+ using AnyPtrLocked = LockedPtr<Any>;
2622
2723/* *
2824 * @brief The Blackboard is the mechanism used by BehaviorTrees to exchange
@@ -45,7 +41,7 @@ class Blackboard
4541 {
4642 Any value;
4743 PortInfo port_info;
48- std::shared_mutex entry_mutex;
44+ mutable std::mutex entry_mutex;
4945
5046 Entry (const PortInfo& info) : port_info(info)
5147 {}
@@ -91,43 +87,43 @@ class Blackboard
9187 return const_cast <Entry*>( static_cast <const Blackboard &>(*this ).getEntry (key));
9288 }
9389
94- [[nodiscard]] AnyPtrReadLock getAnyRead (const std::string& key) const
90+ [[nodiscard]] AnyPtrLocked getAnyLocked (const std::string& key)
9591 {
9692 if (auto entry = getEntry (key))
9793 {
98- return AnyPtrReadLock (&entry->value , const_cast <std::shared_mutex*>( &entry->entry_mutex ) );
94+ return AnyPtrLocked (&entry->value , &entry->entry_mutex );
9995 }
10096 return {};
10197 }
10298
103- [[nodiscard]] AnyPtrWriteLock getAnyWrite (const std::string& key)
99+ [[nodiscard]] AnyPtrLocked getAnyLocked (const std::string& key) const
104100 {
105101 if (auto entry = getEntry (key))
106102 {
107- return AnyPtrWriteLock (&entry->value , &entry->entry_mutex );
103+ return AnyPtrLocked (&entry->value , const_cast <std::mutex*>( &entry->entry_mutex ) );
108104 }
109105 return {};
110106 }
111107
112- [[deprecated(" Use getAnyRead instead" )]]
108+ [[deprecated(" Use getAnyLocked instead" )]]
113109 const Any* getAny (const std::string& key) const
114110 {
115- return getAnyRead (key).get ();
111+ return getAnyLocked (key).get ();
116112 }
117113
118- [[deprecated(" Use getAnyWrite instead" )]]
114+ [[deprecated(" Use getAnyLocked instead" )]]
119115 Any* getAny (const std::string& key)
120116 {
121- return getAnyWrite ( key).get ();
117+ return const_cast <Any*>( getAnyLocked ( key).get () );
122118 }
123119
124120 /* * Return true if the entry with the given key was found.
125- * Note that this method may throw an exception if the cast to T failed.
126- */
121+ * Note that this method may throw an exception if the cast to T failed.
122+ */
127123 template <typename T> [[nodiscard]]
128124 bool get (const std::string& key, T& value) const
129125 {
130- if (auto any_ref = getAnyRead (key))
126+ if (auto any_ref = getAnyLocked (key))
131127 {
132128 value = any_ref.get ()->cast <T>();
133129 return true ;
@@ -136,12 +132,12 @@ class Blackboard
136132 }
137133
138134 /* *
139- * Version of get() that throws if it fails.
140- */
135+ * Version of get() that throws if it fails.
136+ */
141137 template <typename T> [[nodiscard]]
142138 T get (const std::string& key) const
143139 {
144- if (auto any_ref = getAnyRead (key))
140+ if (auto any_ref = getAnyLocked (key))
145141 {
146142 return any_ref.get ()->cast <T>();
147143 }
0 commit comments