ForestHub SDK 0.1.0
C++14 LLM SDK for PC and embedded platforms
Loading...
Searching...
No Matches
optional.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (c) 2026 ForestHub. All rights reserved.
3// For commercial licensing, visit https://github.com/ForestHubAI/fh-sdk
4
5#ifndef FORESTHUB_UTIL_OPTIONAL_HPP
6#define FORESTHUB_UTIL_OPTIONAL_HPP
7
10
11#include <utility>
12
13namespace foresthub {
14
20template <typename T>
21struct Optional {
22 bool has_value = false;
23 T value{};
24
26 Optional() = default;
27
29 Optional(const T& val) : has_value(true), value(val) {}
30
32 Optional(T&& val) : has_value(true), value(std::move(val)) {}
33
35 bool HasValue() const { return has_value; }
36
38 explicit operator bool() const { return has_value; }
39
41 const T& operator*() const { return value; }
43 T& operator*() { return value; }
45 const T* operator->() const { return &value; }
47 T* operator->() { return &value; }
48
50 Optional& operator=(const T& val) {
51 has_value = true;
52 value = val;
53 return *this;
54 }
55
57 Optional& operator=(T&& val) {
58 has_value = true;
59 value = std::move(val);
60 return *this;
61 }
62};
63
64} // namespace foresthub
65
66#endif // FORESTHUB_UTIL_OPTIONAL_HPP
Top-level namespace for the ForestHub SDK.
Optional & operator=(T &&val)
Move-assign a value (sets has_value to true).
Definition optional.hpp:57
const T & operator*() const
Returns a reference to the stored value (undefined if empty).
Definition optional.hpp:41
Optional(const T &val)
Construct with a copy of the given value.
Definition optional.hpp:29
T * operator->()
Returns a mutable pointer to the stored value (undefined if empty).
Definition optional.hpp:47
T value
Stored value (default-constructed when empty).
Definition optional.hpp:23
Optional()=default
Default-construct without a stored value.
T & operator*()
Returns a mutable reference to the stored value (undefined if empty).
Definition optional.hpp:43
bool has_value
Whether a value is stored.
Definition optional.hpp:22
const T * operator->() const
Returns a pointer to the stored value (undefined if empty).
Definition optional.hpp:45
Optional & operator=(const T &val)
Assign a value (sets has_value to true).
Definition optional.hpp:50
Optional(T &&val)
Construct by moving the given value.
Definition optional.hpp:32
bool HasValue() const
Returns true if a value is stored.
Definition optional.hpp:35