Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading and writing vector value with EmptyDeployment #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions include/CommonAPI/SomeIP/InputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,29 @@ class InputStream: public CommonAPI::InputStream<InputStream> {
return (*this);
}

template<typename ElementType_,
typename std::enable_if<(std::is_same<int8_t, ElementType_>::value ||
std::is_same<uint8_t, ElementType_>::value), int>::type = 0>
COMMONAPI_EXPORT InputStream &readValue(std::vector<ElementType_> &_value,
const EmptyDeployment *_depl) {
bitAlign();

(void)_depl;

uint32_t itsSize;

_value.clear();

errorOccurred_ = _readBitValue(itsSize, 32, false);

if (!hasError()) {
ElementType_ *base = reinterpret_cast<ElementType_ *>(_readRaw(itsSize));
_value.assign(base, base + itsSize);
}

return (*this);
}

template<typename ElementType_, typename ElementDepl_,
typename std::enable_if<(!std::is_same<int8_t, ElementType_>::value &&
!std::is_same<uint8_t, ElementType_>::value), int>::type = 0>
Expand Down Expand Up @@ -381,6 +404,42 @@ class InputStream: public CommonAPI::InputStream<InputStream> {
return (*this);
}

template<typename ElementType_,
typename std::enable_if<(!std::is_same<int8_t, ElementType_>::value &&
!std::is_same<uint8_t, ElementType_>::value), int>::type = 0>
COMMONAPI_EXPORT InputStream &readValue(std::vector<ElementType_> &_value,
const EmptyDeployment *_depl) {
bitAlign();

(void)_depl;

uint32_t itsSize;

_value.clear();

errorOccurred_ = _readBitValue(itsSize, 32, false);

while (itsSize > 0)
{
size_t remainingBeforeRead = remaining_;
ElementType_ itsElement;
readValue(itsElement, static_cast<EmptyDeployment *>(nullptr));
if (hasError()) {
break;
}

_value.push_back(std::move(itsElement));

itsSize -= uint32_t(remainingBeforeRead - remaining_);
}

if (itsSize != 0) {
errorOccurred_ = true;
}

return (*this);
}

template<typename Deployment_, typename KeyType_, typename ValueType_, typename HasherType_>
COMMONAPI_EXPORT InputStream &readValue(std::unordered_map<KeyType_,
ValueType_, HasherType_> &_value,
Expand Down
45 changes: 45 additions & 0 deletions include/CommonAPI/SomeIP/OutputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ class OutputStream: public CommonAPI::OutputStream<OutputStream> {
return (*this);
}

template<typename ElementType_,
typename std::enable_if<(std::is_same<int8_t, ElementType_>::value ||
std::is_same<uint8_t, ElementType_>::value), int>::type = 0>
COMMONAPI_EXPORT OutputStream &writeValue(const std::vector<ElementType_> &_value,
const EmptyDeployment *_depl) {
bitAlign();

(void)_depl;

_writeValue(uint32_t(_value.size()), 4);
if (!hasError() && _value.size()) {
_writeRaw(reinterpret_cast<const byte_t *>(&_value[0]), _value.size());
}

return (*this);
}

template<typename ElementType_, typename ElementDepl_,
typename std::enable_if<(!std::is_same<int8_t, ElementType_>::value &&
!std::is_same<uint8_t, ElementType_>::value), int>::type = 0>
Expand Down Expand Up @@ -355,6 +372,34 @@ class OutputStream: public CommonAPI::OutputStream<OutputStream> {
return (*this);
}

template<typename ElementType_,
typename std::enable_if<(!std::is_same<int8_t, ElementType_>::value &&
!std::is_same<uint8_t, ElementType_>::value), int>::type = 0>
COMMONAPI_EXPORT OutputStream &writeValue(const std::vector<ElementType_> &_value,
const EmptyDeployment *_depl) {
bitAlign();

(void)_depl;

pushPosition();
// Length field placeholder
_writeValue(static_cast<uint32_t>(0), 4);
pushPosition(); // Start of vector data

for (auto i : _value) {
writeValue(i, static_cast<EmptyDeployment *>(nullptr));
if (hasError()) {
break;
}
}

// Write number of written bytes to placeholder position
uint32_t length = uint32_t(getPosition() - popPosition());
_writeValueAt(length, popPosition());

return (*this);
}

template<typename Deployment_, typename KeyType_, typename ValueType_, typename HasherType_>
COMMONAPI_EXPORT OutputStream &writeValue(const std::unordered_map<KeyType_, ValueType_, HasherType_> &_value,
const Deployment_ *_depl) {
Expand Down