From 1a832ce483ff8c1276bc3cd3d97e3caadb38c4df Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Mon, 25 Sep 2017 01:10:42 +0200 Subject: Implement recursionCheck using observer pattern Change-Id: Ic404c098f316106abe1918c3cd100ae047f4f555 Signed-off-by: Romain Forlot --- signal-composer-binding/signal.cpp | 99 +++++++++++--------------------------- 1 file changed, 27 insertions(+), 72 deletions(-) (limited to 'signal-composer-binding/signal.cpp') diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp index 2dcc552..a74889d 100644 --- a/signal-composer-binding/signal.cpp +++ b/signal-composer-binding/signal.cpp @@ -184,52 +184,6 @@ void Signal::attachToSourceSignals(Composer& composer) } } -/// @brief Call update() method on observer Signal with -/// current Signal timestamp and value -/*void Signal::notify() const -{ - for (int i = 0; i < Observers_.size(); ++i) - Observers_[i]->update(timestamp_, value_); -} - -/// @brief Inner recursion check. Argument is the Signal id coming -/// from the original Signal that made a recursion check. -/// -/// @param[in] origId - name of the origine of the recursion check -/// -/// @return 0 if no infinite loop detected, -1 if not. -int Signal::recursionCheck(const std::string& origId) const -{ - for (const auto& obs: observersList_) - { - if( id_ == obs->id()) - {return -1;} - if( origId == obs->id()) - {return -1;} - if(! obs->recursionCheck(origId)) - {return -1;} - } - return 0; -} - -/// @brief Recursion check to ensure that there is no infinite loop -/// in the Observers/Observables structure. -/// This will check that observer signal is not the same than itself -/// then trigger the check against the following eventuals observers -/// -/// @return 0 if no infinite loop detected, -1 if not. -int Signal::recursionCheck() const -{ - for (const auto& obs: observersList_) - { - if( id_ == obs->id()) - {return -1;} - if( obs->recursionCheck(id_)) - {return -1;} - } - return 0; -}*/ - /// @brief Make an average over the last X 'seconds' /// /// @param[in] seconds - period to calculate the average @@ -339,39 +293,40 @@ struct SignalValue Signal::last() const return history_.rbegin()->second; } -/* -template -Observable::~Observable() -{ - iterator_ itb = observerList_.begin(); - const_iterator_ ite = observerList_.end(); - - for(;itb!=ite;++itb) - { - (*itb)->delObservable(this); - } -} -template -Observer::~Observer() +/// @brief Recursion check to ensure that there is no infinite loop +/// in the Observers/Observables structure. +/// This will check that observer signal is not the same than itself +/// then trigger the check against the following eventuals observers +/// +/// @return 0 if no infinite loop detected, -1 if not. +int Signal::initialRecursionCheck() { - const_iterator_ ite = observableList_.end(); - - for(iterator_ itb=observableList_.begin();itb!=ite;++itb) + for (auto& obs: observerList_) { - (*itb)->delObserver(this); + if(obs == this) + {return -1;} + if(static_cast(obs)->recursionCheck(static_cast(obs))) + {return -1;} } + return 0; } -template -void Observable::notify() +/// @brief Inner recursion check. Argument is the Signal id coming +/// from the original Signal that made a recursion check. +/// +/// @param[in] origId - name of the origine of the recursion check +/// +/// @return 0 if no infinite loop detected, -1 if not. +int Signal::recursionCheck(Signal* parentSig) { - iterator_ itb=observerList_.begin(); - const_iterator_ ite=observerList_.end(); - - for(;itb!=ite;++itb) + for (const auto& obs: observerList_) { - (*itb)->update(static_cast(this)); + Signal* obsSig = static_cast(obs); + if(parentSig->id() == static_cast(obsSig)->id()) + {return -1;} + if(! obsSig->recursionCheck(obsSig)) + {return -1;} } + return 0; } -*/ -- cgit 1.2.3-korg