summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-08-15 23:38:19 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-08-21 21:47:19 +0300
commita69958fcd6c7bf394859f5c5177c4a685f512f45 (patch)
tree867bce46496139e111f847b03a4918f81554e8b8 /src
parentdc688f1d8869798d5e856810bcdd55a22968a443 (diff)
libcamera: yaml_parser: Enable YamlObject::get() for int8_t and uint8_t
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@ideasonboard.com> Reviewed-by: Paul Elder <paul.elder@ideasboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Diffstat (limited to 'src')
-rw-r--r--src/libcamera/yaml_parser.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
index f928b723..85a52c05 100644
--- a/src/libcamera/yaml_parser.cpp
+++ b/src/libcamera/yaml_parser.cpp
@@ -132,6 +132,61 @@ std::optional<bool> YamlObject::get() const
}
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
{
if (type_ != Type::Value)
@@ -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;