mirror of https://github.com/microsoft/vcpkg.git
[vcpkg] Remove OptBool in favor of Optional<bool>
This commit is contained in:
parent
5419aebcfe
commit
cbc52bc6a4
|
|
@ -1,48 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace vcpkg
|
||||
{
|
||||
struct OptBool final
|
||||
{
|
||||
enum class BackingEnum
|
||||
{
|
||||
UNSPECIFIED = 0,
|
||||
ENABLED,
|
||||
DISABLED
|
||||
};
|
||||
|
||||
static OptBool parse(const std::string& s);
|
||||
|
||||
template<class T>
|
||||
static OptBool from_map(const std::map<T, std::string>& map, const T& key);
|
||||
|
||||
constexpr OptBool() : backing_enum(BackingEnum::UNSPECIFIED) {}
|
||||
constexpr explicit OptBool(BackingEnum backing_enum) : backing_enum(backing_enum) {}
|
||||
constexpr operator BackingEnum() const { return backing_enum; }
|
||||
|
||||
private:
|
||||
BackingEnum backing_enum;
|
||||
};
|
||||
|
||||
namespace OptBoolC
|
||||
{
|
||||
static constexpr OptBool UNSPECIFIED(OptBool::BackingEnum::UNSPECIFIED);
|
||||
static constexpr OptBool ENABLED(OptBool::BackingEnum::ENABLED);
|
||||
static constexpr OptBool DISABLED(OptBool::BackingEnum::DISABLED);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
OptBool OptBool::from_map(const std::map<T, std::string>& map, const T& key)
|
||||
{
|
||||
auto it = map.find(key);
|
||||
if (it == map.cend())
|
||||
{
|
||||
return OptBoolC::UNSPECIFIED;
|
||||
}
|
||||
|
||||
return parse(*it);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include "OptBool.h"
|
||||
#include "PostBuildLint_BuildPolicies.h"
|
||||
#include "PostBuildLint_LinkageType.h"
|
||||
#include "filesystem_fs.h"
|
||||
|
|
@ -15,7 +14,7 @@ namespace vcpkg::PostBuildLint
|
|||
LinkageType crt_linkage;
|
||||
LinkageType library_linkage;
|
||||
|
||||
std::map<BuildPolicies, OptBool> policies;
|
||||
std::map<BuildPolicies, bool> policies;
|
||||
};
|
||||
|
||||
BuildInfo read_build_info(const Files::Filesystem& fs, const fs::path& filepath);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "OptBool.h"
|
||||
#include "vcpkg_optional.h"
|
||||
#include <memory>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
|
@ -14,9 +14,9 @@ namespace vcpkg
|
|||
|
||||
std::unique_ptr<std::string> vcpkg_root_dir;
|
||||
std::unique_ptr<std::string> triplet;
|
||||
OptBool debug = OptBoolC::UNSPECIFIED;
|
||||
OptBool sendmetrics = OptBoolC::UNSPECIFIED;
|
||||
OptBool printmetrics = OptBoolC::UNSPECIFIED;
|
||||
Optional<bool> debug = nullopt;
|
||||
Optional<bool> sendmetrics = nullopt;
|
||||
Optional<bool> printmetrics = nullopt;
|
||||
|
||||
std::string command;
|
||||
std::vector<std::string> command_arguments;
|
||||
|
|
|
|||
|
|
@ -64,4 +64,29 @@ namespace vcpkg
|
|||
bool m_is_present;
|
||||
T m_t;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
bool operator==(const Optional<T>& o, const T& t)
|
||||
{
|
||||
if (auto p = o.get()) return *p == t;
|
||||
return false;
|
||||
}
|
||||
template<class T>
|
||||
bool operator==(const T& t, const Optional<T>& o)
|
||||
{
|
||||
if (auto p = o.get()) return t == *p;
|
||||
return false;
|
||||
}
|
||||
template<class T>
|
||||
bool operator!=(const Optional<T>& o, const T& t)
|
||||
{
|
||||
if (auto p = o.get()) return *p != t;
|
||||
return true;
|
||||
}
|
||||
template<class T>
|
||||
bool operator!=(const T& t, const Optional<T>& o)
|
||||
{
|
||||
if (auto p = o.get()) return t != *p;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
#include "pch.h"
|
||||
|
||||
#include "OptBool.h"
|
||||
#include "vcpkg_Checks.h"
|
||||
|
||||
namespace vcpkg
|
||||
{
|
||||
static const std::string UNSPECIFIED_NAME = "unspecified";
|
||||
static const std::string ENABLED_NAME = "enabled";
|
||||
static const std::string DISABLED_NAME = "disabled";
|
||||
|
||||
OptBool OptBool::parse(const std::string& s)
|
||||
{
|
||||
if (s == UNSPECIFIED_NAME)
|
||||
{
|
||||
return OptBoolC::UNSPECIFIED;
|
||||
}
|
||||
|
||||
if (s == ENABLED_NAME)
|
||||
{
|
||||
return OptBoolC::ENABLED;
|
||||
}
|
||||
|
||||
if (s == DISABLED_NAME)
|
||||
{
|
||||
return OptBoolC::DISABLED;
|
||||
}
|
||||
|
||||
Checks::exit_with_message(VCPKG_LINE_INFO, "Could not convert string [%s] to OptBool", s);
|
||||
}
|
||||
}
|
||||
|
|
@ -59,19 +59,16 @@ namespace vcpkg::PostBuildLint
|
|||
}
|
||||
|
||||
template<class T>
|
||||
static bool contains_and_enabled(const std::map<T, OptBool> map, const T& key)
|
||||
static bool contains_and_enabled(const std::map<T, bool> map, const T& key)
|
||||
{
|
||||
auto it = map.find(key);
|
||||
if (it != map.cend() && it->second == OptBoolC::ENABLED)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (it != map.cend()) return it->second;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static LintStatus check_for_files_in_include_directory(const Files::Filesystem& fs,
|
||||
const std::map<BuildPolicies, OptBool>& policies,
|
||||
const std::map<BuildPolicies, bool>& policies,
|
||||
const fs::path& package_dir)
|
||||
{
|
||||
if (contains_and_enabled(policies, BuildPoliciesC::EMPTY_INCLUDE_FOLDER))
|
||||
|
|
@ -495,14 +492,13 @@ namespace vcpkg::PostBuildLint
|
|||
return LintStatus::ERROR_DETECTED;
|
||||
}
|
||||
|
||||
static LintStatus check_lib_files_are_available_if_dlls_are_available(
|
||||
const std::map<BuildPolicies, OptBool>& policies,
|
||||
const size_t lib_count,
|
||||
const size_t dll_count,
|
||||
const fs::path& lib_dir)
|
||||
static LintStatus check_lib_files_are_available_if_dlls_are_available(const std::map<BuildPolicies, bool>& policies,
|
||||
const size_t lib_count,
|
||||
const size_t dll_count,
|
||||
const fs::path& lib_dir)
|
||||
{
|
||||
auto it = policies.find(BuildPoliciesC::DLLS_WITHOUT_LIBS);
|
||||
if (it != policies.cend() && it->second == OptBoolC::ENABLED)
|
||||
if (it != policies.cend() && it->second)
|
||||
{
|
||||
return LintStatus::SUCCESS;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
#include "pch.h"
|
||||
|
||||
#include "OptBool.h"
|
||||
#include "Paragraphs.h"
|
||||
#include "PostBuildLint_BuildInfo.h"
|
||||
#include "vcpkg_Checks.h"
|
||||
#include "vcpkg_optional.h"
|
||||
#include "vcpkglib_helpers.h"
|
||||
|
||||
namespace vcpkg::PostBuildLint
|
||||
|
|
@ -39,8 +39,12 @@ namespace vcpkg::PostBuildLint
|
|||
const BuildPolicies policy = BuildPolicies::parse(p.first);
|
||||
Checks::check_exit(
|
||||
VCPKG_LINE_INFO, policy != BuildPoliciesC::NULLVALUE, "Unknown policy found: %s", p.first);
|
||||
const OptBool status = OptBool::parse(p.second);
|
||||
build_info.policies.emplace(policy, status);
|
||||
if (p.second == "enabled")
|
||||
build_info.policies.emplace(policy, true);
|
||||
else if (p.second == "disabled")
|
||||
build_info.policies.emplace(policy, false);
|
||||
else
|
||||
Checks::exit_with_message(VCPKG_LINE_INFO, "Unknown setting for policy '%s': %s", p.first, p.second);
|
||||
}
|
||||
|
||||
return build_info;
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ namespace vcpkg
|
|||
option_field = std::make_unique<std::string>(*arg_begin);
|
||||
}
|
||||
|
||||
static void parse_switch(OptBool new_setting, const std::string& option_name, OptBool& option_field)
|
||||
static void parse_switch(bool new_setting, const std::string& option_name, Optional<bool>& option_field)
|
||||
{
|
||||
if (option_field != OptBoolC::UNSPECIFIED && option_field != new_setting)
|
||||
if (option_field && option_field != new_setting)
|
||||
{
|
||||
System::println(System::Color::error, "Error: conflicting values specified for --%s", option_name);
|
||||
Metrics::track_property("error", "error conflicting switches");
|
||||
|
|
@ -94,27 +94,27 @@ namespace vcpkg
|
|||
}
|
||||
if (arg == "--debug")
|
||||
{
|
||||
parse_switch(OptBoolC::ENABLED, "debug", args.debug);
|
||||
parse_switch(true, "debug", args.debug);
|
||||
continue;
|
||||
}
|
||||
if (arg == "--sendmetrics")
|
||||
{
|
||||
parse_switch(OptBoolC::ENABLED, "sendmetrics", args.sendmetrics);
|
||||
parse_switch(true, "sendmetrics", args.sendmetrics);
|
||||
continue;
|
||||
}
|
||||
if (arg == "--printmetrics")
|
||||
{
|
||||
parse_switch(OptBoolC::ENABLED, "printmetrics", args.printmetrics);
|
||||
parse_switch(true, "printmetrics", args.printmetrics);
|
||||
continue;
|
||||
}
|
||||
if (arg == "--no-sendmetrics")
|
||||
{
|
||||
parse_switch(OptBoolC::DISABLED, "sendmetrics", args.sendmetrics);
|
||||
parse_switch(false, "sendmetrics", args.sendmetrics);
|
||||
continue;
|
||||
}
|
||||
if (arg == "--no-printmetrics")
|
||||
{
|
||||
parse_switch(OptBoolC::DISABLED, "printmetrics", args.printmetrics);
|
||||
parse_switch(false, "printmetrics", args.printmetrics);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,9 +17,9 @@ namespace UnitTest1
|
|||
std::vector<std::string> t = {"--vcpkg-root", "C:\\vcpkg", "--debug", "--sendmetrics", "--printmetrics"};
|
||||
auto v = VcpkgCmdArguments::create_from_arg_sequence(t.data(), t.data() + t.size());
|
||||
Assert::AreEqual("C:\\vcpkg", v.vcpkg_root_dir.get()->c_str());
|
||||
Assert::IsTrue(vcpkg::OptBoolC::ENABLED == v.debug);
|
||||
Assert::IsTrue(vcpkg::OptBoolC::ENABLED == v.sendmetrics);
|
||||
Assert::IsTrue(vcpkg::OptBoolC::ENABLED == v.printmetrics);
|
||||
Assert::IsTrue(v.debug && *v.debug.get());
|
||||
Assert::IsTrue(v.sendmetrics && v.sendmetrics.get());
|
||||
Assert::IsTrue(v.printmetrics && *v.printmetrics.get());
|
||||
}
|
||||
|
||||
TEST_METHOD(create_from_arg_sequence_options_upper)
|
||||
|
|
@ -27,9 +27,9 @@ namespace UnitTest1
|
|||
std::vector<std::string> t = {"--VCPKG-ROOT", "C:\\vcpkg", "--DEBUG", "--SENDMETRICS", "--PRINTMETRICS"};
|
||||
auto v = VcpkgCmdArguments::create_from_arg_sequence(t.data(), t.data() + t.size());
|
||||
Assert::AreEqual("C:\\vcpkg", v.vcpkg_root_dir.get()->c_str());
|
||||
Assert::IsTrue(vcpkg::OptBoolC::ENABLED == v.debug);
|
||||
Assert::IsTrue(vcpkg::OptBoolC::ENABLED == v.sendmetrics);
|
||||
Assert::IsTrue(vcpkg::OptBoolC::ENABLED == v.printmetrics);
|
||||
Assert::IsTrue(v.debug && *v.debug.get());
|
||||
Assert::IsTrue(v.sendmetrics && v.sendmetrics.get());
|
||||
Assert::IsTrue(v.printmetrics && *v.printmetrics.get());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -207,13 +207,9 @@ int wmain(const int argc, const wchar_t* const* const argv)
|
|||
|
||||
const VcpkgCmdArguments args = VcpkgCmdArguments::create_from_command_line(argc, argv);
|
||||
|
||||
if (args.printmetrics != OptBoolC::UNSPECIFIED) Metrics::set_print_metrics(args.printmetrics == OptBoolC::ENABLED);
|
||||
if (args.sendmetrics != OptBoolC::UNSPECIFIED) Metrics::set_send_metrics(args.sendmetrics == OptBoolC::ENABLED);
|
||||
|
||||
if (args.debug != OptBoolC::UNSPECIFIED)
|
||||
{
|
||||
g_debugging = (args.debug == OptBoolC::ENABLED);
|
||||
}
|
||||
if (auto p = args.printmetrics.get()) Metrics::set_print_metrics(*p);
|
||||
if (auto p = args.sendmetrics.get()) Metrics::set_send_metrics(*p);
|
||||
if (auto p = args.debug.get()) g_debugging = *p;
|
||||
|
||||
if (g_debugging)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -149,7 +149,6 @@
|
|||
<ClInclude Include="..\include\SortedVector.h" />
|
||||
<ClInclude Include="..\include\MachineType.h" />
|
||||
<ClInclude Include="..\include\metrics.h" />
|
||||
<ClInclude Include="..\include\OptBool.h" />
|
||||
<ClInclude Include="..\include\PackageSpec.h" />
|
||||
<ClInclude Include="..\include\PackageSpecParseResult.h" />
|
||||
<ClInclude Include="..\include\Paragraphs.h" />
|
||||
|
|
@ -213,7 +212,6 @@
|
|||
<ClCompile Include="..\src\commands_version.cpp" />
|
||||
<ClCompile Include="..\src\MachineType.cpp" />
|
||||
<ClCompile Include="..\src\metrics.cpp" />
|
||||
<ClCompile Include="..\src\OptBool.cpp" />
|
||||
<ClCompile Include="..\src\pch.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
|
||||
|
|
|
|||
Loading…
Reference in New Issue