NIM PC Cross Platform SDK
载入中...
搜索中...
未找到
ne_stl_optional.h
浏览该文件的文档.
1#ifndef NE_STL_INCLUDENE_STL_INUSE_VALUE_H
2#define NE_STL_INCLUDENE_STL_INUSE_VALUE_H
3
4#include <optional>
5#include "ne_stl_build.h"
7
8namespace nstd {
9
10template <class T>
12 bool has_value_{false};
14};
15template <class T>
17public:
18 _optional() = default;
19 _optional(const _optional& other) {
20 has_value_ = other.has_value_;
21 if (has_value_) {
22 value_ = other.value_;
23 }
24 }
25 _optional(const T& value) {
26 has_value_ = true;
27 value_ = value;
28 }
29 _optional(std::nullptr_t) {}
30 _optional(std::nullopt_t) {}
31 template <class TValue>
32 _optional(const std::optional<TValue>& other) {
33 has_value_ = other.has_value();
34 if (has_value_) {
35 value_ = static_cast<T>(other.value());
36 }
37 }
38
39 template <class TValue>
40 operator std::optional<TValue>() const {
41 if (!has_value_) {
42 return std::nullopt;
43 }
44 return std::optional<TValue>(static_cast<TValue>(value_));
45 }
46
47 template <class TValue>
48 operator _optional<TValue>() const {
49 if (!has_value_) {
50 return std::nullopt;
51 }
52 return _optional<TValue>(static_cast<TValue>(value_));
53 }
54
55 template <class TValue>
56 _optional(const TValue* other) {
57 has_value_ = true;
58 value_ = *other;
59 }
60 template <class TValue>
61 _optional& operator=(const TValue* value) {
62 has_value_ = true;
63 value_ = *value;
64 return *this;
65 }
66 _optional& operator=(const char* value) {
67 has_value_ = true;
68 value_ = value;
69 return *this;
70 }
71 _optional& operator=(std::nullptr_t) {
72 has_value_ = false;
73 return *this;
74 }
75 _optional& operator=(std::nullopt_t) {
76 has_value_ = false;
77 return *this;
78 }
79 template <class TValue>
80 _optional& operator=(const std::optional<TValue>& other) {
81 has_value_ = other.has_value();
82 if (has_value_) {
83 value_ = static_cast<T>(other.value());
84 }
85 return *this;
86 }
87
89 has_value_ = true;
90 value_ = value;
91 return *this;
92 }
93 _optional& operator=(const _optional& other) {
94 has_value_ = other.has_value_;
95 value_ = other.value_;
96 return *this;
97 }
98 template <class TValue>
99 _optional& operator=(const TValue& value) {
100 has_value_ = true;
101 value_ = value;
102 return *this;
103 }
104 template <class TValue>
106 has_value_ = other.has_value();
107 value_ = other.value();
108 return *this;
109 }
110 bool operator==(const _optional& other) const {
111 if (has_value_ != other.has_value_) {
112 return false;
113 }
114 if (!has_value_) {
115 return true;
116 }
117 return value_ == other.value_;
118 }
119 bool operator!=(const _optional& other) const { return !(*this == other); }
120 void reset() { has_value_ = false; }
121 const T& value() const& { return value_; }
122 T value_or(T&& default_value) const& { return has_value_ ? value_ : default_value; }
123 bool has_value() const { return has_value_; }
124 operator bool() const { return has_value_; }
125 const T* operator->() const { return &value_; }
126 T* operator->() { return &value_; }
127 const T& operator*() const& { return value_; }
128 T& operator*() & { return value_; }
129 const T&& operator*() const&& { return value_; }
130 T&& operator*() && { return value_; }
134};
135template <class T>
137
138inline constexpr std::nullopt_t nullopt = std::nullopt;
139
140} // namespace nstd
141
142#endif // !NE_STL_INCLUDENE_STL_INUSE_VALUE_H
unsigned char bool
Definition: stdbool.h:25
Definition: ne_stl_any.h:7
constexpr std::nullopt_t nullopt
Definition: ne_stl_optional.h:138
Definition: ne_stl_optional.h:11
bool has_value_
Definition: ne_stl_optional.h:12
T value_
Definition: ne_stl_optional.h:13
Definition: ne_stl_optional.h:16
_optional & operator=(const TValue *value)
Definition: ne_stl_optional.h:61
const T && operator*() const &&
Definition: ne_stl_optional.h:129
const T * operator->() const
Definition: ne_stl_optional.h:125
_optional(std::nullptr_t)
Definition: ne_stl_optional.h:29
_optional(const _optional &other)
Definition: ne_stl_optional.h:19
_optional & operator=(const T &value)
Definition: ne_stl_optional.h:88
void reset()
Definition: ne_stl_optional.h:120
_optional(const T &value)
Definition: ne_stl_optional.h:25
T && operator*() &&
Definition: ne_stl_optional.h:130
bool has_value_
Definition: ne_stl_optional.h:12
T * operator->()
Definition: ne_stl_optional.h:126
T value_or(T &&default_value) const &
Definition: ne_stl_optional.h:122
bool operator!=(const _optional &other) const
Definition: ne_stl_optional.h:119
_optional & operator=(const std::optional< TValue > &other)
Definition: ne_stl_optional.h:80
T value_
Definition: ne_stl_optional.h:13
_optional(const std::optional< TValue > &other)
Definition: ne_stl_optional.h:32
_optional & operator=(const _optional< TValue > &other)
Definition: ne_stl_optional.h:105
_optional()=default
_optional(const TValue *other)
Definition: ne_stl_optional.h:56
bool has_value() const
Definition: ne_stl_optional.h:123
const T & operator*() const &
Definition: ne_stl_optional.h:127
_optional & operator=(std::nullptr_t)
Definition: ne_stl_optional.h:71
T & operator*() &
Definition: ne_stl_optional.h:128
bool operator==(const _optional &other) const
Definition: ne_stl_optional.h:110
_optional & operator=(const char *value)
Definition: ne_stl_optional.h:66
_optional & operator=(const TValue &value)
Definition: ne_stl_optional.h:99
const T & value() const &
Definition: ne_stl_optional.h:121
_optional & operator=(const _optional &other)
Definition: ne_stl_optional.h:93
_optional(std::nullopt_t)
Definition: ne_stl_optional.h:30
_optional & operator=(std::nullopt_t)
Definition: ne_stl_optional.h:75