diff --git a/src/Algos.cc b/src/Algos.cc index 5679f6730..fa5b644ff 100644 --- a/src/Algos.cc +++ b/src/Algos.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -145,7 +146,7 @@ equalsInsensitive( ); } -Option +std::optional findSimilarStr( const std::string_view lhs, std::span candidates ) { @@ -163,7 +164,7 @@ findSimilarStr( const usize length = lhs.size(); const usize maxDist = length < 3 ? length - 1 : length / 3; - Option> similarStr = None; + std::optional> similarStr = std::nullopt; for (const std::string_view str : candidates) { const usize curDist = levDistance(lhs, str); if (curDist <= maxDist) { @@ -177,7 +178,7 @@ findSimilarStr( if (similarStr.has_value()) { return similarStr->first; } else { - return None; + return std::nullopt; } } @@ -246,8 +247,10 @@ testFindSimilarStr() { assertEq(findSimilarStr("els", candidates), "else"sv); assertEq(findSimilarStr("endi", candidates), "endif"sv); - assertEq(findSimilarStr("i", candidates), None); - assertEq(findSimilarStr("special_compiler_directive", candidates), None); + assertEq(findSimilarStr("i", candidates), std::nullopt); + assertEq( + findSimilarStr("special_compiler_directive", candidates), std::nullopt + ); pass(); } @@ -256,7 +259,7 @@ void testFindSimilarStr2() { constexpr std::array candidates{ "aaab", "aaabc" }; assertEq(findSimilarStr("aaaa", candidates), "aaab"sv); - assertEq(findSimilarStr("1111111111", candidates), None); + assertEq(findSimilarStr("1111111111", candidates), std::nullopt); constexpr std::array candidateS2{ "AAAA" }; assertEq(findSimilarStr("aaaa", candidateS2), "AAAA"sv); diff --git a/src/Algos.hpp b/src/Algos.hpp index 8703480a8..718c88b82 100644 --- a/src/Algos.hpp +++ b/src/Algos.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -84,7 +85,7 @@ topoSort( /// \param candidates the candidates to find a similar string. /// /// \returns a similar string if exists. If no similar string exists, -/// returns None. -Option findSimilarStr( +/// returns std::nullopt. +std::optional findSimilarStr( std::string_view lhs, std::span candidates ); diff --git a/src/BuildConfig.cc b/src/BuildConfig.cc index 1018f209f..f78b6097e 100644 --- a/src/BuildConfig.cc +++ b/src/BuildConfig.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -89,7 +90,7 @@ operator<<(std::ostream& os, const Variable& var) { struct Target { std::vector commands; - Option sourceFile; + std::optional sourceFile; HashSet remDeps; }; @@ -101,8 +102,8 @@ struct BuildConfig { HashMap> varDeps; HashMap targets; HashMap> targetDeps; - Option> phony; - Option> all; + std::optional> phony; + std::optional> all; std::string OUT_DIR; std::string CXX = "clang++"; @@ -155,7 +156,7 @@ struct BuildConfig { void defineTarget( const std::string& name, const std::vector& commands, const HashSet& remDeps = {}, - const Option& sourceFile = None + const std::optional& sourceFile = std::nullopt ) { targets[name] = { commands, sourceFile, remDeps }; @@ -212,7 +213,7 @@ static void emitTarget( std::ostream& os, const std::string_view target, const HashSet& dependsOn, - const Option& sourceFile = None, + const std::optional& sourceFile = std::nullopt, const std::vector& commands = {} ) { usize offset = 0; @@ -324,8 +325,8 @@ BuildConfig::emitCompdb(const std::string_view baseDir, std::ostream& os) continue; } - // We don't check the Option value because we know the first dependency - // always exists for compile targets. + // We don't check the std::optional value because we know the first + // dependency always exists for compile targets. const std::string file = targetInfo.sourceFile.value(); // The output is the target. const std::string output = target; diff --git a/src/Cli.hpp b/src/Cli.hpp index 8c20bca1d..69233c5a4 100644 --- a/src/Cli.hpp +++ b/src/Cli.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -142,7 +143,7 @@ class Subcmd : public CliBase, public ShortAndHidden { friend class Cli; std::string_view cmdName; - Option> globalOpts = None; + std::optional> globalOpts = std::nullopt; std::vector localOpts; Arg arg; std::function)> mainFn; @@ -204,11 +205,12 @@ class Cli : public CliBase { static constexpr int CONTINUE = -1; - // Returns the exit code if the global option was handled, otherwise None. - // Returns CONTINUE if the caller should not propagate the exit code. + // Returns the exit code if the global option was handled, otherwise + // std::nullopt. Returns CONTINUE if the caller should not propagate the exit + // code. // TODO: -1 is not a good idea. // TODO: result-like types make more sense. - [[nodiscard]] static inline Option handleGlobalOpts( + [[nodiscard]] static inline std::optional handleGlobalOpts( std::forward_iterator auto& itr, const std::forward_iterator auto end, std::string_view subcmd = "" ) { @@ -237,7 +239,7 @@ class Cli : public CliBase { return EXIT_FAILURE; } } - return None; + return std::nullopt; } private: diff --git a/src/Cmd/Add.cc b/src/Cmd/Add.cc index af794b61f..e28e7dfb3 100644 --- a/src/Cmd/Add.cc +++ b/src/Cmd/Add.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -36,7 +37,7 @@ const Subcmd ADD_CMD = .setPlaceholder("")) .setMainFn(addMain); -static Option +static std::optional handleNextArg( std::span::iterator& itr, const std::span::iterator& end, std::string& arg @@ -46,7 +47,7 @@ handleNextArg( return Subcmd::missingArgumentForOpt(*--itr); } arg = std::string(*itr); - return None; + return std::nullopt; } static void @@ -167,13 +168,13 @@ addMain(const std::span args) { // clang-format off HashMap< std::string_view, - std::function(decltype(args)::iterator&, decltype(args)::iterator)> + std::function(decltype(args)::iterator&, decltype(args)::iterator)> > handlers = { { "--sys", [&](auto&, auto) { isSystemDependency = true; - return None; + return std::nullopt; } }, { diff --git a/src/Manifest.cc b/src/Manifest.cc index 2a19bfaaf..ebcba38a2 100644 --- a/src/Manifest.cc +++ b/src/Manifest.cc @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -118,7 +119,7 @@ findManifest() { struct GitDependency { std::string name; std::string url; - Option target; + std::optional target; DepMetadata install() const; }; @@ -158,21 +159,21 @@ struct Manifest { return instance; } - Option manifestPath = None; + std::optional manifestPath = std::nullopt; - Option data = None; + std::optional data = std::nullopt; - Option package = None; - Option>> - dependencies = None; - Option>> - devDependencies = None; + std::optional package = std::nullopt; + std::optional>> + dependencies = std::nullopt; + std::optional>> + devDependencies = std::nullopt; - Option profile = None; - Option devProfile = None; - Option releaseProfile = None; + std::optional profile = std::nullopt; + std::optional devProfile = std::nullopt; + std::optional releaseProfile = std::nullopt; - Option> cpplintFilters = None; + std::optional> cpplintFilters = std::nullopt; private: Manifest() noexcept = default; @@ -199,7 +200,7 @@ getManifestPath() { } // Returns an error message if the package name is invalid. -Option // TODO: result-like types make more sense. +std::optional // TODO: result-like types make more sense. validatePackageName(const std::string_view name) noexcept { // Empty if (name.empty()) { @@ -237,7 +238,7 @@ validatePackageName(const std::string_view name) noexcept { return "must not be a C++ keyword"; } - return None; + return std::nullopt; } static Package& @@ -324,7 +325,7 @@ parseProfile(const toml::table& table) { } static Profile -getProfile(Option profileName) { +getProfile(std::optional profileName) { Manifest& manifest = Manifest::instance(); if (!manifest.data.value().contains("profile")) { return {}; @@ -357,7 +358,7 @@ getBaseProfile() { return manifest.profile.value(); } - const Profile baseProfile = getProfile(None); + const Profile baseProfile = getProfile(std::nullopt); manifest.profile = baseProfile; return manifest.profile.value(); } @@ -507,7 +508,7 @@ static GitDependency parseGitDep(const std::string& name, const toml::table& info) { validateDepName(name); std::string gitUrlStr; - Option target = None; + std::optional target = std::nullopt; const auto& gitUrl = info.at("git"); if (gitUrl.is_string()) { @@ -539,13 +540,13 @@ parseSystemDep(const std::string& name, const toml::table& info) { return { name, VersionReq::parse(versionReq) }; } -static Option>> +static std::optional>> parseDependencies(const char* key) { Manifest& manifest = Manifest::instance(); const auto& table = toml::get(manifest.data.value()); if (!table.contains(key)) { logger::debug("[dependencies] not found"); - return None; + return std::nullopt; } const auto tomlDeps = toml::find(manifest.data.value(), key); diff --git a/src/Manifest.hpp b/src/Manifest.hpp index 3e8c1cdc0..27bbb18ec 100644 --- a/src/Manifest.hpp +++ b/src/Manifest.hpp @@ -4,6 +4,7 @@ #include "Semver.hpp" #include +#include #include #include #include @@ -16,8 +17,8 @@ struct DepMetadata { struct Profile { HashSet cxxflags; bool lto = false; - Option debug = None; - Option opt_level = None; + std::optional debug = std::nullopt; + std::optional opt_level = std::nullopt; // Merges this profile with another profile. If a field in this profile is // set, it will not be overwritten by the other profile. Only default values @@ -57,7 +58,7 @@ struct Edition { }; const fs::path& getManifestPath(); -Option validatePackageName(std::string_view name) noexcept; +std::optional validatePackageName(std::string_view name) noexcept; const std::string& getPackageName(); const Edition& getPackageEdition(); const Version& getPackageVersion(); diff --git a/src/Rustify/Aliases.hpp b/src/Rustify/Aliases.hpp index 92ddea653..d9c338b12 100644 --- a/src/Rustify/Aliases.hpp +++ b/src/Rustify/Aliases.hpp @@ -4,12 +4,9 @@ #include #include #include -#include -#include #include #include #include -#include namespace fs = std::filesystem; @@ -37,31 +34,6 @@ using HashMap = std::unordered_map; template using HashSet = std::unordered_set; -template -using Option = std::optional; - -struct NoneT : protected std::monostate { - constexpr bool operator==(const usize rhs) const { - return std::string_view::npos == rhs; - } - - // NOLINTNEXTLINE(google-explicit-constructor) - constexpr operator std::nullopt_t() const { - return std::nullopt; - } - - template - constexpr operator Option() const { // NOLINT(google-explicit-constructor) - return std::nullopt; - } -}; -inline constinit const NoneT None; // NOLINT(readability-identifier-naming) - -inline std::ostream& -operator<<(std::ostream& os, const NoneT& /*unused*/) { - return os << "None"; -} - // NOLINTBEGIN(google-global-names-in-headers) using std::literals::string_literals::operator""s; using std::literals::string_view_literals::operator""sv; diff --git a/src/Rustify/Traits.hpp b/src/Rustify/Traits.hpp index 541162791..c099011ba 100644 --- a/src/Rustify/Traits.hpp +++ b/src/Rustify/Traits.hpp @@ -5,6 +5,12 @@ #include #include +inline std::ostream& +operator<<(std::ostream& os, const std::nullopt_t&) { + os << "None"; + return os; +} + template std::ostream& operator<<(std::ostream& os, const std::optional& opt) { diff --git a/src/VersionReq.hpp b/src/VersionReq.hpp index 6e2d61291..150f35d2a 100644 --- a/src/VersionReq.hpp +++ b/src/VersionReq.hpp @@ -17,14 +17,15 @@ #include "Rustify.hpp" #include "Semver.hpp" +#include #include #include #include struct OptVersion { u64 major{}; - Option minor; - Option patch; + std::optional minor; + std::optional patch; Prerelease pre; }; @@ -70,10 +71,10 @@ struct Comparator { }; using enum Op; - Option op; + std::optional op; u64 major{}; - Option minor; - Option patch; + std::optional minor; + std::optional patch; Prerelease pre; static Comparator parse(std::string_view str); @@ -86,7 +87,7 @@ struct Comparator { struct VersionReq { Comparator left; - Option right; + std::optional right; static VersionReq parse(std::string_view str); bool satisfiedBy(const Version& ver) const noexcept; diff --git a/srcOld/Cfg.hpp b/srcOld/Cfg.hpp index 40cc41690..5546ebb35 100644 --- a/srcOld/Cfg.hpp +++ b/srcOld/Cfg.hpp @@ -277,13 +277,13 @@ struct Lexer { explicit Lexer(std::string_view str) : str(str) {} - inline auto next() -> Option { + inline auto next() -> std::optional { const auto [diff, token] = tokenize(this->index); this->step_n(diff); return token; } - [[nodiscard]] inline auto peek() const -> Option { + [[nodiscard]] inline auto peek() const -> std::optional { const auto [diff, token] = tokenize(this->index); static_cast(diff); return token; @@ -291,21 +291,21 @@ struct Lexer { private: [[nodiscard]] inline auto generate_token( - SizeType index_, const Option& token - ) const -> std::pair> { + SizeType index_, const std::optional& token + ) const -> std::pair> { return { this->diff_step(index_), token }; } [[nodiscard]] inline auto generate_token( SizeType index_, std::string_view kind - ) const -> std::pair> { + ) const -> std::pair> { return generate_token(index_, Token{ to_kind(kind) }); } [[nodiscard]] auto analyze_two_phrase(SizeType index_, char kind) const - -> std::pair>; + -> std::pair>; [[nodiscard]] auto tokenize(SizeType index_ - ) const -> std::pair>; + ) const -> std::pair>; void step(SizeType& index_) const noexcept; void step_n(SizeType n) noexcept; @@ -325,12 +325,13 @@ struct Lexer { [[nodiscard]] auto ident(SizeType index_) const -> std::pair; - static auto to_ident(std::string_view s) noexcept -> Option; + static auto to_ident(std::string_view s + ) noexcept -> std::optional; }; auto Lexer::analyze_two_phrase(SizeType index_, const char kind) const - -> std::pair> { + -> std::pair> { if (this->one(index_) == '=') { this->step(index_); return generate_token(index_, std::string{ kind } + '='); @@ -341,9 +342,9 @@ Lexer::analyze_two_phrase(SizeType index_, const char kind) const auto Lexer::tokenize(SizeType index_ -) const -> std::pair> { +) const -> std::pair> { if (index_ >= this->str.size()) { - return generate_token(index_, None); + return generate_token(index_, std::nullopt); } const char one = this->one(index_); switch (one) { @@ -432,7 +433,7 @@ Lexer::ident(SizeType index_) const -> std::pair { } auto -Lexer::to_ident(std::string_view s) noexcept -> Option { +Lexer::to_ident(std::string_view s) noexcept -> std::optional { if (s == "cfg") { return Token::ident::cfg; } else if (s == "not") { @@ -454,7 +455,7 @@ Lexer::to_ident(std::string_view s) noexcept -> Option { } else if (s == "os_version") { return Token::ident::os_version; } else { - return None; + return std::nullopt; } } diff --git a/srcOld/Lockfile.hpp b/srcOld/Lockfile.hpp index 963a155ff..5da91707f 100644 --- a/srcOld/Lockfile.hpp +++ b/srcOld/Lockfile.hpp @@ -136,7 +136,8 @@ convert_to_deps(const Lockfile& lock ) -> resolver::UniqDeps { resolver::UniqDeps deps; for (const auto& package : lock.package) { - resolver::UniqDeps::mapped_type inner_deps = None; + resolver::UniqDeps::mapped_type inner_deps = + std::nullopt; if (!package.dependencies.empty()) { // When serializing lockfile, package version of inner dependencies // will be dropped (ref: `convert_to_lock` function). @@ -156,9 +157,9 @@ convert_to_deps(const Lockfile& lock [[nodiscard]] auto read(const fs::path& base_dir -) -> Result>> { +) -> Result>> { if (!fs::exists(base_dir / LOCKFILE_NAME)) { - return Ok(None); + return Ok(std::nullopt); } try { diff --git a/srcOld/Resolver.hpp b/srcOld/Resolver.hpp index 0c08b01f0..3e2c4a0fe 100644 --- a/srcOld/Resolver.hpp +++ b/srcOld/Resolver.hpp @@ -333,17 +333,17 @@ get_resolved_deps(const toml::value& manifest) -> Result { // If lockfile is not outdated, read it. [[nodiscard]] auto -try_to_read_lockfile() -> Result> { +try_to_read_lockfile() -> Result> { if (!data::lockfile::is_outdated(config::cwd)) { return data::lockfile::read(config::cwd); } else { - return Ok(None); + return Ok(std::nullopt); } } [[nodiscard]] auto resolve_deps(const toml::value& manifest) -> Result { - const Option locked_deps = Try(try_to_read_lockfile()); + const std::optional locked_deps = Try(try_to_read_lockfile()); if (locked_deps.has_value()) { // Lockfile exists and is not outdated. return Ok(locked_deps.value()); diff --git a/srcOld/Resolver/Resolve.hpp b/srcOld/Resolver/Resolve.hpp index c184b7f1e..86e0c10b5 100644 --- a/srcOld/Resolver/Resolve.hpp +++ b/srcOld/Resolver/Resolve.hpp @@ -314,7 +314,7 @@ gather_deps( const UniqDeps deps_api_res = util::net::api::deps(package.name, package.dep_info.version_rq).unwrap(); if (deps_api_res.empty()) { - new_deps.emplace_back(package, None); + new_deps.emplace_back(package, std::nullopt); } else { const DupDeps deps_of_deps = gather_deps_of_deps(deps_api_res, interval_cache); @@ -341,7 +341,7 @@ gather_all_deps(const UniqDeps& deps // We don't resolve deps of conan packages, this is defer to conan itself if (poac::util::registry::conan::v1::resolver::is_conan(package)) { - duplicate_deps.emplace_back(package, None); + duplicate_deps.emplace_back(package, std::nullopt); continue; } diff --git a/srcOld/Resolver/Types.hpp b/srcOld/Resolver/Types.hpp index ce3783e0b..be1c35c9c 100644 --- a/srcOld/Resolver/Types.hpp +++ b/srcOld/Resolver/Types.hpp @@ -84,7 +84,7 @@ struct DuplicateDeps { using Type = std::vector; }; -using Deps = Option>; +using Deps = std::optional>; template <> struct DuplicateDeps {