From 54e45d113d3509920e5620336ac355bb7229c57b Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Sat, 12 Jun 2021 14:58:35 +0300 Subject: [PATCH 1/5] optional solution --- module-1/homework/Optional/optional.h | 203 +++++++++++++++++++++++++- 1 file changed, 196 insertions(+), 7 deletions(-) diff --git a/module-1/homework/Optional/optional.h b/module-1/homework/Optional/optional.h index 1c2bbf91..cf95a1df 100644 --- a/module-1/homework/Optional/optional.h +++ b/module-1/homework/Optional/optional.h @@ -6,21 +6,115 @@ namespace task { struct NullOpt { - // Your code goes here; + explicit constexpr NullOpt(int) { + } }; -constexpr NullOpt kNullOpt = // Your code goes here; - +constexpr NullOpt kNullOpt = NullOpt(0); struct InPlace { - // Your code goes here; + explicit InPlace() = default; }; -constexpr InPlace kInPlace = //Your code goes here; +constexpr InPlace kInPlace = InPlace(); + +template +class OptionalBase { +public: + constexpr OptionalBase() { + } + + constexpr OptionalBase(NullOpt) { // NOLINT + } + + template + constexpr OptionalBase(InPlace, Args&&... args) + : value_(std::forward(args)...), + is_none_(false) { + } + + template + constexpr OptionalBase(U&& value) + : value_(std::forward(value)), + is_none_(false) { + } + +protected: + template + void Set(U&& value) { + value_ = std::forward(value); + is_none_ = false; + } + + void Reset() { + is_none_ = true; + } + + union { + T value_; + char none_; + }; + + bool is_none_ = true; +}; template -class Optional : public // Your code goes here; { +class OptionalBase { public: - using value_type = // Your code goes here; + constexpr OptionalBase() { + } + + constexpr OptionalBase(NullOpt) { // NOLINT + } + + template + constexpr OptionalBase(InPlace, Args&&... args) + : value_(std::forward(args)...), + is_none_(false) { + } + + template + constexpr OptionalBase(U&& value) + : value_(std::forward(value)), + is_none_(false) { + } + + ~OptionalBase() { + if (!is_none_) { + value_.~T(); + } + } + +protected: + template + void Set(U&& value) { + if (!is_none_) { + value_.~T(); + } + value_ = std::forward(value); + is_none_ = false; + } + + void Reset() { + if (!is_none_) { + value_.~T(); + } + is_none_ = true; + } + + union { + T value_; + char none_; + }; + + bool is_none_ = true; +}; + +template +class Optional : public OptionalBase> { +private: + using base = OptionalBase::value>; +public: + using value_type = T; constexpr Optional() noexcept; @@ -61,4 +155,99 @@ class Optional : public // Your code goes here; { constexpr value_type&& operator*() &&; }; + +template +constexpr Optional::Optional() noexcept { +} + +template +template +constexpr Optional::Optional(U&& value) : base(std::forward(value)) { +} + +template +constexpr Optional::Optional(NullOpt) noexcept { +} + +template +template +constexpr Optional::Optional(InPlace, Args&&... args) + : base{kInPlace, std::forward(args)...} { +} + +template +Optional& Optional::operator=(NullOpt) noexcept { + base::Reset(); + return *this; +} + +template +template +Optional& Optional::operator=(U&& value) { + base::Set(std::forward(value)); + return *this; +} + +template +void Optional::Reset() noexcept { + base::Reset(); +} + +template +template +constexpr T Optional::ValueOr(U&& default_value) const& { + if (!base::is_none_) { + return base::value_; + } + return default_value; +} + +template +template +constexpr T Optional::ValueOr(U&& default_value) && { + if (!base::is_none_) { + return base::value_; + } + return default_value; +} + +template +constexpr bool Optional::HasValue() const noexcept { + return !base::is_none_; +} + +template +constexpr Optional::operator bool() const noexcept { + return HasValue(); +} + +template +constexpr std::add_pointer_t::value_type> Optional::operator->() const { + return base::value_; +} + +template +constexpr std::add_pointer_t::value_type> Optional::operator->() { + return &(base::value_); +} + +template +constexpr const typename Optional::value_type& Optional::operator*() const& { + return base::value_; +} + +template +constexpr typename Optional::value_type& Optional::operator*() & { + return base::value_; +} + +template +constexpr const typename Optional::value_type&& Optional::operator*() const&& { + return std::move(base::value_); +} + +template +constexpr typename Optional::value_type&& Optional::operator*() && { + return std::move(base::value_); +} } // namespace task \ No newline at end of file From a195b3b9387064361ecf0d21293f514016744fc4 Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Sat, 12 Jun 2021 15:06:30 +0300 Subject: [PATCH 2/5] optional solution 2.0 --- module-1/homework/Optional/optional.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/module-1/homework/Optional/optional.h b/module-1/homework/Optional/optional.h index cf95a1df..6140e13e 100644 --- a/module-1/homework/Optional/optional.h +++ b/module-1/homework/Optional/optional.h @@ -23,17 +23,18 @@ class OptionalBase { constexpr OptionalBase() { } - constexpr OptionalBase(NullOpt) { // NOLINT + constexpr OptionalBase(NullOpt) // NOLINT + { } template - constexpr OptionalBase(InPlace, Args&&... args) + constexpr OptionalBase(InPlace, Args&&... args) // NOLINT : value_(std::forward(args)...), is_none_(false) { } template - constexpr OptionalBase(U&& value) + constexpr OptionalBase(U&& value) // NOLINT : value_(std::forward(value)), is_none_(false) { } @@ -63,17 +64,18 @@ class OptionalBase { constexpr OptionalBase() { } - constexpr OptionalBase(NullOpt) { // NOLINT + constexpr OptionalBase(NullOpt) // NOLINT + { } template - constexpr OptionalBase(InPlace, Args&&... args) + constexpr OptionalBase(InPlace, Args&&... args) // NOLINT : value_(std::forward(args)...), is_none_(false) { } template - constexpr OptionalBase(U&& value) + constexpr OptionalBase(U&& value) // NOLINT : value_(std::forward(value)), is_none_(false) { } @@ -113,6 +115,7 @@ template class Optional : public OptionalBase> { private: using base = OptionalBase::value>; + public: using value_type = T; @@ -222,7 +225,8 @@ constexpr Optional::operator bool() const noexcept { } template -constexpr std::add_pointer_t::value_type> Optional::operator->() const { +constexpr std::add_pointer_t::value_type> Optional::operator->() + const { return base::value_; } From 095d00842c0bf1925c2b74e777eb0e6a1c199e54 Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Sat, 12 Jun 2021 15:09:25 +0300 Subject: [PATCH 3/5] optional solution 2.0 --- module-1/homework/Optional/optional.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/module-1/homework/Optional/optional.h b/module-1/homework/Optional/optional.h index 6140e13e..2a450ca1 100644 --- a/module-1/homework/Optional/optional.h +++ b/module-1/homework/Optional/optional.h @@ -23,18 +23,18 @@ class OptionalBase { constexpr OptionalBase() { } - constexpr OptionalBase(NullOpt) // NOLINT + constexpr OptionalBase(NullOpt) // NOLINT { } template - constexpr OptionalBase(InPlace, Args&&... args) // NOLINT + constexpr OptionalBase(InPlace, Args&&... args) // NOLINT : value_(std::forward(args)...), is_none_(false) { } template - constexpr OptionalBase(U&& value) // NOLINT + constexpr OptionalBase(U&& value) // NOLINT : value_(std::forward(value)), is_none_(false) { } @@ -64,18 +64,18 @@ class OptionalBase { constexpr OptionalBase() { } - constexpr OptionalBase(NullOpt) // NOLINT + constexpr OptionalBase(NullOpt) // NOLINT { } template - constexpr OptionalBase(InPlace, Args&&... args) // NOLINT + constexpr OptionalBase(InPlace, Args&&... args) // NOLINT : value_(std::forward(args)...), is_none_(false) { } template - constexpr OptionalBase(U&& value) // NOLINT + constexpr OptionalBase(U&& value) // NOLINT : value_(std::forward(value)), is_none_(false) { } From 1df3a222dc5850b7727c83e178bc0fea1d2bb17d Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Sat, 12 Jun 2021 15:14:20 +0300 Subject: [PATCH 4/5] optional solution 2.2 --- module-1/homework/Optional/optional.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/module-1/homework/Optional/optional.h b/module-1/homework/Optional/optional.h index 2a450ca1..f41f5533 100644 --- a/module-1/homework/Optional/optional.h +++ b/module-1/homework/Optional/optional.h @@ -29,14 +29,12 @@ class OptionalBase { template constexpr OptionalBase(InPlace, Args&&... args) // NOLINT - : value_(std::forward(args)...), - is_none_(false) { + : value_(std::forward(args)...), is_none_(false) { } template constexpr OptionalBase(U&& value) // NOLINT - : value_(std::forward(value)), - is_none_(false) { + : value_(std::forward(value)), is_none_(false) { } protected: @@ -70,14 +68,12 @@ class OptionalBase { template constexpr OptionalBase(InPlace, Args&&... args) // NOLINT - : value_(std::forward(args)...), - is_none_(false) { + : value_(std::forward(args)...), is_none_(false) { } template constexpr OptionalBase(U&& value) // NOLINT - : value_(std::forward(value)), - is_none_(false) { + : value_(std::forward(value)), is_none_(false) { } ~OptionalBase() { @@ -225,7 +221,7 @@ constexpr Optional::operator bool() const noexcept { } template -constexpr std::add_pointer_t::value_type> Optional::operator->() +constexpr std::add_pointer_t::value_type> Optional::operator->() const { return base::value_; } From fb9a2aac1b2f0681b4b22be65db053dfbdad3371 Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Sat, 12 Jun 2021 20:41:58 +0300 Subject: [PATCH 5/5] changed () -> {} --- module-1/homework/Optional/optional.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/module-1/homework/Optional/optional.h b/module-1/homework/Optional/optional.h index f41f5533..f10ad2da 100644 --- a/module-1/homework/Optional/optional.h +++ b/module-1/homework/Optional/optional.h @@ -29,12 +29,12 @@ class OptionalBase { template constexpr OptionalBase(InPlace, Args&&... args) // NOLINT - : value_(std::forward(args)...), is_none_(false) { + : value_{std::forward(args)...}, is_none_{false} { } template constexpr OptionalBase(U&& value) // NOLINT - : value_(std::forward(value)), is_none_(false) { + : value_{std::forward(value)}, is_none_{false} { } protected: @@ -68,12 +68,12 @@ class OptionalBase { template constexpr OptionalBase(InPlace, Args&&... args) // NOLINT - : value_(std::forward(args)...), is_none_(false) { + : value_{std::forward(args)...}, is_none_{false} { } template constexpr OptionalBase(U&& value) // NOLINT - : value_(std::forward(value)), is_none_(false) { + : value_{std::forward(value)}, is_none_{false} { } ~OptionalBase() { @@ -161,7 +161,7 @@ constexpr Optional::Optional() noexcept { template template -constexpr Optional::Optional(U&& value) : base(std::forward(value)) { +constexpr Optional::Optional(U&& value) : base{std::forward(value)} { } template