Skip to content

Commit ec17d66

Browse files
committed
Document runnable
1 parent ccb048d commit ec17d66

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

include/runnable.hpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,55 @@
11
#ifndef RUNNABLE_H
22
#define RUNNABLE_H
3+
#include <memory>
34
#include <string>
4-
#include <experimental/optional>
55

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+
*/
612
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.
818
bool status = true;
9-
std::experimental::optional<Child*> parent;
1019

1120
public:
1221
virtual ~Child(){};
1322

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+
}
1932

2033
bool get_status() { return this->status; }
2134
void failed() {
2235
this->status = false;
2336
// 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() + " " : "";
2542
}
2643
};
2744

45+
/**
46+
*
47+
*
48+
*/
2849
class Runnable : public Child {
2950
public:
3051
virtual ~Runnable() {}
3152
virtual bool run() = 0;
3253
};
3354

34-
std::string Child::padding() {
35-
if (has_parent()) {
36-
return get_parent()->padding() + " ";
37-
} else {
38-
return "";
39-
}
40-
}
41-
4255
#endif /* RUNNABLE_H */

0 commit comments

Comments
 (0)