From 7b1e0e684044fc8dcbfda34a907dfd2122b6c6e1 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Thu, 14 Nov 2024 13:29:17 -0700 Subject: [PATCH] use builtin types --- addon/compress.h | 5 +-- addon/compression_worker.h | 79 +++++++++++++------------------------- addon/decompress.h | 5 +-- binding.gyp | 3 +- 4 files changed, 32 insertions(+), 60 deletions(-) diff --git a/addon/compress.h b/addon/compress.h index c33cc67..1b5ed17 100644 --- a/addon/compress.h +++ b/addon/compress.h @@ -11,11 +11,10 @@ CompressionResult compress(const std::vector data, size_t compression_l ZSTD_compress(output.data(), output.size(), data.data(), data.size(), compression_level); if (ZSTD_isError(result_code)) { - std::string error(ZSTD_getErrorName(result_code)); - return CompressionResult::Error(error); + return std::string(ZSTD_getErrorName(result_code)); } output.resize(result_code); - return CompressionResult::Ok(output); + return output; } diff --git a/addon/compression_worker.h b/addon/compression_worker.h index f79367c..0aab388 100644 --- a/addon/compression_worker.h +++ b/addon/compression_worker.h @@ -6,43 +6,16 @@ using namespace Napi; -/** - * @brief A class that represents the result of a compression operation. Once the - * MACOS_DEPLOYMENT_TARGET can be raised to 10.13 and use a c++17, we can remove this class and use - * a std::optional, std::string>>> instead. - */ -struct CompressionResult { - CompressionResult(std::string error, - std::vector result, - bool hasError, - bool hasResult, - bool initialized) - : error(error), - result(result), - hasError(hasError), - hasResult(hasResult), - initialized(true) {} - - public: - static CompressionResult Error(std::string error) { - return CompressionResult(error, std::vector(), true, false, true); - } - - static CompressionResult Ok(std::vector result) { - return CompressionResult(std::string(""), result, false, true, true); - } - - static CompressionResult Empty() { - return CompressionResult(std::string(""), std::vector(), false, false, false); - } +using CompressionResult = std::variant, std::string>; - std::string error; - std::vector result; - - bool hasError; - bool hasResult; - bool initialized; +// Implementation of the Overload pattern: +// https://www.cppstories.com/2019/02/2lines3featuresoverload.html/ +template +struct overload : Ts... { + using Ts::operator()...; }; +template +overload(Ts...) -> overload; /** * @brief An asynchronous Napi::Worker that can be with any function that produces @@ -51,10 +24,7 @@ struct CompressionResult { class CompressionWorker : public Napi::AsyncWorker { public: CompressionWorker(const Napi::Env& env, std::function worker) - : Napi::AsyncWorker{env, "Worker"}, - m_deferred{env}, - worker(worker), - result(CompressionResult::Empty()) {} + : Napi::AsyncWorker{env, "Worker"}, m_deferred{env}, worker(worker), result{} {} Napi::Promise GetPromise() { return m_deferred.Promise(); @@ -66,24 +36,27 @@ class CompressionWorker : public Napi::AsyncWorker { } void OnOK() { - if (!result.initialized) { - m_deferred.Reject(Napi::Error::New(Env(), - "zstd runtime error - async worker finished without " - "a compression or decompression result.") - .Value()); - } else if (result.hasError) { - m_deferred.Reject(Napi::Error::New(Env(), result.error).Value()); - } else if (result.hasResult) { - Buffer output = - Buffer::Copy(m_deferred.Env(), result.result.data(), result.result.size()); - - m_deferred.Resolve(output); - } else { + if (!result.has_value()) { m_deferred.Reject(Napi::Error::New(Env(), "zstd runtime error - async worker finished without " "a compression or decompression result.") .Value()); + return; } + + auto result_visitor = overload{ + [m_deferred = this->m_deferred](std::string error_message) { + auto error = Napi::Error::New(m_deferred.Env(), error_message); + m_deferred.Reject(error.Value()); + }, + [m_deferred = this->m_deferred](std::vector result) { + Buffer output = + Buffer::Copy(m_deferred.Env(), result.data(), result.size()); + + m_deferred.Resolve(output); + }, + }; + std::visit(visitor, *result_visitor); } void OnError(const Napi::Error& err) { @@ -93,7 +66,7 @@ class CompressionWorker : public Napi::AsyncWorker { private: Napi::Promise::Deferred m_deferred; std::function worker; - CompressionResult result; + std::optional result; }; #endif diff --git a/addon/decompress.h b/addon/decompress.h index 3972c2a..b956288 100644 --- a/addon/decompress.h +++ b/addon/decompress.h @@ -18,8 +18,7 @@ CompressionResult decompress(const std::vector& compressed) { ZSTD_outBuffer output = {chunk.data(), chunk.size(), 0}; size_t const ret = ZSTD_decompressStream(decompression_context.get(), &output, &input); if (ZSTD_isError(ret)) { - std::string error(ZSTD_getErrorName(ret)); - return CompressionResult::Error(error); + return std::string(ZSTD_getErrorName(ret)); } for (size_t i = 0; i < output.pos; ++i) { @@ -27,5 +26,5 @@ CompressionResult decompress(const std::vector& compressed) { } } - return CompressionResult::Ok(decompressed); + return decompressed; } diff --git a/binding.gyp b/binding.gyp index abe9483..304920f 100644 --- a/binding.gyp +++ b/binding.gyp @@ -21,11 +21,12 @@ 'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES', 'CLANG_CXX_LIBRARY': 'libc++', - 'MACOSX_DEPLOYMENT_TARGET': '10.12', + 'MACOSX_DEPLOYMENT_TARGET': '11', 'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden }, 'cflags!': [ '-fno-exceptions' ], 'cflags_cc!': [ '-fno-exceptions' ], + 'cflags_cc': ['-std=c++17'], 'msvs_settings': { 'VCCLCompilerTool': { 'ExceptionHandling': 1 }, },