1#ifndef SRC_COMMON_PIPELINE_PIPELINES_HPP_
2#define SRC_COMMON_PIPELINE_PIPELINES_HPP_
11#include "common/pipeline/stages.hpp"
14#define DEFAULT_NUM_STAGES 10
28template<
typename Input,
30 size_t N = DEFAULT_NUM_STAGES>
63 virtual Output
Run(
const Input &input) = 0;
89 assert(this->size < N);
90 if (this->ready)
throw std::invalid_argument(
"Pipeline is already ready");
91 this->stages[
size++] = std::move(stage);
109 assert(this->size < N);
110 if (this->ready)
throw std::invalid_argument(
"Pipeline is already ready");
111 this->stages[
size++] = std::move(stage);
121 for (
size_t i = 0; i < this->
size; i++) {
122 this->stages[i]->DoAction();
147template<
typename Input,
typename Output,
size_t N = DEFAULT_NUM_STAGES>
173 if (this->
size == 0) {
174 if (!std::is_same<Input, I>::value) {
175 throw std::invalid_argument(
"The initial input type is not correct");
205 assert(this->
size < N);
206 if (this->
ready)
throw std::invalid_argument(
"Pipeline is already ready");
228 Output
Run(
const Input &input)
override {
232 throw std::runtime_error(
"This is an illegal action: the pipeline is not ready yet");
237 if (this->
product ==
nullptr) {
263template<
typename T,
size_t N = DEFAULT_NUM_STAGES>
279 assert(this->
size < N - 1);
295 assert(this->
size < N);
308 T
Run(
const T &input)
override {
311 throw std::runtime_error(
"This is an illegal action: the pipeline is not ready yet");
314 if (this->
product ==
nullptr) {
320 for (
size_t i = 0; i < this->
size; i++) {
A FunctionStage is a data structure that wraps a function, and taking in parameter Input and returnin...
Definition stages.hpp:57
Output * product
The pointer to the stored output for this.
Definition stages.hpp:97
Input & GetResource()
Returns the stored input of this.
Definition stages.hpp:83
Output *& GetProduct()
Returns the stored output of this.
Definition stages.hpp:91
Input resource
The stored input for this.
Definition stages.hpp:95
A ModifyingPipeline modifies a resource with a given set of stages.
Definition pipelines.hpp:264
ModifyingPipeline & Complete(std::unique_ptr< ModifyingStage< T > > stage)
Completes a pipeline with a stage.
Definition pipelines.hpp:294
ModifyingPipeline()=default
Constructs a ModifyingPipeline.
T Run(const T &input) override
Executes this Modifying Pipeline.
Definition pipelines.hpp:308
ModifyingPipeline & AddStage(std::unique_ptr< ModifyingStage< T > > stage)
Adds a stage to this.
Definition pipelines.hpp:278
ModifyingStage is a stage that modifies a resource.
Definition stages.hpp:106
A Pipeline<Input, Output, N> is an abstract Pipeline that takes an Input, outputs an Output,...
Definition pipelines.hpp:31
void AddStageHelper(std::unique_ptr< Action > &&stage)
Adds a stage to this pipeline, taking ownership of the provided stage.
Definition pipelines.hpp:88
void CompleteHelper(std::unique_ptr< Action > &&stage)
Completes a pipeline with a final stage, taking ownership of it.
Definition pipelines.hpp:108
void DoActionHelper()
Runs the entire pipeline.
Definition pipelines.hpp:120
std::optional< Output > finalProduct
The final product. This is only sometimes used.
Definition pipelines.hpp:73
size_t size
The number of stages.
Definition pipelines.hpp:69
std::unique_ptr< Action > stages[N]
Ownership storage for the stages.
Definition pipelines.hpp:67
virtual Output Run(const Input &input)=0
Runs this Pipeline.
void DoAction() override
Runs a Pipeline.
Definition pipelines.hpp:43
bool ready
Whether we're complete.
Definition pipelines.hpp:71
SequentialPipeline is composite Stage (i.e.
Definition pipelines.hpp:148
void ** lastProduct
A temporary variable that always points to the last Stage's product field.
Definition pipelines.hpp:253
SequentialPipeline & AddStage(std::unique_ptr< FunctionStage< I, O > > stage)
Adds a stage to this pipeline.
Definition pipelines.hpp:171
SequentialPipeline()=default
Constructs an empty SequentialPipeline.
SequentialPipeline & Complete(std::unique_ptr< FunctionStage< I, Output > > stage)
Adds a stage to the pipeline and marks it as the last stage, preventing further manipulation of the S...
Definition pipelines.hpp:204
Input * firstResource
The pointer to the variable that will store the first input.
Definition pipelines.hpp:251
Output Run(const Input &input) override
Executes this SequentialPipeline.
Definition pipelines.hpp:228