|
1 | 1 | #include "behaviortree_cpp/blackboard.h"
|
| 2 | +#include <unordered_set> |
2 | 3 | #include "behaviortree_cpp/json_export.h"
|
3 | 4 |
|
4 | 5 | namespace BT
|
@@ -166,15 +167,44 @@ void Blackboard::createEntry(const std::string& key, const TypeInfo& info)
|
166 | 167 |
|
167 | 168 | void Blackboard::cloneInto(Blackboard& dst) const
|
168 | 169 | {
|
169 |
| - std::unique_lock lk(dst.mutex_); |
170 |
| - dst.storage_.clear(); |
| 170 | + std::unique_lock lk1(mutex_); |
| 171 | + std::unique_lock lk2(dst.mutex_); |
171 | 172 |
|
172 |
| - for(const auto& [key, entry] : storage_) |
| 173 | + // keys that are not updated must be removed. |
| 174 | + std::unordered_set<std::string> keys_to_remove; |
| 175 | + auto& dst_storage = dst.storage_; |
| 176 | + for(const auto& [key, _] : dst_storage) |
| 177 | + { |
| 178 | + keys_to_remove.insert(key); |
| 179 | + } |
| 180 | + |
| 181 | + // update or create entries in dst_storage |
| 182 | + for(const auto& [src_key, src_entry] : storage_) |
| 183 | + { |
| 184 | + keys_to_remove.erase(src_key); |
| 185 | + |
| 186 | + auto it = dst_storage.find(src_key); |
| 187 | + if(it != dst_storage.end()) |
| 188 | + { |
| 189 | + // overwite |
| 190 | + auto& dst_entry = it->second; |
| 191 | + dst_entry->string_converter = src_entry->string_converter; |
| 192 | + dst_entry->value = src_entry->value; |
| 193 | + dst_entry->info = src_entry->info; |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + // create new |
| 198 | + auto new_entry = std::make_shared<Entry>(src_entry->info); |
| 199 | + new_entry->value = src_entry->value; |
| 200 | + new_entry->string_converter = src_entry->string_converter; |
| 201 | + dst_storage.insert({ src_key, new_entry }); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + for(const auto& key : keys_to_remove) |
173 | 206 | {
|
174 |
| - auto new_entry = std::make_shared<Entry>(entry->info); |
175 |
| - new_entry->value = entry->value; |
176 |
| - new_entry->string_converter = entry->string_converter; |
177 |
| - dst.storage_.insert({ key, new_entry }); |
| 207 | + dst_storage.erase(key); |
178 | 208 | }
|
179 | 209 | }
|
180 | 210 |
|
|
0 commit comments