Line data Source code
1 : //
2 : // Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
3 : // Use of this source code is governed by a BSD-style license that can be
4 : // found in the LICENSE file.
5 : //
6 : // Optional.h:
7 : // Represents a type that may be invalid, similar to std::optional.
8 : //
9 :
10 : #ifndef COMMON_OPTIONAL_H_
11 : #define COMMON_OPTIONAL_H_
12 :
13 : template <class T>
14 0 : struct Optional
15 : {
16 0 : Optional()
17 : : mValid(false),
18 0 : mValue(T())
19 0 : {}
20 :
21 0 : Optional(const T &valueIn) : mValid(true), mValue(valueIn) {}
22 :
23 : Optional(const Optional &other)
24 : : mValid(other.mValid),
25 : mValue(other.mValue)
26 : {}
27 :
28 : Optional &operator=(const Optional &other)
29 : {
30 : this->mValid = other.mValid;
31 : this->mValue = other.mValue;
32 : return *this;
33 : }
34 :
35 : Optional &operator=(const T &value)
36 : {
37 : mValue = value;
38 : mValid = true;
39 : return *this;
40 : }
41 :
42 0 : Optional &operator=(T &&value)
43 : {
44 0 : mValue = std::move(value);
45 0 : mValid = true;
46 0 : return *this;
47 : }
48 :
49 : void reset()
50 : {
51 : mValid = false;
52 : }
53 :
54 0 : static Optional Invalid() { return Optional(); }
55 :
56 0 : bool valid() const { return mValid; }
57 0 : const T &value() const { return mValue; }
58 :
59 : bool operator==(const Optional &other) const
60 : {
61 : return ((mValid == other.mValid) && (!mValid || (mValue == other.mValue)));
62 : }
63 :
64 : bool operator!=(const Optional &other) const
65 : {
66 : return !(*this == other);
67 : }
68 :
69 : private:
70 : bool mValid;
71 : T mValue;
72 : };
73 :
74 : #endif // COMMON_OPTIONAL_H_
|