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
1 change: 0 additions & 1 deletion src/ystdlib/io_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ cpp_library(
WriterInterface.hpp
PRIVATE_SOURCES
ReaderInterface.cpp
WriterInterface.cpp
TESTS_SOURCES
test/test_ReaderInterface.cpp
test/test_WriterInterface.cpp
Expand Down
38 changes: 0 additions & 38 deletions src/ystdlib/io_interface/WriterInterface.cpp

This file was deleted.

70 changes: 41 additions & 29 deletions src/ystdlib/io_interface/WriterInterface.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef YSTDLIB_IO_INTERFACE_WRITERINTERFACE_HPP
#define YSTDLIB_IO_INTERFACE_WRITERINTERFACE_HPP
// NOLINTBEGIN

// TODO: https://github.com/y-scope/ystdlib-cpp/issues/50
// NOLINTNEXTLINE(misc-include-cleaner)
#include <sys/types.h>

#include <cstddef>
#include <string>
Expand All @@ -10,69 +13,78 @@
namespace ystdlib::io_interface {
class WriterInterface {
public:
// Types
class OperationFailed : public std::exception {
public:
OperationFailed(ErrorCode error_code) {}
};
// Constructor
WriterInterface() = default;

// Delete copy constructor and assignment operator
WriterInterface(WriterInterface const&) = delete;
auto operator=(WriterInterface const&) -> WriterInterface& = delete;

// Default move constructor and assignment operator
WriterInterface(WriterInterface&&) noexcept = default;
auto operator=(WriterInterface&&) noexcept -> WriterInterface& = default;

// Destructor
virtual ~WriterInterface() = default;

// Methods
/**
* Writes the given data to the underlying medium
* Writes the given data to the underlying medium.
* @param data
* @param data_length
*/
virtual void write(char const* data, size_t data_length) = 0;
virtual void flush() = 0;
virtual ErrorCode try_seek_from_begin(size_t pos) = 0;
virtual ErrorCode try_seek_from_current(off_t offset) = 0;
virtual ErrorCode try_get_pos(size_t& pos) const = 0;
[[nodiscard]] virtual auto write(char const* data, size_t data_length) -> ErrorCode = 0;

/**
* Writes a numeric value
* @param val Value to write
* Forces any buffered output data to be available at the underlying medium.
*/
[[nodiscard]] virtual auto flush() -> ErrorCode = 0;

/**
* Writes a numeric value to the underlying medium.
* @param val
*/
template <typename ValueType>
void write_numeric_value(ValueType value);
[[nodiscard]] auto write_numeric_value(ValueType value) -> ErrorCode;

/**
* Writes a character to the underlying medium
* Writes a character to the underlying medium.
* @param c
*/
void write_char(char c);
[[nodiscard]] virtual auto write_char(char c) -> ErrorCode { return write(&c, 1); }

/**
* Writes a string to the underlying medium
* Writes a string to the underlying medium.
* @param str
*/
void write_string(std::string const& str);
[[nodiscard]] virtual auto write_string(std::string const& str) -> ErrorCode {
return write(str.c_str(), str.length());
}

/**
* Seeks from the beginning to the given position
* Seeks from the beginning to the given position.
* @param pos
*/
void seek_from_begin(size_t pos);
[[nodiscard]] virtual auto seek_from_begin(size_t pos) -> ErrorCode = 0;

/**
* Offsets from the current position by the given amount
* Seeks from the current position to the next position by the given offset amount.
* @param offset
*/
void seek_from_current(off_t offset);
// TODO: https://github.com/y-scope/ystdlib-cpp/issues/50
// NOLINTNEXTLINE(misc-include-cleaner)
[[nodiscard]] virtual auto seek_from_current(off_t offset) -> ErrorCode = 0;

/**
* Gets the current position of the write head
* @return Position of the write head
* @param pos Returns the current position of the read pointer.
*/
size_t get_pos() const;
[[nodiscard]] virtual auto get_pos(size_t& pos) -> ErrorCode = 0;
};

template <typename ValueType>
void WriterInterface::write_numeric_value(ValueType val) {
write(reinterpret_cast<char*>(&val), sizeof(val));
auto WriterInterface::write_numeric_value(ValueType val) -> ErrorCode {
return write(static_cast<char const*>(&val), sizeof(ValueType));
}
} // namespace ystdlib::io_interface

// NOLINTEND
#endif // YSTDLIB_IO_INTERFACE_WRITERINTERFACE_HPP