Skip to content

Commit

Permalink
fix after finally discovering what to do to NOT need the set_value2
Browse files Browse the repository at this point in the history
… naming hack:

the Param::set_value methods will not be considered by the compiler here, resulting in at least 1 compile error in params.cpp,
due to this nasty little blurb:

> Member lookup rules are defined in Section 10.2/2
>
> The following steps define the result of name lookup in a class scope, C.
> First, every declaration for the name in the class and in each of its base class sub-objects is considered. A member name f
> in one sub-object B hides a member name f in a sub-object A if A is a base class sub-object of B. Any declarations that are
> so hidden are eliminated from consideration.          <-- !!!
> Each of these declarations that was introduced by a using-declaration is considered to be from each sub-object of C that is
> of the type containing the declara-tion designated by the using-declaration. If the resulting set of declarations are not
> all from sub-objects of the same type, or the set has a nonstatic member and includes members from distinct sub-objects,
> there is an ambiguity and the program is ill-formed. Otherwise that set is the result of the lookup.

Found here: https://stackoverflow.com/questions/5368862/why-do-multiple-inherited-functions-with-same-name-but-different-signatures-not
which seems to be off-topic due to the mutiple-inheritance issues discussed there, but the phrasing of that little C++ standards blurb
is such that it applies to our situation as well, where we only replace/override a *subset* of the available set_value() methods from
the Params class. Half a year later and I stumble across that little paragraph; would never have thought to apply a `using` statement
here, but it works! !@#$%^&* C++!

Incidentally, the fruity thing about it all is that it only errors out for StringParam in params.cpp, while a sane individual would've
reckoned it'd bother all four of them: IntParam, FloatParam, etc.
  • Loading branch information
GerHobbelt committed Apr 4, 2024
1 parent bf6914f commit 4a08d1a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/ccutil/params.cpp
Expand Up @@ -444,15 +444,15 @@ std::vector<ParamPtr> ParamsVectorSet::as_list(
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////

bool Param::set_value2(const ParamValueContainer &v, ParamSetBySourceType source_type, ParamPtr source) {
bool Param::set_value(const ParamValueContainer &v, ParamSetBySourceType source_type, ParamPtr source) {
if (const int32_t* val = std::get_if<int32_t>(&v))
return set_value(*val, source_type, source);
else if (const bool* val = std::get_if<bool>(&v))
return set_value(*val, source_type, source);
else if (const double* val = std::get_if<double>(&v))
return set_value(*val, source_type, source);
else if (const std::string* val = std::get_if<std::string>(&v))
return set_value2(*val, source_type, source);
return set_value(*val, source_type, source);
else
throw new std::logic_error(fmt::format("tesseract param '{}' error: failed to get value from variant input arg", name_));
}
Expand Down Expand Up @@ -496,7 +496,7 @@ void Param::reset_access_counts() {
access_counts_.changing = 0;
}

bool Param::set_value2(const std::string& v, ParamSetBySourceType source_type, ParamPtr source) {
bool Param::set_value(const std::string& v, ParamSetBySourceType source_type, ParamPtr source) {
return set_value(v.c_str(), source_type, source);
}

Expand Down Expand Up @@ -997,7 +997,7 @@ const std::string& StringParam::value() const {

void StringParam::ResetToDefault(ParamSetBySourceType source_type) {
const std::string& v = default_;
(void) Param::set_value2(v, source_type, nullptr);
(void) Param::set_value(v, source_type, nullptr);
}

void StringParam::ResetFrom(const ParamsVectorSet& vec, ParamSetBySourceType source_type) {
Expand Down Expand Up @@ -1260,13 +1260,13 @@ bool ParamUtils::SetParam(
{
StringParam* param = FindParam<StringParam>(name, set);
if (param != nullptr) {
return param->set_value2(value, source_type, source);
return param->set_value(value, source_type, source);
}
}
{
Param* param = FindParam<Param>(name, set);
if (param != nullptr) {
return param->set_value2(value, source_type, source);
return param->set_value(value, source_type, source);
}
}
return false;
Expand Down
34 changes: 29 additions & 5 deletions src/ccutil/params.h
Expand Up @@ -298,7 +298,7 @@ class TESS_API ParamUtils {
// Variable names are followed by one of more whitespace characters,
// followed by the Value, which spans the rest of line.
//
// Any Variables listed in the file, which do not match the given
// Any Variables listed in the file which do not match the given
// constraint are ignored, but are reported via `tprintf()` as ignored,
// unless you set `quietly_ignore`.
static bool ReadParamsFile(const std::string &file, // filename to read
Expand Down Expand Up @@ -656,9 +656,9 @@ class Param {
virtual bool set_value(bool v, SOURCE_REF) = 0;
virtual bool set_value(double v, SOURCE_REF) = 0;

// name hack to prevent compiler errors in MSVC2022 :-((
bool set_value2(const ParamValueContainer &v, SOURCE_REF);
bool set_value2(const std::string& v, SOURCE_REF);
// generic:
bool set_value(const ParamValueContainer &v, SOURCE_REF);
bool set_value(const std::string &v, SOURCE_REF);

virtual void ResetToDefault(SOURCE_TYPE) = 0;
virtual void ResetFrom(const ParamsVectorSet &vec, SOURCE_TYPE) = 0;
Expand Down Expand Up @@ -786,7 +786,31 @@ class StringParam : public Param {
virtual bool set_value(bool v, SOURCE_REF) override;
virtual bool set_value(double v, SOURCE_REF) override;

const std::string& value() const;
// the Param::set_value methods will not be considered by the compiler here, resulting in at least 1 compile error in params.cpp,
// due to this nasty little blurb:
//
// > Member lookup rules are defined in Section 10.2/2
// >
// > The following steps define the result of name lookup in a class scope, C.
// > First, every declaration for the name in the class and in each of its base class sub-objects is considered. A member name f
// > in one sub-object B hides a member name f in a sub-object A if A is a base class sub-object of B. Any declarations that are
// > so hidden are eliminated from consideration. <-- !!!
// > Each of these declarations that was introduced by a using-declaration is considered to be from each sub-object of C that is
// > of the type containing the declara-tion designated by the using-declaration. If the resulting set of declarations are not
// > all from sub-objects of the same type, or the set has a nonstatic member and includes members from distinct sub-objects,
// > there is an ambiguity and the program is ill-formed. Otherwise that set is the result of the lookup.
//
// Found here: https://stackoverflow.com/questions/5368862/why-do-multiple-inherited-functions-with-same-name-but-different-signatures-not
// which seems to be off-topic due to the mutiple-inheritance issues discussed there, but the phrasing of that little C++ standards blurb
// is such that it applies to our situation as well, where we only replace/override a *subset* of the available set_value() methods from
// the Params class. Half a year later and I stumble across that little paragraph; would never have thought to apply a `using` statement
// here, but it works! !@#$%^&* C++!
//
// Incidentally, the fruity thing about it all is that it only errors out for StringParam in params.cpp, while a sane individual would've
// reckoned it'd bother all four of them: IntParam, FloatParam, etc.
using Param::set_value;

const std::string &value() const;

virtual void ResetToDefault(SOURCE_TYPE) override;
virtual void ResetFrom(const ParamsVectorSet& vec, SOURCE_TYPE) override;
Expand Down

0 comments on commit 4a08d1a

Please sign in to comment.