[libcamera-devel] [PATCH v4 5/9] libcamera: yaml_parser: Enable YamlObject::get() for int8_t and uint8_t
paul.elder at ideasonboard.com
paul.elder at ideasonboard.com
Thu Aug 18 14:24:54 CEST 2022
On Tue, Aug 16, 2022 at 04:54:10AM +0300, Laurent Pinchart via libcamera-devel wrote:
> The YamlObject::get() function template is implemented for 16-bit and
> 32-bit integers. Add an 8-bit specialization that will be used in the
> rkisp1 IPA module, and extend the unit tests accordingly.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder at ideasonboard.com>
> ---
> include/libcamera/internal/yaml_parser.h | 4 ++
> src/libcamera/yaml_parser.cpp | 59 ++++++++++++++++
> test/yaml-parser.cpp | 86 ++++++++++++++++++++----
> 3 files changed, 137 insertions(+), 12 deletions(-)
>
> diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h
> index 5ba777d364fa..8ca71df867ab 100644
> --- a/include/libcamera/internal/yaml_parser.h
> +++ b/include/libcamera/internal/yaml_parser.h
> @@ -166,6 +166,8 @@ public:
> std::enable_if_t<
> std::is_same_v<bool, T> ||
> std::is_same_v<double, T> ||
> + std::is_same_v<int8_t, T> ||
> + std::is_same_v<uint8_t, T> ||
> std::is_same_v<int16_t, T> ||
> std::is_same_v<uint16_t, T> ||
> std::is_same_v<int32_t, T> ||
> @@ -188,6 +190,8 @@ public:
> std::enable_if_t<
> std::is_same_v<bool, T> ||
> std::is_same_v<double, T> ||
> + std::is_same_v<int8_t, T> ||
> + std::is_same_v<uint8_t, T> ||
> std::is_same_v<int16_t, T> ||
> std::is_same_v<uint16_t, T> ||
> std::is_same_v<int32_t, T> ||
> diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
> index f928b7238a19..85a52c05e682 100644
> --- a/src/libcamera/yaml_parser.cpp
> +++ b/src/libcamera/yaml_parser.cpp
> @@ -131,6 +131,61 @@ std::optional<bool> YamlObject::get() const
> return std::nullopt;
> }
>
> +template<>
> +std::optional<int8_t> YamlObject::get() const
> +{
> + if (type_ != Type::Value)
> + return std::nullopt;
> +
> + if (value_ == "")
> + return std::nullopt;
> +
> + char *end;
> +
> + errno = 0;
> + long value = std::strtol(value_.c_str(), &end, 10);
> +
> + if ('\0' != *end || errno == ERANGE ||
> + value < std::numeric_limits<int8_t>::min() ||
> + value > std::numeric_limits<int8_t>::max())
> + return std::nullopt;
> +
> + return value;
> +}
> +
> +template<>
> +std::optional<uint8_t> YamlObject::get() const
> +{
> + if (type_ != Type::Value)
> + return std::nullopt;
> +
> + if (value_ == "")
> + return std::nullopt;
> +
> + /*
> + * libyaml parses all scalar values as strings. When a string has
> + * leading spaces before a minus sign, for example " -10", strtoul
> + * skips leading spaces, accepts the leading minus sign, and the
> + * calculated digits are negated as if by unary minus. Rule it out in
> + * case the user gets a large number when the value is negative.
> + */
> + std::size_t found = value_.find_first_not_of(" \t");
> + if (found != std::string::npos && value_[found] == '-')
> + return std::nullopt;
> +
> + char *end;
> +
> + errno = 0;
> + unsigned long value = std::strtoul(value_.c_str(), &end, 10);
> +
> + if ('\0' != *end || errno == ERANGE ||
> + value < std::numeric_limits<uint8_t>::min() ||
> + value > std::numeric_limits<uint8_t>::max())
> + return std::nullopt;
> +
> + return value;
> +}
> +
> template<>
> std::optional<int16_t> YamlObject::get() const
> {
> @@ -310,6 +365,8 @@ template<typename T,
> std::enable_if_t<
> std::is_same_v<bool, T> ||
> std::is_same_v<double, T> ||
> + std::is_same_v<int8_t, T> ||
> + std::is_same_v<uint8_t, T> ||
> std::is_same_v<int16_t, T> ||
> std::is_same_v<uint16_t, T> ||
> std::is_same_v<int32_t, T> ||
> @@ -336,6 +393,8 @@ std::optional<std::vector<T>> YamlObject::getList() const
>
> template std::optional<std::vector<bool>> YamlObject::getList<bool>() const;
> template std::optional<std::vector<double>> YamlObject::getList<double>() const;
> +template std::optional<std::vector<int8_t>> YamlObject::getList<int8_t>() const;
> +template std::optional<std::vector<uint8_t>> YamlObject::getList<uint8_t>() const;
> template std::optional<std::vector<int16_t>> YamlObject::getList<int16_t>() const;
> template std::optional<std::vector<uint16_t>> YamlObject::getList<uint16_t>() const;
> template std::optional<std::vector<int32_t>> YamlObject::getList<int32_t>() const;
> diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp
> index 28f8cc8822b3..2d92463aed98 100644
> --- a/test/yaml-parser.cpp
> +++ b/test/yaml-parser.cpp
> @@ -24,6 +24,8 @@ using namespace std;
> static const string testYaml =
> "string: libcamera\n"
> "double: 3.14159\n"
> + "int8_t: -100\n"
> + "uint8_t: 100\n"
> "int16_t: -1000\n"
> "uint16_t: 1000\n"
> "int32_t: -100000\n"
> @@ -76,6 +78,8 @@ protected:
>
> enum class Type {
> String,
> + Int8,
> + UInt8,
> Int16,
> UInt16,
> Int32,
> @@ -90,10 +94,13 @@ protected:
> {
> bool isList = type == Type::List || type == Type::Size;
> bool isScalar = !isList && type != Type::Dictionary;
> + bool isInteger8 = type == Type::Int8 || type == Type::UInt8;
> bool isInteger16 = type == Type::Int16 || type == Type::UInt16;
> bool isInteger32 = type == Type::Int32 || type == Type::UInt32;
> - bool isInteger = isInteger16 || isInteger32;
> - bool isSigned = type == Type::Int16 || type == Type::Int32;
> + bool isIntegerUpTo16 = isInteger8 || isInteger16;
> + bool isIntegerUpTo32 = isIntegerUpTo16 || isInteger32;
> + bool isSigned = type == Type::Int8 || type == Type::Int16 ||
> + type == Type::Int32;
>
> if ((isScalar && !obj.isValue()) || (!isScalar && obj.isValue())) {
> std::cerr
> @@ -124,35 +131,49 @@ protected:
> return TestFail;
> }
>
> - if (!isInteger16 && obj.get<int16_t>()) {
> + if (!isInteger8 && obj.get<int8_t>()) {
> + std::cerr
> + << "Object " << name << " didn't fail to parse as "
> + << "int8_t" << std::endl;
> + return TestFail;
> + }
> +
> + if ((!isInteger8 || isSigned) && obj.get<uint8_t>()) {
> + std::cerr
> + << "Object " << name << " didn't fail to parse as "
> + << "uint8_t" << std::endl;
> + return TestFail;
> + }
> +
> + if (!isIntegerUpTo16 && obj.get<int16_t>()) {
> std::cerr
> << "Object " << name << " didn't fail to parse as "
> << "int16_t" << std::endl;
> return TestFail;
> }
>
> - if ((!isInteger16 || isSigned) && obj.get<uint16_t>()) {
> + if ((!isIntegerUpTo16 || isSigned) && obj.get<uint16_t>()) {
> std::cerr
> << "Object " << name << " didn't fail to parse as "
> << "uint16_t" << std::endl;
> return TestFail;
> }
>
> - if (!isInteger && obj.get<int32_t>()) {
> + if (!isIntegerUpTo32 && obj.get<int32_t>()) {
> std::cerr
> << "Object " << name << " didn't fail to parse as "
> << "int32_t" << std::endl;
> return TestFail;
> }
>
> - if ((!isInteger || isSigned) && obj.get<uint32_t>()) {
> + if ((!isIntegerUpTo32 || isSigned) && obj.get<uint32_t>()) {
> std::cerr
> << "Object " << name << " didn't fail to parse as "
> << "uint32_t" << std::endl;
> return TestFail;
> }
>
> - if (!isInteger && type != Type::Double && obj.get<double>()) {
> + if (!isIntegerUpTo32 && type != Type::Double && obj.get<double>()) {
> std::cerr
> << "Object " << name << " didn't fail to parse as "
> << "double" << std::endl;
> @@ -174,8 +195,10 @@ protected:
> {
> uint64_t unsignedValue = static_cast<uint64_t>(value);
> std::string strValue = std::to_string(value);
> + bool isInteger8 = type == Type::Int8 || type == Type::UInt8;
> bool isInteger16 = type == Type::Int16 || type == Type::UInt16;
> - bool isSigned = type == Type::Int16 || type == Type::Int32;
> + bool isSigned = type == Type::Int8 || type == Type::Int16 ||
> + type == Type::Int32;
>
> /* All integers can be parsed as strings or double. */
>
> @@ -195,7 +218,27 @@ protected:
> return TestFail;
> }
>
> - if (isInteger16) {
> + if (isInteger8) {
> + if (obj.get<int8_t>().value_or(0) != value ||
> + obj.get<int8_t>(0) != value) {
> + std::cerr
> + << "Object " << name << " failed to parse as "
> + << "int8_t" << std::endl;
> + return TestFail;
> + }
> + }
> +
> + if (isInteger8 && !isSigned) {
> + if (obj.get<uint8_t>().value_or(0) != unsignedValue ||
> + obj.get<uint8_t>(0) != unsignedValue) {
> + std::cerr
> + << "Object " << name << " failed to parse as "
> + << "uint8_t" << std::endl;
> + return TestFail;
> + }
> + }
> +
> + if (isInteger8 || isInteger16) {
> if (obj.get<int16_t>().value_or(0) != value ||
> obj.get<int16_t>(0) != value) {
> std::cerr
> @@ -205,7 +248,7 @@ protected:
> }
> }
>
> - if (isInteger16 && !isSigned) {
> + if ((isInteger8 || isInteger16) && !isSigned) {
> if (obj.get<uint16_t>().value_or(0) != unsignedValue ||
> obj.get<uint16_t>(0) != unsignedValue) {
> std::cerr
> @@ -272,8 +315,9 @@ protected:
> }
>
> std::vector<const char *> rootElemNames = {
> - "string", "double", "int16_t", "uint16_t", "int32_t",
> - "uint32_t", "size", "list", "dictionary", "level1",
> + "string", "double", "int8_t", "uint8_t", "int16_t",
> + "uint16_t", "int32_t", "uint32_t", "size", "list",
> + "dictionary", "level1",
> };
>
> for (const char *name : rootElemNames) {
> @@ -296,6 +340,24 @@ protected:
> return TestFail;
> }
>
> + /* Test int8_t object */
> + auto &int8Obj = (*root)["int8_t"];
> +
> + if (testObjectType(int8Obj, "int8_t", Type::Int8) != TestPass)
> + return TestFail;
> +
> + if (testIntegerObject(int8Obj, "int8_t", Type::Int8, -100) != TestPass)
> + return TestFail;
> +
> + /* Test uint8_t object */
> + auto &uint8Obj = (*root)["uint8_t"];
> +
> + if (testObjectType(uint8Obj, "uint8_t", Type::UInt8) != TestPass)
> + return TestFail;
> +
> + if (testIntegerObject(uint8Obj, "uint8_t", Type::UInt8, 100) != TestPass)
> + return TestFail;
> +
> /* Test int16_t object */
> auto &int16Obj = (*root)["int16_t"];
>
> --
> Regards,
>
> Laurent Pinchart
>
More information about the libcamera-devel
mailing list