From 264cd050e6280e5b87ec055e0a9d8985a7ba30b3 Mon Sep 17 00:00:00 2001 From: Daniel Shaw Date: Mon, 5 Jun 2017 15:58:47 -0700 Subject: ExpectedT factory class --- toolsrc/include/LineInfo.h | 2 + toolsrc/include/PackageSpec.h | 6 ++- toolsrc/include/PackageSpecParseResult.h | 31 ++++++------- toolsrc/include/Paragraphs.h | 3 +- toolsrc/include/SourceParagraph.h | 17 ++++++- toolsrc/include/vcpkg_expected.h | 79 ++++++++++++++++++++++++-------- 6 files changed, 97 insertions(+), 41 deletions(-) (limited to 'toolsrc/include') diff --git a/toolsrc/include/LineInfo.h b/toolsrc/include/LineInfo.h index 66d91c520..62973462a 100644 --- a/toolsrc/include/LineInfo.h +++ b/toolsrc/include/LineInfo.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace vcpkg { struct LineInfo diff --git a/toolsrc/include/PackageSpec.h b/toolsrc/include/PackageSpec.h index 050d9d079..58edb8274 100644 --- a/toolsrc/include/PackageSpec.h +++ b/toolsrc/include/PackageSpec.h @@ -7,9 +7,11 @@ namespace vcpkg { struct PackageSpec { - static Expected from_string(const std::string& spec_as_string, const Triplet& default_triplet); + static ExpectedT from_string(const std::string& spec_as_string, + const Triplet& default_triplet); static std::string to_string(const std::string& name, const Triplet& triplet); - static Expected from_name_and_triplet(const std::string& name, const Triplet& triplet); + static ExpectedT from_name_and_triplet(const std::string& name, + const Triplet& triplet); const std::string& name() const; diff --git a/toolsrc/include/PackageSpecParseResult.h b/toolsrc/include/PackageSpecParseResult.h index b72c534c2..17d151626 100644 --- a/toolsrc/include/PackageSpecParseResult.h +++ b/toolsrc/include/PackageSpecParseResult.h @@ -1,5 +1,6 @@ #pragma once -#include + +#include "vcpkg_expected.h" namespace vcpkg { @@ -10,27 +11,21 @@ namespace vcpkg INVALID_CHARACTERS }; - struct PackageSpecParseResultCategoryImpl final : std::error_category - { - virtual const char* name() const noexcept override; + CStringView to_string(PackageSpecParseResult ev) noexcept; - virtual std::string message(int ev) const noexcept override; - }; - - const std::error_category& package_spec_parse_result_category(); + template<> + struct ErrorHolder + { + ErrorHolder() : m_err(PackageSpecParseResult::SUCCESS) {} + ErrorHolder(PackageSpecParseResult err) : m_err(err) {} - std::error_code make_error_code(PackageSpecParseResult e); + constexpr bool has_error() const { return m_err != PackageSpecParseResult::SUCCESS; } - PackageSpecParseResult to_package_spec_parse_result(int i); + PackageSpecParseResult error() const { return m_err; } - PackageSpecParseResult to_package_spec_parse_result(std::error_code ec); -} + CStringView to_string() const { return vcpkg::to_string(m_err); } -// Enable implicit conversion to std::error_code -namespace std -{ - template<> - struct is_error_code_enum : ::std::true_type - { + private: + PackageSpecParseResult m_err; }; } diff --git a/toolsrc/include/Paragraphs.h b/toolsrc/include/Paragraphs.h index 66f6bd2e4..59f0eefc8 100644 --- a/toolsrc/include/Paragraphs.h +++ b/toolsrc/include/Paragraphs.h @@ -16,7 +16,8 @@ namespace vcpkg::Paragraphs Expected parse_single_paragraph(const std::string& str); Expected> parse_paragraphs(const std::string& str); - Expected try_load_port(const Files::Filesystem& fs, const fs::path& control_path); + ExpectedT try_load_port(const Files::Filesystem& fs, + const fs::path& control_path); Expected try_load_cached_package(const VcpkgPaths& paths, const PackageSpec& spec); diff --git a/toolsrc/include/SourceParagraph.h b/toolsrc/include/SourceParagraph.h index 19f558170..9874eb3a9 100644 --- a/toolsrc/include/SourceParagraph.h +++ b/toolsrc/include/SourceParagraph.h @@ -1,5 +1,6 @@ #pragma once +#include "vcpkg_expected.h" #include #include @@ -15,14 +16,23 @@ namespace vcpkg const std::string& to_string(const Dependency& dep); + struct ParseControlErrorInfo + { + std::string name; + std::string remaining_fields_as_string; + std::string valid_fields_as_string; + std::error_code error; + }; + /// /// Port metadata (CONTROL file) /// struct SourceParagraph { - SourceParagraph(); + static ExpectedT parse_control_file( + std::unordered_map fields); - explicit SourceParagraph(std::unordered_map fields); + SourceParagraph(); std::string name; std::string version; @@ -31,6 +41,9 @@ namespace vcpkg std::vector depends; }; + void print_error_message(const ParseControlErrorInfo& info); + void print_error_message(std::vector error_info_list); + std::vector filter_dependencies(const std::vector& deps, const Triplet& t); std::vector expand_qualified_dependencies(const std::vector& depends); diff --git a/toolsrc/include/vcpkg_expected.h b/toolsrc/include/vcpkg_expected.h index 3a920022b..15dbf5e79 100644 --- a/toolsrc/include/vcpkg_expected.h +++ b/toolsrc/include/vcpkg_expected.h @@ -5,27 +5,63 @@ namespace vcpkg { - template - class Expected + template + struct ErrorHolder { - public: - // Constructors are intentionally implicit - Expected(const std::error_code& ec) : m_error_code(ec), m_t() {} + ErrorHolder() : m_is_error(false) {} + ErrorHolder(const Err& err) : m_is_error(true), m_err(err) {} + ErrorHolder(Err&& err) : m_is_error(true), m_err(std::move(err)) {} + + constexpr bool has_error() const { return m_is_error; } + + const Err& error() const { return m_err; } + Err& error() { return m_err; } + + CStringView to_string() const { return "value was error"; } + + private: + bool m_is_error; + Err m_err; + }; + + template<> + struct ErrorHolder + { + ErrorHolder() = default; + ErrorHolder(const std::error_code& err) : m_err(err) {} + + constexpr bool has_error() const { return bool(m_err); } - Expected(std::errc ec) : Expected(std::make_error_code(ec)) {} + const std::error_code& error() const { return m_err; } + std::error_code& error() { return m_err; } - Expected(const T& t) : m_error_code(), m_t(t) {} + CStringView to_string() const { return "value was error"; } - Expected(T&& t) : m_error_code(), m_t(std::move(t)) {} + private: + std::error_code m_err; + }; + + template + class ExpectedT + { + public: + constexpr ExpectedT() = default; + + // Constructors are intentionally implicit + + ExpectedT(const S& s) : m_s(s) {} + ExpectedT(S&& s) : m_s(std::move(s)) {} - Expected() : Expected(std::error_code(), T()) {} + ExpectedT(const T& t) : m_t(t) {} + ExpectedT(T&& t) : m_t(std::move(t)) {} - Expected(const Expected&) = default; - Expected(Expected&&) = default; - Expected& operator=(const Expected&) = default; - Expected& operator=(Expected&&) = default; + ExpectedT(const ExpectedT&) = default; + ExpectedT(ExpectedT&&) = default; + ExpectedT& operator=(const ExpectedT&) = default; + ExpectedT& operator=(ExpectedT&&) = default; - std::error_code error_code() const { return this->m_error_code; } + explicit constexpr operator bool() const noexcept { return !m_s.has_error(); } + constexpr bool has_value() const noexcept { return !m_s.has_error(); } T&& value_or_exit(const LineInfo& line_info) && { @@ -39,9 +75,13 @@ namespace vcpkg return this->m_t; } + const S& error() const & { return this->m_s.error(); } + + S&& error() && { return std::move(this->m_s.error()); } + const T* get() const { - if (m_error_code) + if (!this->has_value()) { return nullptr; } @@ -50,7 +90,7 @@ namespace vcpkg T* get() { - if (m_error_code) + if (!this->has_value()) { return nullptr; } @@ -60,10 +100,13 @@ namespace vcpkg private: void exit_if_error(const LineInfo& line_info) const { - Checks::check_exit(line_info, !this->m_error_code, this->m_error_code.message()); + Checks::check_exit(line_info, !m_s.has_error(), m_s.to_string()); } - std::error_code m_error_code; + ErrorHolder m_s; T m_t; }; + + template + using Expected = ExpectedT; } -- cgit v1.2.3