From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
234 lines
5.5 KiB
C++
234 lines
5.5 KiB
C++
#include "attribute.hpp"
|
|
#include <algorithm>
|
|
#include <utility>
|
|
namespace attr {
|
|
Service::Service()
|
|
: attributes_(slot_count), next_token_(1), notifying_(false)
|
|
{
|
|
}
|
|
bool Service::slot_valid(int slot) const
|
|
{
|
|
return slot >= 0 && slot < slot_count;
|
|
}
|
|
int Service::readable(int slot) const
|
|
{
|
|
if (!slot_valid(slot)) {
|
|
return invalid;
|
|
}
|
|
if (!attributes_[slot].active) {
|
|
return missing;
|
|
}
|
|
return 0;
|
|
}
|
|
Service::Observer* Service::observer(int64_t token)
|
|
{
|
|
for (auto& entry : observers_) {
|
|
if (entry.active && entry.token == token) {
|
|
return &entry;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
void Service::compact()
|
|
{
|
|
if (notifying_) {
|
|
return;
|
|
}
|
|
auto first = std::remove_if(observers_.begin(), observers_.end(),
|
|
[](const Observer& item) {
|
|
return !item.active;
|
|
});
|
|
observers_.erase(first, observers_.end());
|
|
}
|
|
int Service::create(int slot, const std::string& name, int32_t minimum,
|
|
int32_t maximum, int32_t initial, bool writable)
|
|
{
|
|
if (!slot_valid(slot)) {
|
|
return invalid;
|
|
}
|
|
if (notifying_) {
|
|
return busy;
|
|
}
|
|
if (!valid_name(name)) {
|
|
return invalid;
|
|
}
|
|
if (minimum > maximum || initial < minimum || initial > maximum) {
|
|
return range;
|
|
}
|
|
if (attributes_[slot].active) {
|
|
return duplicate;
|
|
}
|
|
if (lookup(name) >= 0) {
|
|
return duplicate;
|
|
}
|
|
Attribute& item = attributes_[slot];
|
|
item.name = name;
|
|
item.minimum = minimum;
|
|
item.maximum = maximum;
|
|
item.value = initial;
|
|
item.epoch = 0;
|
|
++item.generation;
|
|
item.writable = writable;
|
|
item.active = true;
|
|
return 0;
|
|
}
|
|
int Service::drop(int slot)
|
|
{
|
|
int rc = readable(slot);
|
|
if (rc != 0) {
|
|
return rc;
|
|
}
|
|
if (notifying_) {
|
|
return busy;
|
|
}
|
|
Attribute& item = attributes_[slot];
|
|
for (auto& entry : observers_) {
|
|
if (entry.slot == slot && entry.generation == item.generation) {
|
|
entry.active = false;
|
|
}
|
|
}
|
|
item.active = false;
|
|
item.name.clear();
|
|
compact();
|
|
return 0;
|
|
}
|
|
int Service::lookup(const std::string& name) const
|
|
{
|
|
if (!valid_name(name)) {
|
|
return invalid;
|
|
}
|
|
for (int slot = 0; slot < slot_count; ++slot) {
|
|
if (attributes_[slot].active && attributes_[slot].name == name) {
|
|
return slot;
|
|
}
|
|
}
|
|
return missing;
|
|
}
|
|
int Service::snapshot(int slot, Snapshot& output) const
|
|
{
|
|
int rc = readable(slot);
|
|
if (rc != 0) {
|
|
return rc;
|
|
}
|
|
const Attribute& item = attributes_[slot];
|
|
Snapshot captured;
|
|
captured.value = item.value;
|
|
captured.epoch = item.epoch;
|
|
captured.generation = item.generation;
|
|
captured.minimum = item.minimum;
|
|
captured.maximum = item.maximum;
|
|
captured.writable = item.writable;
|
|
output = captured;
|
|
return 0;
|
|
}
|
|
int Service::show(int slot, char* output, size_t capacity) const
|
|
{
|
|
int rc = readable(slot);
|
|
if (rc != 0) {
|
|
return rc;
|
|
}
|
|
return format_value(attributes_[slot].value, output, capacity);
|
|
}
|
|
void Service::notify(int slot, uint64_t epoch)
|
|
{
|
|
|
|
const uint64_t generation = attributes_[slot].generation;
|
|
notifying_ = true;
|
|
for (size_t position = 0; position < observers_.size(); ++position) {
|
|
const Observer& current = observers_[position];
|
|
if (!current.active || current.slot != slot || current.generation != generation) {
|
|
continue;
|
|
}
|
|
Notice notice{slot, current.token, epoch, generation};
|
|
Callback callback = current.callback;
|
|
callback(notice);
|
|
}
|
|
notifying_ = false;
|
|
compact();
|
|
}
|
|
int Service::store(int slot, const std::string& bytes)
|
|
{
|
|
int rc = readable(slot);
|
|
if (rc != 0) {
|
|
return rc;
|
|
}
|
|
if (notifying_) {
|
|
return busy;
|
|
}
|
|
Attribute& item = attributes_[slot];
|
|
if (!item.writable) {
|
|
return denied;
|
|
}
|
|
int32_t proposed = 0;
|
|
rc = parse_value(bytes, proposed);
|
|
if (rc != 0) {
|
|
return rc;
|
|
}
|
|
if (proposed < item.minimum || proposed > item.maximum) {
|
|
return range;
|
|
}
|
|
if (item.value == proposed) {
|
|
return static_cast<int>(bytes.size());
|
|
}
|
|
item.value = proposed;
|
|
++item.epoch;
|
|
notify(slot, item.epoch);
|
|
return static_cast<int>(bytes.size());
|
|
}
|
|
int64_t Service::watch(int slot, Callback callback)
|
|
{
|
|
int rc = readable(slot);
|
|
if (rc != 0) {
|
|
return rc;
|
|
}
|
|
if (notifying_) {
|
|
return busy;
|
|
}
|
|
if (!callback) {
|
|
return invalid;
|
|
}
|
|
if (watcher_count() == watch_count) {
|
|
return full;
|
|
}
|
|
int64_t token = next_token_++;
|
|
Observer entry;
|
|
entry.slot = slot;
|
|
entry.token = token;
|
|
entry.generation = attributes_[slot].generation;
|
|
entry.callback = std::move(callback);
|
|
entry.active = true;
|
|
observers_.push_back(std::move(entry));
|
|
return token;
|
|
}
|
|
int Service::cancel(int64_t token)
|
|
{
|
|
Observer* entry = observer(token);
|
|
if (entry == nullptr) {
|
|
return missing;
|
|
}
|
|
entry->active = false;
|
|
compact();
|
|
return 0;
|
|
}
|
|
size_t Service::active_count() const
|
|
{
|
|
size_t count = 0;
|
|
for (const auto& item : attributes_) {
|
|
if (item.active) {
|
|
++count;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
size_t Service::watcher_count() const
|
|
{
|
|
size_t count = 0;
|
|
for (const auto& entry : observers_) {
|
|
if (entry.active) {
|
|
++count;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
}
|