Skip to content

Commit

Permalink
Remove custom hash for unordered map since not supported by CI
Browse files Browse the repository at this point in the history
android/linux builds

* Make mAllOptions strings and not string_views
* Make the functions that need to search mAllOptions take input
  parameters of const std::string& instead
  • Loading branch information
angela28chen committed Aug 10, 2023
1 parent c5e95be commit 7fe93d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
24 changes: 7 additions & 17 deletions include/ppx/command_line_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CliOptions
public:
CliOptions() = default;

bool HasExtraOption(std::string_view option) const { return mAllOptions.contains(option); }
bool HasExtraOption(const std::string& option) const { return mAllOptions.contains(option); }

// Returns the number of unique options and flags that were specified on the commandline,
// not counting multiple appearances of the same flag such as: --assets-path a --assets-path b
Expand All @@ -57,7 +57,7 @@ class CliOptions
// Warning: If this is called instead of the vector overload for multiple-value flags,
// only the last value will be returned.
template <typename T>
T GetOptionValueOrDefault(std::string_view optionName, const T& defaultValue) const
T GetOptionValueOrDefault(const std::string& optionName, const T& defaultValue) const
{
auto it = mAllOptions.find(optionName);
if (it == mAllOptions.cend()) {
Expand All @@ -70,7 +70,7 @@ class CliOptions
// Same as above, but intended for list flags that are specified on the command line
// with multiple instances of the same flag
template <typename T>
std::vector<T> GetOptionValueOrDefault(std::string_view optionName, const std::vector<T>& defaultValues) const
std::vector<T> GetOptionValueOrDefault(const std::string& optionName, const std::vector<T>& defaultValues) const
{
auto it = mAllOptions.find(optionName);
if (it == mAllOptions.cend()) {
Expand All @@ -86,14 +86,14 @@ class CliOptions

// Same as above, but intended for resolution flags that are specified on command line
// with <Width>x<Height>
std::pair<int, int> GetOptionValueOrDefault(std::string_view optionName, const std::pair<int, int>& defaultValue) const;
std::pair<int, int> GetOptionValueOrDefault(const std::string& optionName, const std::pair<int, int>& defaultValue) const;

// (WILL BE DEPRECATED, USE KNOBS INSTEAD)
// Get the parameter value after converting it into the desired integral,
// floating-point, or boolean type. If the value fails to be converted,
// return the specified default value.
template <typename T>
T GetExtraOptionValueOrDefault(std::string_view optionName, const T& defaultValue) const
T GetExtraOptionValueOrDefault(const std::string& optionName, const T& defaultValue) const
{
static_assert(std::is_integral_v<T> || std::is_floating_point_v<T> || std::is_same_v<T, std::string>, "GetExtraOptionValueOrDefault must be called with an integral, floating-point, boolean, or std::string type");

Expand All @@ -103,7 +103,7 @@ class CliOptions
private:
// Adds new option if the option does not already exist
// Otherwise, the new value is appended to the end of the vector of stored parameters for this option
void AddOption(std::string_view optionName, std::string_view valueStr);
void AddOption(std::string_view optionName, std::string_view value);

// Same as above, but appends an array of values at the same key
void AddOption(std::string_view optionName, const std::vector<std::string>& valueArray);
Expand Down Expand Up @@ -136,18 +136,8 @@ class CliOptions
}

private:
struct string_hash
{
using hash_type = std::hash<std::string_view>;
using is_transparent = void;

size_t operator()(const char* str) const { return hash_type{}(str); }
size_t operator()(std::string_view str) const { return hash_type{}(str); }
size_t operator()(std::string const& str) const { return hash_type{}(str); }
};

// All flag names (string) and parameters (vector of strings) specified on the command line
std::unordered_map<std::string, std::vector<std::string>, string_hash, std::equal_to<>> mAllOptions;
std::unordered_map<std::string, std::vector<std::string>> mAllOptions;

friend class CommandLineParser;
};
Expand Down
19 changes: 11 additions & 8 deletions src/ppx/command_line_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool StartsWithDoubleDash(std::string_view s)

namespace ppx {

std::pair<int, int> CliOptions::GetOptionValueOrDefault(std::string_view optionName, const std::pair<int, int>& defaultValue) const
std::pair<int, int> CliOptions::GetOptionValueOrDefault(const std::string& optionName, const std::pair<int, int>& defaultValue) const
{
auto it = mAllOptions.find(optionName);
if (it == mAllOptions.cend()) {
Expand All @@ -55,22 +55,25 @@ std::pair<int, int> CliOptions::GetOptionValueOrDefault(std::string_view optionN
return std::make_pair(N, M);
}

void CliOptions::AddOption(std::string_view optionName, std::string_view valueStr)
void CliOptions::AddOption(std::string_view optionName, std::string_view value)
{
auto it = mAllOptions.find(optionName);
std::string optionNameStr{optionName};
std::string valueStr{value};
auto it = mAllOptions.find(optionNameStr);
if (it == mAllOptions.cend()) {
std::vector<std::string> v{std::string(valueStr)};
mAllOptions.emplace(std::string(optionName), v);
std::vector<std::string> v{valueStr};
mAllOptions.emplace(optionName, v);
return;
}
it->second.push_back(std::string(valueStr));
it->second.push_back(valueStr);
}

void CliOptions::AddOption(std::string_view optionName, const std::vector<std::string>& valueArray)
{
auto it = mAllOptions.find(optionName);
std::string optionNameStr{optionName};
auto it = mAllOptions.find(optionNameStr);
if (it == mAllOptions.cend()) {
mAllOptions.emplace(std::string(optionName), valueArray);
mAllOptions.emplace(optionNameStr, valueArray);
return;
}
auto storedValueArray = it->second;
Expand Down

0 comments on commit 7fe93d3

Please sign in to comment.