From a38d8d9483653941cb3e6d9d3d79d662c1fcdef3 Mon Sep 17 00:00:00 2001 From: mmyjona Date: Wed, 22 Mar 2023 11:06:45 +0800 Subject: [PATCH] fix: compatibility with GCC <10 (#74) Co-authored-by: Aaron <29749331+aarnphm@users.noreply.github.com> --- src/whispercpp/context.cc | 52 +++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/whispercpp/context.cc b/src/whispercpp/context.cc index 4883622..7e3f0b0 100644 --- a/src/whispercpp/context.cc +++ b/src/whispercpp/context.cc @@ -7,6 +7,12 @@ #include #endif +#if __GNUC__ > 10 || defined(__clang__) +#define STREAM_CAST +#else +#define STREAM_CAST static_cast +#endif + #define NO_STATE_WARNING(no_state) \ do { \ if (no_state) { \ @@ -17,6 +23,7 @@ } \ } while (0) +#if __GNUC__ > 10 || defined(__clang__) #define RAISE_RUNTIME_ERROR(msg) \ do { \ throw std::runtime_error((std::stringstream() \ @@ -25,6 +32,17 @@ << "\n") \ .str()); \ } while (0) +#else +#define RAISE_RUNTIME_ERROR(msg) \ + do { \ + throw std::runtime_error( \ + static_cast(std::stringstream() \ + << __FILE__ << "#L" \ + << std::to_string(__LINE__) \ + << ": " << msg << "\n") \ + .str()); \ + } while (0) +#endif #define RAISE_IF_NULL(ptr) \ do { \ @@ -257,14 +275,14 @@ std::vector Context::lang_detect(size_t offset_ms, size_t threads) { } if (res == -1) { - RAISE_RUNTIME_ERROR((std::stringstream() - << "offset " << offset_ms - << "ms is before the start of audio.") + RAISE_RUNTIME_ERROR(STREAM_CAST(std::stringstream() + << "offset " << offset_ms + << "ms is before the start of audio.") .str()); } else if (res == -2) { - RAISE_RUNTIME_ERROR((std::stringstream() - << "offset " << offset_ms - << "ms is past the end of the audio.") + RAISE_RUNTIME_ERROR(STREAM_CAST(std::stringstream() + << "offset " << offset_ms + << "ms is past the end of the audio.") .str()); } else if (res == -6) { RAISE_RUNTIME_ERROR("Failed to encode."); @@ -372,11 +390,12 @@ int Context::full(Params params, std::vector data) { } else if (ret == -3) { RAISE_RUNTIME_ERROR("Failed to auto-detect language."); } else if (ret == -5) { - RAISE_RUNTIME_ERROR((std::stringstream() - << "audio_ctx is larger than maximum allowed (" - << std::to_string(params.get()->audio_ctx) << " > " - << this->n_audio_ctx() << ").") - .str()); + RAISE_RUNTIME_ERROR( + STREAM_CAST(std::stringstream() + << "audio_ctx is larger than maximum allowed (" + << std::to_string(params.get()->audio_ctx) << " > " + << this->n_audio_ctx() << ").") + .str()); } else if (ret == -6) { RAISE_RUNTIME_ERROR("Failed to encode."); } else if (ret == -7 || ret == -8) { @@ -418,11 +437,12 @@ int Context::full_parallel(Params params, std::vector data, } else if (ret == -3) { RAISE_RUNTIME_ERROR("Failed to auto-detect language."); } else if (ret == -5) { - RAISE_RUNTIME_ERROR((std::stringstream() - << "audio_ctx is larger than maximum allowed (" - << std::to_string(params.get()->audio_ctx) << " > " - << this->n_audio_ctx() << ").") - .str()); + RAISE_RUNTIME_ERROR( + STREAM_CAST(std::stringstream() + << "audio_ctx is larger than maximum allowed (" + << std::to_string(params.get()->audio_ctx) << " > " + << this->n_audio_ctx() << ").") + .str()); } else if (ret == -6) { RAISE_RUNTIME_ERROR("Failed to encode."); } else if (ret == -7 || ret == -8) {