Add shared feeding community with atomic resource transfers
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/processes/producer.hpp"
|
||||
|
||||
namespace evo::processes {
|
||||
struct ConsumerConfig {
|
||||
accounting::Inventory opening;
|
||||
accounting::Inventory intake_limit;
|
||||
std::uint16_t assimilation_per_thousand;
|
||||
};
|
||||
struct FeedIntent {
|
||||
std::uint64_t sequence;
|
||||
std::uint32_t consumer;
|
||||
std::uint32_t target;
|
||||
accounting::Inventory amount;
|
||||
};
|
||||
struct FeedReceipt {
|
||||
std::uint64_t sequence;
|
||||
accounting::Inventory assimilated;
|
||||
bool target_depleted;
|
||||
auto operator<=>(const FeedReceipt&) const = default;
|
||||
};
|
||||
enum class FeedingError : std::uint8_t {
|
||||
invalid_config,
|
||||
wrong_tick,
|
||||
invalid_batch,
|
||||
invalid_actor,
|
||||
inactive_actor,
|
||||
intake_limit,
|
||||
insufficient_inventory,
|
||||
accounting_failure
|
||||
};
|
||||
inline constexpr std::size_t max_consumers = 16;
|
||||
|
||||
class FeedingCommunity {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<FeedingCommunity, FeedingError>
|
||||
create(ProducerConfig producer, accounting::Picojoules source,
|
||||
std::array<accounting::Femtograms, 4> nutrients,
|
||||
std::span<const ConsumerConfig> consumers);
|
||||
[[nodiscard]] std::expected<std::vector<FeedReceipt>, FeedingError>
|
||||
advance(Tick target, std::span<const FeedIntent> inputs = {});
|
||||
[[nodiscard]] std::span<const accounting::Inventory> balances() const noexcept {
|
||||
return producer_.balances();
|
||||
}
|
||||
[[nodiscard]] ProducerProgress progress() const noexcept {
|
||||
return producer_.progress();
|
||||
}
|
||||
// Process IDs are one-based. Out-of-range is a query error, not an inactive actor.
|
||||
[[nodiscard]] std::expected<bool, FeedingError> active(std::uint32_t id) const noexcept;
|
||||
|
||||
private:
|
||||
FeedingCommunity(ProducerProcess producer, std::vector<ConsumerConfig> consumers);
|
||||
[[nodiscard]] std::expected<FeedReceipt, FeedingError> feed(const FeedIntent& input);
|
||||
ProducerProcess producer_;
|
||||
std::vector<ConsumerConfig> consumers_;
|
||||
std::vector<std::uint8_t> active_;
|
||||
};
|
||||
} // namespace evo::processes
|
||||
Reference in New Issue
Block a user