FOUND
Loading...
Searching...
No Matches
pipeline.hpp
Go to the documentation of this file.
1#ifndef PIPELINE_H_
2#define PIPELINE_H_
3
4#include <vector>
5#include <functional>
6#include <stdexcept>
7#include <utility>
8
9namespace found {
10
15class Action {
16 public:
20 virtual void DoAction() = 0;
21};
22
31template<typename Input, typename Output>
32class Stage : public Action {
33 public:
37 Stage() = default;
38
42 virtual ~Stage() = default;
43
51 virtual Output Run(const Input &input) = 0;
52
56 void DoAction() override { // GCOVR_EXCL_START
57 *this->product = this->Run(this->resource);
58 }; // GCOVR_EXCL_STOP
59
65 Input &GetResource() {return resource;} // GCOVR_EXCL_LINE
66
73 Output *&GetProduct() {return product;} // GCOVR_EXCL_LINE
74
75 protected:
77 Input resource;
79 Output *product;
80};
81
98template<typename Input, typename Output>
99class Pipeline : public Stage<Input, Output> {
100 public:
106 explicit Pipeline(std::vector<std::reference_wrapper<Action>> &stages)
107 : stages(stages), firstResource(nullptr), ready(false) {}
108
112 Pipeline() : firstResource(nullptr), ready(false) {}
113
128 template<typename I, typename O> Pipeline &AddStage(Stage<I, O> &stage) {
129 // Check the input
130 if (this->ready) throw std::invalid_argument("Pipeline is already ready"); // GCOVR_EXCL_LINE
131 if (this->stages.empty()) {
132 if (!std::is_same<Input, I>::value) {
133 throw std::invalid_argument("The initial input type is not correct"); // GCOVR_EXCL_LINE
134 }
135 this->firstResource = reinterpret_cast<Input *>(&stage.GetResource());
136 } else {
137 // Chain here, and blindly trust the user
138 *this->lastProduct = static_cast<void *>(&stage.GetResource());
139 }
140 // Add to our list
141 this->stages.push_back(stage);
142 // Now, reset the lastProduct to be of this stage
143 this->lastProduct = reinterpret_cast<void **>(&stage.GetProduct());
144 // Return the pipeline for chaining
145 return *this;
146 }
147
160 template<typename I> Pipeline &Complete(Stage<I, Output> &stage) {
161 this->AddStage(stage);
162 this->ready = true;
163 stage.GetProduct() = &this->finalProduct;
164 return *this;
165 }
166
183 Output Run(const Input &input) override {
184 // MANUAL VERIFICATION: The below branch is fully tested via pipeline-test.cpp
185 if (!this->ready) // GCOVR_EXCL_LINE
186 throw std::runtime_error("This is an illegal action: the pipeline is not ready yet"); // GCOVR_EXCL_LINE
187 this->resource = input;
188 *this->firstResource = input;
189 for (Action &stage : this->stages) {
190 stage.DoAction();
191 }
192 this->product = &this->finalProduct;
193 return this->finalProduct;
194 }
195
196 private:
198 std::vector<std::reference_wrapper<Action>> stages;
204 void **lastProduct = nullptr;
206 bool ready;
207};
208
209} // namespace found
210
211#endif
Action is an interface that wraps a function that does something.
Definition pipeline.hpp:15
virtual void DoAction()=0
Performs some action.
Pipeline is composite Stage (i.e.
Definition pipeline.hpp:99
Output Run(const Input &input) override
Executes this Pipeline.
Definition pipeline.hpp:183
Input * firstResource
The pointer to the variable that will store the first input.
Definition pipeline.hpp:200
Pipeline & Complete(Stage< I, Output > &stage)
Adds a stage to the pipeline and marks it as the last stage, preventing further manipulation of the P...
Definition pipeline.hpp:160
bool ready
An indicator for if this Pipeline is ready.
Definition pipeline.hpp:206
void ** lastProduct
A temporary variable that always points to the last Stage's product field.
Definition pipeline.hpp:204
Pipeline & AddStage(Stage< I, O > &stage)
Adds a stage to this pipeline.
Definition pipeline.hpp:128
Pipeline(std::vector< std::reference_wrapper< Action > > &stages)
Constructs a Pipeline.
Definition pipeline.hpp:106
Pipeline()
Constructs an empty Pipeline.
Definition pipeline.hpp:112
std::vector< std::reference_wrapper< Action > > stages
The stages of this.
Definition pipeline.hpp:198
Output finalProduct
The final product (output)
Definition pipeline.hpp:202
A Stage is a data structure that wraps a function, and taking in parameter Input and returning Output...
Definition pipeline.hpp:32
Input & GetResource()
Returns the stored input of this.
Definition pipeline.hpp:65
Output * product
The pointer to the stored output for this.
Definition pipeline.hpp:79
Output *& GetProduct()
Returns the stored output of this.
Definition pipeline.hpp:73
void DoAction() override
Executes Run (with a stored input and storing the output)
Definition pipeline.hpp:56
virtual ~Stage()=default
Destroys this.
virtual Output Run(const Input &input)=0
Runs this stage.
Stage()=default
Constructs a new Stage.
Input resource
The stored input for this.
Definition pipeline.hpp:77
Definition calibrate.cpp:7