d: Merge upstream dmd, druntime 4c18eed967, phobos d945686a4.
[official-gcc.git] / gcc / d / dmd / root / optional.h
blob353332c2199e2db60bb8aca3e584b00fb313c451
1 #pragma once
3 /**
4 * Optional implementation.
6 * Copyright: Copyright (C) 1999-2023 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/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/src/dmd/root/optional.h
14 #include "dcompat.h" // for d_bool
16 /// Optional type that is either `empty` or contains a value of type `T`
17 template<typename T>
18 struct Optional final
20 private:
21 /** the value (if present) **/
22 T value;
24 /** whether `value` is set **/
25 d_bool present;
27 public:
28 /** Creates an `Optional` with the given value **/
29 Optional(T);
31 /** Creates an `Optional` with the given value **/
32 static Optional<T> create(T);
34 /** Checks whether this `Optional` contains a value **/
35 bool isPresent() const;
37 /** Checks whether this `Optional` does not contain a value **/
38 bool isEmpty() const;
40 /** Returns: The value if present **/
41 T get();
43 bool hasValue(const T) const;