Skip to content

Commit

Permalink
review cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vimes committed Aug 8, 2024
1 parent 86bc40a commit 3c88eec
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions common/stream.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <inttypes.h>
#include <string.h>

#include <map>
#include <memory>
Expand Down Expand Up @@ -291,9 +290,7 @@ inline stream_in_t::stream_in_t(const std::vector<uint8_t>& buffer) :
template<typename TType>
inline void stream_in_t::pop(TType& value)
{
if constexpr (std::is_trivially_copyable_v<TType> &&
!(std::is_pointer_v<TType> ||
std::is_reference_v<TType>))
if constexpr (std::is_trivially_copyable_v<TType>)
{
if (inBuffer.size() - inPosition < sizeof(TType))
{
Expand Down Expand Up @@ -327,7 +324,7 @@ inline void stream_in_t::pop(char* buffer, uint64_t bufferSize)
return;
}

memcpy(buffer, &this->inBuffer[inPosition], bufferSize);
std::copy(&this->inBuffer[inPosition], &this->inBuffer[inPosition] + bufferSize, buffer);
inPosition += bufferSize;
}

Expand All @@ -348,7 +345,7 @@ inline void stream_in_t::pop(std::string& value)
return;
}

value = std::string(reinterpret_cast<const char*>(&this->inBuffer[inPosition]), size);
value.assign(reinterpret_cast<const char*>(&this->inBuffer[inPosition]), size);
inPosition += size;
}

Expand Down Expand Up @@ -562,13 +559,10 @@ inline stream_out_t::stream_out_t()
template<typename TType>
inline void stream_out_t::push(const TType& value)
{
if constexpr (std::is_trivially_copyable_v<TType> &&
!(std::is_pointer_v<TType> ||
std::is_reference_v<TType>))
if constexpr (std::is_trivially_copyable_v<TType>)
{
integer_t size = outBuffer.size();
outBuffer.resize(size + sizeof(TType));
reinterpret_cast<TType&>(outBuffer[size]) = value;
auto& data = reinterpret_cast<const uint8_t(&)[sizeof(TType)]>(value);
outBuffer.insert(outBuffer.end(), std::begin(data), std::end(data));
}
else
{
Expand All @@ -582,9 +576,7 @@ inline void stream_out_t::push(const char* buffer, uint64_t bufferSize)
{
return;
}
integer_t size = outBuffer.size();
outBuffer.resize(size + bufferSize);
memcpy(&outBuffer[size], buffer, bufferSize);
outBuffer.insert(outBuffer.end(), buffer, buffer + bufferSize);
}

inline void stream_out_t::push(const std::string& value)
Expand Down

0 comments on commit 3c88eec

Please sign in to comment.