AWS IoT Device SDK C++ v2  1.33.0
AWS IoT Device SDK C++ v2
Cbor.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include <aws/common/cbor.h>
8 
9 #include <aws/crt/Types.h>
10 
11 namespace Aws
12 {
13  namespace Crt
14  {
15  namespace Cbor
16  {
35  enum class CborType
36  {
37  Unknown = AWS_CBOR_TYPE_UNKNOWN,
38  UInt = AWS_CBOR_TYPE_UINT,
39  NegInt = AWS_CBOR_TYPE_NEGINT,
40  Float = AWS_CBOR_TYPE_FLOAT,
41  Bytes = AWS_CBOR_TYPE_BYTES,
42  Text = AWS_CBOR_TYPE_TEXT,
43  ArrayStart = AWS_CBOR_TYPE_ARRAY_START,
44  MapStart = AWS_CBOR_TYPE_MAP_START,
45  Tag = AWS_CBOR_TYPE_TAG,
46  Bool = AWS_CBOR_TYPE_BOOL,
47  Null = AWS_CBOR_TYPE_NULL,
48  Undefined = AWS_CBOR_TYPE_UNDEFINED,
49  Break = AWS_CBOR_TYPE_BREAK,
50  IndefBytesStart = AWS_CBOR_TYPE_INDEF_BYTES_START,
51  IndefTextStart = AWS_CBOR_TYPE_INDEF_TEXT_START,
52  IndefArrayStart = AWS_CBOR_TYPE_INDEF_ARRAY_START,
53  IndefMapStart = AWS_CBOR_TYPE_INDEF_MAP_START,
54  };
55 
57  {
58  public:
59  CborEncoder(const CborEncoder &) = delete;
60  CborEncoder(CborEncoder &&) = delete;
61  CborEncoder &operator=(const CborEncoder &) = delete;
63 
64  CborEncoder(Allocator *allocator = ApiAllocator()) noexcept;
65  ~CborEncoder() noexcept;
66 
73  ByteCursor GetEncodedData() noexcept;
74 
78  void Reset() noexcept;
79 
86  void WriteUInt(uint64_t value) noexcept;
87 
95  void WriteNegInt(uint64_t value) noexcept;
96 
106  void WriteFloat(double value) noexcept;
107 
115  void WriteBytes(ByteCursor value) noexcept;
116 
124  void WriteText(ByteCursor value) noexcept;
125 
133  void WriteArrayStart(size_t number_entries) noexcept;
134 
144  void WriteMapStart(size_t number_entries) noexcept;
145 
155  void WriteTag(uint64_t tag_number) noexcept;
156 
160  void WriteNull() noexcept;
161 
165  void WriteUndefined() noexcept;
166 
170  void WriteBool(bool value) noexcept;
171 
177  void WriteBreak() noexcept;
178 
184  void WriteIndefBytesStart() noexcept;
185 
191  void WriteIndefTextStart() noexcept;
192 
198  void WriteIndefArrayStart() noexcept;
199 
205  void WriteIndefMapStart() noexcept;
206 
207  private:
208  struct aws_cbor_encoder *m_encoder;
209  };
210 
212  {
213 
214  public:
215  CborDecoder(const CborDecoder &) = delete;
216  CborDecoder(CborDecoder &&) = delete;
217  CborDecoder &operator=(const CborDecoder &) = delete;
219 
226  CborDecoder(ByteCursor src, Allocator *allocator = ApiAllocator()) noexcept;
227  ~CborDecoder() noexcept;
228 
235  size_t GetRemainingLength() noexcept;
236 
245  Optional<CborType> PeekType() noexcept;
246 
266  bool ConsumeNextWholeDataItem() noexcept;
267 
286  bool ConsumeNextSingleElement() noexcept;
287 
302  Optional<uint64_t> PopNextUnsignedIntVal() noexcept;
303  Optional<uint64_t> PopNextNegativeIntVal() noexcept;
304  Optional<double> PopNextFloatVal() noexcept;
305  Optional<bool> PopNextBooleanVal() noexcept;
306  Optional<ByteCursor> PopNextBytesVal() noexcept;
307  Optional<ByteCursor> PopNextTextVal() noexcept;
308 
324  Optional<uint64_t> PopNextArrayStart() noexcept;
325 
341  Optional<uint64_t> PopNextMapStart() noexcept;
342 
352  Optional<uint64_t> PopNextTagVal() noexcept;
353 
357  int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
358 
359  private:
360  struct aws_cbor_decoder *m_decoder;
361  /* Error */
362  int m_lastError;
363  };
364  } // namespace Cbor
365 
366  } // namespace Crt
367 } // namespace Aws
Aws::Crt::Cbor::CborDecoder
Definition: Cbor.h:212
Aws::Crt::Cbor::CborDecoder::operator=
CborDecoder & operator=(CborDecoder &&)=delete
Aws::Crt::ApiAllocator
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Aws::Crt::Cbor::CborDecoder::CborDecoder
CborDecoder(CborDecoder &&)=delete
Aws::Crt::LastError
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:422
Aws::Crt::Cbor::CborEncoder
Definition: Cbor.h:57
Aws::Crt::Cbor::CborType::Unknown
@ Unknown
Aws::Crt::Cbor::CborEncoder::CborEncoder
CborEncoder(const CborEncoder &)=delete
Aws::Crt::Cbor::CborEncoder::operator=
CborEncoder & operator=(CborEncoder &&)=delete
Aws::Crt::Cbor::CborEncoder::CborEncoder
CborEncoder(CborEncoder &&)=delete
Aws::Crt::Optional
Definition: Optional.h:18
Aws::Crt::Cbor::CborType
CborType
Definition: Cbor.h:36
Types.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws
Definition: Allocator.h:11
Aws::Crt::Cbor::CborEncoder::operator=
CborEncoder & operator=(const CborEncoder &)=delete
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:31
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Aws::Crt::Cbor::CborDecoder::operator=
CborDecoder & operator=(const CborDecoder &)=delete
Aws::Crt::Cbor::CborDecoder::CborDecoder
CborDecoder(const CborDecoder &)=delete