Skip to content

Commit

Permalink
Update OpenImageIO paramlist impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
furby-tm committed Aug 3, 2024
1 parent c1724ce commit 966c1c8
Showing 1 changed file with 223 additions and 1 deletion.
224 changes: 223 additions & 1 deletion Sources/OpenImageIO/libutil/paramlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ ParamValue::get_string_indexed(int index) const
if (element.vecsemantics == TypeDesc::RATIONAL
&& element.aggregate == TypeDesc::VEC2) {
const int* val = (const int*)data() + 2 * index;
out = Strutil::fmt::format("{}/{}}", val[0], val[1]);
out = Strutil::fmt::format("{}/{}", val[0], val[1]);
} else if (type() == TypeTimeCode) {
out += tostring(TypeTimeCode, data());
} else {
Expand Down Expand Up @@ -654,4 +654,226 @@ ParamValueList::merge(const ParamValueList& other, bool override)
}
}



ParamValueSpan::const_iterator
ParamValueSpan::find(ustring name, TypeDesc type, bool casesensitive) const
{
if (casesensitive) {
for (const_iterator i = cbegin(), e = cend(); i != e; ++i) {
if (i->name() == name && (type == TypeUnknown || type == i->type()))
return i;
}
} else {
for (const_iterator i = cbegin(), e = cend(); i != e; ++i) {
if (Strutil::iequals(i->name(), name)
&& (type == TypeUnknown || type == i->type()))
return i;
}
}
return cend();
}



ParamValueSpan::const_iterator
ParamValueSpan::find(string_view name, TypeDesc type, bool casesensitive) const
{
if (casesensitive) {
return find(ustring(name), type, casesensitive);
} else {
for (const_iterator i = cbegin(), e = cend(); i != e; ++i) {
if (Strutil::iequals(i->name(), name)
&& (type == TypeUnknown || type == i->type()))
return i;
}
}
return cend();
}



int
ParamValueSpan::get_int(ustring name, int defaultval, bool casesensitive,
bool convert) const
{
auto p = find(name, convert ? TypeUnknown : TypeInt, casesensitive);
return (p == cend()) ? defaultval : p->get_int(defaultval);
}



int
ParamValueSpan::get_int(string_view name, int defaultval, bool casesensitive,
bool convert) const
{
auto p = find(name, convert ? TypeUnknown : TypeInt, casesensitive);
return (p == cend()) ? defaultval : p->get_int(defaultval);
}



float
ParamValueSpan::get_float(string_view name, float defaultval,
bool casesensitive, bool convert) const
{
auto p = find(name, convert ? TypeUnknown : TypeFloat, casesensitive);
return (p == cend()) ? defaultval : p->get_float(defaultval);
}



float
ParamValueSpan::get_float(ustring name, float defaultval, bool casesensitive,
bool convert) const
{
auto p = find(name, convert ? TypeUnknown : TypeFloat, casesensitive);
return (p == cend()) ? defaultval : p->get_float(defaultval);
}



string_view
ParamValueSpan::get_string(string_view name, string_view defaultval,
bool casesensitive, bool convert) const
{
auto p = find(name, convert ? TypeUnknown : TypeString, casesensitive);
return (p == cend()) ? defaultval : string_view(p->get_ustring());
}



string_view
ParamValueSpan::get_string(ustring name, string_view defaultval,
bool casesensitive, bool convert) const
{
auto p = find(name, convert ? TypeUnknown : TypeString, casesensitive);
return (p == cend()) ? defaultval : string_view(p->get_ustring());
}



ustring
ParamValueSpan::get_ustring(string_view name, string_view defaultval,
bool casesensitive, bool convert) const
{
auto p = find(name, convert ? TypeUnknown : TypeString, casesensitive);
return (p == cend()) ? ustring(defaultval) : p->get_ustring();
}



ustring
ParamValueSpan::get_ustring(ustring name, string_view defaultval,
bool casesensitive, bool convert) const
{
auto p = find(name, convert ? TypeUnknown : TypeString, casesensitive);
return (p == cend()) ? ustring(defaultval) : p->get_ustring();
}



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
{
auto p = find(name, TypeUnknown, casesensitive);
if (p != cend()) {
return convert_type(p->type(), p->data(), type, value);
} else {
return false;
}
}



bool
ParamValueSpan::getattribute(string_view name, std::string& value,
bool casesensitive) const
{
auto p = find(name, TypeUnknown, casesensitive);
if (p != cend()) {
ustring s;
bool ok = convert_type(p->type(), p->data(), TypeString, &s);
if (ok)
value = s.string();
return ok;
} else {
return false;
}
}



bool
ParamValueSpan::getattribute_indexed(string_view name, int index, TypeDesc type,
void* value, bool casesensitive) const
{
auto p = find(name, TypeUnknown, casesensitive);
if (p != cend()) {
if (index >= int(p->type().basevalues()))
return false;
TypeDesc basetype = p->type().scalartype();
return convert_type(basetype,
(const char*)p->data() + index * basetype.size(),
type, value);
} else {
return false;
}
}



bool
ParamValueSpan::getattribute_indexed(string_view name, int index,
std::string& value,
bool casesensitive) const
{
auto p = find(name, TypeUnknown, casesensitive);
if (p != cend()) {
if (index >= int(p->type().basevalues()))
return false;
TypeDesc basetype = p->type().scalartype();
ustring s;
bool ok = convert_type(basetype,
(const char*)p->data() + index * basetype.size(),
TypeString, &s);
if (ok)
value = s.string();
return ok;
} else {
return false;
}
}



OIIO_NAMESPACE_END

0 comments on commit 966c1c8

Please sign in to comment.