From 318778362a13ae1a6d8be81758e8d359885d9ccc Mon Sep 17 00:00:00 2001 From: Jefferson Amstutz Date: Wed, 23 Oct 2024 12:40:28 -0500 Subject: [PATCH] fix const-correctness of ParameterizedObject get methods --- src/helium/utility/ParameterizedObject.cpp | 42 +++++++++++++------ src/helium/utility/ParameterizedObject.h | 26 +++++++----- .../unit/test_helium_ParameterizedObject.cpp | 4 ++ 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/helium/utility/ParameterizedObject.cpp b/src/helium/utility/ParameterizedObject.cpp index 0431228f..20d7fdc5 100644 --- a/src/helium/utility/ParameterizedObject.cpp +++ b/src/helium/utility/ParameterizedObject.cpp @@ -7,24 +7,31 @@ namespace helium { -bool ParameterizedObject::hasParam(const std::string &name) +bool ParameterizedObject::hasParam(const std::string &name) const { - return findParam(name, false) != nullptr; + return findParam(name) != nullptr; +} + +bool ParameterizedObject::hasParam( + const std::string &name, ANARIDataType type) const +{ + auto *p = findParam(name); + return p ? p->second.type() == type : false; } void ParameterizedObject::setParam( const std::string &name, ANARIDataType type, const void *v) { - findParam(name, true)->second = AnariAny(type, v); + findParam(name)->second = AnariAny(type, v); } bool ParameterizedObject::getParam( - const std::string &name, ANARIDataType type, void *v) + const std::string &name, ANARIDataType type, void *v) const { if (type == ANARI_STRING || anari::isObject(type)) return false; - auto *p = findParam(name, false); + auto *p = findParam(name); if (!p || !p->second.is(type)) return false; @@ -33,13 +40,13 @@ bool ParameterizedObject::getParam( } std::string ParameterizedObject::getParamString( - const std::string &name, const std::string &valIfNotFound) + const std::string &name, const std::string &valIfNotFound) const { auto *p = findParam(name); return p ? p->second.getString() : valIfNotFound; } -AnariAny ParameterizedObject::getParamDirect(const std::string &name) +AnariAny ParameterizedObject::getParamDirect(const std::string &name) const { auto *p = findParam(name); return p ? p->second : AnariAny(); @@ -48,7 +55,7 @@ AnariAny ParameterizedObject::getParamDirect(const std::string &name) void ParameterizedObject::setParamDirect( const std::string &name, const AnariAny &v) { - findParam(name, true)->second = v; + findParam(name)->second = v; } void ParameterizedObject::removeParam(const std::string &name) @@ -76,8 +83,20 @@ ParameterizedObject::ParameterList::iterator ParameterizedObject::params_end() return m_params.end(); } +const ParameterizedObject::Param *ParameterizedObject::findParam( + const std::string &name) const +{ + auto foundParam = std::find_if(m_params.begin(), + m_params.end(), + [&](const Param &p) { return p.first == name; }); + + if (foundParam != m_params.end()) + return &(*foundParam); + return nullptr; +} + ParameterizedObject::Param *ParameterizedObject::findParam( - const std::string &name, bool addIfNotExist) + const std::string &name) { auto foundParam = std::find_if(m_params.begin(), m_params.end(), @@ -85,11 +104,10 @@ ParameterizedObject::Param *ParameterizedObject::findParam( if (foundParam != m_params.end()) return &(*foundParam); - else if (addIfNotExist) { + else { m_params.emplace_back(name, AnariAny()); return &m_params[m_params.size() - 1]; - } else - return nullptr; + } } } // namespace helium diff --git a/src/helium/utility/ParameterizedObject.h b/src/helium/utility/ParameterizedObject.h index 29be596e..b031b287 100644 --- a/src/helium/utility/ParameterizedObject.h +++ b/src/helium/utility/ParameterizedObject.h @@ -20,7 +20,11 @@ struct ParameterizedObject virtual ~ParameterizedObject() = default; // Return true if there was a parameter set with the corresponding 'name' - bool hasParam(const std::string &name); + bool hasParam(const std::string &name) const; + + // Return true if there was a parameter set with the corresponding 'name' and + // if it matches the corresponding type + bool hasParam(const std::string &name, ANARIDataType type) const; // Set the value of the parameter 'name', or add it if it doesn't exist yet void setParam(const std::string &name, ANARIDataType type, const void *v); @@ -37,13 +41,13 @@ struct ParameterizedObject // access ANARIObject or ANARIString parameters, see special methods for // getting parameters of those types. template - T getParam(const std::string &name, T valIfNotFound); + T getParam(const std::string &name, T valIfNotFound) const; // Get the value of the parameter associated with 'name' and write it to // location 'v', returning whether the was actually read. Just like the // templated version above, this requires that 'type' exactly match what the // application set. This function also cannot get objects or strings. - bool getParam(const std::string &name, ANARIDataType type, void *v); + bool getParam(const std::string &name, ANARIDataType type, void *v) const; // Get the pointer to an object parameter (returns null if not present). While // ParameterizedObject will track object lifetime appropriately, accessing @@ -51,17 +55,17 @@ struct ParameterizedObject // should consider using `helium::IntrusivePtr<>` to guarantee correct // lifetime handling. template - T *getParamObject(const std::string &name); + T *getParamObject(const std::string &name) const; // Get a string parameter value std::string getParamString( - const std::string &name, const std::string &valIfNotFound); + const std::string &name, const std::string &valIfNotFound) const; // Get/Set the container holding the value of a parameter (default constructed // AnariAny if not present). Getting this container will create a copy of the // parameter value, which for objects will incur the correct ref count changes // accordingly (handled by AnariAny). - AnariAny getParamDirect(const std::string &name); + AnariAny getParamDirect(const std::string &name) const; void setParamDirect(const std::string &name, const AnariAny &v); // Remove the value of the parameter associated with 'name'. @@ -80,7 +84,8 @@ struct ParameterizedObject private: // Data members // - Param *findParam(const std::string &name, bool addIfNotExist = false); + const Param *findParam(const std::string &name) const; + Param *findParam(const std::string &name); ParameterList m_params; }; @@ -110,7 +115,8 @@ inline void ParameterizedObject::setParam( } template -inline T ParameterizedObject::getParam(const std::string &name, T valIfNotFound) +inline T ParameterizedObject::getParam( + const std::string &name, T valIfNotFound) const { constexpr ANARIDataType type = anari::ANARITypeFor::value; static_assert(!anari::isObject(type), @@ -123,14 +129,14 @@ inline T ParameterizedObject::getParam(const std::string &name, T valIfNotFound) template <> inline bool ParameterizedObject::getParam( - const std::string &name, bool valIfNotFound) + const std::string &name, bool valIfNotFound) const { auto *p = findParam(name); return p && p->second.is(ANARI_BOOL) ? p->second.get() : valIfNotFound; } template -inline T *ParameterizedObject::getParamObject(const std::string &name) +inline T *ParameterizedObject::getParamObject(const std::string &name) const { auto *p = findParam(name); return p ? p->second.getObject() : nullptr; diff --git a/tests/unit/test_helium_ParameterizedObject.cpp b/tests/unit/test_helium_ParameterizedObject.cpp index 6c7afe15..7417a874 100644 --- a/tests/unit/test_helium_ParameterizedObject.cpp +++ b/tests/unit/test_helium_ParameterizedObject.cpp @@ -55,6 +55,7 @@ SCENARIO( THEN("The named parameter should have the correct type and value") { REQUIRE(obj.hasParam(name)); + REQUIRE(obj.hasParam(name, ANARI_INT32)); REQUIRE(obj.getParam(name, 4) == v); REQUIRE(obj.getParam(name, 4) == 4); @@ -71,6 +72,7 @@ SCENARIO( THEN("The paramter should no longer exist on the object") { REQUIRE(!obj.hasParam(name)); + REQUIRE(!obj.hasParam(name, ANARI_INT32)); REQUIRE(obj.getParam(name, 4) == 4); REQUIRE(obj.getParam(name, 4) == 4); @@ -99,6 +101,7 @@ SCENARIO( THEN("The named parameter should have the correct type and value") { REQUIRE(obj.hasParam(name)); + REQUIRE(obj.hasParam(name, ANARI_STRING)); REQUIRE(obj.getParamString(name, "") == testStr); REQUIRE(obj.getParam(name, 4) == 4); } @@ -110,6 +113,7 @@ SCENARIO( THEN("The paramter should no longer exist on the object") { REQUIRE(!obj.hasParam(name)); + REQUIRE(!obj.hasParam(name, ANARI_STRING)); REQUIRE(obj.getParamString(name, "") == ""); REQUIRE(obj.getParam(name, 4) == 4); }