summaryrefslogtreecommitdiff
path: root/common/external/nillable.h
diff options
context:
space:
mode:
authora1xd <[email protected]>2020-07-31 05:12:04 -0400
committerGitHub <[email protected]>2020-07-31 05:12:04 -0400
commitd5c012bcd5b7321671f9591a3e58d0b2c9507467 (patch)
treefe64110f7678607947d19c5ba0b46ea2b15d69c4 /common/external/nillable.h
parentMerge pull request #5 from JacobPalecki/WrapperAndGrapher (diff)
parentupdate grapher/wrapper for st-refactor (diff)
downloadrawaccel-d5c012bcd5b7321671f9591a3e58d0b2c9507467.tar.xz
rawaccel-d5c012bcd5b7321671f9591a3e58d0b2c9507467.zip
Merge pull request #6 from a1xd/st-refactor
Refactor
Diffstat (limited to 'common/external/nillable.h')
-rw-r--r--common/external/nillable.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/common/external/nillable.h b/common/external/nillable.h
new file mode 100644
index 0000000..40cf01c
--- /dev/null
+++ b/common/external/nillable.h
@@ -0,0 +1,30 @@
+inline constexpr struct nil_t {} nil;
+
+// Requirements: T is default-constructible and trivially-destructible
+template<typename T>
+struct nillable {
+ bool has_value = false;
+ T value;
+
+ nillable() = default;
+
+ nillable(nil_t) : nillable() {}
+ nillable(const T& v) : has_value(true), value(v) {}
+
+ nillable& operator=(nil_t) {
+ has_value = false;
+ return *this;
+ }
+ nillable& operator=(const T& v) {
+ value = v;
+ has_value = true;
+ return *this;
+ }
+
+ const T* operator->() const { return &value; }
+ T* operator->() { return &value; }
+
+ explicit operator bool() const { return has_value; }
+};
+
+template<typename T> nillable(T)->nillable<T>;