Skip to content

Commit

Permalink
paramlist.h: Add ParamValueSpan::get_bool() (AcademySoftwareFoundatio…
Browse files Browse the repository at this point in the history
…n#4303)

Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz authored Jun 27, 2024
1 parent 0d83c87 commit 1f0f072
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/include/OpenImageIO/paramlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,15 @@ class OIIO_UTIL_API ParamValueSpan : public cspan<ParamValue> {
ustring get_ustring(ustring name, string_view defaultval = string_view(),
bool casesensitive = false, bool convert = true) const;

/// Search for the attribute and return its "truth-like" value: false if
/// it exists but is empty, or is a numeric value equal to 0, or a string
/// value that is "0", "no", "off", or "false". Otherwise, any non-empty
/// value returns true.
bool get_bool(string_view name, bool defaultval = false,
bool casesensitive = false) const;
bool get_bool(ustring name, bool defaultval = false,
bool casesensitive = false) const;

/// Does the span contain the named attribute?
bool contains(string_view name, TypeDesc type = TypeUnknown,
bool casesensitive = false) const
Expand Down
28 changes: 28 additions & 0 deletions src/libutil/paramlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,34 @@ ParamValueSpan::get_ustring(ustring name, string_view defaultval,



bool
ParamValueSpan::get_bool(ustring name, bool defaultval,
bool casesensitive) const
{
auto p = find(name, TypeUnknown, casesensitive);
if (p == cend())
return defaultval;
if (p->type().basetype == TypeDesc::INT)
return p->get_int() ? 1 : 0;
return Strutil::eval_as_bool(p->get_string());
}



bool
ParamValueSpan::get_bool(string_view name, bool defaultval,
bool casesensitive) const
{
auto p = find(name, TypeUnknown, casesensitive);
if (p == cend())
return defaultval;
if (p->type().basetype == TypeDesc::INT)
return p->get_int() ? 1 : 0;
return Strutil::eval_as_bool(p->get_string());
}



bool
ParamValueSpan::getattribute(string_view name, TypeDesc type, void* value,
bool casesensitive) const
Expand Down
8 changes: 8 additions & 0 deletions src/libutil/paramlist_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ test_implied_construction()
pvl["f"] = 2.5f;
pvl["s"] = "forty two";
pvl["i42s"] = "42";
pvl["zero"] = 0;
print_pvspan("Testing of PVS from PVL", pvl);

ParamValueSpan pvs(pvl);
Expand All @@ -511,6 +512,13 @@ test_implied_construction()
OIIO_CHECK_EQUAL(pvs.get_float("i"), 1.0f);
OIIO_CHECK_EQUAL(pvs.get_float("i42s"), 42.0f);
OIIO_CHECK_EQUAL(pvs.get_string("i"), "1");
OIIO_CHECK_EQUAL(pvs.get_string("zero"), "0");
OIIO_CHECK_EQUAL(pvs.get_int("zero"), 0);
OIIO_CHECK_EQUAL(pvs.get_bool("zero"), false);
OIIO_CHECK_EQUAL(pvs.get_bool("i"), true);
OIIO_CHECK_EQUAL(pvs.get_bool("f"), true);
OIIO_CHECK_EQUAL(pvs.get_bool("s"), true);
OIIO_CHECK_EQUAL(pvs.get_bool("unknown"), false);
}


Expand Down

0 comments on commit 1f0f072

Please sign in to comment.