Skip to content

Commit

Permalink
refactor(core): Fix clang-tidy warnings in the streaming compressor i…
Browse files Browse the repository at this point in the history
…nterface and its implementations; Modernize and refactor `test-StreamingCompression` for conciseness. (y-scope#599)

Co-authored-by: Bingran Hu <[email protected]>
Co-authored-by: Lin Zhihao <[email protected]>
  • Loading branch information
3 people authored and Jack Luo committed Dec 4, 2024
1 parent 44604c1 commit a402054
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 303 deletions.
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
#define __FILENAME__ ((__FILE__) + SOURCE_PATH_SIZE)
#else
// We don't know the source path size, so just default to __FILE__
Expand Down
45 changes: 30 additions & 15 deletions components/core/src/clp/streaming_compression/Compressor.hpp
Original file line number Diff line number Diff line change
@@ -1,63 +1,78 @@
#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 {
/**
* Abstract compressor interface.
*/
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) {}
Compressor() = default;

// 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;
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 void close() = 0;
virtual auto close() -> void = 0;

protected:
// Variables
CompressorType m_type;
/**
* Initializes the compression stream
* @param file_writer
*/
virtual auto open(FileWriter& file_writer) -> void = 0;
};
} // namespace clp::streaming_compression

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "Compressor.hpp"

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

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

namespace clp::streaming_compression::passthrough {
void Compressor::write(char const* data, size_t const data_length) {
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 +22,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,58 +19,64 @@ 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) {}
// Constructor
Compressor() = default;

// 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
FileWriter* m_compressed_stream_file_writer;
FileWriter* m_compressed_stream_file_writer{nullptr};
};
} // namespace clp::streaming_compression::passthrough

Expand Down
Loading

0 comments on commit a402054

Please sign in to comment.