encoding: change Block::fromBuffer return type
Block::fromBuffer is changed from taking constructed Block as output parameter
to returning the constructed Block as part of returned tuple.
Old API becomes deprecated.
refs #2553
Change-Id: I64610353b8c0780ee09ebe9089c539fa27506a5e
diff --git a/src/encoding/block.cpp b/src/encoding/block.cpp
index ce13427..4353c96 100644
--- a/src/encoding/block.cpp
+++ b/src/encoding/block.cpp
@@ -222,35 +222,31 @@
return dataBlock(type, buf, length);
}
-bool
-Block::fromBuffer(const ConstBufferPtr& wire, size_t offset, Block& block)
+std::tuple<bool, Block>
+Block::fromBuffer(ConstBufferPtr buffer, size_t offset)
{
- Buffer::const_iterator tempBegin = wire->begin() + offset;
+ Buffer::const_iterator tempBegin = buffer->begin() + offset;
uint32_t type;
- bool isOk = tlv::readType(tempBegin, wire->end(), type);
+ bool isOk = tlv::readType(tempBegin, buffer->end(), type);
if (!isOk)
- return false;
+ return std::make_tuple(false, Block());
uint64_t length;
- isOk = tlv::readVarNumber(tempBegin, wire->end(), length);
+ isOk = tlv::readVarNumber(tempBegin, buffer->end(), length);
if (!isOk)
- return false;
+ return std::make_tuple(false, Block());
- if (length > static_cast<uint64_t>(wire->end() - tempBegin))
- {
- return false;
- }
+ if (length > static_cast<uint64_t>(buffer->end() - tempBegin))
+ return std::make_tuple(false, Block());
- block = Block(wire, type,
- wire->begin() + offset, tempBegin + length,
- tempBegin, tempBegin + length);
-
- return true;
+ return std::make_tuple(true, Block(buffer, type,
+ buffer->begin() + offset, tempBegin + length,
+ tempBegin, tempBegin + length));
}
-bool
-Block::fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block)
+std::tuple<bool, Block>
+Block::fromBuffer(const uint8_t* buffer, size_t maxSize)
{
const uint8_t* tempBegin = buffer;
const uint8_t* tempEnd = buffer + maxSize;
@@ -258,24 +254,21 @@
uint32_t type = 0;
bool isOk = tlv::readType(tempBegin, tempEnd, type);
if (!isOk)
- return false;
+ return std::make_tuple(false, Block());
uint64_t length;
isOk = tlv::readVarNumber(tempBegin, tempEnd, length);
if (!isOk)
- return false;
+ return std::make_tuple(false, Block());
if (length > static_cast<uint64_t>(tempEnd - tempBegin))
- {
- return false;
- }
+ return std::make_tuple(false, Block());
BufferPtr sharedBuffer = make_shared<Buffer>(buffer, tempBegin + length);
- block = Block(sharedBuffer, type,
- sharedBuffer->begin(), sharedBuffer->end(),
- sharedBuffer->begin() + (tempBegin - buffer), sharedBuffer->end());
-
- return true;
+ return std::make_tuple(true,
+ Block(sharedBuffer, type,
+ sharedBuffer->begin(), sharedBuffer->end(),
+ sharedBuffer->begin() + (tempBegin - buffer), sharedBuffer->end()));
}
void