add copy-constructor version of Observer::Target::Notify

This commit is contained in:
falsycat 2023-08-01 21:12:42 +09:00
parent eafa05fbf5
commit 3089bccf55
2 changed files with 11 additions and 7 deletions

View File

@ -49,7 +49,7 @@ class Taker : public Dealer<T>, public Observer<T>::Target {
public: public:
explicit Taker(const DealerMeta& meta = {}) noexcept : Dealer<T>(meta) { } explicit Taker(const DealerMeta& meta = {}) noexcept : Dealer<T>(meta) { }
void Take(const T& v) noexcept { Notify(v); } void Take(const T& v) noexcept { Observer<T>::Target::Notify(v); }
}; };
} // namespace nf7 } // namespace nf7

View File

@ -48,6 +48,15 @@ class Observer<T>::Target {
Target& operator=(Target&&) = delete; Target& operator=(Target&&) = delete;
protected: protected:
void Notify(const T& v) noexcept {
assert(!calling_observer_ && "do not call Notify from observer callback");
calling_observer_ = true;
for (auto obs : obs_) {
obs->Notify(v);
}
calling_observer_ = false;
}
void Notify(T&& v) noexcept { void Notify(T&& v) noexcept {
assert(!calling_observer_ && "do not call Notify from observer callback"); assert(!calling_observer_ && "do not call Notify from observer callback");
@ -58,12 +67,7 @@ class Observer<T>::Target {
calling_observer_ = false; calling_observer_ = false;
return; return;
} }
Notify(v);
calling_observer_ = true;
for (auto obs : obs_) {
obs->Notify(v);
}
calling_observer_ = false;
} }
bool observed(const T* = nullptr) const noexcept { return !obs_.empty(); } bool observed(const T* = nullptr) const noexcept { return !obs_.empty(); }