Skip to content

Commit

Permalink
feat(core): add writeExact and readExact to Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Jul 28, 2024
1 parent 80054d7 commit a8dad24
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
28 changes: 20 additions & 8 deletions core/include/cubos/core/memory/stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,31 @@ namespace cubos::core::memory
/// @brief Reads data from the stream.
/// @param data Buffer to read data into.
/// @param size Size of the buffer.
/// @note Data may be read partially, @ref readExact is recommended.
/// @return Number of bytes read.
virtual std::size_t read(void* data, std::size_t size) = 0;

/// @brief Writes data to the stream.
/// @param data Buffer to write data from.
/// @param size Size of the buffer.
/// @note Data may be written partially, @ref writeExact is recommended.
/// @return Number of bytes written.
virtual std::size_t write(const void* data, std::size_t size) = 0;

/// @brief Reads data from the stream with the exact specified size.
/// @param data Buffer to read data into.
/// @param size Size of the buffer.
/// @note This function will only return after buffer is completed or an error occurs.
/// @return Whether reading was sucessful.
bool readExact(void* data, std::size_t size);

/// @brief Writes data to the stream with the exact specified size.
/// @param data Buffer to write data from.
/// @param size Size of the buffer.
/// @note This function will only return after whole buffer is written or an error occurs.
/// @return Whether writing was sucessful.
bool writeExact(const void* data, std::size_t size);

/// @brief Gets the current position in the stream.
/// @return Current position in the stream, or SIZE_MAX if the position is unknown.
virtual std::size_t tell() const = 0;
Expand Down Expand Up @@ -94,16 +110,14 @@ namespace cubos::core::memory
/// @param value Value to print.
/// @param base Base to use.
template <typename T>
requires(::std::is_integral_v<T> && ::std::is_signed_v<T>)
void print(T value, std::size_t base = 10);
requires(::std::is_integral_v<T>&& ::std::is_signed_v<T>) void print(T value, std::size_t base = 10);

/// @brief Prints a signed integer to the stream.
/// @tparam T Integer type.
/// @param value Value to print.
/// @param base Base to use.
template <typename T>
requires(::std::is_integral_v<T> && !::std::is_signed_v<T>)
inline void print(T value, std::size_t base = 10);
requires(::std::is_integral_v<T> && !::std::is_signed_v<T>) inline void print(T value, std::size_t base = 10);

/// @brief Prints a 64 bit signed integer to the stream.
/// @param value Value to print.
Expand Down Expand Up @@ -229,15 +243,13 @@ namespace cubos::core::memory
// Implementation

template <typename T>
requires(std::is_integral_v<T> && std::is_signed_v<T>)
inline void Stream::print(T value, std::size_t base)
requires(std::is_integral_v<T>&& std::is_signed_v<T>) inline void Stream::print(T value, std::size_t base)
{
this->print(static_cast<int64_t>(value), base);
}

template <typename T>
requires(std::is_integral_v<T> && !std::is_signed_v<T>)
inline void Stream::print(T value, std::size_t base)
requires(std::is_integral_v<T> && !std::is_signed_v<T>) inline void Stream::print(T value, std::size_t base)
{
this->print(static_cast<uint64_t>(value), base);
}
Expand Down
38 changes: 38 additions & 0 deletions core/src/memory/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,41 @@ void Stream::ignore(std::size_t size)
this->get();
}
}

bool Stream::writeExact(const void* data, std::size_t size)
{
const char* dataPtr = static_cast<const char*>(data);
std::size_t totalBytesWritten = 0;

while (totalBytesWritten < size)
{
auto bytesWritten = write(dataPtr + totalBytesWritten, size - totalBytesWritten);
if (bytesWritten <= 0)
{
return false;
}

totalBytesWritten += bytesWritten;
}

return true;
}

bool Stream::readExact(void* data, std::size_t size)
{
char* dataPtr = static_cast<char*>(data);
std::size_t totalBytesRead = 0;

while (totalBytesRead < size)
{
auto bytesRead = read(dataPtr + totalBytesRead, size - totalBytesRead);
if (bytesRead <= 0)
{
return false;
}

totalBytesRead += bytesRead;
}

return true;
}

0 comments on commit a8dad24

Please sign in to comment.