From 64ed07d4d057411c3fb669dc1faa27e61efd2811 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Thu, 13 Jun 2024 02:52:15 +0300 Subject: ipa: libipa: vector: Specialize YamlObject getter Implement a specialization of the YamlObject::Getter structure to support deserializing ipa::Vector objects from YAML data. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder --- src/ipa/libipa/vector.cpp | 17 +++++++++++++++++ src/ipa/libipa/vector.h | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'src') diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp index 5de4ae48..4e987d82 100644 --- a/src/ipa/libipa/vector.cpp +++ b/src/ipa/libipa/vector.cpp @@ -148,6 +148,23 @@ namespace ipa { * \return True if the two vectors are not equal, false otherwise */ +#ifndef __DOXYGEN__ +bool vectorValidateYaml(const YamlObject &obj, unsigned int size) +{ + if (!obj.isList()) + return false; + + if (obj.size() != size) { + LOG(Vector, Error) + << "Wrong number of values in YAML vector: expected " + << size << ", got " << obj.size(); + return false; + } + + return true; +} +#endif /* __DOXYGEN__ */ + } /* namespace ipa */ } /* namespace libcamera */ diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h index 7c444363..4b2fe581 100644 --- a/src/ipa/libipa/vector.h +++ b/src/ipa/libipa/vector.h @@ -180,6 +180,10 @@ bool operator!=(const Vector &lhs, const Vector &rhs) return !(lhs == rhs); } +#ifndef __DOXYGEN__ +bool vectorValidateYaml(const YamlObject &obj, unsigned int size); +#endif /* __DOXYGEN__ */ + } /* namespace ipa */ #ifndef __DOXYGEN__ @@ -195,6 +199,27 @@ std::ostream &operator<<(std::ostream &out, const ipa::Vector &v) return out; } + +template +struct YamlObject::Getter> { + std::optional> get(const YamlObject &obj) const + { + if (!ipa::vectorValidateYaml(obj, Rows)) + return std::nullopt; + + ipa::Vector vector; + + unsigned int i = 0; + for (const YamlObject &entry : obj.asList()) { + const auto value = entry.get(); + if (!value) + return std::nullopt; + vector[i++] = *value; + } + + return vector; + } +}; #endif /* __DOXYGEN__ */ } /* namespace libcamera */ -- cgit v1.2.1