diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-09-25 18:29:48 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-14 11:00:25 +0100 |
commit | b7dd0a6e19d5387bc8d499bd1b3a99d457fa58d9 (patch) | |
tree | 0d1a26964435e8f91b96713e2b5beb3c1e94d0b2 /signal-composer-binding/source.cpp | |
parent | db45a68715c07e6b8fe35e322ddcbbc6c473cb2b (diff) |
Enhance pointer manage and container browsing
- SourceAPI now holds share_ptr of Signal
- Signal are stored in a map<string,signal> that change
way to browse the signals
- subscribed property included in SourceAPI.
- Clean old code
- renaming struct SignalValue->signalValue
Change-Id: Ic5c28296ddd7197c6562e12fbc91c504c1cc0b4d
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'signal-composer-binding/source.cpp')
-rw-r--r-- | signal-composer-binding/source.cpp | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/signal-composer-binding/source.cpp b/signal-composer-binding/source.cpp index f7dee11..63709a1 100644 --- a/signal-composer-binding/source.cpp +++ b/signal-composer-binding/source.cpp @@ -30,9 +30,7 @@ int SourceAPI::init() if(init_) {return ActionExecOne(init_, nullptr);} else if(api_ == afbBindingV2.api) - { - api_ = Composer::instance().ctlConfig()->api; - } + {api_ = Composer::instance().ctlConfig()->api;} return 0; } @@ -44,29 +42,43 @@ std::string SourceAPI::api() const void SourceAPI::addSignal(const std::string& id, const std::string& event, std::vector<std::string>& depends, const std::string& sClass, const std::string& unit, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs) { - Signal* sig = new Signal(id, event, depends, unit, frequency, onReceived, getSignalsArgs); + std::shared_ptr<Signal> sig = std::make_shared<Signal>(id, event, depends, unit, frequency, onReceived, getSignalsArgs); - signalsMap_[sig] = false; + signalsMap_[id] = sig; } -std::vector<Signal*> SourceAPI::getSignals() const +std::vector<std::shared_ptr<Signal>> SourceAPI::getSignals() const { - std::vector<Signal*> signals; + std::vector<std::shared_ptr<Signal>> signals; for (auto& sig: signalsMap_) { - signals.push_back(sig.first); + signals.push_back(sig.second); } return signals; } -std::vector<Signal*> SourceAPI::searchSignals(const std::string& name) const +/// @brief Search a signal in a source instance. If an exact signal name is find +/// then it will be returned else it will be search against each signals +/// contained in the map and signal will be deeper evaluated. +/// +/// @param[in] name - A signal name to be searched +/// +/// @return Returns a vector of found signals. +std::vector<std::shared_ptr<Signal>> SourceAPI::searchSignals(const std::string& name) { - std::vector<Signal*> signals; - for (auto& sig: signalsMap_) + std::vector<std::shared_ptr<Signal>> signals; + + if(signalsMap_.count(name)) + {signals.emplace_back(signalsMap_[name]);} + else { - if(*sig.first == name) - {signals.emplace_back(sig.first);} + for (auto& sig: signalsMap_) + { + if(*sig.second == name) + {signals.emplace_back(sig.second);} + } } + return signals; } @@ -77,19 +89,19 @@ int SourceAPI::makeSubscription() { for(auto& sig: signalsMap_) { - json_object* signalJ = sig.first->toJSON(); + json_object* signalJ = sig.second->toJSON(); if(!signalJ) { - AFB_ERROR("Error building JSON query object to subscribe to for signal %s", sig.first->id().c_str()); + AFB_ERROR("Error building JSON query object to subscribe to for signal %s", sig.second->id().c_str()); err = -1; break; } - err += sig.second ? 0:ActionExecOne(getSignals_, signalJ); + err += ActionExecOne(getSignals_, signalJ); if(err) {AFB_WARNING("Fails to subscribe to signal '%s/%s'", - api_.c_str(), sig.first->id().c_str());} + api_.c_str(), sig.second->id().c_str());} else - {sig.second = true;} + {sig.second->subscribed_ = true;} } err += ActionExecOne(getSignals_, nullptr); } |