From 3728c89f532c43d3a573f6bf3dd1ce4e20a1dcec Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 12 Aug 2018 15:31:24 +0300 Subject: [PATCH] Make optional copyable when possible. GitOrigin-RevId: 28c37171a9db3e868a21bc028ba24fee05833b35 --- tdutils/td/utils/optional.h | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tdutils/td/utils/optional.h b/tdutils/td/utils/optional.h index b498db840..55c95f4ff 100644 --- a/tdutils/td/utils/optional.h +++ b/tdutils/td/utils/optional.h @@ -13,7 +13,7 @@ namespace td { -template +template ::value> class optional { public: optional() = default; @@ -22,6 +22,25 @@ class optional { int> = 0> optional(T1 &&t) : impl_(std::forward(t)) { } + + optional(const optional &other) { + if (other) { + impl_ = Result(other.value()); + } + } + + optional &operator=(const optional &other) { + if (other) { + impl_ = Result(other.value()); + } else { + impl_ = Result(); + } + } + + optional(optional &&other) = default; + optional &operator=(optional &&other) = default; + ~optional() = default; + explicit operator bool() const { return impl_.is_ok(); } @@ -39,4 +58,17 @@ class optional { Result impl_; }; +template +struct optional : optional { + optional() = default; + + using optional::optional; + + optional(const optional &other) = delete; + optional &operator=(const optional &other) = delete; + optional(optional &&) = default; + optional &operator=(optional &&) = default; + ~optional() = default; +}; + } // namespace td