OpenLCP/protocol/esta/dmx/patch.cpp

153 lines
3.6 KiB
C++
Raw Normal View History

2021-08-25 17:20:33 -04:00
/*
dmx/patch.cpp
Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
2023-04-01 11:17:56 -04:00
#include "dmx.h"
#include "patch.h"
2021-08-25 17:20:33 -04:00
namespace DMX {
/**
* @brief Patch::Patch
*/
Patch::Patch()
{
}
2021-08-25 17:20:33 -04:00
2023-04-01 11:17:56 -04:00
/**
* @brief Add a Device to the patch.
* @param dev
*/
void Patch::addDevice(const std::shared_ptr<Device> dev)
{
devices_.emplace(dev);
}
/**
* @brief Remove a Device from the patch.
* @param dev
*/
void Patch::remDevice(const std::shared_ptr<Device> dev)
{
for (auto it = devices_.begin(); it != devices_.end();)
{
if (it->expired()) // purge stale devices
it = devices_.erase(it);
else if (it->lock() == dev)
it = devices_.erase(it);
else
++it;
}
}
/**
* @brief Check if a Device is in the patch.
* @param dev
* @return
*/
bool Patch::hasDevice(const std::shared_ptr<Device> dev)
{
for (auto it = devices_.begin(); it != devices_.end();)
{
if (it->expired()) // purge stale devices
it = devices_.erase(it);
else if (it->lock() == dev)
return true;
else
++it;
}
return false;
}
/**
* @brief Get the list of patched Devices.
* @return
*/
const std::set<std::weak_ptr<Device>> * Patch::devices() const
{
return &devices_;
}
/**
* @brief Check the patch for device footprint conflicts
* @return
*/
std::set<std::weak_ptr<Device>> Patch::conflicts()
{
std::set<std::weak_ptr<Device>> bad;
for (auto it = devices_.begin(); it != devices_.end();)
{
if (it->expired()) { // purge stale devices
it = devices_.erase(it);
continue;
}
auto dev = it->lock();
// footprint cannot overlow universe
if (dev->address() + dev->footprint() >= E111_LAST_SLOT) {
bad.emplace(dev);
++it;
continue;
}
// start address cannot overlap the prior device
if (it != devices_.begin()) {
auto prev = std::prev(it)->lock();
if (dev->address() < prev->address() + prev->footprint()) {
bad.emplace(dev);
++it;
continue;
}
}
// footprint cannot overlap the next device
auto nextItr = std::next(it);
while(true) {
if (nextItr == devices_.end()) // device is the last member of the patch
break;
if (nextItr->expired()) { // next device is stale, purge it
nextItr = devices_.erase(nextItr);
continue;
}
auto next = nextItr->lock();
if (dev->address() + dev->footprint() >= next->address())
bad.emplace(dev);
break;
}
++it;
}
return bad;
}
2021-08-25 17:20:33 -04:00
} // namespace DMX