Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix const-correctness of ParameterizedObject get methods #250

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/helium/utility/ParameterizedObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -76,20 +83,31 @@ 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(),
[&](const Param &p) { return p.first == name; });

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
26 changes: 16 additions & 10 deletions src/helium/utility/ParameterizedObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -37,31 +41,31 @@ struct ParameterizedObject
// access ANARIObject or ANARIString parameters, see special methods for
// getting parameters of those types.
template <typename T>
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
// an object parameter does _not_ influence lifetime considerations -- devices
// should consider using `helium::IntrusivePtr<>` to guarantee correct
// lifetime handling.
template <typename T>
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'.
Expand All @@ -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;
};
Expand Down Expand Up @@ -110,7 +115,8 @@ inline void ParameterizedObject::setParam(
}

template <typename T>
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<T>::value;
static_assert(!anari::isObject(type),
Expand All @@ -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<bool>() : valIfNotFound;
}

template <typename T>
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<T>() : nullptr;
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_helium_ParameterizedObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(name, 4) == v);
REQUIRE(obj.getParam<short>(name, 4) == 4);

Expand All @@ -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<int>(name, 4) == 4);
REQUIRE(obj.getParam<short>(name, 4) == 4);

Expand Down Expand Up @@ -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<short>(name, 4) == 4);
}
Expand All @@ -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<short>(name, 4) == 4);
}
Expand Down
Loading