2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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
|
|
|
|
|
|
|
|
#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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
optional(optional &&other) = default;
|
|
|
|
optional &operator=(optional &&other) = default;
|
|
|
|
~optional() = default;
|
|
|
|
|
2018-04-06 21:37:30 +02:00
|
|
|
explicit operator bool() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return impl_.is_ok();
|
|
|
|
}
|
|
|
|
T &value() {
|
|
|
|
return impl_.ok_ref();
|
|
|
|
}
|
2018-04-06 21:37:30 +02:00
|
|
|
const T &value() const {
|
|
|
|
return impl_.ok_ref();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
T &operator*() {
|
|
|
|
return value();
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
T unwrap() {
|
|
|
|
CHECK(*this);
|
|
|
|
auto res = std::move(value());
|
|
|
|
impl_ = {};
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ArgsT>
|
|
|
|
void emplace(ArgsT &&... args) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
optional(const optional &other) = delete;
|
|
|
|
optional &operator=(const optional &other) = delete;
|
|
|
|
optional(optional &&) = default;
|
|
|
|
optional &operator=(optional &&) = default;
|
|
|
|
~optional() = default;
|
|
|
|
};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|