#include "counter.hpp" #include #include namespace resource { Counter::Counter() : owners_{}, finalized_{}, next_id_(1) {} Counter::~Counter() = default; bool Counter::slot_ok(std::size_t slot) { return slot < slot_count; } bool Counter::value_ok(int value) { return value_min <= value && value <= value_max; } Result Counter::ok() { return {Code::ok, false, 0}; } Result Counter::ok(int payload) { return {Code::ok, true, payload}; } Result Counter::fail(Code code) { return {code, false, 0}; } std::shared_ptr Counter::fresh(int value) { if (next_id_ > maximum_commands) return {}; auto pointer = std::shared_ptr(new Object{next_id_, value}, [this](Object* object) { finalized_.push_back(object->id); delete object; }); ++next_id_; return pointer; } Result Counter::create(std::size_t slot, int value) { if (!slot_ok(slot)) return fail(Code::bad_argument); if (!value_ok(value)) return fail(Code::range); if (owners_[slot]) return fail(Code::occupied); auto object = fresh(value); if (!object) return fail(Code::limit); owners_[slot] = std::move(object); return ok(); } Result Counter::retain(std::size_t source, std::size_t destination) { if (!slot_ok(source) || !slot_ok(destination)) return fail(Code::bad_argument); if (!owners_[source]) return fail(Code::empty); if (owners_[destination]) return fail(Code::occupied); owners_[destination] = owners_[source]; return ok(); } Result Counter::release(std::size_t slot) { if (!slot_ok(slot)) return fail(Code::bad_argument); if (!owners_[slot]) return fail(Code::empty); owners_[slot].reset(); return ok(); } Result Counter::move(std::size_t source, std::size_t destination) { if (!slot_ok(source) || !slot_ok(destination)) return fail(Code::bad_argument); if (!owners_[source]) return fail(Code::empty); if (source == destination) return ok(); owners_[destination] = std::move(owners_[source]); return ok(); } Result Counter::swap(std::size_t left, std::size_t right) { if (!slot_ok(left) || !slot_ok(right)) return fail(Code::bad_argument); owners_[left].swap(owners_[right]); return ok(); } Result Counter::set(std::size_t slot, int value) { if (!slot_ok(slot)) return fail(Code::bad_argument); if (!value_ok(value)) return fail(Code::range); if (!owners_[slot]) return fail(Code::empty); owners_[slot]->value = value; return ok(); } Result Counter::add(std::size_t slot, int delta) { if (!slot_ok(slot)) return fail(Code::bad_argument); if (!owners_[slot]) return fail(Code::empty); long long sum = static_cast(owners_[slot]->value) + delta; if (sum < value_min || sum > value_max) return fail(Code::range); owners_[slot]->value = static_cast(sum); return ok(); } Result Counter::read(std::size_t slot) const { if (!slot_ok(slot)) return fail(Code::bad_argument); if (!owners_[slot]) return fail(Code::empty); return ok(owners_[slot]->value); } Result Counter::count(std::size_t slot) const { if (!slot_ok(slot)) return fail(Code::bad_argument); if (!owners_[slot]) return fail(Code::empty); return ok(static_cast(owners_[slot].use_count())); } Result Counter::clone(std::size_t source, std::size_t destination) { if (!slot_ok(source) || !slot_ok(destination)) return fail(Code::bad_argument); if (!owners_[source]) return fail(Code::empty); if (owners_[destination]) return fail(Code::occupied); auto object = fresh(owners_[source]->value); if (!object) return fail(Code::limit); owners_[destination] = std::move(object); return ok(); } Result Counter::replace(std::size_t slot, int value) { if (!slot_ok(slot)) return fail(Code::bad_argument); if (!value_ok(value)) return fail(Code::range); auto object = fresh(value); if (!object) return fail(Code::limit); owners_[slot] = std::move(object); return ok(); } Result Counter::clear() { for (auto& owner : owners_) owner.reset(); return ok(); } Result Counter::snapshot_result() const { return ok(); } Snapshot Counter::snapshot() const { Snapshot state{}; state.present.fill(false); state.finalized = finalized_; std::map live; for (std::size_t slot = 0; slot < slot_count; ++slot) { const auto& owner = owners_[slot]; if (!owner) continue; state.present[slot] = true; state.slots[slot] = {owner->id, owner->value, static_cast(owner.use_count())}; live[owner->id] = true; } state.active = static_cast(live.size()); return state; } unsigned Counter::created_count() const { return next_id_ - 1; } bool Counter::invariant() const { return true; } }