F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Optional.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Optional.hpp
3 // \author celskeggs
4 // \brief hpp file for Optional type
5 //
6 // \description
7 // A lightweight optional type for C++14 environments where
8 // std::optional (C++17) is not available.
9 //
10 // \copyright
11 // Copyright (C) 2025-2026 California Institute of Technology.
12 // ALL RIGHTS RESERVED. United States Government Sponsorship
13 // acknowledged.
14 //
15 // ======================================================================
16 
17 #ifndef Fw_Optional_HPP
18 #define Fw_Optional_HPP
19 
20 #include <Fw/Types/Assert.hpp>
21 #include <type_traits>
22 
23 namespace Fw {
24 
26 struct None_t {
27  constexpr explicit None_t() {}
28 };
29 
31 constexpr None_t NONE = None_t();
32 
42 template <typename T>
43 class Optional {
44  static_assert(std::is_trivially_copyable<T>::value, "Fw::Optional only supports trivially copyable types");
45 
46  T m_val;
47  bool m_engaged;
48 
49  public:
50  // ----------------------------------------------------------------------
51  // Construction
52  // ----------------------------------------------------------------------
53 
55  constexpr Optional() : m_val(), m_engaged(false) {}
56 
58  constexpr Optional(const None_t& a) : Optional() { (void)a; }
59 
61  constexpr Optional(const T& t) : m_val(t), m_engaged(true) {}
62 
64  Optional(const Optional& other) : Optional() {
65  if (other.has_value()) {
66  *this = other.value();
67  }
68  }
69 
71  ~Optional() = default;
72 
73  // ----------------------------------------------------------------------
74  // Observers
75  // ----------------------------------------------------------------------
76 
79  bool has_value() const { return m_engaged; }
80 
84  T& value() {
85  FW_ASSERT(m_engaged);
86  return m_val;
87  }
88 
92  const T& value() const {
93  FW_ASSERT(m_engaged);
94  return m_val;
95  }
96 
99  T value_or(const T& default_value) const { return m_engaged ? m_val : default_value; }
100 
101  // ----------------------------------------------------------------------
102  // Modifiers
103  // ----------------------------------------------------------------------
104 
106  void reset() { m_engaged = false; }
107 
108  // ----------------------------------------------------------------------
109  // Assignment operators
110  // ----------------------------------------------------------------------
111 
113  Optional& operator=(const T& t) {
114  m_engaged = true;
115  m_val = t;
116  return *this;
117  }
118 
120  Optional& operator=(const None_t& a) {
121  (void)a;
122  reset();
123  return *this;
124  }
125 
127  Optional& operator=(const Optional& other) {
128  if (other.has_value()) {
129  *this = other.value();
130  } else {
131  reset();
132  }
133  return *this;
134  }
135 
136  // ----------------------------------------------------------------------
137  // Comparison operators
138  // ----------------------------------------------------------------------
139 
141  bool operator==(const None_t& a) const {
142  (void)a;
143  return !m_engaged;
144  }
145 
147  bool operator!=(const None_t& a) const {
148  (void)a;
149  return m_engaged;
150  }
151 
153  bool operator==(const Optional& other) const {
154  if (m_engaged != other.m_engaged) {
155  return false;
156  }
157  if (!m_engaged) {
158  return true;
159  }
160  return m_val == other.m_val;
161  }
162 
164  bool operator!=(const Optional& other) const { return !(*this == other); }
165 };
166 
167 } // namespace Fw
168 
169 #endif
bool has_value() const
Definition: Optional.hpp:79
bool operator==(const Optional &other) const
Equality comparison with another Optional.
Definition: Optional.hpp:153
constexpr Optional()
Construct an empty Optional.
Definition: Optional.hpp:55
T value_or(const T &default_value) const
Definition: Optional.hpp:99
constexpr None_t NONE
Global constant representing an absent/empty optional value.
Definition: Optional.hpp:31
~Optional()=default
Destructor (trivial — T must be trivially copyable)
constexpr Optional(const T &t)
Construct an Optional containing a value.
Definition: Optional.hpp:61
bool operator!=(const None_t &a) const
Compare with NONE sentinel (inequality)
Definition: Optional.hpp:147
bool operator!=(const Optional &other) const
Inequality comparison with another Optional.
Definition: Optional.hpp:164
constexpr Optional(const None_t &a)
Construct an empty Optional from NONE sentinel.
Definition: Optional.hpp:58
T & value()
Definition: Optional.hpp:84
Optional & operator=(const Optional &other)
Copy assignment operator.
Definition: Optional.hpp:127
void reset()
Reset the Optional to an empty state.
Definition: Optional.hpp:106
Optional & operator=(const None_t &a)
Reset the Optional via NONE assignment.
Definition: Optional.hpp:120
Optional & operator=(const T &t)
Assign a value into the Optional.
Definition: Optional.hpp:113
const T & value() const
Definition: Optional.hpp:92
Optional(const Optional &other)
Copy constructor.
Definition: Optional.hpp:64
Sentinel type representing the absence of a value in an Optional.
Definition: Optional.hpp:26
bool operator==(const None_t &a) const
Compare with NONE sentinel.
Definition: Optional.hpp:141
constexpr None_t()
Definition: Optional.hpp:27
Implementation of malloc based allocator.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
A type-safe container for an optional value.
Definition: Optional.hpp:43