use the same callback runner for all callback types
This commit is contained in:
parent
5ad7b5505e
commit
e7297c42ff
|
@ -123,7 +123,7 @@ void Universe::setData(const std::vector<uint8_t>& data)
|
||||||
null_start_mutex.unlock(); // free the lock
|
null_start_mutex.unlock(); // free the lock
|
||||||
|
|
||||||
rx_timeout_(true); // update rx times
|
rx_timeout_(true); // update rx times
|
||||||
doDataCallbacks(); // notify callbacks
|
do_callbacks_(cb_dataChange); // run callbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ void Universe::setStatus(uint8_t val)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
status_ = val;
|
status_ = val;
|
||||||
doStatusCallbacks();
|
do_callbacks_(cb_statusChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -198,26 +198,6 @@ std::shared_ptr<void> Universe::onStatusChange(const std::function<void(Universe
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Universe::setValue
|
* @brief Universe::setValue
|
||||||
* @param address
|
* @param address
|
||||||
|
@ -259,7 +239,7 @@ void Universe::setValue(const uint16_t start, const uint16_t footprint,
|
||||||
|
|
||||||
setStatus(DMX_ACTIVE);
|
setStatus(DMX_ACTIVE);
|
||||||
last_updated_ = std::chrono::system_clock::now();
|
last_updated_ = std::chrono::system_clock::now();
|
||||||
doDataCallbacks();
|
do_callbacks_(cb_dataChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -268,10 +248,10 @@ void Universe::setValue(const uint16_t start, const uint16_t footprint,
|
||||||
* @param cb
|
* @param cb
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
std::shared_ptr<void> Universe::onDataChange(const DataHandler cb)
|
std::shared_ptr<void> Universe::onDataChange(const std::function<void(Universe*)> cb)
|
||||||
{
|
{
|
||||||
// wrap the callback with a shared pointer
|
// wrap the callback with a shared pointer
|
||||||
auto sp = std::make_shared<DataHandler>(std::move(cb));
|
auto sp = std::make_shared<std::function<void(Universe*)>>(std::move(cb));
|
||||||
// add callback to list (as a weak pointer)
|
// add callback to list (as a weak pointer)
|
||||||
cb_dataChange.push_back(sp);
|
cb_dataChange.push_back(sp);
|
||||||
// return token that caller must keep throughout it's scope
|
// return token that caller must keep throughout it's scope
|
||||||
|
@ -280,20 +260,21 @@ std::shared_ptr<void> Universe::onDataChange(const DataHandler cb)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Universe::doDataCallbacks
|
* @brief Universe::doCallbacks
|
||||||
|
* @param callbacks
|
||||||
*/
|
*/
|
||||||
void Universe::doDataCallbacks()
|
void Universe::do_callbacks_(std::vector<std::weak_ptr<const std::function<void(Universe*)>>> & callbacks)
|
||||||
{
|
{
|
||||||
for (auto it = cb_dataChange.begin(); it != cb_dataChange.end();)
|
for (auto it = callbacks.begin(); it != callbacks.end();)
|
||||||
{
|
{
|
||||||
if (auto sp = it->lock())
|
if (auto sp = it->lock())
|
||||||
{ // if the caller is still holding the token
|
{ // the owner is still holding the token
|
||||||
(*sp)(this);
|
(*sp)(this);
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ // or remove the callback
|
{ // the owner has released the token
|
||||||
it = cb_dataChange.erase(it);
|
it = callbacks.erase(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,9 +35,7 @@
|
||||||
|
|
||||||
namespace DMX {
|
namespace DMX {
|
||||||
|
|
||||||
class Universe; // forward declare the Univserse class
|
|
||||||
using DimmerData = std::array<uint8_t, E111_LAST_SLOT + 1>; //!< Array of 513 bytes for basic data.
|
using DimmerData = std::array<uint8_t, E111_LAST_SLOT + 1>; //!< Array of 513 bytes for basic data.
|
||||||
using DataHandler = std::function<void(Universe *)>; //!< Callback function for data handling.
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The Universe class
|
* @brief The Universe class
|
||||||
|
@ -60,10 +58,10 @@ class Universe {
|
||||||
const uint8_t* data);
|
const uint8_t* data);
|
||||||
|
|
||||||
void setData (const std::vector<uint8_t> &);
|
void setData (const std::vector<uint8_t> &);
|
||||||
std::shared_ptr<void> onDataChange (const DataHandler);
|
|
||||||
|
|
||||||
virtual void altSCdata(const std::vector<uint8_t> &);
|
virtual void altSCdata(const std::vector<uint8_t> &);
|
||||||
|
|
||||||
|
std::shared_ptr<void> onDataChange(const std::function<void(Universe*)>);
|
||||||
/**
|
/**
|
||||||
* @brief The Status enum
|
* @brief The Status enum
|
||||||
*/
|
*/
|
||||||
|
@ -78,22 +76,22 @@ class Universe {
|
||||||
virtual uint8_t status();
|
virtual uint8_t status();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void doDataCallbacks() {do_callbacks_(cb_dataChange);} //!< Run Data Change Callbacks
|
||||||
|
void doStatusCallbacks() {do_callbacks_(cb_statusChange);} //!< Run Status Change Callbacks
|
||||||
|
|
||||||
DimmerData null_start_data; //!< NULL Start Code data
|
DimmerData null_start_data; //!< NULL Start Code data
|
||||||
mutable std::mutex null_start_mutex; //!< memory protect Null Start data
|
mutable std::mutex null_start_mutex; //!< memory protect Null Start data
|
||||||
|
|
||||||
void doDataCallbacks(); //!< execute data changed callbacks
|
|
||||||
void doStatusCallbacks(); //!< execute status changed callbacks
|
|
||||||
|
|
||||||
std::chrono::system_clock::time_point last_updated_; //!< time of the latest update
|
std::chrono::system_clock::time_point last_updated_; //!< time of the latest update
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::queue<std::chrono::time_point<std::chrono::system_clock>> rx_times_;
|
std::queue<std::chrono::time_point<std::chrono::system_clock>> rx_times_;
|
||||||
|
|
||||||
void rx_timeout_(bool add_now = false);
|
void rx_timeout_(bool add_now = false);
|
||||||
|
void do_callbacks_(std::vector<std::weak_ptr<const std::function<void(Universe*)>>>&);
|
||||||
const int rx_timeout_period_;
|
const int rx_timeout_period_;
|
||||||
|
|
||||||
uint8_t status_; //!< the operating state of the universe
|
uint8_t status_; //!< the operating state of the universe
|
||||||
std::vector<std::weak_ptr<DataHandler>> cb_dataChange;
|
std::vector<std::weak_ptr<const std::function<void(Universe*)>>> cb_dataChange;
|
||||||
std::vector<std::weak_ptr<const std::function<void(Universe*)>>> cb_statusChange;
|
std::vector<std::weak_ptr<const std::function<void(Universe*)>>> cb_statusChange;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue