Skip to content

Commit

Permalink
Remove Option alias (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui authored Jul 23, 2024
1 parent 75451c2 commit 594441d
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 102 deletions.
15 changes: 9 additions & 6 deletions src/Algos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <algorithm>
#include <cctype>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <thread>
Expand Down Expand Up @@ -145,7 +146,7 @@ equalsInsensitive(
);
}

Option<std::string_view>
std::optional<std::string_view>
findSimilarStr(
const std::string_view lhs, std::span<const std::string_view> candidates
) {
Expand All @@ -163,7 +164,7 @@ findSimilarStr(
const usize length = lhs.size();
const usize maxDist = length < 3 ? length - 1 : length / 3;

Option<std::pair<std::string_view, usize>> similarStr = None;
std::optional<std::pair<std::string_view, usize>> similarStr = std::nullopt;
for (const std::string_view str : candidates) {
const usize curDist = levDistance(lhs, str);
if (curDist <= maxDist) {
Expand All @@ -177,7 +178,7 @@ findSimilarStr(
if (similarStr.has_value()) {
return similarStr->first;
} else {
return None;
return std::nullopt;
}
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -256,7 +259,7 @@ void
testFindSimilarStr2() {
constexpr std::array<std::string_view, 2> candidates{ "aaab", "aaabc" };
assertEq(findSimilarStr("aaaa", candidates), "aaab"sv);
assertEq(findSimilarStr("1111111111", candidates), None);
assertEq(findSimilarStr("1111111111", candidates), std::nullopt);

constexpr std::array<std::string_view, 1> candidateS2{ "AAAA" };
assertEq(findSimilarStr("aaaa", candidateS2), "AAAA"sv);
Expand Down
5 changes: 3 additions & 2 deletions src/Algos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <iostream>
#include <list>
#include <memory>
#include <optional>
#include <queue>
#include <span>
#include <sstream>
Expand Down Expand Up @@ -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<std::string_view> findSimilarStr(
/// returns std::nullopt.
std::optional<std::string_view> findSimilarStr(
std::string_view lhs, std::span<const std::string_view> candidates
);
15 changes: 8 additions & 7 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <iomanip>
#include <iostream>
#include <memory>
#include <optional>
#include <ostream>
#include <span>
#include <sstream>
Expand Down Expand Up @@ -89,7 +90,7 @@ operator<<(std::ostream& os, const Variable& var) {

struct Target {
std::vector<std::string> commands;
Option<std::string> sourceFile;
std::optional<std::string> sourceFile;
HashSet<std::string> remDeps;
};

Expand All @@ -101,8 +102,8 @@ struct BuildConfig {
HashMap<std::string, std::vector<std::string>> varDeps;
HashMap<std::string, Target> targets;
HashMap<std::string, std::vector<std::string>> targetDeps;
Option<HashSet<std::string>> phony;
Option<HashSet<std::string>> all;
std::optional<HashSet<std::string>> phony;
std::optional<HashSet<std::string>> all;

std::string OUT_DIR;
std::string CXX = "clang++";
Expand Down Expand Up @@ -155,7 +156,7 @@ struct BuildConfig {
void defineTarget(
const std::string& name, const std::vector<std::string>& commands,
const HashSet<std::string>& remDeps = {},
const Option<std::string>& sourceFile = None
const std::optional<std::string>& sourceFile = std::nullopt
) {
targets[name] = { commands, sourceFile, remDeps };

Expand Down Expand Up @@ -212,7 +213,7 @@ static void
emitTarget(
std::ostream& os, const std::string_view target,
const HashSet<std::string>& dependsOn,
const Option<std::string>& sourceFile = None,
const std::optional<std::string>& sourceFile = std::nullopt,
const std::vector<std::string>& commands = {}
) {
usize offset = 0;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions src/Cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstdlib>
#include <functional>
#include <iterator>
#include <optional>
#include <span>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -142,7 +143,7 @@ class Subcmd : public CliBase<Subcmd>, public ShortAndHidden<Subcmd> {
friend class Cli;

std::string_view cmdName;
Option<std::vector<Opt>> globalOpts = None;
std::optional<std::vector<Opt>> globalOpts = std::nullopt;
std::vector<Opt> localOpts;
Arg arg;
std::function<int(std::span<const std::string_view>)> mainFn;
Expand Down Expand Up @@ -204,11 +205,12 @@ class Cli : public CliBase<Cli> {

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<int> handleGlobalOpts(
[[nodiscard]] static inline std::optional<int> handleGlobalOpts(
std::forward_iterator auto& itr, const std::forward_iterator auto end,
std::string_view subcmd = ""
) {
Expand Down Expand Up @@ -237,7 +239,7 @@ class Cli : public CliBase<Cli> {
return EXIT_FAILURE;
}
}
return None;
return std::nullopt;
}

private:
Expand Down
9 changes: 5 additions & 4 deletions src/Cmd/Add.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstdlib>
#include <fstream>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
#include <toml.hpp>
Expand Down Expand Up @@ -36,7 +37,7 @@ const Subcmd ADD_CMD =
.setPlaceholder("<BRANCH_NAME>"))
.setMainFn(addMain);

static Option<int>
static std::optional<int>
handleNextArg(
std::span<const std::string_view>::iterator& itr,
const std::span<const std::string_view>::iterator& end, std::string& arg
Expand All @@ -46,7 +47,7 @@ handleNextArg(
return Subcmd::missingArgumentForOpt(*--itr);
}
arg = std::string(*itr);
return None;
return std::nullopt;
}

static void
Expand Down Expand Up @@ -167,13 +168,13 @@ addMain(const std::span<const std::string_view> args) {
// clang-format off
HashMap<
std::string_view,
std::function<Option<int>(decltype(args)::iterator&, decltype(args)::iterator)>
std::function<std::optional<int>(decltype(args)::iterator&, decltype(args)::iterator)>
>
handlers = {
{
"--sys", [&](auto&, auto) {
isSystemDependency = true;
return None;
return std::nullopt;
}
},
{
Expand Down
39 changes: 20 additions & 19 deletions src/Manifest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <cctype>
#include <cstdlib>
#include <optional>
#include <string>
#include <string_view>
#include <variant>
Expand Down Expand Up @@ -118,7 +119,7 @@ findManifest() {
struct GitDependency {
std::string name;
std::string url;
Option<std::string> target;
std::optional<std::string> target;

DepMetadata install() const;
};
Expand Down Expand Up @@ -158,21 +159,21 @@ struct Manifest {
return instance;
}

Option<fs::path> manifestPath = None;
std::optional<fs::path> manifestPath = std::nullopt;

Option<toml::value> data = None;
std::optional<toml::value> data = std::nullopt;

Option<Package> package = None;
Option<std::vector<std::variant<GitDependency, SystemDependency>>>
dependencies = None;
Option<std::vector<std::variant<GitDependency, SystemDependency>>>
devDependencies = None;
std::optional<Package> package = std::nullopt;
std::optional<std::vector<std::variant<GitDependency, SystemDependency>>>
dependencies = std::nullopt;
std::optional<std::vector<std::variant<GitDependency, SystemDependency>>>
devDependencies = std::nullopt;

Option<Profile> profile = None;
Option<Profile> devProfile = None;
Option<Profile> releaseProfile = None;
std::optional<Profile> profile = std::nullopt;
std::optional<Profile> devProfile = std::nullopt;
std::optional<Profile> releaseProfile = std::nullopt;

Option<std::vector<std::string>> cpplintFilters = None;
std::optional<std::vector<std::string>> cpplintFilters = std::nullopt;

private:
Manifest() noexcept = default;
Expand All @@ -199,7 +200,7 @@ getManifestPath() {
}

// Returns an error message if the package name is invalid.
Option<std::string> // TODO: result-like types make more sense.
std::optional<std::string> // TODO: result-like types make more sense.
validatePackageName(const std::string_view name) noexcept {
// Empty
if (name.empty()) {
Expand Down Expand Up @@ -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&
Expand Down Expand Up @@ -324,7 +325,7 @@ parseProfile(const toml::table& table) {
}

static Profile
getProfile(Option<std::string> profileName) {
getProfile(std::optional<std::string> profileName) {
Manifest& manifest = Manifest::instance();
if (!manifest.data.value().contains("profile")) {
return {};
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -507,7 +508,7 @@ static GitDependency
parseGitDep(const std::string& name, const toml::table& info) {
validateDepName(name);
std::string gitUrlStr;
Option<std::string> target = None;
std::optional<std::string> target = std::nullopt;

const auto& gitUrl = info.at("git");
if (gitUrl.is_string()) {
Expand Down Expand Up @@ -539,13 +540,13 @@ parseSystemDep(const std::string& name, const toml::table& info) {
return { name, VersionReq::parse(versionReq) };
}

static Option<std::vector<std::variant<GitDependency, SystemDependency>>>
static std::optional<std::vector<std::variant<GitDependency, SystemDependency>>>
parseDependencies(const char* key) {
Manifest& manifest = Manifest::instance();
const auto& table = toml::get<toml::table>(manifest.data.value());
if (!table.contains(key)) {
logger::debug("[dependencies] not found");
return None;
return std::nullopt;
}
const auto tomlDeps = toml::find<toml::table>(manifest.data.value(), key);

Expand Down
7 changes: 4 additions & 3 deletions src/Manifest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Semver.hpp"

#include <compare>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
Expand All @@ -16,8 +17,8 @@ struct DepMetadata {
struct Profile {
HashSet<std::string> cxxflags;
bool lto = false;
Option<bool> debug = None;
Option<usize> opt_level = None;
std::optional<bool> debug = std::nullopt;
std::optional<usize> 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
Expand Down Expand Up @@ -57,7 +58,7 @@ struct Edition {
};

const fs::path& getManifestPath();
Option<std::string> validatePackageName(std::string_view name) noexcept;
std::optional<std::string> validatePackageName(std::string_view name) noexcept;
const std::string& getPackageName();
const Edition& getPackageEdition();
const Version& getPackageVersion();
Expand Down
28 changes: 0 additions & 28 deletions src/Rustify/Aliases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <optional>
#include <ostream>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <variant>

namespace fs = std::filesystem;

Expand Down Expand Up @@ -37,31 +34,6 @@ using HashMap = std::unordered_map<K, V>;
template <typename K>
using HashSet = std::unordered_set<K>;

template <typename T>
using Option = std::optional<T>;

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 <typename T>
constexpr operator Option<T>() 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;
Expand Down
Loading

0 comments on commit 594441d

Please sign in to comment.