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

refactor(core): Fix clang-tidy warnings in the streaming compressor interface and its implementations; Modernize and refactor test-StreamingCompression for conciseness. #599

Merged
merged 12 commits into from
Nov 23, 2024
1 change: 1 addition & 0 deletions components/core/src/clp/TraceableException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TraceableException : public std::exception {
// Macros
// Define a version of __FILE__ that's relative to the source directory
#ifdef SOURCE_PATH_SIZE
// NOLINTNEXTLINE
Bill-hbrhbr marked this conversation as resolved.
Show resolved Hide resolved
#define __FILENAME__ ((__FILE__) + SOURCE_PATH_SIZE)
#else
// We don't know the source path size, so just default to __FILE__
Expand Down
44 changes: 32 additions & 12 deletions components/core/src/clp/streaming_compression/Compressor.hpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,81 @@
#ifndef CLP_STREAMING_COMPRESSION_COMPRESSOR_HPP
#define CLP_STREAMING_COMPRESSION_COMPRESSOR_HPP

#include <cstdint>
#include <string>
#include <sys/types.h>

#include <cstddef>

#include "../ErrorCode.hpp"
#include "../FileWriter.hpp"
#include "../TraceableException.hpp"
#include "../WriterInterface.hpp"
#include "Constants.hpp"

namespace clp::streaming_compression {
/**
* Generic compressor interface.
Bill-hbrhbr marked this conversation as resolved.
Show resolved Hide resolved
*/
class Compressor : public WriterInterface {
public:
// Types
class OperationFailed : public TraceableException {
public:
// Constructors
OperationFailed(ErrorCode error_code, char const* const filename, int line_number)
: TraceableException(error_code, filename, line_number) {}
: TraceableException{error_code, filename, line_number} {}

// Methods
char const* what() const noexcept override {
[[nodiscard]] auto what() const noexcept -> char const* override {
return "streaming_compression::Compressor operation failed";
}
};

// Constructor
explicit Compressor(CompressorType type) : m_type(type) {}
explicit Compressor(CompressorType type) : m_type{type} {}

// Destructor
virtual ~Compressor() = default;

// Explicitly disable copy and move constructor/assignment
// Delete copy constructor and assignment operator
Compressor(Compressor const&) = delete;
Compressor& operator=(Compressor const&) = delete;
auto operator=(Compressor const&) -> Compressor& = delete;

// Default move constructor and assignment operator
Compressor(Compressor&&) noexcept = default;
Bill-hbrhbr marked this conversation as resolved.
Show resolved Hide resolved
auto operator=(Compressor&&) noexcept -> Compressor& = default;

// Methods implementing the WriterInterface
/**
* Unsupported operation
* @param pos
* @return ErrorCode_Unsupported
*/
ErrorCode try_seek_from_begin(size_t pos) override { return ErrorCode_Unsupported; }
[[nodiscard]] auto try_seek_from_begin([[maybe_unused]] size_t pos) -> ErrorCode override {
return ErrorCode_Unsupported;
}

/**
* Unsupported operation
* @param pos
* @return ErrorCode_Unsupported
*/
ErrorCode try_seek_from_current(off_t offset) override { return ErrorCode_Unsupported; }
[[nodiscard]] auto try_seek_from_current([[maybe_unused]] off_t offset) -> ErrorCode override {
return ErrorCode_Unsupported;
}

// Methods
/**
* Closes the compression stream
* Closes the compressor
*/
virtual auto close() -> void = 0;

/**
* Initializes the compression stream
* @param file_writer
*/
virtual void close() = 0;
virtual auto open(FileWriter& file_writer) -> void = 0;

protected:
private:
Bill-hbrhbr marked this conversation as resolved.
Show resolved Hide resolved
// Variables
CompressorType m_type;
Bill-hbrhbr marked this conversation as resolved.
Show resolved Hide resolved
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef CLP_STREAMING_COMPRESSION_CONSTANTS_HPP
#define CLP_STREAMING_COMPRESSION_CONSTANTS_HPP

#include <cstddef>
#include <cstdint>

namespace clp::streaming_compression {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
#include "Compressor.hpp"

#include "../../Defs.h"
#include <cstddef>

#include "../../ErrorCode.hpp"
#include "../../TraceableException.hpp"
#include "../Compressor.hpp"
#include "../Constants.hpp"

namespace clp::streaming_compression::passthrough {
void Compressor::write(char const* data, size_t const data_length) {
Compressor::Compressor()
: ::clp::streaming_compression::Compressor{CompressorType::Passthrough},
m_compressed_stream_file_writer{nullptr} {}

auto Compressor::write(char const* data, size_t const data_length) -> void {
if (nullptr == m_compressed_stream_file_writer) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
}
Expand All @@ -19,27 +28,27 @@ void Compressor::write(char const* data, size_t const data_length) {
m_compressed_stream_file_writer->write(data, data_length);
}

void Compressor::flush() {
auto Compressor::flush() -> void {
if (nullptr == m_compressed_stream_file_writer) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
}

m_compressed_stream_file_writer->flush();
}

ErrorCode Compressor::try_get_pos(size_t& pos) const {
auto Compressor::try_get_pos(size_t& pos) const -> ErrorCode {
if (nullptr == m_compressed_stream_file_writer) {
return ErrorCode_NotInit;
}

return m_compressed_stream_file_writer->try_get_pos(pos);
}

void Compressor::close() {
auto Compressor::close() -> void {
m_compressed_stream_file_writer = nullptr;
}

void Compressor::open(FileWriter& file_writer) {
auto Compressor::open(FileWriter& file_writer) -> void {
m_compressed_stream_file_writer = &file_writer;
}
} // namespace clp::streaming_compression::passthrough
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef CLP_STREAMING_COMPRESSION_PASSTHROUGH_COMPRESSOR_HPP
#define CLP_STREAMING_COMPRESSION_PASSTHROUGH_COMPRESSOR_HPP

#include <cstddef>

#include "../../ErrorCode.hpp"
#include "../../FileWriter.hpp"
#include "../../TraceableException.hpp"
#include "../Compressor.hpp"
Expand All @@ -16,54 +19,60 @@ class Compressor : public ::clp::streaming_compression::Compressor {
public:
// Constructors
OperationFailed(ErrorCode error_code, char const* const filename, int line_number)
: TraceableException(error_code, filename, line_number) {}
: TraceableException{error_code, filename, line_number} {}

// Methods
char const* what() const noexcept override {
[[nodiscard]] auto what() const noexcept -> char const* override {
return "streaming_compression::passthrough::Compressor operation failed";
}
};

// Constructors
Compressor()
: ::clp::streaming_compression::Compressor(CompressorType::Passthrough),
m_compressed_stream_file_writer(nullptr) {}
Compressor();

// Destructor
~Compressor() override = default;

// Explicitly disable copy and move constructor/assignment
// Delete copy constructor and assignment operator
Compressor(Compressor const&) = delete;
Compressor& operator=(Compressor const&) = delete;
auto operator=(Compressor const&) -> Compressor& = delete;

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

// Methods implementing the WriterInterface
/**
* Writes the given data to the compressor
* @param data
* @param data_length
*/
void write(char const* data, size_t data_length) override;
auto write(char const* data, size_t data_length) -> void override;

/**
* Flushes any buffered data
*/
void flush() override;
auto flush() -> void override;

/**
* Tries to get the current position of the write head
* @param pos Position of the write head
* @return ErrorCode_NotInit if the compressor is not open
* @return Same as FileWriter::try_get_pos
*/
ErrorCode try_get_pos(size_t& pos) const override;
[[nodiscard]] auto try_get_pos(size_t& pos) const -> ErrorCode override;

// Methods implementing the Compressor interface
/**
* Closes the compressor
*/
void close() override;
auto close() -> void override;

// Methods
/**
* Initializes the compressor
* Initializes the compression stream
* @param file_writer
*/
void open(FileWriter& file_writer);
auto open(FileWriter& file_writer) -> void override;

private:
// Variables
Expand Down
Loading
Loading