-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathpalOptional.h
103 lines (90 loc) · 3.79 KB
/
palOptional.h
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
***********************************************************************************************************************
*
* Copyright (c) 2023-2025 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************************************/
// palOptional.h : PAL util analog of std::optional
#pragma once
#include "palAssert.h"
#include "palUtil.h"
#include <optional>
namespace Util
{
/**
***********************************************************************************************************************
* @brief Optional container
*
* Optional is a container for another type, where the value is optionally present. It is based on C++17 std::optional,
* but removing its exception-throwing behavior.
*
***********************************************************************************************************************
*/
template<typename T> class Optional : public std::optional<T>
{
public:
// Default constructor.
constexpr Optional() : std::optional<T>() {}
// Constructor given bare value.
template<typename InputT = T> constexpr Optional(InputT&& input) : std::optional<T>(input) {}
// HasValue : has_value renamed to fit PAL style.
constexpr bool HasValue() const { return std::optional<T>::has_value(); }
// Value : value renamed to fit PAL style and avoid exception. We need to avoid an exception even
// on a release build, so we make it trap instead.
constexpr const T& Value() const&
{
if (HasValue() == false)
{
PAL_ASSERT_ALWAYS();
}
return std::optional<T>::value();
}
constexpr T& Value()&
{
if (HasValue() == false)
{
PAL_ASSERT_ALWAYS();
}
return std::optional<T>::value();
}
constexpr T&& Value()&&
{
if (HasValue() == false)
{
PAL_ASSERT_ALWAYS();
}
return std::optional<T>::value();
}
// value : deleted to avoid accidental exception-generating use
constexpr T& value()& = delete;
// ValueOr : value_or renamed to fit PAL style.
template<typename DefaultT> constexpr T ValueOr(DefaultT&& defaultVal) const&
{ return std::optional<T>::value_or(defaultVal); }
template<typename DefaultT> constexpr T ValueOr(DefaultT&& defaultVal)&&
{ return std::optional<T>::value_or(defaultVal); }
// Reset : reset renamed to fit PAL style.
void Reset() { std::optional<T>::reset(); }
// Implicit conversion to bool deleted to avoid client code accidentally using it on an Optional<bool>
// thinking that it tests the embedded bool value.
operator bool() const = delete;
operator bool() = delete;
};
} // Util