| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#ifndef SRC_COMMON_PIPELINE_STAGES_HPP_ |
| 2 |
|
|
#define SRC_COMMON_PIPELINE_STAGES_HPP_ |
| 3 |
|
|
|
| 4 |
|
|
#include <type_traits> |
| 5 |
|
|
|
| 6 |
|
|
namespace found { |
| 7 |
|
|
|
| 8 |
|
|
/// Type Alias Template for resolving any type parameter |
| 9 |
|
|
/// into its non-const non-lvalue |
| 10 |
|
|
template<typename T> |
| 11 |
|
|
using raw_type = std::remove_cv_t<std::remove_reference_t<T>>; |
| 12 |
|
|
|
| 13 |
|
|
/** |
| 14 |
|
|
* Action is an interface that |
| 15 |
|
|
* wraps a function that does something |
| 16 |
|
|
*/ |
| 17 |
|
|
class Action { |
| 18 |
|
|
public: |
| 19 |
|
540 |
virtual ~Action() = default; |
| 20 |
|
|
/** |
| 21 |
|
|
* Performs some action |
| 22 |
|
|
*/ |
| 23 |
|
|
virtual void DoAction() = 0; |
| 24 |
|
|
}; |
| 25 |
|
|
|
| 26 |
|
|
/** |
| 27 |
|
|
* Stage is an interface that |
| 28 |
|
|
* wraps an SISO function |
| 29 |
|
|
* |
| 30 |
|
|
* @param Input The input type |
| 31 |
|
|
* @param Output The output type |
| 32 |
|
|
* |
| 33 |
|
|
* @pre Input must be able to be default constructed |
| 34 |
|
|
*/ |
| 35 |
|
|
template<typename Input, typename Output> |
| 36 |
|
|
class Stage : public Action { |
| 37 |
|
|
public: |
| 38 |
|
|
/** |
| 39 |
|
|
* Runs this stage |
| 40 |
|
|
* |
| 41 |
|
|
* @param input The input to the stage |
| 42 |
|
|
* |
| 43 |
|
|
* @return The output of this stage |
| 44 |
|
|
*/ |
| 45 |
|
|
virtual Output Run(Input input) = 0; |
| 46 |
|
|
}; |
| 47 |
|
|
|
| 48 |
|
|
/** |
| 49 |
|
|
* A FunctionStage is a data structure that |
| 50 |
|
|
* wraps a function, and taking in |
| 51 |
|
|
* parameter Input and returning Output |
| 52 |
|
|
* |
| 53 |
|
|
* @param Input The parameter to accept to the Run function |
| 54 |
|
|
* @param Output The parameter to return from the Run function |
| 55 |
|
|
*/ |
| 56 |
|
|
template<typename Input, typename Output> |
| 57 |
|
|
class FunctionStage : public Stage<const raw_type<Input> &, raw_type<Output>> { |
| 58 |
|
|
public: |
| 59 |
|
|
/** |
| 60 |
|
|
* Canonical, non-reference input type for this stage. |
| 61 |
|
|
*/ |
| 62 |
|
|
using InputType = raw_type<Input>; |
| 63 |
|
|
/** |
| 64 |
|
|
* Canonical, non-reference output type for this stage. |
| 65 |
|
|
*/ |
| 66 |
|
|
using OutputType = raw_type<Output>; |
| 67 |
|
|
|
| 68 |
|
|
/** |
| 69 |
|
|
* Constructs a new Stage |
| 70 |
|
|
*/ |
| 71 |
|
468 |
FunctionStage() = default; |
| 72 |
|
|
|
| 73 |
|
|
/** |
| 74 |
|
|
* Destroys this |
| 75 |
|
|
*/ |
| 76 |
|
500 |
virtual ~FunctionStage() = default; |
| 77 |
|
|
|
| 78 |
|
|
/** |
| 79 |
|
|
* Executes Run (with a stored input and storing the output) |
| 80 |
|
|
* |
| 81 |
|
|
* @pre this->product points to a valid location |
| 82 |
|
|
*/ |
| 83 |
|
171 |
void DoAction() override { |
| 84 |
1/1
✓ Branch 2 taken 27 times.
|
171 |
*this->product = this->Run(this->resource); |
| 85 |
|
171 |
}; |
| 86 |
|
|
|
| 87 |
|
|
/** |
| 88 |
|
|
* Returns the stored input of this |
| 89 |
|
|
* |
| 90 |
|
|
* @return The stored input of this |
| 91 |
|
|
*/ |
| 92 |
|
224 |
Input &GetResource() {return resource;} |
| 93 |
|
|
|
| 94 |
|
|
/** |
| 95 |
|
|
* Returns the stored output of this |
| 96 |
|
|
* |
| 97 |
|
|
* @return The pointer to the stored output |
| 98 |
|
|
* of this |
| 99 |
|
|
*/ |
| 100 |
|
270 |
Output *&GetProduct() {return product;} |
| 101 |
|
|
|
| 102 |
|
|
protected: |
| 103 |
|
|
/// The stored input for this |
| 104 |
|
|
Input resource; |
| 105 |
|
|
/// The pointer to the stored output for this |
| 106 |
|
|
Output *product = nullptr; |
| 107 |
|
|
}; |
| 108 |
|
|
|
| 109 |
|
|
/** |
| 110 |
|
|
* ModifyingStage is a stage that modifies a resource |
| 111 |
|
|
* |
| 112 |
|
|
* @param T The type of resource being modified |
| 113 |
|
|
*/ |
| 114 |
|
|
template<typename T> |
| 115 |
|
|
class ModifyingStage : public Stage<raw_type<T> &, void> { |
| 116 |
|
|
public: |
| 117 |
|
|
/** |
| 118 |
|
|
* Constructs a new ModifyingStage |
| 119 |
|
|
*/ |
| 120 |
|
34 |
ModifyingStage() = default; |
| 121 |
|
|
|
| 122 |
|
|
/** |
| 123 |
|
|
* Destroys this |
| 124 |
|
|
*/ |
| 125 |
|
40 |
virtual ~ModifyingStage() = default; |
| 126 |
|
|
|
| 127 |
|
|
/** |
| 128 |
|
|
* Executes Run (with void return) |
| 129 |
|
|
*/ |
| 130 |
|
26 |
void DoAction() override { |
| 131 |
|
26 |
this->Run(*this->resource); |
| 132 |
|
26 |
}; |
| 133 |
|
|
|
| 134 |
|
|
/** |
| 135 |
|
|
* Sets the resource to modify |
| 136 |
|
|
* |
| 137 |
|
|
* @param resource The resource to modify |
| 138 |
|
|
* |
| 139 |
|
|
* @post When used in a ModifyingPipeline, |
| 140 |
|
|
* this will modify resource via Run(T &) |
| 141 |
|
|
*/ |
| 142 |
|
26 |
void SetResource(T &resource) {this->resource = &resource;} |
| 143 |
|
|
|
| 144 |
|
|
private: |
| 145 |
|
|
/// The pointer to the resource |
| 146 |
|
|
T *resource = nullptr; |
| 147 |
|
|
}; |
| 148 |
|
|
|
| 149 |
|
|
} // namespace found |
| 150 |
|
|
|
| 151 |
|
|
|
| 152 |
|
|
#endif // SRC_COMMON_PIPELINE_STAGES_HPP_ |
| 153 |
|
|
|