Skip to content

Commit

Permalink
comments addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
TingDaoK committed Jun 20, 2024
1 parent 1bf490b commit 9caf9f3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 145 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ endif()

file(GLOB AWS_CRT_CPP_HEADERS
${AWS_CRT_PUBLIC_HEADERS}
${AWS_CRT_EXTERNAL_HEADERS}
)

file(GLOB AWS_CRT_SRC
Expand Down
65 changes: 35 additions & 30 deletions include/aws/crt/cbor/Cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Aws
/**
* The types used by APIs, not 1:1 with major types.
* It's an extension for CBOR major type in RFC8949 section 3.1.
* Major type 0 - Uint
* Major type 0 - UInt
* Major type 1 - NegInt
* Major type 2 - Bytes / IndefBytesStart
* Major type 3 - Text / IndefTextStart
Expand All @@ -35,7 +35,7 @@ namespace Aws
enum class CborType
{
Unknown = AWS_CBOR_TYPE_UNKNOWN,
Uint = AWS_CBOR_TYPE_UINT,
UInt = AWS_CBOR_TYPE_UINT,
NegInt = AWS_CBOR_TYPE_NEGINT,
Float = AWS_CBOR_TYPE_FLOAT,
Bytes = AWS_CBOR_TYPE_BYTES,
Expand All @@ -61,7 +61,7 @@ namespace Aws
CborEncoder &operator=(const CborEncoder &) = delete;
CborEncoder &operator=(CborEncoder &&) = delete;

CborEncoder(Allocator *allocator) noexcept;
CborEncoder(Allocator *allocator = ApiAllocator()) noexcept;
~CborEncoder() noexcept;

/**
Expand All @@ -83,7 +83,7 @@ namespace Aws
*
* @param value value to encode.
*/
void WriteUint(uint64_t value) noexcept;
void WriteUInt(uint64_t value) noexcept;

/**
* Encode a AWS_CBOR_TYPE_NEGINT value to "smallest possible" in encoder's buffer.
Expand Down Expand Up @@ -223,7 +223,7 @@ namespace Aws
* @param allocator
* @param src The src data to decode from.
*/
CborDecoder(Allocator *allocator, ByteCursor src) noexcept;
CborDecoder(ByteCursor src, Allocator *allocator = ApiAllocator()) noexcept;
~CborDecoder() noexcept;

/**
Expand All @@ -238,18 +238,19 @@ namespace Aws
* Decode the next element and store it in the decoder cache if there was no element cached.
* If there was an element cached, just return the type of the cached element.
*
* @param out_type
* @return success/failure
* @return If successful, return the type of next element
* If not, return will be none and Aws::Crt::LastError() can be
* used to retrieve CRT error code.
*/
bool PeekType(CborType &out_type) noexcept;
Optional<CborType> PeekType() noexcept;

/**
* Consume the next data item, includes all the content within the data item.
*
* As an example for the following CBOR, this function will consume all the data
* as it's only one CBOR data item, an indefinite map with 2 key, value pair:
* 0xbf6346756ef563416d7421ff
* BF -- Start indefinite-length map
* BF -- Start indefinite-length map
* 63 -- First key, UTF-8 string length 3
* 46756e -- "Fun"
* F5 -- First value, true
Expand All @@ -260,7 +261,7 @@ namespace Aws
*
* Notes: this function will not ensure the data item is well-formed.
*
* @return success/failure
* @return true if the operation succeed, false otherwise and LastError() will contain the errorCode.
*/
bool ConsumeNextWholeDataItem() noexcept;

Expand All @@ -271,7 +272,7 @@ namespace Aws
* 0xBF, "Start indefinite-length map", not any content of the map represented.
* The next element to decode will start from 0x63.
* 0xbf6346756ef563416d7421ff
* BF -- Start indefinite-length map
* BF -- Start indefinite-length map
* 63 -- First key, UTF-8 string length 3
* 46756e -- "Fun"
* F5 -- First value, true
Expand All @@ -280,29 +281,30 @@ namespace Aws
* 21 -- Second value, -2
* FF -- "break"
*
* @return success/failure
* @return true if the operation succeed, false otherwise and LastError() will contain the errorCode.
*/
bool ConsumeNextSingleElement() noexcept;

/**
* Get the next element based on the type. If the next element doesn't match the expected type, an error
* will be raised. If the next element has already been cached, it will consume the cached item when no
* error was returned. Specifically:
* - Uint - PopNextUnsignedIntVal
* - UInt - PopNextUnsignedIntVal
* - NegInt - PopNextNegativeIntVal, it represents (-1 - &out)
* - Float - PopNextFloatVal
* - Bytes - PopNextBytesVal
* - Text - PopNextTextVal
*
* @param out
* @return success/failure
* @return If successful, return the next element
* If not, return will be none and Aws::Crt::LastError() can be
* used to retrieve CRT error code.
*/
bool PopNextUnsignedIntVal(uint64_t &out) noexcept;
bool PopNextNegativeIntVal(uint64_t &out) noexcept;
bool PopNextFloatVal(double &out) noexcept;
bool PopNextBooleanVal(bool &out) noexcept;
bool PopNextBytesVal(ByteCursor &out) noexcept;
bool PopNextTextVal(ByteCursor &out) noexcept;
Optional<uint64_t> PopNextUnsignedIntVal() noexcept;
Optional<uint64_t> PopNextNegativeIntVal() noexcept;
Optional<double> PopNextFloatVal() noexcept;
Optional<bool> PopNextBooleanVal() noexcept;
Optional<ByteCursor> PopNextBytesVal() noexcept;
Optional<ByteCursor> PopNextTextVal() noexcept;

/**
* Get the next ArrayStart element. Only consume the ArrayStart element and set the size of array to
Expand All @@ -315,10 +317,11 @@ namespace Aws
* - Call ConsumeNextSingleElement to pop the indefinite-length start.
* - Decode the next data item until Break is read.
*
* @param out_size Store the size of array if succeeded.
* @return success/failure
* @return If successful, return the size of array
* If not, return will be none and Aws::Crt::LastError() can be
* used to retrieve CRT error code.
*/
bool PopNextArrayStart(uint64_t &out_size) noexcept;
Optional<uint64_t> PopNextArrayStart() noexcept;

/**
* Get the next MapStart element. Only consume the MapStart element and set the size of array to
Expand All @@ -331,20 +334,22 @@ namespace Aws
* - Call ConsumeNextSingleElement to pop the indefinite-length start.
* - Decode the next data item until Break is read.
*
* @param out_size Store the size of map if succeeded.
* @return success/failure
* @return If successful, return the size of map
* If not, return will be none and Aws::Crt::LastError() can be
* used to retrieve CRT error code.
*/
bool PopNextMapStart(uint64_t &out_size) noexcept;
Optional<uint64_t> PopNextMapStart() noexcept;

/**
* Get the next Tag element. Only consume the Tag element and set the tag value to out_tag_val,
* not the content of the tagged value. The next CBOR data item will be the content of the tagged value
* for a valid CBOR data.
*
* @param out_tag_val Store the tag value if succeeded.
* @return success/failure
* @return If successful, return the tag value
* If not, return will be none and Aws::Crt::LastError() can be
* used to retrieve CRT error code.
*/
bool PopNextTagVal(uint64_t &out_tag_val) noexcept;
Optional<uint64_t> PopNextTagVal() noexcept;

/**
* @return the value of the last aws error encountered by operations on this instance.
Expand Down
74 changes: 41 additions & 33 deletions source/cbor/Cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Aws
aws_cbor_encoder_reset(m_encoder);
}

void CborEncoder::WriteUint(uint64_t value) noexcept
void CborEncoder::WriteUInt(uint64_t value) noexcept
{
aws_cbor_encoder_write_uint(m_encoder, value);
}
Expand Down Expand Up @@ -119,7 +119,7 @@ namespace Aws
* CborDecoder
*
*****************************************************/
CborDecoder::CborDecoder(Crt::Allocator *allocator, ByteCursor src) noexcept
CborDecoder::CborDecoder(ByteCursor src, Crt::Allocator *allocator) noexcept
{
m_decoder = aws_cbor_decoder_new(allocator, src);
}
Expand All @@ -134,16 +134,15 @@ namespace Aws
return aws_cbor_decoder_get_remaining_length(m_decoder);
}

bool CborDecoder::PeekType(CborType &out_type) noexcept
Optional<CborType> CborDecoder::PeekType() noexcept
{
enum aws_cbor_type out_type_c = AWS_CBOR_TYPE_UNKNOWN;
if (aws_cbor_decoder_peek_type(m_decoder, &out_type_c) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<CborType>();
}
out_type = (CborType)out_type_c;
return true;
return Optional<CborType>((CborType)out_type_c);
}
bool CborDecoder::ConsumeNextWholeDataItem() noexcept
{
Expand All @@ -165,94 +164,103 @@ namespace Aws
return true;
}

bool CborDecoder::PopNextUnsignedIntVal(uint64_t &out) noexcept
Optional<uint64_t> CborDecoder::PopNextUnsignedIntVal() noexcept
{
uint64_t out = 0;
if (aws_cbor_decoder_pop_next_unsigned_int_val(m_decoder, &out) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<uint64_t>();
}
return true;
return Optional<uint64_t>(out);
}

bool CborDecoder::PopNextNegativeIntVal(uint64_t &out) noexcept
Optional<uint64_t> CborDecoder::PopNextNegativeIntVal() noexcept
{
uint64_t out = 0;
if (aws_cbor_decoder_pop_next_negative_int_val(m_decoder, &out) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<uint64_t>();
}
return true;
return Optional<uint64_t>(out);
}

bool CborDecoder::PopNextFloatVal(double &out) noexcept
Optional<double> CborDecoder::PopNextFloatVal() noexcept
{
double out = 0;
if (aws_cbor_decoder_pop_next_float_val(m_decoder, &out) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<double>();
}
return true;
return Optional<double>(out);
}

bool CborDecoder::PopNextBooleanVal(bool &out) noexcept
Optional<bool> CborDecoder::PopNextBooleanVal() noexcept
{
bool out = false;
if (aws_cbor_decoder_pop_next_boolean_val(m_decoder, &out) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<bool>();
}
return true;
return Optional<bool>(out);
}

bool CborDecoder::PopNextBytesVal(ByteCursor &out) noexcept
Optional<ByteCursor> CborDecoder::PopNextBytesVal() noexcept
{
ByteCursor out = {0};
if (aws_cbor_decoder_pop_next_bytes_val(m_decoder, &out) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<ByteCursor>();
}
return true;
return Optional<ByteCursor>(out);
}

bool CborDecoder::PopNextTextVal(ByteCursor &out) noexcept
Optional<ByteCursor> CborDecoder::PopNextTextVal() noexcept
{
ByteCursor out = {0};
if (aws_cbor_decoder_pop_next_text_val(m_decoder, &out) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<ByteCursor>();
}
return true;
return Optional<ByteCursor>(out);
}

bool CborDecoder::PopNextArrayStart(uint64_t &out_size) noexcept
Optional<uint64_t> CborDecoder::PopNextArrayStart() noexcept
{
uint64_t out_size = 0;
if (aws_cbor_decoder_pop_next_array_start(m_decoder, &out_size) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<uint64_t>();
}
return true;
return Optional<uint64_t>(out_size);
}

bool CborDecoder::PopNextMapStart(uint64_t &out_size) noexcept
Optional<uint64_t> CborDecoder::PopNextMapStart() noexcept
{
uint64_t out_size = 0;
if (aws_cbor_decoder_pop_next_map_start(m_decoder, &out_size) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<uint64_t>();
}
return true;
return Optional<uint64_t>(out_size);
}

bool CborDecoder::PopNextTagVal(uint64_t &out_tag_val) noexcept
Optional<uint64_t> CborDecoder::PopNextTagVal() noexcept
{
uint64_t out_tag_val = 0;
if (aws_cbor_decoder_pop_next_tag_val(m_decoder, &out_tag_val) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
return Optional<uint64_t>();
}
return true;
return Optional<uint64_t>(out_tag_val);
}

} // namespace Cbor
Expand Down
Loading

0 comments on commit 9caf9f3

Please sign in to comment.