| 1 |
#pragma once |
| 2 |
|
| 3 |
/** |
| 4 |
* Optional implementation. |
| 5 |
* |
| 6 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 7 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 8 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 9 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/root/optional.h, root/_optional.h) |
| 10 |
* Documentation: https://dlang.org/phobos/dmd_root_optional.html |
| 11 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/root/optional.h |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "dcompat.h" // for d_bool |
| 15 |
|
| 16 |
/// Optional type that is either `empty` or contains a value of type `T` |
| 17 |
template<typename T> |
| 18 |
struct Optional final |
| 19 |
{ |
| 20 |
private: |
| 21 |
/** the value (if present) **/ |
| 22 |
T value; |
| 23 |
|
| 24 |
/** whether `value` is set **/ |
| 25 |
d_bool present; |
| 26 |
|
| 27 |
public: |
| 28 |
/** Creates an `Optional` with the given value **/ |
| 29 |
Optional(T); |
| 30 |
|
| 31 |
/** Creates an `Optional` with the given value **/ |
| 32 |
static Optional<T> create(T); |
| 33 |
|
| 34 |
/** Checks whether this `Optional` contains a value **/ |
| 35 |
bool isPresent() const; |
| 36 |
|
| 37 |
/** Checks whether this `Optional` does not contain a value **/ |
| 38 |
bool isEmpty() const; |
| 39 |
|
| 40 |
/** Returns: The value if present **/ |
| 41 |
T get(); |
| 42 |
|
| 43 |
bool hasValue(const T) const; |
| 44 |
}; |