Rework optional<T>

This commit is contained in:
Alexander Karatarakis 2017-03-28 15:05:55 -07:00
parent 67ce764c2e
commit 42bd55e3ae
11 changed files with 154 additions and 85 deletions

View File

@ -1,5 +1,78 @@
#pragma once #pragma once
#include <memory> #include "LineInfo.h"
#include "vcpkg_Checks.h"
template<class T> namespace vcpkg
using optional = std::unique_ptr<T>; {
struct nullopt_t
{
explicit constexpr nullopt_t(int) {}
};
const static constexpr nullopt_t nullopt{ 0 };
template <class T>
class optional
{
public:
// Constructors are intentionally implicit
constexpr optional(nullopt_t) : m_is_present(false), m_t() { }
optional(const T& t) : m_is_present(true), m_t(t) { }
optional(T&& t) : m_is_present(true), m_t(std::move(t)) { }
T&& get_or_exit(const LineInfo& line_info) &&
{
this->exit_if_null(line_info);
return std::move(this->m_t);
}
const T& get_or_exit(const LineInfo& line_info) const &
{
this->exit_if_null(line_info);
return this->m_t;
}
constexpr explicit operator bool() const
{
return this->m_is_present;
}
constexpr bool has_value() const
{
return m_is_present;
}
template <class U>
T value_or(U&& default_value) const &
{
return bool(*this) ? this->m_t : static_cast<T>(std::forward<U>(default_value));
}
template <class U>
T value_or(U&& default_value) &&
{
return bool(*this) ? std::move(this->m_t) : static_cast<T>(std::forward<U>(default_value));
}
const T* get() const
{
return bool(*this) ? &this->m_t : nullptr;
}
T* get()
{
return bool(*this) ? &this->m_t : nullptr;
}
private:
void exit_if_null(const LineInfo& line_info) const
{
Checks::check_exit(line_info, this->m_is_present, "Value was null");
}
bool m_is_present;
T m_t;
};
}

View File

@ -62,7 +62,11 @@ namespace vcpkg::Commands::CI
} }
else if (action.plan.plan_type == install_plan_type::BUILD_AND_INSTALL) else if (action.plan.plan_type == install_plan_type::BUILD_AND_INSTALL)
{ {
const BuildResult result = Commands::Build::build_package(*action.plan.source_pgh, action.spec, paths, paths.port_dir(action.spec), status_db); const BuildResult result = Commands::Build::build_package(action.plan.source_pgh.get_or_exit(VCPKG_LINE_INFO),
action.spec,
paths,
paths.port_dir(action.spec),
status_db);
timing.back() = build_timer.elapsed<std::chrono::milliseconds>().count(); timing.back() = build_timer.elapsed<std::chrono::milliseconds>().count();
results.back() = result; results.back() = result;
if (result != BuildResult::SUCCEEDED) if (result != BuildResult::SUCCEEDED)
@ -77,7 +81,7 @@ namespace vcpkg::Commands::CI
else if (action.plan.plan_type == install_plan_type::INSTALL) else if (action.plan.plan_type == install_plan_type::INSTALL)
{ {
results.back() = BuildResult::SUCCEEDED; results.back() = BuildResult::SUCCEEDED;
Install::install_package(paths, *action.plan.binary_pgh, &status_db); Install::install_package(paths, action.plan.binary_pgh.get_or_exit(VCPKG_LINE_INFO), &status_db);
System::println(System::color::success, "Package %s is installed from cache", action.spec); System::println(System::color::success, "Package %s is installed from cache", action.spec);
} }
else else

View File

@ -22,9 +22,9 @@ namespace vcpkg::Commands::Edit
if (env_EDITOR.empty()) if (env_EDITOR.empty())
{ {
const optional<std::wstring> env_EDITOR_optional = System::get_environmental_variable(L"EDITOR"); const optional<std::wstring> env_EDITOR_optional = System::get_environmental_variable(L"EDITOR");
if (env_EDITOR_optional) if (auto e = env_EDITOR_optional.get())
{ {
env_EDITOR = *env_EDITOR_optional; env_EDITOR = *e;
} }
} }
@ -47,16 +47,16 @@ namespace vcpkg::Commands::Edit
}; };
for (auto&& keypath : regkeys) for (auto&& keypath : regkeys)
{ {
auto code_installpath = System::get_registry_string(HKEY_LOCAL_MACHINE, keypath, L"InstallLocation"); const optional<std::wstring> code_installpath = System::get_registry_string(HKEY_LOCAL_MACHINE, keypath, L"InstallLocation");
if (code_installpath) if (auto c = code_installpath.get())
{ {
auto p = fs::path(*code_installpath) / "Code.exe"; auto p = fs::path(*c) / "Code.exe";
if (fs::exists(p)) if (fs::exists(p))
{ {
env_EDITOR = p.native(); env_EDITOR = p.native();
break; break;
} }
auto p_insiders = fs::path(*code_installpath) / "Code - Insiders.exe"; auto p_insiders = fs::path(*c) / "Code - Insiders.exe";
if (fs::exists(p_insiders)) if (fs::exists(p_insiders))
{ {
env_EDITOR = p_insiders.native(); env_EDITOR = p_insiders.native();

View File

@ -216,7 +216,11 @@ namespace vcpkg::Commands::Install
} }
else if (action.plan.plan_type == install_plan_type::BUILD_AND_INSTALL) else if (action.plan.plan_type == install_plan_type::BUILD_AND_INSTALL)
{ {
const Build::BuildResult result = Commands::Build::build_package(*action.plan.source_pgh, action.spec, paths, paths.port_dir(action.spec), status_db); const Build::BuildResult result = Commands::Build::build_package(action.plan.source_pgh.get_or_exit(VCPKG_LINE_INFO),
action.spec,
paths,
paths.port_dir(action.spec),
status_db);
if (result != Build::BuildResult::SUCCEEDED) if (result != Build::BuildResult::SUCCEEDED)
{ {
System::println(System::color::error, Build::create_error_message(result, action.spec)); System::println(System::color::error, Build::create_error_message(result, action.spec));
@ -229,7 +233,7 @@ namespace vcpkg::Commands::Install
} }
else if (action.plan.plan_type == install_plan_type::INSTALL) else if (action.plan.plan_type == install_plan_type::INSTALL)
{ {
install_package(paths, *action.plan.binary_pgh, &status_db); install_package(paths, action.plan.binary_pgh.get_or_exit(VCPKG_LINE_INFO), &status_db);
System::println(System::color::success, "Package %s is installed", action.spec); System::println(System::color::success, "Package %s is installed", action.spec);
} }
else else

View File

@ -136,7 +136,8 @@ namespace vcpkg::Commands::Integrate
static fs::path get_appdata_targets_path() static fs::path get_appdata_targets_path()
{ {
return fs::path(*System::get_environmental_variable(L"LOCALAPPDATA")) / "vcpkg" / "vcpkg.user.targets"; static const fs::path local_app_data = fs::path(System::get_environmental_variable(L"LOCALAPPDATA").get_or_exit(VCPKG_LINE_INFO));
return local_app_data / "vcpkg" / "vcpkg.user.targets";
} }
static void integrate_install(const vcpkg_paths& paths) static void integrate_install(const vcpkg_paths& paths)

View File

@ -228,11 +228,7 @@ true
std::wstring GetSQMUser() std::wstring GetSQMUser()
{ {
auto hkcu_sqmclient = System::get_registry_string(HKEY_CURRENT_USER, LR"(Software\Microsoft\SQMClient)", L"UserId"); auto hkcu_sqmclient = System::get_registry_string(HKEY_CURRENT_USER, LR"(Software\Microsoft\SQMClient)", L"UserId");
return hkcu_sqmclient.value_or(L"{}");
if (hkcu_sqmclient)
return std::move(*hkcu_sqmclient);
else
return L"{}";
} }
void SetUserInformation(const std::string& user_id, const std::string& first_use_time) void SetUserInformation(const std::string& user_id, const std::string& first_use_time)

View File

@ -46,9 +46,9 @@ static void inner(const vcpkg_cmd_arguments& args)
else else
{ {
const optional<std::wstring> vcpkg_root_dir_env = System::get_environmental_variable(L"VCPKG_ROOT"); const optional<std::wstring> vcpkg_root_dir_env = System::get_environmental_variable(L"VCPKG_ROOT");
if (vcpkg_root_dir_env) if (auto v = vcpkg_root_dir_env.get())
{ {
vcpkg_root_dir = fs::absolute(*vcpkg_root_dir_env); vcpkg_root_dir = fs::absolute(*v);
} }
else else
{ {
@ -77,9 +77,9 @@ static void inner(const vcpkg_cmd_arguments& args)
else else
{ {
const optional<std::wstring> vcpkg_default_triplet_env = System::get_environmental_variable(L"VCPKG_DEFAULT_TRIPLET"); const optional<std::wstring> vcpkg_default_triplet_env = System::get_environmental_variable(L"VCPKG_DEFAULT_TRIPLET");
if (vcpkg_default_triplet_env) if (auto v = vcpkg_default_triplet_env.get())
{ {
default_target_triplet = triplet::from_canonical_name(Strings::utf16_to_utf8(*vcpkg_default_triplet_env)); default_target_triplet = triplet::from_canonical_name(Strings::utf16_to_utf8(*v));
} }
else else
{ {

View File

@ -9,7 +9,7 @@
namespace vcpkg::Dependencies namespace vcpkg::Dependencies
{ {
install_plan_action::install_plan_action() : plan_type(install_plan_type::UNKNOWN), binary_pgh(nullptr), source_pgh(nullptr) install_plan_action::install_plan_action() : plan_type(install_plan_type::UNKNOWN), binary_pgh(nullopt), source_pgh(nullopt)
{ {
} }
@ -68,7 +68,7 @@ namespace vcpkg::Dependencies
auto it = status_db.find(spec); auto it = status_db.find(spec);
if (it != status_db.end() && (*it)->want == want_t::install) if (it != status_db.end() && (*it)->want == want_t::install)
{ {
was_examined.emplace(spec, install_plan_action{install_plan_type::ALREADY_INSTALLED, nullptr, nullptr}); was_examined.emplace(spec, install_plan_action{install_plan_type::ALREADY_INSTALLED, nullopt, nullopt });
continue; continue;
} }
@ -76,7 +76,7 @@ namespace vcpkg::Dependencies
if (BinaryParagraph* bpgh = maybe_bpgh.get()) if (BinaryParagraph* bpgh = maybe_bpgh.get())
{ {
process_dependencies(bpgh->depends); process_dependencies(bpgh->depends);
was_examined.emplace(spec, install_plan_action{install_plan_type::INSTALL, std::make_unique<BinaryParagraph>(std::move(*bpgh)), nullptr}); was_examined.emplace(spec, install_plan_action{install_plan_type::INSTALL, std::move(*bpgh), nullopt });
continue; continue;
} }
@ -84,7 +84,7 @@ namespace vcpkg::Dependencies
SourceParagraph* spgh = maybe_spgh.get(); SourceParagraph* spgh = maybe_spgh.get();
Checks::check_exit(VCPKG_LINE_INFO, spgh != nullptr, "Cannot find package %s", spec.name()); Checks::check_exit(VCPKG_LINE_INFO, spgh != nullptr, "Cannot find package %s", spec.name());
process_dependencies(filter_dependencies(spgh->depends, spec.target_triplet())); process_dependencies(filter_dependencies(spgh->depends, spec.target_triplet()));
was_examined.emplace(spec, install_plan_action{install_plan_type::BUILD_AND_INSTALL, nullptr, std::make_unique<SourceParagraph>(std::move(*spgh))}); was_examined.emplace(spec, install_plan_action{install_plan_type::BUILD_AND_INSTALL, nullopt, std::move(*spgh)});
} }
std::vector<package_spec_with_install_plan> ret; std::vector<package_spec_with_install_plan> ret;

View File

@ -19,14 +19,14 @@ namespace vcpkg::Environment
static optional<fs::path> find_vs2015_installation_instance() static optional<fs::path> find_vs2015_installation_instance()
{ {
const optional<std::wstring> vs2015_cmntools_optional = System::get_environmental_variable(L"VS140COMNTOOLS"); const optional<std::wstring> vs2015_cmntools_optional = System::get_environmental_variable(L"VS140COMNTOOLS");
if (!vs2015_cmntools_optional) if (auto v = vs2015_cmntools_optional.get())
{ {
return nullptr; static const fs::path vs2015_cmntools = fs::path(*v).parent_path(); // The call to parent_path() is needed because the env variable has a trailing backslash
static const fs::path vs2015_path = vs2015_cmntools.parent_path().parent_path();
return vs2015_path;
} }
static const fs::path vs2015_cmntools = fs::path(*vs2015_cmntools_optional).parent_path(); // The call to parent_path() is needed because the env variable has a trailing backslash return nullopt;
static const fs::path vs2015_path = vs2015_cmntools.parent_path().parent_path();
return std::make_unique<fs::path>(vs2015_path);
} }
static const optional<fs::path>& get_VS2015_installation_instance() static const optional<fs::path>& get_VS2015_installation_instance()
@ -69,9 +69,9 @@ namespace vcpkg::Environment
// VS2015 // VS2015
const optional<fs::path>& vs_2015_installation_instance = get_VS2015_installation_instance(); const optional<fs::path>& vs_2015_installation_instance = get_VS2015_installation_instance();
if (vs_2015_installation_instance) if (auto v = vs_2015_installation_instance.get())
{ {
const fs::path vs2015_dumpbin_exe = *vs_2015_installation_instance / "VC" / "bin" / "dumpbin.exe"; const fs::path vs2015_dumpbin_exe = *v / "VC" / "bin" / "dumpbin.exe";
paths_examined.push_back(vs2015_dumpbin_exe); paths_examined.push_back(vs2015_dumpbin_exe);
if (fs::exists(vs2015_dumpbin_exe)) if (fs::exists(vs2015_dumpbin_exe))
{ {
@ -112,9 +112,9 @@ namespace vcpkg::Environment
// VS2015 // VS2015
const optional<fs::path>& vs_2015_installation_instance = get_VS2015_installation_instance(); const optional<fs::path>& vs_2015_installation_instance = get_VS2015_installation_instance();
if (vs_2015_installation_instance) if (auto v = vs_2015_installation_instance.get())
{ {
const fs::path vs2015_vcvarsall_bat = *vs_2015_installation_instance / "VC" / "vcvarsall.bat"; const fs::path vs2015_vcvarsall_bat = *v / "VC" / "vcvarsall.bat";
paths_examined.push_back(vs2015_vcvarsall_bat); paths_examined.push_back(vs2015_vcvarsall_bat);
if (fs::exists(vs2015_vcvarsall_bat)) if (fs::exists(vs2015_vcvarsall_bat))
@ -138,50 +138,37 @@ namespace vcpkg::Environment
return vcvarsall_bat; return vcvarsall_bat;
} }
static fs::path find_ProgramFiles()
{
const optional<std::wstring> program_files = System::get_environmental_variable(L"PROGRAMFILES");
Checks::check_exit(VCPKG_LINE_INFO, program_files.get() != nullptr, "Could not detect the PROGRAMFILES environmental variable");
return *program_files;
}
static const fs::path& get_ProgramFiles() static const fs::path& get_ProgramFiles()
{ {
static const fs::path p = find_ProgramFiles(); static const fs::path p = System::get_environmental_variable(L"PROGRAMFILES").get_or_exit(VCPKG_LINE_INFO);
return p; return p;
} }
static fs::path find_ProgramFiles_32_bit()
{
const optional<std::wstring> program_files_X86 = System::get_environmental_variable(L"ProgramFiles(x86)");
if (program_files_X86)
{
return *program_files_X86;
}
return get_ProgramFiles();
}
const fs::path& get_ProgramFiles_32_bit() const fs::path& get_ProgramFiles_32_bit()
{ {
static const fs::path p = find_ProgramFiles_32_bit(); static const fs::path p = []() -> fs::path
{
auto value = System::get_environmental_variable(L"ProgramFiles(x86)");
if (auto v = value.get())
{
return std::move(*v);
}
return get_ProgramFiles();
}();
return p; return p;
} }
static fs::path find_ProgramFiles_platform_bitness()
{
const optional<std::wstring> program_files_W6432 = System::get_environmental_variable(L"ProgramW6432");
if (program_files_W6432)
{
return *program_files_W6432;
}
return get_ProgramFiles();
}
const fs::path& get_ProgramFiles_platform_bitness() const fs::path& get_ProgramFiles_platform_bitness()
{ {
static const fs::path p = find_ProgramFiles_platform_bitness(); static const fs::path p = []() -> fs::path
{
auto value = System::get_environmental_variable(L"ProgramW6432");
if (auto v = value.get())
{
return std::move(*v);
}
return get_ProgramFiles();
}();
return p; return p;
} }
} }

View File

@ -15,7 +15,7 @@ namespace vcpkg::System
int cmd_execute_clean(const cwstring_view cmd_line) int cmd_execute_clean(const cwstring_view cmd_line)
{ {
static const std::wstring system_root = *get_environmental_variable(L"SystemRoot"); static const std::wstring system_root = get_environmental_variable(L"SystemRoot").get_or_exit(VCPKG_LINE_INFO);
static const std::wstring system_32 = system_root + LR"(\system32)"; static const std::wstring system_32 = system_root + LR"(\system32)";
static const std::wstring new_PATH = Strings::wformat(LR"(Path=%s;%s;%s\WindowsPowerShell\v1.0\)", system_32, system_root, system_32); static const std::wstring new_PATH = Strings::wformat(LR"(Path=%s;%s;%s\WindowsPowerShell\v1.0\)", system_32, system_root, system_32);
@ -69,8 +69,9 @@ namespace vcpkg::System
for (auto&& env_wstring : env_wstrings) for (auto&& env_wstring : env_wstrings)
{ {
auto v = System::get_environmental_variable(env_wstring.c_str()); const optional<std::wstring> value = System::get_environmental_variable(env_wstring.c_str());
if (v == nullptr || v->empty()) auto v = value.get();
if (!v || v->empty())
continue; continue;
env_wstring.push_back(L'='); env_wstring.push_back(L'=');
@ -164,14 +165,14 @@ namespace vcpkg::System
{ {
auto sz = GetEnvironmentVariableW(varname, nullptr, 0); auto sz = GetEnvironmentVariableW(varname, nullptr, 0);
if (sz == 0) if (sz == 0)
return nullptr; return nullopt;
auto ret = std::make_unique<std::wstring>(sz, L'\0'); auto ret = std::make_unique<std::wstring>(sz, L'\0');
Checks::check_exit(VCPKG_LINE_INFO, MAXDWORD >= ret->size()); Checks::check_exit(VCPKG_LINE_INFO, MAXDWORD >= ret->size());
auto sz2 = GetEnvironmentVariableW(varname, ret->data(), static_cast<DWORD>(ret->size())); auto sz2 = GetEnvironmentVariableW(varname, ret->data(), static_cast<DWORD>(ret->size()));
Checks::check_exit(VCPKG_LINE_INFO, sz2 + 1 == sz); Checks::check_exit(VCPKG_LINE_INFO, sz2 + 1 == sz);
ret->pop_back(); ret->pop_back();
return ret; return *ret.release();
} }
void set_environmental_variable(const cwstring_view varname, const cwstring_view varvalue) noexcept void set_environmental_variable(const cwstring_view varname, const cwstring_view varvalue) noexcept
@ -189,21 +190,21 @@ namespace vcpkg::System
HKEY k = nullptr; HKEY k = nullptr;
LSTATUS ec = RegOpenKeyExW(base, subKey, NULL, KEY_READ, &k); LSTATUS ec = RegOpenKeyExW(base, subKey, NULL, KEY_READ, &k);
if (ec != ERROR_SUCCESS) if (ec != ERROR_SUCCESS)
return nullptr; return nullopt;
DWORD dwBufferSize = 0; DWORD dwBufferSize = 0;
DWORD dwType = 0; DWORD dwType = 0;
auto rc = RegQueryValueExW(k, valuename, nullptr, &dwType, nullptr, &dwBufferSize); auto rc = RegQueryValueExW(k, valuename, nullptr, &dwType, nullptr, &dwBufferSize);
if (rc != ERROR_SUCCESS || !is_string_keytype(dwType) || dwBufferSize == 0 || dwBufferSize % sizeof(wchar_t) != 0) if (rc != ERROR_SUCCESS || !is_string_keytype(dwType) || dwBufferSize == 0 || dwBufferSize % sizeof(wchar_t) != 0)
return nullptr; return nullopt;
std::wstring ret; std::wstring ret;
ret.resize(dwBufferSize / sizeof(wchar_t)); ret.resize(dwBufferSize / sizeof(wchar_t));
rc = RegQueryValueExW(k, valuename, nullptr, &dwType, reinterpret_cast<LPBYTE>(ret.data()), &dwBufferSize); rc = RegQueryValueExW(k, valuename, nullptr, &dwType, reinterpret_cast<LPBYTE>(ret.data()), &dwBufferSize);
if (rc != ERROR_SUCCESS || !is_string_keytype(dwType) || dwBufferSize != sizeof(wchar_t) * ret.size()) if (rc != ERROR_SUCCESS || !is_string_keytype(dwType) || dwBufferSize != sizeof(wchar_t) * ret.size())
return nullptr; return nullopt;
ret.pop_back(); // remove extra trailing null byte ret.pop_back(); // remove extra trailing null byte
return std::make_unique<std::wstring>(std::move(ret)); return ret;
} }
} }

View File

@ -47,10 +47,10 @@ namespace vcpkg
if (it != candidate_paths.cend()) if (it != candidate_paths.cend())
{ {
return std::make_unique<fs::path>(std::move(*it)); return std::move(*it);
} }
return nullptr; return nullopt;
} }
static std::vector<fs::path> find_from_PATH(const std::wstring& name) static std::vector<fs::path> find_from_PATH(const std::wstring& name)
@ -101,9 +101,10 @@ namespace vcpkg
candidate_paths.push_back(Environment::get_ProgramFiles_platform_bitness() / "CMake" / "bin" / "cmake.exe"); candidate_paths.push_back(Environment::get_ProgramFiles_platform_bitness() / "CMake" / "bin" / "cmake.exe");
candidate_paths.push_back(Environment::get_ProgramFiles_32_bit() / "CMake" / "bin"); candidate_paths.push_back(Environment::get_ProgramFiles_32_bit() / "CMake" / "bin");
if (auto ret = find_if_has_equal_or_greater_version(candidate_paths, version_check_arguments, expected_version)) const optional<fs::path> path = find_if_has_equal_or_greater_version(candidate_paths, version_check_arguments, expected_version);
if (auto p = path.get())
{ {
return *ret; return *p;
} }
return fetch_dependency(scripts_folder, L"cmake", downloaded_copy); return fetch_dependency(scripts_folder, L"cmake", downloaded_copy);
@ -121,9 +122,10 @@ namespace vcpkg
candidate_paths.push_back(downloaded_copy); candidate_paths.push_back(downloaded_copy);
candidate_paths.insert(candidate_paths.end(), from_path.cbegin(), from_path.cend()); candidate_paths.insert(candidate_paths.end(), from_path.cbegin(), from_path.cend());
if (auto ret = find_if_has_equal_or_greater_version(candidate_paths, version_check_arguments, expected_version)) auto path = find_if_has_equal_or_greater_version(candidate_paths, version_check_arguments, expected_version);
if (auto p = path.get())
{ {
return *ret; return *p;
} }
return fetch_dependency(scripts_folder, L"nuget", downloaded_copy); return fetch_dependency(scripts_folder, L"nuget", downloaded_copy);
@ -143,9 +145,10 @@ namespace vcpkg
candidate_paths.push_back(Environment::get_ProgramFiles_platform_bitness() / "git" / "cmd" / "git.exe"); candidate_paths.push_back(Environment::get_ProgramFiles_platform_bitness() / "git" / "cmd" / "git.exe");
candidate_paths.push_back(Environment::get_ProgramFiles_32_bit() / "git" / "cmd" / "git.exe"); candidate_paths.push_back(Environment::get_ProgramFiles_32_bit() / "git" / "cmd" / "git.exe");
if (auto ret = find_if_has_equal_or_greater_version(candidate_paths, version_check_arguments, expected_version)) const optional<fs::path> path = find_if_has_equal_or_greater_version(candidate_paths, version_check_arguments, expected_version);
if (auto p = path.get())
{ {
return *ret; return *p;
} }
return fetch_dependency(scripts_folder, L"git", downloaded_copy); return fetch_dependency(scripts_folder, L"git", downloaded_copy);