Skip to content

Commit

Permalink
ParamValueList enhancements (#2527)
Browse files Browse the repository at this point in the history
* New `find_pv()` method wraps `find()` but returns a pointer rather than
  an iterator, and nullptr if the attribute is not found.

* Fix `add_or_replace()` -- it was previously failing to "replace" if the
  new attribute had a different type than the existing one.
  • Loading branch information
lgritz committed Mar 25, 2020
1 parent ba42dc3 commit 5bcc3e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/include/OpenImageIO/paramlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,22 @@ class OIIO_API ParamValueList : public std::vector<ParamValue> {
const_iterator find(ustring name, TypeDesc type = TypeDesc::UNKNOWN,
bool casesensitive = true) const;

/// Search for the first entry with matching name, etc., and return
/// a pointer to it, or nullptr if it is not found.
ParamValue* find_pv(string_view name, TypeDesc type = TypeDesc::UNKNOWN,
bool casesensitive = true)
{
iterator f = find(name, type, casesensitive);
return f != end() ? &(*f) : nullptr;
}
const ParamValue* find_pv(string_view name,
TypeDesc type = TypeDesc::UNKNOWN,
bool casesensitive = true) const
{
const_iterator f = find(name, type, casesensitive);
return f != cend() ? &(*f) : nullptr;
}

/// Case insensitive search for an integer, with default if not found.
/// Automatically will return an int even if the data is really
/// unsigned, short, or byte, but not float. It will retrive from a
Expand Down
4 changes: 2 additions & 2 deletions src/libutil/paramlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ ParamValueList::contains(string_view name, TypeDesc type,
void
ParamValueList::add_or_replace(const ParamValue& pv, bool casesensitive)
{
iterator p = find(pv.name(), pv.type(), casesensitive);
iterator p = find(pv.name(), TypeUnknown, casesensitive);
if (p != end())
*p = pv;
else
Expand All @@ -556,7 +556,7 @@ ParamValueList::add_or_replace(const ParamValue& pv, bool casesensitive)
void
ParamValueList::add_or_replace(ParamValue&& pv, bool casesensitive)
{
iterator p = find(pv.name(), pv.type(), casesensitive);
iterator p = find(pv.name(), TypeUnknown, casesensitive);
if (p != end())
*p = pv;
else
Expand Down

0 comments on commit 5bcc3e1

Please sign in to comment.