FastLED 3.9.15
Loading...
Searching...
No Matches
optional.h
Go to the documentation of this file.
1
2
3#pragma once
4
5#include "fl/variant.h"
6
7namespace fl {
8
9template <typename T> class Optional;
10template <typename T> using optional = Optional<T>;
11
12struct Empty {};
13
14template <typename T> class Optional {
15
16 public:
18 Optional(const Optional &other) : mValue(other.mValue) {}
19 Optional(const T &value) : mValue(value) {}
20 ~Optional() { mValue.reset(); }
21 bool empty() const { return !mValue.template is<T>(); }
22 T *ptr() { return mValue.template ptr<T>(); }
23 const T *ptr() const { return mValue.template ptr<T>(); }
24
25 void reset() { mValue.reset(); }
26
27 Optional &operator=(const Optional &other) {
28 if (this != &other) {
29 mValue = other.mValue;
30 }
31 return *this;
32 }
33
34 Optional &operator=(const T &value) {
35 mValue = value;
36 return *this;
37 }
38
39 bool operator()() const { return !empty(); }
40 bool operator!() const { return empty(); }
41
42 bool operator==(const Optional &other) const {
43 if (empty() && other.empty()) {
44 return true;
45 }
46 if (empty() || other.empty()) {
47 return false;
48 }
49 return *ptr() == *other.ptr();
50 }
51
52 bool operator!=(const Optional &other) const { return !(*this == other); }
53
54 bool operator==(const T &value) const {
55 if (empty()) {
56 return false;
57 }
58 return *ptr() == value;
59 }
60
61 template <typename TT, typename UU>
62 bool operator==(const Variant<TT, UU> &other) const {
63 if (!other.template holdsTypeOf<T>()) {
64 return false;
65 }
66 if (empty()) {
67 return false;
68 }
69 if (other.empty()) {
70 return false;
71 }
72 return *ptr() == *other.template ptr<T>();
73 }
74
75 void swap(Optional &other) {
76 if (this != &other) {
77 mValue.swap(other.mValue);
78 }
79 }
80
81 private:
83};
84
85} // namespace fl
bool empty() const
Definition optional.h:21
bool operator!() const
Definition optional.h:40
bool operator==(const Optional &other) const
Definition optional.h:42
Optional & operator=(const T &value)
Definition optional.h:34
bool operator()() const
Definition optional.h:39
void reset()
Definition optional.h:25
bool operator==(const T &value) const
Definition optional.h:54
void swap(Optional &other)
Definition optional.h:75
Optional(const T &value)
Definition optional.h:19
Optional & operator=(const Optional &other)
Definition optional.h:27
fl::Variant< T, Empty > mValue
Definition optional.h:82
bool operator==(const Variant< TT, UU > &other) const
Definition optional.h:62
bool operator!=(const Optional &other) const
Definition optional.h:52
T * ptr()
Definition optional.h:22
const T * ptr() const
Definition optional.h:23
Optional(const Optional &other)
Definition optional.h:18
bool empty() const noexcept
Definition variant.h:100
Optional< T > optional
Definition optional.h:10
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16