diff options
author | regnat <rg@regnat.ovh> | 2022-03-04 13:42:43 +0100 |
---|---|---|
committer | regnat <rg@regnat.ovh> | 2022-03-07 10:09:10 +0100 |
commit | fd45d85b4149a4d153d9c60f7f77244b3cf29ae3 (patch) | |
tree | da6a53b4478f6d97a9d7932131dccc47a3da54d9 /src | |
parent | b44cebd1fd68f89bbc2317e2b7978042309e17bb (diff) |
Move OrSuggestions to its own header
Prevents a recursive inclusion
Diffstat (limited to 'src')
-rw-r--r-- | src/libexpr/eval-cache.hh | 1 | ||||
-rw-r--r-- | src/libutil/or-suggestions.hh | 64 | ||||
-rw-r--r-- | src/libutil/suggestions.hh | 59 |
3 files changed, 65 insertions, 59 deletions
diff --git a/src/libexpr/eval-cache.hh b/src/libexpr/eval-cache.hh index 40f1d4ffc..c0ca76bcf 100644 --- a/src/libexpr/eval-cache.hh +++ b/src/libexpr/eval-cache.hh @@ -3,6 +3,7 @@ #include "sync.hh" #include "hash.hh" #include "eval.hh" +#include "or-suggestions.hh" #include <functional> #include <variant> diff --git a/src/libutil/or-suggestions.hh b/src/libutil/or-suggestions.hh new file mode 100644 index 000000000..cb152a1e4 --- /dev/null +++ b/src/libutil/or-suggestions.hh @@ -0,0 +1,64 @@ +#include "suggestions.hh" +#include "error.hh" + +namespace nix { + +// Either a value of type `T`, or some suggestions +template<typename T> +class OrSuggestions { +public: + using Raw = std::variant<T, Suggestions>; + + Raw raw; + + T* operator ->() + { + return &**this; + } + + T& operator *() + { + if (auto elt = std::get_if<T>(&raw)) + return *elt; + throw Error("Invalid access to a failed value"); + } + + operator bool() const noexcept + { + return std::holds_alternative<T>(raw); + } + + OrSuggestions(T t) + : raw(t) + { + } + + OrSuggestions() + : raw(Suggestions{}) + { + } + + static OrSuggestions<T> failed(const Suggestions & s) + { + auto res = OrSuggestions<T>(); + res.raw = s; + return res; + } + + static OrSuggestions<T> failed() + { + return OrSuggestions<T>::failed(Suggestions{}); + } + + const Suggestions & getSuggestions() + { + static Suggestions noSuggestions; + if (const auto & suggestions = std::get_if<Suggestions>(&raw)) + return *suggestions; + else + return noSuggestions; + } + +}; + +} diff --git a/src/libutil/suggestions.hh b/src/libutil/suggestions.hh index 63426259d..705b4cd1c 100644 --- a/src/libutil/suggestions.hh +++ b/src/libutil/suggestions.hh @@ -39,63 +39,4 @@ public: Suggestions& operator+=(const Suggestions & other); }; - -// Either a value of type `T`, or some suggestions -template<typename T> -class OrSuggestions { -public: - using Raw = std::variant<T, Suggestions>; - - Raw raw; - - T* operator ->() - { - return &**this; - } - - T& operator *() - { - if (auto elt = std::get_if<T>(&raw)) - return *elt; - throw Error("Invalid access to a failed value"); - } - - operator bool() const noexcept - { - return std::holds_alternative<T>(raw); - } - - OrSuggestions(T t) - : raw(t) - { - } - - OrSuggestions() - : raw(Suggestions{}) - { - } - - static OrSuggestions<T> failed(const Suggestions & s) - { - auto res = OrSuggestions<T>(); - res.raw = s; - return res; - } - - static OrSuggestions<T> failed() - { - return OrSuggestions<T>::failed(Suggestions{}); - } - - const Suggestions & get_suggestions() - { - static Suggestions noSuggestions; - if (const auto & suggestions = std::get_if<Suggestions>(&raw)) - return *suggestions; - else - return noSuggestions; - } - -}; - } |