#include "input.hpp" #include #include namespace panel { bool valid_slot(int slot) { return slot >= 0 && slot < 4; } bool valid_name(const std::string& name) { if (name.empty() || name.size() > 32) { return false; } for (unsigned char item : name) { bool letter = (item >= 'a' && item <= 'z') || (item >= 'A' && item <= 'Z'); bool digit = item >= '0' && item <= '9'; if (!letter && !digit && item != '_' && item != '-') { return false; } } return true; } Service::Service() { reset(); } void Service::reset() { slots_ = {}; ledger_.clear(); counters_ = {}; } const std::array& Service::slots() const { return slots_; } const std::vector& Service::ledger() const { return ledger_; } const Counters& Service::counters() const { return counters_; } const Slot* Service::inspect(int slot) const { if (!valid_slot(slot)) { return nullptr; } return &slots_[static_cast(slot)]; } std::size_t Service::find_index(const Slot& slot, int type, int code) const { for (std::size_t index = 0; index < slot.capabilities.size(); ++index) { const Capability& item = slot.capabilities[index]; if (item.type == type && item.code == code) { return index; } } return slot.capabilities.size(); } const Capability* Service::capability(int slot, int type, int code) const { const Slot* item = inspect(slot); if (item == nullptr) { return nullptr; } std::size_t index = find_index(*item, type, code); if (index == item->capabilities.size()) { return nullptr; } return &item->capabilities[index]; } bool Service::event_required(const Capability& cap, int current, int incoming) const { if (cap.type == 1 && incoming == 2) { return false; } return false; } void Service::clear_live(Slot& slot) { slot.values.clear(); slot.pending.clear(); } int Service::allocate(int slot, const std::string& name) { if (!valid_slot(slot)) { return -1; } if (!valid_name(name)) { return -1; } Slot& target = slots_[static_cast(slot)]; if (target.phase != Phase::Empty) { return -2; } target.phase = Phase::Allocated; target.generation += 1; target.name = name; target.capabilities.clear(); clear_live(target); counters_.allocations += 1; return 0; } Reading Service::read(int slot, int type, int code) const { if (!valid_slot(slot)) { return {-1, 0}; } const Slot& target = slots_[static_cast(slot)]; if (target.phase != Phase::Registered) { return {-2, 0}; } std::size_t index = find_index(target, type, code); if (index == target.capabilities.size()) { return {-3, 0}; } return {0, target.values[index]}; } }