1
0

Support reading typed arrays (not just writing them)

This commit is contained in:
Geraint 2025-06-23 23:16:38 +01:00
parent 253e7ffbf0
commit e295fadeb4
2 changed files with 31 additions and 11 deletions

View File

@ -79,9 +79,9 @@ namespace stfx {
.unit("ms", 2, -1, 1) .unit("ms", 2, -1, 1)
.unit("ms", 1, -10, 10) .unit("ms", 1, -10, 10)
.unit("ms", 0, -1000, 1000) .unit("ms", 0, -1000, 1000)
.unit("seconds", 2, sToMs, msToS, -1e3, 1e3) .unit(" sec", 2, sToMs, msToS, -1e3, 1e3)
.unit("seconds", 1, sToMs, msToS, -1e4, 1e4) .unit(" sec", 1, sToMs, msToS, -1e4, 1e4)
.unit("seconds", 0, sToMs, msToS); .unit(" sec", 0, sToMs, msToS);
} }
template<class RangeParam> template<class RangeParam>
RangeParam & rangeSec(RangeParam &param) { RangeParam & rangeSec(RangeParam &param) {
@ -89,9 +89,9 @@ namespace stfx {
.unit("ms", 2, msToS, sToMs, -1e-3, 1e-3) .unit("ms", 2, msToS, sToMs, -1e-3, 1e-3)
.unit("ms", 1, msToS, sToMs, -1e-2, 1e-2) .unit("ms", 1, msToS, sToMs, -1e-2, 1e-2)
.unit("ms", 0, msToS, sToMs, -1e-1, 1e-1) .unit("ms", 0, msToS, sToMs, -1e-1, 1e-1)
.unit("seconds", 2, -1, 1) .unit(" sec", 2, -1, 1)
.unit("seconds", 1, -10, 10) .unit(" sec", 1, -10, 10)
.unit("seconds", 0); .unit(" sec", 0);
} }
} }

View File

@ -73,7 +73,7 @@ struct StorageCborWriter {
writeValue(value); writeValue(value);
} }
protected: private:
signalsmith::cbor::CborWriter cbor; signalsmith::cbor::CborWriter cbor;
#define STORAGE_BASIC_INT(V) \ #define STORAGE_BASIC_INT(V) \
@ -165,6 +165,10 @@ struct StorageCborReader {
} }
private: private:
Cbor cbor;
const char *filterKeyBytes = nullptr;
size_t filterKeyLength = 0;
template<class Obj> template<class Obj>
void readValue(Obj &obj) { void readValue(Obj &obj) {
if (compact) { if (compact) {
@ -225,11 +229,27 @@ private:
}); });
array.resize(length); array.resize(length);
} }
protected:
Cbor cbor;
const char *filterKeyBytes = nullptr; #define STORAGE_TYPED_ARRAY(T) \
size_t filterKeyLength = 0; void readValue(std::vector<T> &array) { \
if (cbor.isTypedArray()) { \
array.resize(cbor.typedArrayLength()); \
cbor.readTypedArray(array); \
} else { \
readValue<std::vector<T>>(array); \
} \
}
STORAGE_TYPED_ARRAY(uint8_t)
STORAGE_TYPED_ARRAY(int8_t)
STORAGE_TYPED_ARRAY(uint16_t)
STORAGE_TYPED_ARRAY(int16_t)
STORAGE_TYPED_ARRAY(uint32_t)
STORAGE_TYPED_ARRAY(int32_t)
STORAGE_TYPED_ARRAY(uint64_t)
STORAGE_TYPED_ARRAY(int64_t)
STORAGE_TYPED_ARRAY(float)
STORAGE_TYPED_ARRAY(double)
#undef STORAGE_TYPED_ARRAY
static bool keyMatch(const char *key, const char *filterKeyBytes, size_t filterKeyLength) { static bool keyMatch(const char *key, const char *filterKeyBytes, size_t filterKeyLength) {
for (size_t i = 0; i < filterKeyLength; ++i) { for (size_t i = 0; i < filterKeyLength; ++i) {