Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply the normal "to_string" pattern in more places. #1521

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/vcpkg/base/contractual-constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,14 @@ namespace vcpkg
inline constexpr StringLiteral AbiTagPostBuildChecks = "post_build_checks";
inline constexpr StringLiteral AbiTagPowershell = "powershell";
inline constexpr StringLiteral AbiTagPublicAbiOverride = "public_abi_override";

inline constexpr StringLiteral StatusDeinstall = "deinstall";
inline constexpr StringLiteral StatusError = "error";
inline constexpr StringLiteral StatusHalfInstalled = "half-installed";
inline constexpr StringLiteral StatusHold = "hold";
inline constexpr StringLiteral StatusInstall = "install";
inline constexpr StringLiteral StatusInstalled = "installed";
inline constexpr StringLiteral StatusNotInstalled = "not-installed";
inline constexpr StringLiteral StatusPurge = "purge";
inline constexpr StringLiteral StatusUnknown = "unknown";
}
8 changes: 8 additions & 0 deletions include/vcpkg/base/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ VCPKG_MSVC_WARNING(disable : 6239 4702)
#include <fmt/format.h>
#include <fmt/ranges.h>
VCPKG_MSVC_WARNING(pop)

template<class T>
std::string adapt_to_string(const T& val)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a constexpr functor object?

constexpr struct AdaptToString
{
    template<class T>
    std::string operator()(const T& val) {
        std::string result;
        val.to_string(result);
        return result;
    }
} adapt_to_string;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe anything else would want the name adapt_to_string to require such ADL defenses?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, to put this another way: Maybe. To accomplish what?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it "better" when used in functional style like .map(adapt_to_string)

{
std::string result;
val.to_string(result);
return result;
}
1 change: 0 additions & 1 deletion include/vcpkg/base/graphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace vcpkg
struct AdjacencyProvider
{
virtual std::vector<V> adjacency_list(const U& vertex) const = 0;
virtual std::string to_string(const V& vertex) const = 0;
virtual U load_vertex_data(const V& vertex) const = 0;
};

Expand Down
4 changes: 1 addition & 3 deletions include/vcpkg/binaryparagraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

namespace vcpkg
{
/// <summary>
/// Built package metadata
/// </summary>
// metadata for a package in the 'packages' tree
struct BinaryParagraph
{
BinaryParagraph() = default;
Expand Down
18 changes: 9 additions & 9 deletions include/vcpkg/statusparagraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

namespace vcpkg
{
/// <summary>
/// Installed package metadata
/// </summary>

// metadata for a package's representation in the 'installed' tree
struct StatusParagraph
{
StatusParagraph() noexcept;
Expand All @@ -30,14 +29,12 @@ namespace vcpkg

void serialize(const StatusParagraph& pgh, std::string& out_str);

std::string to_string(InstallState f);

std::string to_string(Want f);
StringLiteral to_string_literal(InstallState f);
StringLiteral to_string_literal(Want f);

struct InstalledPackageView
{
InstalledPackageView() noexcept : core(nullptr) { }

InstalledPackageView() = default;
InstalledPackageView(const StatusParagraph* c, std::vector<const StatusParagraph*>&& fs)
: core(c), features(std::move(fs))
{
Expand All @@ -51,11 +48,14 @@ namespace vcpkg

std::vector<StatusParagraph> all_status_paragraphs() const;

const StatusParagraph* core;
const StatusParagraph* core = nullptr;
std::vector<const StatusParagraph*> features;
};

Json::Value serialize_ipv(const InstalledPackageView& ipv,
const InstalledPaths& installed,
const ReadOnlyFilesystem& fs);
}

VCPKG_FORMAT_WITH_TO_STRING_LITERAL_NONMEMBER(vcpkg::InstallState);
VCPKG_FORMAT_WITH_TO_STRING_LITERAL_NONMEMBER(vcpkg::Want);
6 changes: 4 additions & 2 deletions include/vcpkg/versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace vcpkg
VersionDiff(const Version& left, const Version& right);

std::string to_string() const;
void to_string(std::string& out) const;
};

struct VersionMapLess
Expand Down Expand Up @@ -85,6 +86,7 @@ namespace vcpkg
VersionSpec(const std::string& port_name, const std::string& version_string, int port_version);

std::string to_string() const;
void to_string(std::string& out) const;

friend bool operator==(const VersionSpec& lhs, const VersionSpec& rhs);
friend bool operator!=(const VersionSpec& lhs, const VersionSpec& rhs);
Expand All @@ -97,7 +99,7 @@ namespace vcpkg

struct DotVersion
{
DotVersion() { } // intentionally disable making this type an aggregate
DotVersion() noexcept { } // intentionally disable making this type an aggregate

std::string original_string;
std::string version_string;
Expand All @@ -122,7 +124,7 @@ namespace vcpkg

struct DateVersion
{
DateVersion() { } // intentionally disable making this type an aggregate
DateVersion() noexcept { } // intentionally disable making this type an aggregate

std::string original_string;
std::string version_string;
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/binaryparagraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ namespace vcpkg
return fmt::format(
"\nspec: \"{}\"\nversion: \"{}\"\nport_version: {}\ndescription: [\"{}\"]\nmaintainers: [\"{}\"]\nfeature: "
"\"{}\"\ndefault_features: [\"{}\"]\ndependencies: [\"{}\"]\nabi: \"{}\"",
paragraph.spec.to_string(),
paragraph.spec,
paragraph.version.text,
paragraph.version.port_version,
Strings::join(join_str, paragraph.description),
Expand Down
10 changes: 5 additions & 5 deletions src/vcpkg/commands.ci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ namespace
(void)filesystem.create_directory(target_path, VCPKG_LINE_INFO);
if (children.empty())
{
std::string message =
"There are no build logs for " + spec.to_string() +
" build.\n"
"This is usually because the build failed early and outside of a task that is logged.\n"
"See the console output logs from vcpkg for more information on the failure.\n";
auto message =
fmt::format("There are no build logs for {} build.\n"
"This is usually because the build failed early and outside of a task that is logged.\n"
"See the console output logs from vcpkg for more information on the failure.\n",
spec);
filesystem.write_contents(std::move(target_path) / "readme.log", message, VCPKG_LINE_INFO);
}
else
Expand Down
3 changes: 1 addition & 2 deletions src/vcpkg/commands.export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,7 @@ namespace
Checks::unreachable(VCPKG_LINE_INFO);
}

const std::string display_name = action.spec.to_string();
msg::println(msgExportingPackage, msg::package_name = display_name);
msg::println(msgExportingPackage, msg::package_name = action.spec);

const BinaryParagraph& binary_paragraph = action.core_paragraph().value_or_exit(VCPKG_LINE_INFO);

Expand Down
9 changes: 5 additions & 4 deletions src/vcpkg/commands.list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ namespace
Json::Object obj;
for (const StatusParagraph* status_paragraph : installed_packages)
{
auto current_spec = status_paragraph->package.spec;
if (obj.contains(current_spec.to_string()))
const auto& current_spec = status_paragraph->package.spec;
const auto current_spec_string = current_spec.to_string();
if (obj.contains(current_spec_string))
{
if (status_paragraph->package.is_feature())
{
Json::Value* value_obj = obj.get(current_spec.to_string());
Json::Value* value_obj = obj.get(current_spec_string);
auto& feature_list = value_obj->object(VCPKG_LINE_INFO)[JsonIdFeatures].array(VCPKG_LINE_INFO);
feature_list.push_back(Json::Value::string(status_paragraph->package.feature));
}
}
else
{
Json::Object& library_obj = obj.insert(current_spec.to_string(), Json::Object());
Json::Object& library_obj = obj.insert(current_spec_string, Json::Object());
library_obj.insert(JsonIdPackageUnderscoreName, Json::Value::string(current_spec.name()));
library_obj.insert(JsonIdTriplet, Json::Value::string(current_spec.triplet().to_string()));
library_obj.insert(JsonIdVersion, Json::Value::string(status_paragraph->package.version.text));
Expand Down
5 changes: 0 additions & 5 deletions src/vcpkg/dependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,6 @@ namespace vcpkg
}

PackageSpec load_vertex_data(const PackageSpec& s) const override { return s; }

std::string to_string(const PackageSpec& spec) const override { return spec.to_string(); }
};

RemoveAdjacencyProvider p;
Expand Down Expand Up @@ -746,8 +744,6 @@ namespace vcpkg

return ExportPlanAction{spec, request_type};
}

std::string to_string(const PackageSpec& spec) const override { return spec.to_string(); }
};

const std::unordered_set<PackageSpec> specs_as_set(specs.cbegin(), specs.cend());
Expand Down Expand Up @@ -997,7 +993,6 @@ namespace vcpkg
{
BaseEdgeProvider(const ClusterGraph& parent) : m_parent(parent) { }

std::string to_string(const PackageSpec& spec) const override { return spec.to_string(); }
const Cluster* load_vertex_data(const PackageSpec& spec) const override
{
return &m_parent.find_or_exit(spec, VCPKG_LINE_INFO);
Expand Down
7 changes: 1 addition & 6 deletions src/vcpkg/export.prefab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ namespace vcpkg::Prefab
return paths;
}

std::string NdkVersion::to_string() const
{
std::string ret;
this->to_string(ret);
return ret;
}
std::string NdkVersion::to_string() const { return adapt_to_string(*this); }
void NdkVersion::to_string(std::string& out) const
{
out.append("NdkVersion{major=")
Expand Down
7 changes: 1 addition & 6 deletions src/vcpkg/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,7 @@ namespace vcpkg
last_completed_survey);
}

std::string MetricsUserConfig::to_string() const
{
std::string ret;
to_string(ret);
return ret;
}
std::string MetricsUserConfig::to_string() const { return adapt_to_string(*this); }

void MetricsUserConfig::try_write(const Filesystem& fs) const
{
Expand Down
9 changes: 2 additions & 7 deletions src/vcpkg/packagespec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@ namespace

namespace vcpkg
{
std::string FeatureSpec::to_string() const
{
std::string ret;
this->to_string(ret);
return ret;
}
std::string FeatureSpec::to_string() const { return adapt_to_string(*this); }
void FeatureSpec::to_string(std::string& out) const
{
if (feature().empty()) return spec().to_string(out);
Expand Down Expand Up @@ -130,7 +125,7 @@ namespace vcpkg

std::string PackageSpec::dir() const { return fmt::format("{}_{}", this->m_name, this->m_triplet); }

std::string PackageSpec::to_string() const { return fmt::format("{}:{}", this->name(), this->triplet()); }
std::string PackageSpec::to_string() const { return adapt_to_string(*this); }
void PackageSpec::to_string(std::string& s) const
{
fmt::format_to(std::back_inserter(s), "{}:{}", this->name(), this->triplet());
Expand Down
22 changes: 11 additions & 11 deletions src/vcpkg/spdx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ static StringView extract_cmake_invocation_argument(StringView command, StringVi
}

static Json::Object make_resource(
std::string spdxid, std::string name, std::string downloadLocation, StringView sha512, StringView filename)
std::string spdxid, StringView name, std::string downloadLocation, StringView sha512, StringView filename)
{
Json::Object obj;
obj.insert(SpdxSpdxId, std::move(spdxid));
obj.insert(JsonIdName, std::move(name));
obj.insert(JsonIdName, name.to_string());
if (!filename.empty())
{
obj.insert(SpdxPackageFileName, filename);
Expand Down Expand Up @@ -97,7 +97,7 @@ Json::Value vcpkg::run_resource_heuristics(StringView contents, StringView versi
auto sha = extract_cmake_invocation_argument(github, CMakeVariableSHA512);

packages.push_back(make_resource(fmt::format("SPDXRef-resource-{}", ++n),
repo.to_string(),
repo,
fmt::format("git+https://github.com/{}@{}", repo, ref),
sha,
{}));
Expand All @@ -107,17 +107,17 @@ Json::Value vcpkg::run_resource_heuristics(StringView contents, StringView versi
{
auto url = extract_cmake_invocation_argument(github, CMakeVariableUrl);
auto ref = fix_ref_version(extract_cmake_invocation_argument(github, CMakeVariableRef), version_text);
packages.push_back(make_resource(
fmt::format("SPDXRef-resource-{}", ++n), url.to_string(), fmt::format("git+{}@{}", url, ref), {}, {}));
packages.push_back(
make_resource(fmt::format("SPDXRef-resource-{}", ++n), url, fmt::format("git+{}@{}", url, ref), {}, {}));
}
auto distfile = find_cmake_invocation(contents, "vcpkg_download_distfile");
if (!distfile.empty())
{
auto url = extract_cmake_invocation_argument(distfile, CMakeVariableUrls);
auto filename = extract_cmake_invocation_argument(distfile, CMakeVariableFilename);
auto sha = extract_cmake_invocation_argument(distfile, CMakeVariableSHA512);
packages.push_back(make_resource(
fmt::format("SPDXRef-resource-{}", ++n), filename.to_string(), url.to_string(), sha, filename));
packages.push_back(
make_resource(fmt::format("SPDXRef-resource-{}", ++n), filename, url.to_string(), sha, filename));
}
auto sfg = find_cmake_invocation(contents, "vcpkg_from_sourceforge");
if (!sfg.empty())
Expand All @@ -126,9 +126,9 @@ Json::Value vcpkg::run_resource_heuristics(StringView contents, StringView versi
auto ref = fix_ref_version(extract_cmake_invocation_argument(sfg, CMakeVariableRef), version_text);
auto filename = extract_cmake_invocation_argument(sfg, CMakeVariableFilename);
auto sha = extract_cmake_invocation_argument(sfg, CMakeVariableSHA512);
auto url = Strings::concat("https://sourceforge.net/projects/", repo, "/files/", ref, '/', filename);
packages.push_back(make_resource(
fmt::format("SPDXRef-resource-{}", ++n), filename.to_string(), std::move(url), sha, filename));
auto url = fmt::format("https://sourceforge.net/projects/{}/files/{}/{}", repo, ref, filename);
packages.push_back(
make_resource(fmt::format("SPDXRef-resource-{}", ++n), filename, std::move(url), sha, filename));
}
return Json::Value::object(std::move(ret));
}
Expand Down Expand Up @@ -156,7 +156,7 @@ std::string vcpkg::create_spdx_sbom(const InstallPlanAction& action,
doc.insert(SpdxDataLicense, SpdxCCZero);
doc.insert(SpdxSpdxId, SpdxRefDocument);
doc.insert(SpdxDocumentNamespace, std::move(document_namespace));
doc.insert(JsonIdName, Strings::concat(action.spec.to_string(), '@', cpgh.version.to_string(), ' ', abi));
doc.insert(JsonIdName, fmt::format("{}@{} {}", action.spec, cpgh.version, abi));
{
auto& cinfo = doc.insert(SpdxCreationInfo, Json::Object());
auto& creators = cinfo.insert(JsonIdCreators, Json::Array());
Expand Down
Loading
Loading