Skip to content

Commit

Permalink
src: remove invalid casts in options parser
Browse files Browse the repository at this point in the history
Fixes: #26131

PR-URL: #26139
Reviewed-By: Yang Guo <yangguo@chromium.org>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
addaleax authored and rvagg committed Feb 28, 2019
1 parent 4ca0789 commit 778db67
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
13 changes: 7 additions & 6 deletions src/node_options-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void OptionsParser<Options>::Implies(const std::string& from,
CHECK_NE(it, options_.end());
CHECK_EQ(it->second.type, kBoolean);
implications_.emplace(from, Implication {
std::static_pointer_cast<OptionField<bool>>(it->second.field), true
it->second.field, true
});
}

Expand All @@ -159,7 +159,7 @@ void OptionsParser<Options>::ImpliesNot(const std::string& from,
CHECK_NE(it, options_.end());
CHECK_EQ(it->second.type, kBoolean);
implications_.emplace(from, Implication {
std::static_pointer_cast<OptionField<bool>>(it->second.field), false
it->second.field, false
});
}

Expand Down Expand Up @@ -205,8 +205,7 @@ auto OptionsParser<Options>::Convert(
typename OptionsParser<ChildOptions>::Implication original,
ChildOptions* (Options::* get_child)()) {
return Implication {
std::static_pointer_cast<OptionField<bool>>(
Convert(original.target_field, get_child)),
Convert(original.target_field, get_child),
original.target_value
};
}
Expand Down Expand Up @@ -378,8 +377,10 @@ void OptionsParser<Options>::Parse(

{
auto implications = implications_.equal_range(name);
for (auto it = implications.first; it != implications.second; ++it)
*it->second.target_field->Lookup(options) = it->second.target_value;
for (auto it = implications.first; it != implications.second; ++it) {
*it->second.target_field->template Lookup<bool>(options) =
it->second.target_value;
}
}

const OptionInfo& info = it->second;
Expand Down
18 changes: 6 additions & 12 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,17 @@ class OptionsParser {
public:
virtual ~BaseOptionField() {}
virtual void* LookupImpl(Options* options) const = 0;
};

// Represents a field of type T within `Options`.
template <typename T>
class OptionField : public BaseOptionField {
public:
typedef T Type;

T* Lookup(Options* options) const {
return static_cast<T*>(this->LookupImpl(options));
template <typename T>
inline T* Lookup(Options* options) const {
return static_cast<T*>(LookupImpl(options));
}
};

// Represents a field of type T within `Options` that can be looked up
// as a C++ member field.
template <typename T>
class SimpleOptionField : public OptionField<T> {
class SimpleOptionField : public BaseOptionField {
public:
explicit SimpleOptionField(T Options::* field) : field_(field) {}
void* LookupImpl(Options* options) const override {
Expand All @@ -366,7 +360,7 @@ class OptionsParser {
template <typename T>
inline T* Lookup(std::shared_ptr<BaseOptionField> field,
Options* options) const {
return std::static_pointer_cast<OptionField<T>>(field)->Lookup(options);
return field->template Lookup<T>(options);
}

// An option consists of:
Expand All @@ -383,7 +377,7 @@ class OptionsParser {
// An implied option is composed of the information on where to store a
// specific boolean value (if another specific option is encountered).
struct Implication {
std::shared_ptr<OptionField<bool>> target_field;
std::shared_ptr<BaseOptionField> target_field;
bool target_value;
};

Expand Down

0 comments on commit 778db67

Please sign in to comment.