summaryrefslogtreecommitdiff
path: root/gcc/d/dmd/root/optional.d
blob: f2f7389efd26efb76484e39b498f0674973781a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
 * Implementation of an 'Optional' type
 *
 * Copyright:   Copyright (C) 1999-2022 by The D Language Foundation, All Rights Reserved
 * Authors:     $(LINK2 https://www.digitalmars.com, Walter Bright)
 * License:     $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
 * Source:      $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/root/optional.d, root/_optional.d)
 * Documentation:  https://dlang.org/phobos/dmd_root_optional.html
 * Coverage:    https://codecov.io/gh/dlang/dmd/src/master/src/dmd/root/optional.d
 */
module dmd.root.optional;

///
unittest
{
    import core.exception : AssertError;

    Optional!int opt;
    assert( opt.isEmpty());
    assert(!opt.isPresent());
    assert(!opt.hasValue(1));
    assert(!opt.hasValue(2));

    bool caught;
    try
        cast(void) opt.get();
    catch (AssertError)
        caught = true;
    assert(caught);

    opt = Optional!int(1);
    assert(!opt.isEmpty());
    assert( opt.isPresent());
    assert( opt.get() == 1);
    assert( opt.hasValue(1));
    assert(!opt.hasValue(2));
}

/// Optional type that is either `empty` or contains a value of type `T`
extern (C++) struct Optional(T)
{
    /// the value (if present)
    private T value;

    /// whether `value` is set
    private bool present;

    /// Creates an `Optional` with the given value
    this(T value)
    {
        this.value = value;
        this.present = true;
    }

    // Ctor wrapper for the C++ interface (required by older host compilers)
    /// ditto
    static Optional!T create(T val)
    {
        return Optional!T(val);
    }

    /// Returns: Whether this `Optional` contains a value
    bool isPresent() const
    {
        return this.present;
    }

    /// Returns: Whether this `Optional` does not contain a value
    bool isEmpty() const
    {
        return !this.present;
    }

    /// Returns: The value if present
    inout(T) get() inout
    {
        assert(present);
        return value;
    }

    /// Returns: Whether this `Optional` contains the supplied value
    bool hasValue(const T exp) const
    {
        return present && value == exp;
    }
}