diff options
Diffstat (limited to 'src/cbor_wrapper/include')
| -rw-r--r-- | src/cbor_wrapper/include/cbor_decoder.hpp | 113 | ||||
| -rw-r--r-- | src/cbor_wrapper/include/cbor_encoder.hpp | 52 |
2 files changed, 165 insertions, 0 deletions
diff --git a/src/cbor_wrapper/include/cbor_decoder.hpp b/src/cbor_wrapper/include/cbor_decoder.hpp new file mode 100644 index 00000000..193e7843 --- /dev/null +++ b/src/cbor_wrapper/include/cbor_decoder.hpp @@ -0,0 +1,113 @@ +#pragma once + +#include <stdint.h> + +#include <cstdint> +#include <string> + +#include "cbor.h" +#include "result.hpp" + +namespace cbor { + +class ArrayDecoder { + public: + static auto Create(uint8_t* buffer, size_t buffer_len) + -> cpp::result<std::unique_ptr<ArrayDecoder>, CborError>; + + static auto Create(CborValue& root) + -> cpp::result<std::unique_ptr<ArrayDecoder>, CborError>; + + ArrayDecoder() {} + + template <typename T> + auto NextValue() -> cpp::result<T, CborError>; + + template <typename T> + auto NextValue(bool (*is_valid)(const CborValue*), + CborError (*parse)(const CborValue*, T*)) + -> cpp::result<T, CborError> { + if (error_ != CborNoError) { + return cpp::fail(error_); + } + if (!is_valid(&it_)) { + error_ = CborErrorIllegalType; + return cpp::fail(error_); + } + T ret; + error_ = parse(&it_, &ret); + if (error_ != CborNoError) { + return cpp::fail(error_); + } + error_ = cbor_value_advance(&it_); + if (error_ != CborNoError) { + return cpp::fail(error_); + } + return ret; + } + + auto Failed() -> CborError { return error_; } + + auto Iterator() -> CborValue& { return it_; } + + ArrayDecoder(const ArrayDecoder&) = delete; + ArrayDecoder& operator=(const ArrayDecoder&) = delete; + + private: + CborParser parser_; + CborValue root_; + + CborValue it_; + CborError error_ = CborNoError; +}; + +class MapDecoder { + public: + static auto Create(uint8_t* buffer, size_t buffer_len) + -> cpp::result<std::unique_ptr<MapDecoder>, CborError>; + + static auto Create(CborValue& root) + -> cpp::result<std::unique_ptr<MapDecoder>, CborError>; + + MapDecoder() {} + + template <typename T> + auto FindValue(const std::string& key) -> std::optional<T>; + + template <typename T> + auto FindValue(const std::string& key, + bool (*is_valid)(const CborValue*), + CborError (*parse)(const CborValue*, T*)) -> std::optional<T> { + if (error_ != CborNoError) { + return {}; + } + CborValue val; + if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) { + return {}; + } + if (!is_valid(&val)) { + error_ = CborErrorIllegalType; + return {}; + } + T ret; + error_ = parse(&val, &ret); + if (error_ != CborNoError) { + return {}; + } + return ret; + } + + auto Failed() -> CborError { return error_; } + + MapDecoder(const MapDecoder&) = delete; + MapDecoder& operator=(const MapDecoder&) = delete; + + private: + CborParser parser_; + CborValue root_; + + CborValue it_; + CborError error_ = CborNoError; +}; + +} // namespace cbor diff --git a/src/cbor_wrapper/include/cbor_encoder.hpp b/src/cbor_wrapper/include/cbor_encoder.hpp new file mode 100644 index 00000000..cc57e8a4 --- /dev/null +++ b/src/cbor_wrapper/include/cbor_encoder.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include <cstdint> +#include <optional> +#include <string> + +#include "cbor.h" +#include "result.hpp" + +namespace cbor { + +enum ContainerType { CONTAINER_ARRAY, CONTAINER_MAP }; + +class Encoder { + public: + Encoder(ContainerType type, + uint32_t container_len, + uint8_t* buffer, + size_t buffer_len); + + auto WriteValue(const std::string& val) -> void; + auto WriteValue(uint32_t val) -> void; + auto WriteValue(int32_t val) -> void; + + template <typename T> + auto WriteKeyValue(const std::string& key, const T&& val) -> void { + WriteValue(key); + WriteValue(val); + } + + template <typename T> + auto WriteKeyValue(const std::string& key, const std::optional<T>& val) + -> void { + if (val) { + WriteKeyValue<T>(key, val.value()); + } + } + + auto Finish() -> cpp::result<size_t, CborError>; + + Encoder(const Encoder&) = delete; + Encoder& operator=(const Encoder&) = delete; + + private: + uint8_t* buffer_; + CborEncoder root_encoder_; + CborEncoder container_encoder_; + + CborError error_ = CborNoError; +}; + +} // namespace cbor |
