blob: 6fc207f425f95217281a791ab7b59669f17dab1e (
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#pragma once
#include "Exceptions.hpp"
namespace armnn
{
// NOTE: the members of the Optional class don't follow the ArmNN
// coding convention because the interface to be close to
// the C++-17 interface so we can easily migrate to std::optional
// later.
template <typename T>
class Optional final
{
public:
Optional(T&& value)
: m_HasValue{true}
{
new (m_Storage) T(value);
}
Optional(const T& value)
: m_HasValue{true}
{
new (m_Storage) T(value);
}
Optional(const Optional& other)
: m_HasValue{false}
{
*this = other;
}
Optional() noexcept
: m_HasValue{false}
{
}
~Optional()
{
reset();
}
operator bool() const noexcept
{
return has_value();
}
Optional& operator=(T&& value)
{
reset();
new (m_Storage) T(value);
m_HasValue = true;
return *this;
}
Optional& operator=(const T& value)
{
reset();
new(m_Storage) T(value);
m_HasValue = true;
return *this;
}
Optional& operator=(const Optional& other)
{
reset();
if (other.has_value())
{
new (m_Storage) T(other.value());
m_HasValue = true;
}
return *this;
}
const T& value() const
{
if (!has_value())
{
throw BadOptionalAccessException("Optional has no value");
}
auto valuePtr = reinterpret_cast<const T*>(m_Storage);
return *valuePtr;
}
T& value()
{
if (!has_value())
{
throw BadOptionalAccessException("Optional has no value");
}
auto valuePtr = reinterpret_cast<T*>(m_Storage);
return *valuePtr;
}
bool has_value() const noexcept
{
return m_HasValue;
}
void reset()
{
if (has_value())
{
value().T::~T();
m_HasValue = false;
}
}
private:
alignas(alignof(T)) unsigned char m_Storage[sizeof(T)];
bool m_HasValue;
};
}
|