From dec9e42dd3a1a9da5eab30a0b4cc5674881cb7a3 Mon Sep 17 00:00:00 2001 From: hit9 Date: Sun, 5 May 2024 14:18:14 +0800 Subject: [PATCH] Update blinker.h --- example/onsignal/third_party/blinker.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/example/onsignal/third_party/blinker.h b/example/onsignal/third_party/blinker.h index 15ee331..21d7ead 100644 --- a/example/onsignal/third_party/blinker.h +++ b/example/onsignal/third_party/blinker.h @@ -64,7 +64,7 @@ // board.Flip(); // } -// Version: 0.1.5 +// Version: 0.1.7 #ifndef HIT9_BLINKER_H #define HIT9_BLINKER_H @@ -125,7 +125,7 @@ class SignalTrie { auto t = this; // t is the node walked through for (const auto& p : parts) { // Creates a node if not exist. - if (t->children.find(p) == t->children.end()) t->children[p] = new SignalTrie(); + if (auto [it, inserted] = t->children.try_emplace(p, nullptr); inserted) it->second = new SignalTrie(); // Mark this signal id to its signature. t->signature[id] = 1; t = t->children[p]; @@ -135,7 +135,7 @@ class SignalTrie { } // Match signals by given pattern, returns a signature of matched signal ids. Signature Match(std::string_view pattern) const { - Signature signature; + Signature sig; std::vector parts; split(pattern, parts, '.'); auto t = this; @@ -145,13 +145,13 @@ class SignalTrie { return t->signature; else { // match by exact name // match failure, returns empty signature - if (t->children.find(p) == t->children.end()) return signature; + if (t->children.find(p) == t->children.end()) return sig; t = t->children.at(p); } } // The last node, matches a single signal. - signature[t->id] = 1; - return signature; + sig[t->id] = 1; + return sig; } }; @@ -195,7 +195,7 @@ class IBoardPoller { class Signal { private: - std::string_view name; + std::string name; const SignalId id; // Reference to the board belongs to. IBoardEmitter* board; @@ -261,7 +261,8 @@ class Board : public IBoardPoller, public IBoardEmitter { } [[nodiscard]] std::unique_ptr> Connect(const std::vector& patterns) { Signature signature; - for (auto& pattern : patterns) signature |= tree.Match(pattern); + for (const auto& pattern : patterns) + signature |= tree.Match(pattern); // cppcheck-suppress useStlAlgorithm return std::make_unique>(signature, this); } // Emits a signal to backend buffer by signal id.