Skip to content
Merged
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
16 changes: 8 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Options
option(BUFFERSTREAM_BUILD_TESTS "Build tests" OFF)
option(BUFFERSTREAM_BUILD_TESTS "Build tests" ${PROJECT_IS_TOP_LEVEL})

# Create library
add_library(${PROJECT_NAME} INTERFACE
Expand All @@ -27,21 +27,21 @@ if(BUFFERSTREAM_BUILD_TESTS)
FetchContent_MakeAvailable(googletest)
enable_testing()

set(CMAKE_COMPILE_WARNING_AS_ERROR ON)

add_executable(${BUFFERSTREAM_TEST_NAME}
"${CMAKE_CURRENT_SOURCE_DIR}/test/BufferStream.cpp")

target_link_libraries(${BUFFERSTREAM_TEST_NAME} PUBLIC
gtest_main ${PROJECT_NAME})

if (MSVC)
target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE
/W4)
# Enable warnings, warnings are errors
if(MSVC)
target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE /W4 /WX)
else()
target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE
-Wall -Wextra -Wpedantic)
target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE -Wall -Wextra -Wpedantic)
endif()
set_target_properties(${BUFFERSTREAM_TEST_NAME} PROPERTIES
COMPILE_WARNING_AS_ERROR ON
LINK_WARNING_AS_ERROR ON)

include(GoogleTest)
gtest_discover_tests(${BUFFERSTREAM_TEST_NAME})
Expand Down
10 changes: 7 additions & 3 deletions include/BufferStream.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// ReSharper disable CppDiscardedPostfixOperatorResult
// ReSharper disable CppRedundantTypenameKeyword
// ReSharper disable CppUnusedIncludeDirective

#pragma once

#include <algorithm>
Expand Down Expand Up @@ -178,7 +182,7 @@ class BufferStream {

template<BufferStreamPODType T = std::byte>
BufferStream& skip_u(std::uint64_t n = 1) {
return this->skip(static_cast<std::int64_t>(n));
return this->skip<T>(static_cast<std::int64_t>(n));
}

[[nodiscard]] const std::byte* data() const {
Expand Down Expand Up @@ -626,7 +630,7 @@ class BufferStream {
BufferStream& write(std::string_view obj, bool addNullTerminator = true, std::uint64_t maxSize = 0) {
static_assert(BufferStreamPODByteType<typename std::string_view::value_type>, "String char width must be 1 byte!");

bool bundledTerminator = !obj.empty() && obj[obj.size() - 1] == '\0';
const bool bundledTerminator = !obj.empty() && obj[obj.size() - 1] == '\0';
if (maxSize == 0) {
// Add true, bundled true - one null terminator
// Add false, bundled true - null terminator removed
Expand Down Expand Up @@ -672,7 +676,7 @@ class BufferStream {

obj.reserve(n);
for (std::uint64_t i = 0; i < n; i++) {
char temp = this->read<char>();
const auto temp = this->read<char>();
if (temp == '\0' && stopOnNullTerminator) {
// Read the required number of characters and exit
this->skip_u<char>(n - i - 1);
Expand Down
17 changes: 9 additions & 8 deletions include/FileStream.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ReSharper disable CppRedundantTypenameKeyword

#pragma once

#include <filesystem>
Expand All @@ -24,8 +26,7 @@ class FileStream {
explicit FileStream(const std::filesystem::path& path, int options = OPT_READ)
: useExceptions(true)
, bigEndian(false) {
std::error_code ec;
if (((options & OPT_CREATE_IF_NONEXISTENT) && !std::filesystem::exists(path, ec)) || ec) {
if (std::error_code ec; (options & OPT_CREATE_IF_NONEXISTENT && !std::filesystem::exists(path, ec)) || ec) {
if (!std::filesystem::exists(path.parent_path(), ec)) {
std::filesystem::create_directories(path.parent_path(), ec);
}
Expand Down Expand Up @@ -108,7 +109,7 @@ class FileStream {

template<BufferStreamPODType T = std::byte>
FileStream& skip_in_u(std::uint64_t n = 1) {
return this->skip_in(static_cast<std::int64_t>(n));
return this->skip_in<T>(static_cast<std::int64_t>(n));
}

template<BufferStreamPODType T = std::byte>
Expand All @@ -121,7 +122,7 @@ class FileStream {

template<BufferStreamPODType T = std::byte>
FileStream& skip_out_u(std::uint64_t n = 1) {
return this->skip_out(static_cast<std::int64_t>(n));
return this->skip_out<T>(static_cast<std::int64_t>(n));
}

[[nodiscard]] std::uint64_t tell_in() {
Expand Down Expand Up @@ -330,7 +331,7 @@ class FileStream {
}) {
obj.reserve(n);
}
for (int i = 0; i < n; i++) {
for (uint64_t i = 0; i < n; i++) {
obj.push_back(this->read<typename T::value_type>());
}
}
Expand Down Expand Up @@ -494,7 +495,7 @@ class FileStream {
FileStream& write(std::string_view obj, bool addNullTerminator = true, std::uint64_t maxSize = 0) {
static_assert(sizeof(typename std::string_view::value_type) == 1, "String char width must be 1 byte!");

bool bundledTerminator = !obj.empty() && obj[obj.size() - 1] == '\0';
const bool bundledTerminator = !obj.empty() && obj[obj.size() - 1] == '\0';
if (maxSize == 0) {
// Add true, bundled true - one null terminator
// Add false, bundled true - null terminator removed
Expand Down Expand Up @@ -531,8 +532,8 @@ class FileStream {
}

obj.reserve(n);
for (int i = 0; i < n; i++) {
char temp = this->read<char>();
for (uint64_t i = 0; i < n; i++) {
const auto temp = this->read<char>();
if (temp == '\0' && stopOnNullTerminator) {
// Read the required number of characters and exit
this->skip_in_u<char>(n - i - 1);
Expand Down
1 change: 1 addition & 0 deletions test/BufferStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <deque>

#include <BufferStream.h>
#include <FileStream.h>

struct POD {
int x;
Expand Down
Loading