/* 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. */ #include "dmx.h" #include "patch.h" namespace DMX { /** * @brief Patch::Patch */ Patch::Patch() { } /** * @brief Add a Device to the patch. * @param dev */ void Patch::addDevice(const std::shared_ptr dev) { devices_.emplace(dev); } /** * @brief Remove a Device from the patch. * @param dev */ void Patch::remDevice(const std::shared_ptr 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 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> * Patch::devices() const { return &devices_; } /** * @brief Check the patch for device footprint conflicts * @return */ std::set> Patch::conflicts() { std::set> 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; } } // namespace DMX