Porytiles
Loading...
Searching...
No Matches
operation.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <expected>
4#include <vector>
5
11
12namespace porytiles2 {
13
22class Operation {
23 public:
24 virtual ~Operation() = default;
25
31 [[nodiscard]] virtual std::vector<OperandDeclaration> declare_inputs() const = 0;
32
38 [[nodiscard]] virtual std::vector<OperandDeclaration> declare_outputs() const = 0;
39
50 [[nodiscard]] virtual ChainableResult<OperandBundle> apply(const OperandBundle &inputs)
51 {
52 const auto declared_inputs = declare_inputs();
53 if (!inputs.satisfies_declarations(declared_inputs)) {
54 panic(fmt::format("op '{}' declared inputs were not satisfied", name()));
55 }
56 return execute(inputs);
57 }
58
64 [[nodiscard]] const std::string &name() const
65 {
66 return name_;
67 }
68
74 void set_name(const std::string &name)
75 {
76 name_ = name;
77 }
78
79 protected:
90 [[nodiscard]] virtual ChainableResult<OperandBundle> execute(const OperandBundle &inputs) = 0;
91
92 private:
93 std::string name_;
94};
95
96} // namespace porytiles2
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A type-erased container for orchestration operands with runtime type checking.
bool satisfies_declarations(const std::vector< OperandDeclaration > &declarations) const
Validates that the bundle satisfies a set of operand declarations.
Abstract base class for operations in a processing pipeline.
Definition operation.hpp:22
virtual ~Operation()=default
virtual std::vector< OperandDeclaration > declare_outputs() const =0
Declares the output operands produced by this operation.
virtual ChainableResult< OperandBundle > apply(const OperandBundle &inputs)
Applies this operation to the given input operands.
Definition operation.hpp:50
virtual ChainableResult< OperandBundle > execute(const OperandBundle &inputs)=0
Executes the operation's processing logic.
void set_name(const std::string &name)
Sets the name of this operation.
Definition operation.hpp:74
const std::string & name() const
Gets the name of this operation.
Definition operation.hpp:64
virtual std::vector< OperandDeclaration > declare_inputs() const =0
Declares the input operands required by this operation.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.hpp:53