Skip to content

Commit f4ccc74

Browse files
committed
renamings and fixes
1 parent 81ef54e commit f4ccc74

File tree

9 files changed

+102
-56
lines changed

9 files changed

+102
-56
lines changed

examples/ex04_waypoints.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ static const char* xml_tree = R"(
106106
<root BTCPP_format="4" >
107107
<BehaviorTree ID="TreeA">
108108
<Sequence>
109-
<LoopPopDouble queue="1;2;3" value="{number}">
109+
<LoopDouble queue="1;2;3" value="{number}">
110110
<PrintNumber value="{number}" />
111-
</LoopPopDouble>
111+
</LoopDouble>
112112
113113
<GenerateWaypoints waypoints="{waypoints}" />
114-
<LoopPopPose queue="{waypoints}" value="{wp}">
114+
<LoopPose queue="{waypoints}" value="{wp}">
115115
<UseWaypoint waypoint="{wp}" />
116-
</LoopPopPose>
116+
</LoopPose>
117117
</Sequence>
118118
</BehaviorTree>
119119
</root>
@@ -124,8 +124,8 @@ static const char* xml_tree = R"(
124124
int main()
125125
{
126126
BehaviorTreeFactory factory;
127-
128-
factory.registerNodeType<LoopPopNode<Pose2D>>("LoopPopPose");
127+
128+
factory.registerNodeType<LoopNode<Pose2D>>("LoopPose");
129129

130130
factory.registerNodeType<UseWaypoint>("UseWaypoint");
131131
factory.registerNodeType<PrintNumber>("PrintNumber");

include/behaviortree_cpp/blackboard.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616

1717
namespace BT
1818
{
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>;
1922

20-
using AnyReadRef = LockedConstRef<Any>;
21-
using AnyWriteRef = LockedRef<Any>;
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>;
2226

2327
/**
2428
* @brief The Blackboard is the mechanism used by BehaviorTrees to exchange
@@ -61,7 +65,7 @@ class Blackboard
6165

6266
virtual ~Blackboard() = default;
6367

64-
const Entry* getEntry(const std::string& key) const
68+
[[nodiscard]] const Entry* getEntry(const std::string& key) const
6569
{
6670
std::unique_lock<std::mutex> lock(mutex_);
6771
// search first if this port was remapped
@@ -80,27 +84,27 @@ class Blackboard
8084
return (it == storage_.end()) ? nullptr : it->second.get();
8185
}
8286

83-
Entry* getEntry(const std::string& key)
87+
[[nodiscard]] Entry* getEntry(const std::string& key)
8488
{
8589
// "Avoid Duplication in const and Non-const Member Function,"
8690
// on p. 23, in Item 3 "Use const whenever possible," in Effective C++, 3d ed
8791
return const_cast<Entry*>( static_cast<const Blackboard &>(*this).getEntry(key));
8892
}
8993

90-
AnyReadRef getAnyRead(const std::string& key) const
94+
[[nodiscard]] AnyPtrReadLock getAnyRead(const std::string& key) const
9195
{
9296
if(auto entry = getEntry(key))
9397
{
94-
return AnyReadRef(&entry->value, const_cast<std::shared_mutex*>(&entry->entry_mutex));
98+
return AnyPtrReadLock(&entry->value, const_cast<std::shared_mutex*>(&entry->entry_mutex));
9599
}
96100
return {};
97101
}
98102

99-
AnyWriteRef getAnyWrite(const std::string& key)
103+
[[nodiscard]] AnyPtrWriteLock getAnyWrite(const std::string& key)
100104
{
101105
if(auto entry = getEntry(key))
102106
{
103-
return AnyWriteRef(&entry->value, &entry->entry_mutex);
107+
return AnyPtrWriteLock(&entry->value, &entry->entry_mutex);
104108
}
105109
return {};
106110
}
@@ -120,7 +124,7 @@ class Blackboard
120124
/** Return true if the entry with the given key was found.
121125
* Note that this method may throw an exception if the cast to T failed.
122126
*/
123-
template <typename T>
127+
template <typename T> [[nodiscard]]
124128
bool get(const std::string& key, T& value) const
125129
{
126130
if (auto any_ref = getAnyRead(key))
@@ -134,7 +138,7 @@ class Blackboard
134138
/**
135139
* Version of get() that throws if it fails.
136140
*/
137-
template <typename T>
141+
template <typename T> [[nodiscard]]
138142
T get(const std::string& key) const
139143
{
140144
if (auto any_ref = getAnyRead(key))
@@ -241,13 +245,13 @@ class Blackboard
241245

242246
void setPortInfo(const std::string &key, const PortInfo& info);
243247

244-
const PortInfo* portInfo(const std::string& key);
248+
[[nodiscard]] const PortInfo* portInfo(const std::string& key);
245249

246250
void addSubtreeRemapping(StringView internal, StringView external);
247251

248252
void debugMessage() const;
249253

250-
std::vector<StringView> getKeys(bool include_remapped = true) const;
254+
[[nodiscard]] std::vector<StringView> getKeys(bool include_remapped = true) const;
251255

252256
void clear()
253257
{

include/behaviortree_cpp/bt_factory.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Tree
141141
rootNode()->resetStatus();
142142
}
143143

144-
TreeNode* rootNode() const;
144+
[[nodiscard]] TreeNode* rootNode() const;
145145

146146
/// Sleep for a certain amount of time.
147147
/// This sleep could be interrupted by the method
@@ -167,22 +167,22 @@ class Tree
167167
NodeStatus
168168
tickWhileRunning(std::chrono::milliseconds sleep_time = std::chrono::milliseconds(10));
169169

170-
Blackboard::Ptr rootBlackboard();
170+
[[nodiscard]] Blackboard::Ptr rootBlackboard();
171171

172172
//Call the visitor for each node of the tree.
173173
void applyVisitor(const std::function<void(const TreeNode*)>& visitor);
174174

175175
//Call the visitor for each node of the tree.
176176
void applyVisitor(const std::function<void(TreeNode*)>& visitor);
177177

178-
uint16_t getUID();
178+
[[nodiscard]] uint16_t getUID();
179179

180180
/// Get a list of nodes which fullPath() match a wildcard filter and
181181
/// a given path. Example:
182182
///
183183
/// move_nodes = tree.getNodesByPath<MoveBaseNode>("move_*");
184184
///
185-
template <typename NodeType = BT::TreeNode>
185+
template <typename NodeType = BT::TreeNode> [[nodiscard]]
186186
std::vector<const TreeNode*> getNodesByPath(StringView wildcard_filter) {
187187
std::vector<const TreeNode*> nodes;
188188
for (auto const& subtree : subtrees) {

include/behaviortree_cpp/decorators/loop_node.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@ using SharedQueue = std::shared_ptr<std::deque<T>>;
3535
* the factory.
3636
*/
3737
template <typename T = Any>
38-
class LoopPopNode : public DecoratorNode
38+
class LoopNode : public DecoratorNode
3939
{
4040
bool child_running_ = false;
4141
SharedQueue<T> static_queue_;
4242
SharedQueue<T> current_queue_;
43-
std::string static_string_;
4443

4544
public:
46-
LoopPopNode(const std::string& name, const NodeConfig& config) :
45+
LoopNode(const std::string& name, const NodeConfig& config) :
4746
DecoratorNode(name, config)
4847
{
4948
auto raw_port = getRawPortValue("queue");
5049
if(!isBlackboardPointer(raw_port))
5150
{
52-
static_queue_ = convertFromString<SharedQueue<T>>(static_string_);
51+
static_queue_ = convertFromString<SharedQueue<T>>(raw_port);
5352
}
5453
}
5554

@@ -71,8 +70,8 @@ class LoopPopNode : public DecoratorNode
7170
{
7271
// if the port is static, any_ref is empty, otherwise it will keep access to
7372
// port locked for thread-safety
74-
AnyWriteRef any_ref = static_queue_ ?
75-
AnyWriteRef() :
73+
AnyPtrWriteLock any_ref = static_queue_ ?
74+
AnyPtrWriteLock() :
7675
getLockedPortContent("queue");
7776
if(any_ref)
7877
{

include/behaviortree_cpp/tree_node.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,16 @@ class TreeNode
139139

140140
void haltNode();
141141

142-
bool isHalted() const;
142+
[[nodiscard]] bool isHalted() const;
143143

144-
NodeStatus status() const;
144+
[[nodiscard]] NodeStatus status() const;
145145

146146
/// Name of the instance, not the type
147-
const std::string& name() const;
147+
[[nodiscard]] const std::string& name() const;
148148

149149
/// Blocking function that will sleep until the setStatus() is called with
150150
/// either RUNNING, FAILURE or SUCCESS.
151-
BT::NodeStatus waitValidStatus();
151+
[[nodiscard]] BT::NodeStatus waitValidStatus();
152152

153153
virtual NodeType type() const = 0;
154154

@@ -170,7 +170,7 @@ class TreeNode
170170
*
171171
* @return the subscriber handle.
172172
*/
173-
StatusChangeSubscriber subscribeToStatusChange(StatusChangeCallback callback);
173+
[[nodiscard]] StatusChangeSubscriber subscribeToStatusChange(StatusChangeCallback callback);
174174

175175
/** This method attaches to the TreeNode a callback with signature:
176176
*
@@ -195,18 +195,18 @@ class TreeNode
195195

196196
/// The unique identifier of this instance of treeNode.
197197
/// It is assigneld by the factory
198-
uint16_t UID() const;
198+
[[nodiscard]] uint16_t UID() const;
199199

200200
/// Human readable identifier, that includes the hierarchy of Subtrees
201201
/// See tutorial 10 as an example.
202-
const std::string& fullPath() const;
202+
[[nodiscard]] const std::string& fullPath() const;
203203

204204
/// registrationName is the ID used by BehaviorTreeFactory to create an instance.
205-
const std::string& registrationName() const;
205+
[[nodiscard]] const std::string& registrationName() const;
206206

207207
/// Configuration passed at construction time. Can never change after the
208208
/// creation of the TreeNode instance.
209-
const NodeConfig& config() const;
209+
[[nodiscard]] const NodeConfig& config() const;
210210

211211
/** Read an input port, which, in practice, is an entry in the blackboard.
212212
* If the blackboard contains a std::string and T is not a string,
@@ -223,7 +223,7 @@ class TreeNode
223223
*
224224
* @param key the name of the port.
225225
*/
226-
template <typename T>
226+
template <typename T> [[nodiscard]]
227227
Expected<T> getInput(const std::string& key) const
228228
{
229229
T out;
@@ -270,24 +270,28 @@ class TreeNode
270270
* @return empty AnyWriteRef if the blackboard entry doesn't exist or the content
271271
* of the port was a static string.
272272
*/
273-
AnyWriteRef getLockedPortContent(const std::string& key);
273+
[[nodiscard]] AnyPtrWriteLock getLockedPortContent(const std::string& key);
274274

275275
// function provided mostly for debugging purpose to see the raw value
276276
// in the port (no remapping and no conversion to a type)
277-
StringView getRawPortValue(const std::string& key) const;
277+
[[nodiscard]] StringView getRawPortValue(const std::string& key) const;
278278

279279
/// Check a string and return true if it matches either one of these
280280
/// two patterns: {...} or ${...}
281+
[[nodiscard]]
281282
static bool isBlackboardPointer(StringView str);
282283

284+
[[nodiscard]]
283285
static StringView stripBlackboardPointer(StringView str);
284286

287+
[[nodiscard]]
285288
static Expected<StringView> getRemappedKey(StringView port_name,
286289
StringView remapped_port);
287290

288291
/// Notify that the tree should be ticked again()
289292
void emitWakeUpSignal();
290293

294+
[[nodiscard]]
291295
bool requiresWakeUp() const;
292296

293297
/** Used to inject config into a node, even if it doesn't have the proper

include/behaviortree_cpp/utils/locked_reference.hpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66

77
namespace BT
88
{
9+
/**
10+
* @brief The LockedPtr class is used to share a pointer to an object
11+
* and a mutex that protects the write access to that object.
12+
*
13+
* As long as the object remains in scope, the mutex is locked
14+
*/
915
template <typename T>
10-
class LockedRef {
16+
class LockedPtr {
1117
public:
1218

13-
LockedRef() = default;
19+
LockedPtr() = default;
1420

15-
LockedRef(T* obj, std::shared_mutex* obj_mutex):
21+
LockedPtr(T* obj, std::shared_mutex* obj_mutex):
1622
ref_(obj), mutex_(obj_mutex) {
1723
mutex_->lock();
1824
}
1925

20-
~LockedRef() {
26+
~LockedPtr() {
2127
if(mutex_) {
2228
mutex_->unlock();
2329
}
@@ -56,22 +62,29 @@ class LockedRef {
5662
std::shared_mutex* mutex_ = nullptr;
5763
};
5864

59-
65+
/**
66+
* @brief The LockedPtrConst class is used to share a pointer to an object
67+
* and a mutex that protects the access to that object.
68+
* It has a read-only interface, the object can not be modified.
69+
* Multiple instances of LockedPtrConst can exist without a dead-lock.
70+
*
71+
* As long as the object remains in scope, the shared mutex is locked
72+
*/
6073
template <typename T>
61-
class LockedConstRef {
74+
class LockedPtrConst {
6275
public:
6376

64-
LockedConstRef() = default;
77+
LockedPtrConst() = default;
6578

66-
LockedConstRef(LockedConstRef const&) = delete;
67-
LockedConstRef& operator=(LockedConstRef const&) = delete;
79+
LockedPtrConst(LockedPtrConst const&) = delete;
80+
LockedPtrConst& operator=(LockedPtrConst const&) = delete;
6881

69-
LockedConstRef(const T* obj, std::shared_mutex* obj_mutex):
82+
LockedPtrConst(const T* obj, std::shared_mutex* obj_mutex):
7083
ref_(obj), mutex_(obj_mutex) {
7184
mutex_->lock_shared();
7285
}
7386

74-
~LockedConstRef() {
87+
~LockedPtrConst() {
7588
if(mutex_) {
7689
mutex_->unlock_shared();
7790
}
@@ -85,7 +98,7 @@ class LockedConstRef {
8598
return ref_ == nullptr;
8699
}
87100

88-
const T* get() const {
101+
const T * get() const {
89102
return ref_;
90103
}
91104

src/bt_factory.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ BehaviorTreeFactory::BehaviorTreeFactory()
7575
registerNodeType<SwitchNode<4>>("Switch4");
7676
registerNodeType<SwitchNode<5>>("Switch5");
7777
registerNodeType<SwitchNode<6>>("Switch6");
78-
79-
registerNodeType<LoopPopNode<double>>("LoopPopDouble");
80-
registerNodeType<LoopPopNode<std::string>>("LoopPopString");
78+
79+
registerNodeType<LoopNode<double>>("LoopDouble");
80+
registerNodeType<LoopNode<std::string>>("LoopString");
8181

8282
for (const auto& it : builders_)
8383
{

src/tree_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ std::string toStr<PostCond>(PostCond pre)
411411
}
412412
}
413413

414-
AnyWriteRef BT::TreeNode::getLockedPortContent(const std::string &key)
414+
AnyPtrWriteLock BT::TreeNode::getLockedPortContent(const std::string &key)
415415
{
416416
if(auto remapped_key = getRemappedKey(key, getRawPortValue(key)))
417417
{

0 commit comments

Comments
 (0)