diff options
author | Barnabás Pőcze <barnabas.pocze@ideasonboard.com> | 2025-04-16 21:44:25 +0200 |
---|---|---|
committer | Barnabás Pőcze <barnabas.pocze@ideasonboard.com> | 2025-04-21 16:24:16 +0200 |
commit | 3e4de5f54e9c60de9808fc49ff5783b899c9b8b8 (patch) | |
tree | 34979c6701165719c5e0b80219615b662ef41d34 | |
parent | 83543f08d528ab600300f347d26d8e79f2829730 (diff) |
apps: cam: capture_script: Simplify bool array parsing
`std::vector<bool>` is a specialization that implements a dynamic
bit vector, therefore it is not suitable to provide storage for
an array of `bool`. Hence a statically sized array is used when
parsing an array of boolean values.
Instead, use the array overload of `std::make_unique` since the
size is known beforehand.
Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
-rw-r--r-- | src/apps/cam/capture_script.cpp | 36 |
1 files changed, 7 insertions, 29 deletions
diff --git a/src/apps/cam/capture_script.cpp b/src/apps/cam/capture_script.cpp index fc1dfa75..e7e69960 100644 --- a/src/apps/cam/capture_script.cpp +++ b/src/apps/cam/capture_script.cpp @@ -8,6 +8,7 @@ #include "capture_script.h" #include <iostream> +#include <memory> #include <stdio.h> #include <stdlib.h> @@ -521,45 +522,22 @@ ControlValue CaptureScript::parseArrayControl(const ControlId *id, case ControlTypeNone: break; case ControlTypeBool: { - /* - * This is unpleasant, but we cannot use an std::vector<> as its - * boolean type overload does not allow to access the raw data, - * as boolean values are stored in a bitmask for efficiency. - * - * As we need a contiguous memory region to wrap in a Span<>, - * use an array instead but be strict about not overflowing it - * by limiting the number of controls we can store. - * - * Be loud but do not fail, as the issue would present at - * runtime and it's not fatal. - */ - static constexpr unsigned int kMaxNumBooleanControls = 1024; - std::array<bool, kMaxNumBooleanControls> values; - unsigned int idx = 0; + auto values = std::make_unique<bool[]>(repr.size()); - for (const std::string &s : repr) { - bool val; + for (std::size_t i = 0; i < repr.size(); i++) { + const auto &s = repr[i]; if (s == "true") { - val = true; + values[i] = true; } else if (s == "false") { - val = false; + values[i] = false; } else { unpackFailure(id, s); return value; } - - if (idx == kMaxNumBooleanControls) { - std::cerr << "Cannot parse more than " - << kMaxNumBooleanControls - << " boolean controls" << std::endl; - break; - } - - values[idx++] = val; } - value = Span<bool>(values.data(), idx); + value = Span<bool>(values.get(), repr.size()); break; } case ControlTypeByte: { |