2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2018-04-28 10:56:10 +02:00
|
|
|
#include <type_traits>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-08-12 14:31:24 +02:00
|
|
|
template <class T, bool = std::is_copy_constructible<T>::value>
|
2018-12-31 20:04:05 +01:00
|
|
|
class optional {
|
|
|
|
public:
|
|
|
|
optional() = default;
|
2018-08-05 14:24:50 +02:00
|
|
|
template <class T1,
|
|
|
|
std::enable_if_t<!std::is_same<std::decay_t<T1>, optional>::value && std::is_constructible<T, T1>::value,
|
|
|
|
int> = 0>
|
2018-12-31 20:04:05 +01:00
|
|
|
optional(T1 &&t) : impl_(std::forward<T1>(t)) {
|
|
|
|
}
|
2018-08-12 14:31:24 +02:00
|
|
|
|
|
|
|
optional(const optional &other) {
|
|
|
|
if (other) {
|
|
|
|
impl_ = Result<T>(other.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
optional &operator=(const optional &other) {
|
2021-10-29 14:05:28 +02:00
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
2018-08-12 14:31:24 +02:00
|
|
|
if (other) {
|
|
|
|
impl_ = Result<T>(other.value());
|
|
|
|
} else {
|
|
|
|
impl_ = Result<T>();
|
|
|
|
}
|
2019-02-14 12:20:40 +01:00
|
|
|
return *this;
|
2018-08-12 14:31:24 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 12:51:19 +02:00
|
|
|
optional(optional &&) = default;
|
|
|
|
optional &operator=(optional &&) = default;
|
2018-08-12 14:31:24 +02:00
|
|
|
~optional() = default;
|
|
|
|
|
2022-10-09 18:30:18 +02:00
|
|
|
explicit operator bool() const noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
return impl_.is_ok();
|
|
|
|
}
|
|
|
|
T &value() {
|
2020-06-24 13:47:36 +02:00
|
|
|
DCHECK(*this);
|
2018-12-31 20:04:05 +01:00
|
|
|
return impl_.ok_ref();
|
|
|
|
}
|
2018-04-06 21:37:30 +02:00
|
|
|
const T &value() const {
|
2020-06-24 13:47:36 +02:00
|
|
|
DCHECK(*this);
|
2018-04-06 21:37:30 +02:00
|
|
|
return impl_.ok_ref();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
T &operator*() {
|
|
|
|
return value();
|
|
|
|
}
|
2024-06-05 13:02:51 +02:00
|
|
|
const T &operator*() const {
|
|
|
|
return value();
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
T unwrap() {
|
|
|
|
CHECK(*this);
|
|
|
|
auto res = std::move(value());
|
|
|
|
impl_ = {};
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
optional<T> copy() const {
|
2020-06-24 13:47:36 +02:00
|
|
|
if (*this) {
|
|
|
|
return value();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
template <class... ArgsT>
|
2021-12-09 22:27:13 +01:00
|
|
|
void emplace(ArgsT &&...args) {
|
2019-07-06 13:29:15 +02:00
|
|
|
impl_.emplace(std::forward<ArgsT>(args)...);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Result<T> impl_;
|
|
|
|
};
|
|
|
|
|
2018-08-12 14:31:24 +02:00
|
|
|
template <typename T>
|
|
|
|
struct optional<T, false> : optional<T, true> {
|
|
|
|
optional() = default;
|
|
|
|
|
|
|
|
using optional<T, true>::optional;
|
|
|
|
|
2023-05-05 12:51:19 +02:00
|
|
|
optional(const optional &) = delete;
|
|
|
|
optional &operator=(const optional &) = delete;
|
2018-08-12 14:31:24 +02:00
|
|
|
optional(optional &&) = default;
|
|
|
|
optional &operator=(optional &&) = default;
|
|
|
|
~optional() = default;
|
|
|
|
};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|