summaryrefslogtreecommitdiff
path: root/test/yaml-parser.cpp
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-07-26 04:33:19 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-07-28 13:47:13 +0300
commitfeb8c9be78474599f85f37ae733e564a4bda4e06 (patch)
tree1a33fdfa4f75b8a8798f341506ab36b5bc27fab0 /test/yaml-parser.cpp
parent22ffeae04de2e7ce6b2476a35233c790beafb67f (diff)
libcamera: yaml_parser: Replace ok flag to get() with std::optional
The YamlObject::get() function takes a default value and an optional bool ok flag to handle parsing errors. This ad-hoc mechanism complicates error handling in callers. A better API is possible by dropping the default value and ok flag and returning an std::optional. Not only does it simplify the calls, it also lets callers handle errors through the standard std::optional class instead of the current ad-hoc mechanism. Provide a get() wrapper around std::optional::value_or() to further simplify callers that don't need any specific error handling. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Naushir Patuck <naush@raspberrypi.com> Tested-by: Naushir Patuck <naush@raspberrypi.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Diffstat (limited to 'test/yaml-parser.cpp')
-rw-r--r--test/yaml-parser.cpp71
1 files changed, 35 insertions, 36 deletions
diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp
index 38f84823..ebb654f2 100644
--- a/test/yaml-parser.cpp
+++ b/test/yaml-parser.cpp
@@ -148,7 +148,6 @@ protected:
}
/* Test string object */
- bool ok;
auto &strObj = (*root)["string"];
if (strObj.isDictionary()) {
@@ -161,27 +160,27 @@ protected:
return TestFail;
}
- if (strObj.get<string>("", &ok) != "libcamera" || !ok) {
+ if (strObj.get<string>().value_or("") != "libcamera") {
cerr << "String object parse as wrong content" << std::endl;
return TestFail;
}
- if (strObj.get<int32_t>(-1, &ok) != -1 || ok) {
+ if (strObj.get<int32_t>()) {
cerr << "String object parse as integer" << std::endl;
return TestFail;
}
- if (strObj.get<uint32_t>(1, &ok) != 1 || ok) {
+ if (strObj.get<uint32_t>()) {
cerr << "String object parse as unsigned integer" << std::endl;
return TestFail;
}
- if (strObj.get<double>(1.0, &ok) != 1.0 || ok) {
+ if (strObj.get<double>()) {
cerr << "String object parse as double" << std::endl;
return TestFail;
}
- if (strObj.get<Size>(Size(0, 0), &ok) != Size(0, 0) || ok) {
+ if (strObj.get<Size>()) {
cerr << "String object parse as Size" << std::endl;
return TestFail;
}
@@ -199,27 +198,27 @@ protected:
return TestFail;
}
- if (int32Obj.get<int32_t>(-100, &ok) != -100 || !ok) {
+ if (int32Obj.get<int32_t>().value_or(0) != -100) {
cerr << "Integer object parse as wrong value" << std::endl;
return TestFail;
}
- if (int32Obj.get<string>("", &ok) != "-100" || !ok) {
+ if (int32Obj.get<string>().value_or("") != "-100") {
cerr << "Integer object fail to parse as string" << std::endl;
return TestFail;
}
- if (int32Obj.get<double>(1.0, &ok) != -100.0 || !ok) {
+ if (int32Obj.get<double>().value_or(0.0) != -100.0) {
cerr << "Integer object fail to parse as double" << std::endl;
return TestFail;
}
- if (int32Obj.get<uint32_t>(1, &ok) != 1 || ok) {
+ if (int32Obj.get<uint32_t>()) {
cerr << "Negative integer object parse as unsigned integer" << std::endl;
return TestFail;
}
- if (int32Obj.get<Size>(Size(0, 0), &ok) != Size(0, 0) || ok) {
+ if (int32Obj.get<Size>()) {
cerr << "Integer object parse as Size" << std::endl;
return TestFail;
}
@@ -237,27 +236,27 @@ protected:
return TestFail;
}
- if (uint32Obj.get<int32_t>(-1, &ok) != 100 || !ok) {
+ if (uint32Obj.get<int32_t>().value_or(0) != 100) {
cerr << "Unsigned integer object fail to parse as integer" << std::endl;
return TestFail;
}
- if (uint32Obj.get<string>("", &ok) != "100" || !ok) {
+ if (uint32Obj.get<string>().value_or("") != "100") {
cerr << "Unsigned integer object fail to parse as string" << std::endl;
return TestFail;
}
- if (uint32Obj.get<double>(1.0, &ok) != 100.0 || !ok) {
+ if (uint32Obj.get<double>().value_or(0.0) != 100.0) {
cerr << "Unsigned integer object fail to parse as double" << std::endl;
return TestFail;
}
- if (uint32Obj.get<uint32_t>(100, &ok) != 100 || !ok) {
+ if (uint32Obj.get<uint32_t>().value_or(0) != 100) {
cerr << "Unsigned integer object parsed as wrong value" << std::endl;
return TestFail;
}
- if (uint32Obj.get<Size>(Size(0, 0), &ok) != Size(0, 0) || ok) {
+ if (uint32Obj.get<Size>()) {
cerr << "Unsigned integer object parsed as Size" << std::endl;
return TestFail;
}
@@ -275,27 +274,27 @@ protected:
return TestFail;
}
- if (doubleObj.get<string>("", &ok) != "3.14159" || !ok) {
+ if (doubleObj.get<string>().value_or("") != "3.14159") {
cerr << "Double object fail to parse as string" << std::endl;
return TestFail;
}
- if (doubleObj.get<double>(1.0, &ok) != 3.14159 || !ok) {
+ if (doubleObj.get<double>().value_or(0.0) != 3.14159) {
cerr << "Double object parse as wrong value" << std::endl;
return TestFail;
}
- if (doubleObj.get<int32_t>(-1, &ok) != -1 || ok) {
+ if (doubleObj.get<int32_t>()) {
cerr << "Double object parse as integer" << std::endl;
return TestFail;
}
- if (doubleObj.get<uint32_t>(1, &ok) != 1 || ok) {
+ if (doubleObj.get<uint32_t>()) {
cerr << "Double object parse as unsigned integer" << std::endl;
return TestFail;
}
- if (doubleObj.get<Size>(Size(0, 0), &ok) != Size(0, 0) || ok) {
+ if (doubleObj.get<Size>()) {
cerr << "Double object parse as Size" << std::endl;
return TestFail;
}
@@ -313,27 +312,27 @@ protected:
return TestFail;
}
- if (sizeObj.get<string>("", &ok) != "" || ok) {
+ if (sizeObj.get<string>()) {
cerr << "Size object parse as string" << std::endl;
return TestFail;
}
- if (sizeObj.get<double>(1.0, &ok) != 1.0 || ok) {
+ if (sizeObj.get<double>()) {
cerr << "Size object parse as double" << std::endl;
return TestFail;
}
- if (sizeObj.get<int32_t>(-1, &ok) != -1 || ok) {
+ if (sizeObj.get<int32_t>()) {
cerr << "Size object parse as integer" << std::endl;
return TestFail;
}
- if (sizeObj.get<uint32_t>(1, &ok) != 1 || ok) {
+ if (sizeObj.get<uint32_t>()) {
cerr << "Size object parse as unsigned integer" << std::endl;
return TestFail;
}
- if (sizeObj.get<Size>(Size(0, 0), &ok) != Size(1920, 1080) || !ok) {
+ if (sizeObj.get<Size>().value_or(Size(0, 0)) != Size(1920, 1080)) {
cerr << "Size object parse as wrong value" << std::endl;
return TestFail;
}
@@ -351,27 +350,27 @@ protected:
return TestFail;
}
- if (listObj.get<string>("", &ok) != "" || ok) {
+ if (listObj.get<string>()) {
cerr << "List object parse as string" << std::endl;
return TestFail;
}
- if (listObj.get<double>(1.0, &ok) != 1.0 || ok) {
+ if (listObj.get<double>()) {
cerr << "List object parse as double" << std::endl;
return TestFail;
}
- if (listObj.get<int32_t>(-1, &ok) != -1 || ok) {
+ if (listObj.get<int32_t>()) {
cerr << "List object parse as integer" << std::endl;
return TestFail;
}
- if (listObj.get<uint32_t>(1, &ok) != 1 || ok) {
+ if (listObj.get<uint32_t>()) {
cerr << "List object parse as unsigne integer" << std::endl;
return TestFail;
}
- if (listObj.get<Size>(Size(0, 0), &ok) != Size(0, 0) || ok) {
+ if (listObj.get<Size>()) {
cerr << "String list object parse as Size" << std::endl;
return TestFail;
}
@@ -424,27 +423,27 @@ protected:
return TestFail;
}
- if (dictObj.get<string>("", &ok) != "" || ok) {
+ if (dictObj.get<string>()) {
cerr << "Dictionary object parse as string" << std::endl;
return TestFail;
}
- if (dictObj.get<double>(1.0, &ok) != 1.0 || ok) {
+ if (dictObj.get<double>()) {
cerr << "Dictionary object parse as double" << std::endl;
return TestFail;
}
- if (dictObj.get<int32_t>(-1, &ok) != -1 || ok) {
+ if (dictObj.get<int32_t>()) {
cerr << "Dictionary object parse as integer" << std::endl;
return TestFail;
}
- if (dictObj.get<uint32_t>(1, &ok) != 1 || ok) {
+ if (dictObj.get<uint32_t>()) {
cerr << "Dictionary object parse as unsigned integer" << std::endl;
return TestFail;
}
- if (dictObj.get<Size>(Size(0, 0), &ok) != Size(0, 0) || ok) {
+ if (dictObj.get<Size>()) {
cerr << "Dictionary object parse as Size" << std::endl;
return TestFail;
}