Skip to content

Commit

Permalink
src: reduce to simple const char* in OptionsParser
Browse files Browse the repository at this point in the history
> A lot of the `std::string` usage here could be reduced
  to simple `const char*`s if it's reasonable to expect the values to be
  known at compile-time.

So this commit uses `const char*` to replace most of `std::string` in
`OptionsParser`.

PR-URL: #26297
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
ZYSzys authored and BridgeAR committed Mar 5, 2019
1 parent 50e42c9 commit cb62c24
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 49 deletions.
48 changes: 24 additions & 24 deletions src/node_options-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ EnvironmentOptions* PerIsolateOptions::get_per_env_options() {
namespace options_parser {

template <typename Options>
void OptionsParser<Options>::AddOption(const std::string& name,
const std::string& help_text,
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
bool Options::* field,
OptionEnvvarSettings env_setting) {
options_.emplace(name,
Expand All @@ -40,8 +40,8 @@ void OptionsParser<Options>::AddOption(const std::string& name,
}

template <typename Options>
void OptionsParser<Options>::AddOption(const std::string& name,
const std::string& help_text,
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
uint64_t Options::* field,
OptionEnvvarSettings env_setting) {
options_.emplace(
Expand All @@ -53,8 +53,8 @@ void OptionsParser<Options>::AddOption(const std::string& name,
}

template <typename Options>
void OptionsParser<Options>::AddOption(const std::string& name,
const std::string& help_text,
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
int64_t Options::* field,
OptionEnvvarSettings env_setting) {
options_.emplace(
Expand All @@ -66,8 +66,8 @@ void OptionsParser<Options>::AddOption(const std::string& name,
}

template <typename Options>
void OptionsParser<Options>::AddOption(const std::string& name,
const std::string& help_text,
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
std::string Options::* field,
OptionEnvvarSettings env_setting) {
options_.emplace(
Expand All @@ -80,8 +80,8 @@ void OptionsParser<Options>::AddOption(const std::string& name,

template <typename Options>
void OptionsParser<Options>::AddOption(
const std::string& name,
const std::string& help_text,
const char* name,
const char* help_text,
std::vector<std::string> Options::* field,
OptionEnvvarSettings env_setting) {
options_.emplace(name, OptionInfo {
Expand All @@ -93,8 +93,8 @@ void OptionsParser<Options>::AddOption(
}

template <typename Options>
void OptionsParser<Options>::AddOption(const std::string& name,
const std::string& help_text,
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
HostPort Options::* field,
OptionEnvvarSettings env_setting) {
options_.emplace(
Expand All @@ -106,44 +106,44 @@ void OptionsParser<Options>::AddOption(const std::string& name,
}

template <typename Options>
void OptionsParser<Options>::AddOption(const std::string& name,
const std::string& help_text,
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
NoOp no_op_tag,
OptionEnvvarSettings env_setting) {
options_.emplace(name, OptionInfo{kNoOp, nullptr, env_setting, help_text});
}

template <typename Options>
void OptionsParser<Options>::AddOption(const std::string& name,
const std::string& help_text,
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
V8Option v8_option_tag,
OptionEnvvarSettings env_setting) {
options_.emplace(name,
OptionInfo{kV8Option, nullptr, env_setting, help_text});
}

template <typename Options>
void OptionsParser<Options>::AddAlias(const std::string& from,
const std::string& to) {
void OptionsParser<Options>::AddAlias(const char* from,
const char* to) {
aliases_[from] = { to };
}

template <typename Options>
void OptionsParser<Options>::AddAlias(const std::string& from,
void OptionsParser<Options>::AddAlias(const char* from,
const std::vector<std::string>& to) {
aliases_[from] = to;
}

template <typename Options>
void OptionsParser<Options>::AddAlias(
const std::string& from,
const char* from,
const std::initializer_list<std::string>& to) {
AddAlias(from, std::vector<std::string>(to));
}

template <typename Options>
void OptionsParser<Options>::Implies(const std::string& from,
const std::string& to) {
void OptionsParser<Options>::Implies(const char* from,
const char* to) {
auto it = options_.find(to);
CHECK_NE(it, options_.end());
CHECK_EQ(it->second.type, kBoolean);
Expand All @@ -153,8 +153,8 @@ void OptionsParser<Options>::Implies(const std::string& from,
}

template <typename Options>
void OptionsParser<Options>::ImpliesNot(const std::string& from,
const std::string& to) {
void OptionsParser<Options>::ImpliesNot(const char* from,
const char* to) {
auto it = options_.find(to);
CHECK_NE(it, options_.end());
CHECK_EQ(it->second.type, kBoolean);
Expand Down
46 changes: 21 additions & 25 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,43 +239,39 @@ class OptionsParser {
struct NoOp {};
struct V8Option {};

// TODO(addaleax): A lot of the `std::string` usage here could be reduced
// to simple `const char*`s if it's reasonable to expect the values to be
// known at compile-time.

// These methods add a single option to the parser. Optionally, it can be
// specified whether the option should be allowed from environment variable
// sources (i.e. NODE_OPTIONS).
void AddOption(const std::string& name,
const std::string& help_text,
void AddOption(const char* name,
const char* help_text,
bool Options::* field,
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
void AddOption(const std::string& name,
const std::string& help_text,
void AddOption(const char* name,
const char* help_text,
uint64_t Options::* field,
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
void AddOption(const std::string& name,
const std::string& help_text,
void AddOption(const char* name,
const char* help_text,
int64_t Options::* field,
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
void AddOption(const std::string& name,
const std::string& help_text,
void AddOption(const char* name,
const char* help_text,
std::string Options::* field,
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
void AddOption(const std::string& name,
const std::string& help_text,
void AddOption(const char* name,
const char* help_text,
std::vector<std::string> Options::* field,
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
void AddOption(const std::string& name,
const std::string& help_text,
void AddOption(const char* name,
const char* help_text,
HostPort Options::* field,
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
void AddOption(const std::string& name,
const std::string& help_text,
void AddOption(const char* name,
const char* help_text,
NoOp no_op_tag,
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
void AddOption(const std::string& name,
const std::string& help_text,
void AddOption(const char* name,
const char* help_text,
V8Option v8_option_tag,
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);

Expand All @@ -286,15 +282,15 @@ class OptionsParser {
// the option is presented in that form (i.e. with a '=').
// If `from` has the form "--option-a <arg>", the alias will only be expanded
// if the option has a non-option argument (not starting with -) following it.
void AddAlias(const std::string& from, const std::string& to);
void AddAlias(const std::string& from, const std::vector<std::string>& to);
void AddAlias(const std::string& from,
void AddAlias(const char* from, const char* to);
void AddAlias(const char* from, const std::vector<std::string>& to);
void AddAlias(const char* from,
const std::initializer_list<std::string>& to);

// Add implications from some arbitrary option to a boolean one, either
// in a way that makes `from` set `to` to true or to false.
void Implies(const std::string& from, const std::string& to);
void ImpliesNot(const std::string& from, const std::string& to);
void Implies(const char* from, const char* to);
void ImpliesNot(const char* from, const char* to);

// Insert options from another options parser into this one, along with
// a method that yields the target options type from this parser's options
Expand Down

0 comments on commit cb62c24

Please sign in to comment.