|
1 | 1 | #ifndef RUNNABLE_H
|
2 | 2 | #define RUNNABLE_H
|
| 3 | +#include <memory> |
3 | 4 | #include <string>
|
4 |
| -#include <experimental/optional> |
5 | 5 |
|
| 6 | +/** |
| 7 | + * The base class for all objects that comprise some abstract structure |
| 8 | + * with a nesting concept. Used to propogate ('pass') failures from leaf |
| 9 | + * to root without exceptions (and/or code-jumping), thus allowing |
| 10 | + * execution to continue virtually uninterrupted. |
| 11 | + */ |
6 | 12 | class Child {
|
7 |
| - // represents whether the children were all healthy/successful. |
| 13 | + std::shared_ptr<Child> parent = nullptr; // The parent of this Child. |
| 14 | + |
| 15 | + // Represents whether the Child is healthy (has not failed). |
| 16 | + // A Child is healthy if and only if all of its children are healthy. |
| 17 | + // All instances of Child start out healthy. |
8 | 18 | bool status = true;
|
9 |
| - std::experimental::optional<Child*> parent; |
10 | 19 |
|
11 | 20 | public:
|
12 | 21 | virtual ~Child(){};
|
13 | 22 |
|
14 |
| - std::string padding(); |
15 |
| - |
16 |
| - bool has_parent() { return static_cast<bool>(parent); } |
17 |
| - Child* get_parent() { return parent.value(); } |
18 |
| - void set_parent(Child* parent) { this->parent = parent; } |
| 23 | + bool has_parent() { return (parent != nullptr); } |
| 24 | + std::shared_ptr<Child> get_parent() { return parent; } |
| 25 | + void set_parent(Child* parent) { |
| 26 | + this->parent = std::shared_ptr<Child>(parent); |
| 27 | + } |
| 28 | + void set_parent(std::shared_ptr<Child> parent) { this->parent = parent; } |
| 29 | + void set_parent(Child& parent) { |
| 30 | + this->parent = std::shared_ptr<Child>(&parent); |
| 31 | + } |
19 | 32 |
|
20 | 33 | bool get_status() { return this->status; }
|
21 | 34 | void failed() {
|
22 | 35 | this->status = false;
|
23 | 36 | // propogates the failure up the tree
|
24 |
| - if (parent) parent.value()->failed(); |
| 37 | + if (has_parent()) this->get_parent()->failed(); |
| 38 | + } |
| 39 | + |
| 40 | + std::string padding() { |
| 41 | + return has_parent() ? get_parent()->padding() + " " : ""; |
25 | 42 | }
|
26 | 43 | };
|
27 | 44 |
|
| 45 | +/** |
| 46 | + * |
| 47 | + * |
| 48 | + */ |
28 | 49 | class Runnable : public Child {
|
29 | 50 | public:
|
30 | 51 | virtual ~Runnable() {}
|
31 | 52 | virtual bool run() = 0;
|
32 | 53 | };
|
33 | 54 |
|
34 |
| -std::string Child::padding() { |
35 |
| - if (has_parent()) { |
36 |
| - return get_parent()->padding() + " "; |
37 |
| - } else { |
38 |
| - return ""; |
39 |
| - } |
40 |
| -} |
41 |
| - |
42 | 55 | #endif /* RUNNABLE_H */
|
0 commit comments