Files
aevolution/include/evo/processes/feeding.hpp
T

77 lines
2.6 KiB
C++

#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;
};
struct TerminalEvent {
std::uint32_t actor;
};
struct DecompositionConfig {
accounting::Inventory per_tick_limit{};
};
enum class FeedingError : std::uint8_t {
invalid_config,
wrong_tick,
invalid_batch,
invalid_actor,
inactive_actor,
not_corpse,
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, DecompositionConfig decomposition = {});
[[nodiscard]] std::expected<std::vector<FeedReceipt>, FeedingError>
advance(Tick target, std::span<const FeedIntent> inputs = {},
std::span<const TerminalEvent> terminal_events = {});
[[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;
[[nodiscard]] std::expected<accounting::Inventory, FeedingError>
corpse(std::uint32_t id) const noexcept;
[[nodiscard]] accounting::Inventory last_decomposed() const noexcept {
return last_decomposed_;
}
private:
FeedingCommunity(ProducerProcess producer, std::vector<ConsumerConfig> consumers,
DecompositionConfig decomposition);
[[nodiscard]] std::expected<FeedReceipt, FeedingError> feed(const FeedIntent& input);
[[nodiscard]] std::expected<void, FeedingError> decompose();
ProducerProcess producer_;
std::vector<ConsumerConfig> consumers_;
std::vector<std::uint8_t> active_;
DecompositionConfig decomposition_;
accounting::Inventory last_decomposed_{};
};
} // namespace evo::processes