Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
maybe.h
Go to the documentation of this file.
1
9
10/*
11Copyright (c) 2026 MrXie1109
12
13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files (the "Software"), to deal
15in the Software without restriction, including without limitation the rights
16to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions:
19
20The above copyright notice and this permission notice shall be included in all
21copies or substantial portions of the Software.
22
23THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29SOFTWARE.
30*/
31
32#pragma once
33#include <utility>
34#include <memory>
35#include <iostream>
36#include "csexc.h"
37
38namespace console
39{
44 struct
45 {
47
55 template <class T>
56 class Maybe
57 {
58 std::unique_ptr<T> ptr;
59
60 public:
62 Maybe() : ptr(nullptr) {}
63
69 template <class... Args>
70 Maybe(Args &&...args)
71 : ptr(new T(std::forward<Args>(args)...)) {}
72
78 Maybe(const Maybe &other)
79 : ptr(other.ptr ? new T(other.value()) : nullptr) {}
80
85 Maybe(Maybe &&other)
86 : ptr(std::move(other.ptr)) {}
87
92 Maybe(decltype(nothing)) : ptr(nullptr) {}
93
100 {
101 if (ptr)
102 return *ptr;
103 throw bad_maybe_access("Nothing");
104 }
105
111 const T &value() const
112 {
113 if (ptr)
114 return *ptr;
115 throw bad_maybe_access("Nothing");
116 }
117
123 const Maybe &operator=(const T &value)
124 {
125 ptr.reset(new T(value));
126 return *this;
127 }
128
135 {
136 ptr.reset(new T(std::move(value)));
137 return *this;
138 }
139
145 Maybe &operator=(const Maybe &other)
146 {
147 if (this != &other)
148 ptr.reset(other.ptr ? new T(*other.ptr) : nullptr);
149 return *this;
150 }
151
157 Maybe &operator=(Maybe &&other) noexcept
158 {
159 if (this != &other)
160 ptr = std::move(other.ptr);
161 return *this;
162 }
163
169 const Maybe &operator=(decltype(nothing))
170 {
171 ptr = nullptr;
172 return *this;
173 }
174
178 void reset()
179 {
180 ptr.reset();
181 }
182
188 template <class... Args>
189 void reset(Args &&...args)
190 {
191 ptr.reset(new T(std::forward<Args>(args)...));
192 }
193
200 friend std::ostream &operator<<(std::ostream &os, const Maybe &maybe)
201 {
202 if (maybe.ptr)
203 return os << maybe.value();
204 return os << "(nothing)";
205 }
206
214 friend std::istream &operator>>(std::istream &is, Maybe &maybe)
215 {
216 T tmp;
217 if (is >> tmp)
218 maybe.reset(std::move(tmp));
219 else
220 {
221 maybe = nothing;
222 is.clear();
223 }
224 return is;
225 }
226
231 explicit operator bool() const noexcept { return ptr; }
232
237 bool has_value() const noexcept { return ptr; }
238
240 T &operator*() { return *ptr; }
241
243 const T &operator*() const { return *ptr; }
244
246 T *operator->() { return ptr.get(); }
247
249 const T *operator->() const { return ptr.get(); }
250
257 template <class U>
258 T value_or(U &&default_value) const
259 {
260 return ptr ? *ptr : T(std::forward<U>(default_value));
261 }
262
267 void swap(Maybe &other) noexcept
268 {
269 ptr.swap(other.ptr);
270 }
271 };
272}
const Maybe & operator=(T &&value)
从 T 值赋值(移动)。
Definition maybe.h:134
friend std::ostream & operator<<(std::ostream &os, const Maybe &maybe)
输出 Maybe 到流。若包含值则输出值,否则输出 "(nothing)"。
Definition maybe.h:200
Maybe(Args &&...args)
从参数包直接构造一个包含值的 Maybe。
Definition maybe.h:70
Maybe & operator=(Maybe &&other) noexcept
移动赋值。
Definition maybe.h:157
Maybe & operator=(const Maybe &other)
拷贝赋值。
Definition maybe.h:145
const T & operator*() const
解引用获取值(常量),前置条件:has_value() 为 true。
Definition maybe.h:243
bool has_value() const noexcept
检查 Maybe 是否包含值。
Definition maybe.h:237
T * operator->()
成员访问运算符(非常量),前置条件:has_value() 为 true。
Definition maybe.h:246
const T * operator->() const
成员访问运算符(常量),前置条件:has_value() 为 true。
Definition maybe.h:249
T & operator*()
解引用获取值(非常量),前置条件:has_value() 为 true。
Definition maybe.h:240
friend std::istream & operator>>(std::istream &is, Maybe &maybe)
从流读取一个值到 Maybe。
Definition maybe.h:214
T value_or(U &&default_value) const
返回当前值,若为空则返回提供的默认值。
Definition maybe.h:258
void swap(Maybe &other) noexcept
交换两个 Maybe 的内容。
Definition maybe.h:267
const T & value() const
获取内部值的引用(常量)。
Definition maybe.h:111
T & value()
获取内部值的引用(非常量)。
Definition maybe.h:99
const Maybe & operator=(const T &value)
从 T 值赋值(拷贝)。
Definition maybe.h:123
Maybe(Maybe &&other)
移动构造,转移所有权。
Definition maybe.h:85
void reset(Args &&...args)
重置为新的值(从参数包构造)。
Definition maybe.h:189
Maybe(decltype(nothing))
从 nothing 标记构造空 Maybe。
Definition maybe.h:92
Maybe()
默认构造一个空 Maybe。
Definition maybe.h:62
const Maybe & operator=(decltype(nothing))
赋值为空(nothing)。
Definition maybe.h:169
Maybe(const Maybe &other)
拷贝构造,深拷贝内部值。
Definition maybe.h:78
void reset()
清空 Maybe,使其为空。
Definition maybe.h:178
表示对空的 Maybe 对象进行取值操作时抛出的异常。
Definition csexc.h:166
定义 console 库使用的自定义异常类层次结构。
本库所有组件所在的顶层命名空间。
@ U
Definition kb.h:73
@ T
Definition kb.h:72
struct console::@002045325103255016017112205301031250312272036216 nothing
空状态标记对象,用于显式构造空的 Maybe。