1
0
Fork 0

add status change callbacks for listeners that want to be notified

This commit is contained in:
Kevin Matz 2022-12-02 13:06:22 -05:00
parent 2491054e1d
commit db7bf1dcb3
7 changed files with 62 additions and 5 deletions

View File

@ -250,6 +250,9 @@ void MultiverseModel::insert(const QModelIndex &parent,
connect(universe, &QSacnUniverse::dataChanged,
this, [refreshRow, index](){ refreshRow(index); });
connect(universe, &QSacnUniverse::statusChanged,
this, [refreshRow, index](){ refreshRow(index); });
connect(universe, &QSacnUniverse::sourceListChanged,
this, [this, universe, child, index, refreshRow]() {
// remove the old children
@ -273,6 +276,8 @@ void MultiverseModel::insert(const QModelIndex &parent,
auto universe = data.value<QSacnUniverse*>();
connect(universe, &QSacnUniverse::dataChanged,
this, [refreshRow, index](){ refreshRow(index); });
connect(universe, &QSacnUniverse::statusChanged,
this, [refreshRow, index](){ refreshRow(index); });
}
});
}

View File

@ -20,6 +20,10 @@ QSacnUniverse::QSacnUniverse(QObject *parent, std::shared_ptr<sACN::Universe> un
list_change_token = universe_->onSourceListChange([this]() {
syncSources();
});
status_change_token = universe_->onStatusChange([this]() {
emit statusChanged();
});
};

View File

@ -45,6 +45,7 @@ public slots:
signals:
void dataChanged();
void sourceListChanged();
void statusChanged();
private:
std::shared_ptr<sACN::Universe> universe_;
@ -52,5 +53,6 @@ private:
std::shared_ptr<void> data_change_token;
std::shared_ptr<void> list_change_token;
std::shared_ptr<void> status_change_token;
};
Q_DECLARE_METATYPE(QSacnUniverse*)

View File

@ -175,6 +175,43 @@ void Universe::setStatus(uint8_t val)
return;
status_ = val;
doStatusCallbacks();
}
/**
* @brief Universe::onStatusChange
* @param cb
* @return
*/
std::shared_ptr<void> Universe::onStatusChange(std::function<void()> cb)
{
// wrap the callback with a shared pointer
auto sp = std::make_shared<std::function<void()>>(std::move(cb));
// add callback to list (as a weak pointer)
cb_statusChange.push_back(sp);
// return token that caller must keep throughout it's scope
return sp;
}
/**
* @brief Universe::doStatusCallbacks
*/
void Universe::doStatusCallbacks()
{
for (auto it = cb_statusChange.begin(); it != cb_statusChange.end();)
{
if (auto sp = it->lock())
{ // if the caller is still holding the token
(*sp)();
++it;
}
else
{ // or remove the callback
it = cb_statusChange.erase(it);
}
}
}

View File

@ -75,13 +75,14 @@ class Universe {
};
virtual void setStatus(uint8_t);
virtual uint8_t status();
virtual std::shared_ptr<void> onStatusChange(std::function<void()>);
protected:
DimmerData null_start_data; //!< NULL Start Code data
mutable std::mutex null_start_mutex; //!< memory protect Null Start data
void doDataCallbacks(); //!< execute valid callbacks
void doDataCallbacks(); //!< execute data changed callbacks
void doStatusCallbacks(); //!< execute status changed callbacks
private:
std::chrono::system_clock::time_point last_updated_;
@ -91,6 +92,7 @@ class Universe {
const int rx_timeout_period_;
uint8_t status_; //!< the operating state of the universe
std::vector<std::weak_ptr<std::function<void()>>> cb_statusChange;
std::vector<std::weak_ptr<DataHandler>> cb_dataChange;
};

View File

@ -103,11 +103,16 @@ bool ArbitratingUniverse::hasSourceUniverse(const DATA::data_header& src) const
std::shared_ptr<Universe> ArbitratingUniverse::addNewSource(const DATA::data_header &src)
{
sources_.emplace(src, std::make_shared<Universe>());
auto token = sources_.at(src)->onData(std::bind(&sACN::ArbitratingUniverse::dataChangedNotifier,
this, std::placeholders::_1));
source_data_tokens.push_back(token);
auto data_token = sources_.at(src)->onData(std::bind(&sACN::ArbitratingUniverse::dataChangedNotifier,
this, std::placeholders::_1));
source_data_tokens.push_back(data_token);
doListChangeCallbacks();
auto status_token = sources_.at(src)->onStatusChange(std::bind(&sACN::ArbitratingUniverse::doStatusCallbacks,
this));
source_status_tokens.push_back(status_token);
doStatusCallbacks();
return sources_.at(src);
}

View File

@ -100,6 +100,8 @@ private:
std::vector<std::shared_ptr<void>> source_data_tokens; //!< source universe data change tokens
std::vector<std::weak_ptr<std::function<void()>>> cb_sourceListChange; //!< list of calback functions
std::vector<std::shared_ptr<void>> source_status_tokens; //!< source universe data change tokens
bool HoldLastLook;
std::shared_ptr<Universe> dominant_();