From dcf4f77fac8bc214afd4851b367ce386f0a34658 Mon Sep 17 00:00:00 2001 From: regnat Date: Mon, 7 Mar 2022 17:45:35 +0100 Subject: [PATCH] Merge `or-suggestions.hh` into `suggestions.hh` No real need for keeping a separate header for such a simple class. This requires changing a bit `OrSuggestions::operator*` to not throw an `Error` to prevent a cyclic dependency. But since this error is only thrown on programmer error, we can replace the whole method by a direct call to `std::get` which will raise its own assertion if needs be. --- src/libexpr/eval-cache.hh | 1 - src/libutil/or-suggestions.hh | 64 ----------------------------------- src/libutil/suggestions.hh | 57 +++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 65 deletions(-) delete mode 100644 src/libutil/or-suggestions.hh diff --git a/src/libexpr/eval-cache.hh b/src/libexpr/eval-cache.hh index c0ca76bcf..40f1d4ffc 100644 --- a/src/libexpr/eval-cache.hh +++ b/src/libexpr/eval-cache.hh @@ -3,7 +3,6 @@ #include "sync.hh" #include "hash.hh" #include "eval.hh" -#include "or-suggestions.hh" #include #include diff --git a/src/libutil/or-suggestions.hh b/src/libutil/or-suggestions.hh deleted file mode 100644 index cb152a1e4..000000000 --- a/src/libutil/or-suggestions.hh +++ /dev/null @@ -1,64 +0,0 @@ -#include "suggestions.hh" -#include "error.hh" - -namespace nix { - -// Either a value of type `T`, or some suggestions -template -class OrSuggestions { -public: - using Raw = std::variant; - - Raw raw; - - T* operator ->() - { - return &**this; - } - - T& operator *() - { - if (auto elt = std::get_if(&raw)) - return *elt; - throw Error("Invalid access to a failed value"); - } - - operator bool() const noexcept - { - return std::holds_alternative(raw); - } - - OrSuggestions(T t) - : raw(t) - { - } - - OrSuggestions() - : raw(Suggestions{}) - { - } - - static OrSuggestions failed(const Suggestions & s) - { - auto res = OrSuggestions(); - res.raw = s; - return res; - } - - static OrSuggestions failed() - { - return OrSuggestions::failed(Suggestions{}); - } - - const Suggestions & getSuggestions() - { - static Suggestions noSuggestions; - if (const auto & suggestions = std::get_if(&raw)) - return *suggestions; - else - return noSuggestions; - } - -}; - -} diff --git a/src/libutil/suggestions.hh b/src/libutil/suggestions.hh index d8f36cb24..d54dd8e31 100644 --- a/src/libutil/suggestions.hh +++ b/src/libutil/suggestions.hh @@ -42,4 +42,61 @@ public: std::ostream & operator<<(std::ostream & str, const Suggestion &); std::ostream & operator<<(std::ostream & str, const Suggestions &); + +// Either a value of type `T`, or some suggestions +template +class OrSuggestions { +public: + using Raw = std::variant; + + Raw raw; + + T* operator ->() + { + return &**this; + } + + T& operator *() + { + return std::get(raw); + } + + operator bool() const noexcept + { + return std::holds_alternative(raw); + } + + OrSuggestions(T t) + : raw(t) + { + } + + OrSuggestions() + : raw(Suggestions{}) + { + } + + static OrSuggestions failed(const Suggestions & s) + { + auto res = OrSuggestions(); + res.raw = s; + return res; + } + + static OrSuggestions failed() + { + return OrSuggestions::failed(Suggestions{}); + } + + const Suggestions & getSuggestions() + { + static Suggestions noSuggestions; + if (const auto & suggestions = std::get_if(&raw)) + return *suggestions; + else + return noSuggestions; + } + +}; + }