summaryrefslogtreecommitdiff
path: root/src/cbor/include/cbor_encoder.hpp
blob: 8f5214f6266af29b68c87e555ce1b3d1eaa9da45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once

#include <cstdint>

#include "cbor.h"

namespace cbor {

class Encoder {
 public:
  enum ContainerType { CONTAINER_ARRAY, CONTAINER_MAP };
  Encoder(ContainerType type,
          uint32_t container_len,
          uint8_t* buffer,
          size_t buffer_len);

  template <typename T>
  auto WriteKeyValue(const std::string& key, const T& val) -> void {
    WriteValue(key);
    WriteValue(val);
  }

  auto WriteValue(const std::string& val) -> void;
  auto WriteValue(uint32_t val) -> void;
  auto WriteValue(int32_t val) -> void;

  auto Finish() -> cpp::result<size_t, CborError>;

  Encoder(const Encoder&) = delete;
  Encoder& operator=(const Encoder&) = delete;

 private:
  CborEncoder root_encoder_;
  CborEncoder container_encoder_;

  CborError error_ = CborNoError;
};

}  // namespace cbor