From 4c192d86c56bc9eec85d09fa5bbcc79eef95f08e Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Mon, 9 Oct 2023 14:05:38 -0400 Subject: [PATCH] Squashed 'externals/coda-oss/' changes from 70a006d8a..b6ead418c b6ead418c fix previous merge (#744) 07bcb3a39 Merge branch 'main' into cpp17 76beb7f34 Throwable always inherits from std::exception (#742) 959532681 reduce use of FmtX macro (#743) f1a857cc4 Revert "simplify Throwable and friends: always derive from std::exception" 8d5f4402f simplify Throwable and friends: always derive from std::exception fffac7fc4 Fix memory leaks in "cli" (#741) git-subtree-dir: externals/coda-oss git-subtree-split: b6ead418cfde26b016a3be199cd8ca7039a0a7be --- modules/c++/cli/include/cli/Results.h | 70 +++++---- modules/c++/cli/include/cli/Value.h | 27 +--- modules/c++/cli/source/Argument.cpp | 2 +- modules/c++/cli/source/ArgumentParser.cpp | 77 ++++++---- modules/c++/cli/unittests/test_cli.cpp | 7 +- modules/c++/except/include/except/Error.h | 50 +------ modules/c++/except/include/except/Exception.h | 51 +------ modules/c++/except/include/except/Throwable.h | 96 +++--------- modules/c++/except/source/Throwable.cpp | 16 +- modules/c++/io/source/FileInputStreamIOS.cpp | 2 +- modules/c++/io/source/FileUtils.cpp | 2 +- modules/c++/io/source/MMapInputStream.cpp | 2 +- modules/c++/io/source/StandardStreams.cpp | 4 +- modules/c++/logging/include/logging/Enums.h | 6 +- .../c++/math.poly/include/math/poly/Fixed1D.h | 4 +- modules/c++/net.ssl/source/SSLConnection.cpp | 10 +- .../source/SSLConnectionClientFactory.cpp | 4 +- modules/c++/net/source/DaemonUnix.cpp | 4 +- modules/c++/net/source/Socket.cpp | 2 +- .../include/plugin/BasicPluginManager.h | 6 +- .../sio.lite/include/sio/lite/SioFileWriter.h | 2 +- modules/c++/str/include/str/Format.h | 141 +++++++++--------- modules/c++/sys/include/sys/ThreadInterface.h | 2 +- modules/c++/sys/source/DLLWin32.cpp | 4 +- modules/c++/sys/source/FileUnix.cpp | 2 +- modules/c++/sys/source/FileWin32.cpp | 4 +- modules/c++/sys/source/OSUnix.cpp | 4 +- modules/c++/sys/source/OSWin32.cpp | 2 +- modules/c++/tiff/include/tiff/IFD.h | 2 +- .../c++/tiff/include/tiff/TiffFileWriter.h | 2 +- modules/c++/tiff/source/IFD.cpp | 2 +- modules/c++/tiff/source/ImageReader.cpp | 2 +- modules/c++/tiff/source/TiffFileReader.cpp | 4 +- modules/c++/xml.lite/source/Attributes.cpp | 6 +- modules/c++/zip/source/ZipEntry.cpp | 4 +- 35 files changed, 241 insertions(+), 384 deletions(-) diff --git a/modules/c++/cli/include/cli/Results.h b/modules/c++/cli/include/cli/Results.h index 1a74cc7aab..bd293cf695 100644 --- a/modules/c++/cli/include/cli/Results.h +++ b/modules/c++/cli/include/cli/Results.h @@ -25,6 +25,9 @@ #define CODA_OSS_cli_Results_h_INCLUDED_ #include +#include +#include +#include #include "sys/Conf.h" @@ -33,10 +36,9 @@ namespace cli { -class Results +class Results final { -protected: - typedef std::map ValueStorage_T; + typedef std::map> ValueStorage_T; typedef ValueStorage_T::iterator ValueIter_T; typedef ValueStorage_T::const_iterator ConstValueIter_T; typedef std::map ResultsStorage_T; @@ -44,13 +46,15 @@ class Results typedef ResultsStorage_T::const_iterator ConstResultsIter_T; public: - Results() - { - } + Results() = default; ~Results() { destroy(); } + Results(const Results&) = delete; + Results& operator=(const Results&) = delete; + Results(Results&&) = default; + Results& operator=(Results&&) = default; bool hasValue(const std::string& key) const { @@ -62,20 +66,30 @@ class Results return mResults.find(key) != mResults.end(); } - cli::Value* operator[](const std::string& key) const + const cli::Value* operator[](const std::string& key) const { return getValue(key); } - cli::Value* getValue(const std::string& key) const + const cli::Value* getValue(const std::string& key) const { - ConstValueIter_T p = mValues.find(key); + auto const p = mValues.find(key); if (p == mValues.end()) { const std::string errorMessage = "No argument named " + key; throw except::NoSuchKeyException(Ctxt(errorMessage)); } - return p->second; + return p->second.get(); + } + cli::Value* getValue(const std::string& key) + { + auto const p = mValues.find(key); + if (p == mValues.end()) + { + const std::string errorMessage = "No argument named " + key; + throw except::NoSuchKeyException(Ctxt(errorMessage)); + } + return p->second.get(); } template @@ -100,15 +114,22 @@ class Results return p->second; } - void put(const std::string& key, cli::Value *value) + void put(const std::string& key, std::unique_ptr value) + { + if (hasValue(key) && (getValue(key) == value.get())) + { + return; + } + mValues[key] = std::move(value); + } + void put(const std::string& key, cli::Value* value) { - if (hasValue(key)) + cli::Value* pExistingValue = hasValue(key) ? getValue(key) : nullptr; + if ((pExistingValue == nullptr) || (pExistingValue != value)) { - cli::Value* existing = getValue(key); - if (existing != value) - delete getValue(key); + // Either 1) we didn't already have a value or 2) the existing value is different + put(key, std::unique_ptr(value)); } - mValues[key] = value; } void put(const std::string& key, cli::Results *args) @@ -122,26 +143,21 @@ class Results mResults[key] = args; } - typedef ValueStorage_T::iterator iterator; - typedef ValueStorage_T::const_iterator const_iterator; + auto begin() { return mValues.begin(); } + auto begin() const { return mValues.begin(); } + auto end() { return mValues.end(); } + auto end() const { return mValues.end(); } - iterator begin() { return mValues.begin(); } - const_iterator begin() const { return mValues.begin(); } - iterator end() { return mValues.end(); } - const_iterator end() const { return mValues.end(); } - -protected: +private: ValueStorage_T mValues; ResultsStorage_T mResults; void destroy() { - for (ValueIter_T it = mValues.begin(), end = mValues.end(); it != end; ++it) - delete it->second; + mValues.clear(); for (ResultsIter_T it = mResults.begin(), end = mResults.end(); it != end; ++it) delete it->second; - mValues.clear(); mResults.clear(); } }; diff --git a/modules/c++/cli/include/cli/Value.h b/modules/c++/cli/include/cli/Value.h index 571565cab5..49f1125ebd 100644 --- a/modules/c++/cli/include/cli/Value.h +++ b/modules/c++/cli/include/cli/Value.h @@ -26,6 +26,7 @@ #include #include +#include #include #include "sys/Conf.h" @@ -38,12 +39,9 @@ namespace cli * The Value class provides access to one or more actual values. * It provides index-based access to parameters. */ -class CODA_OSS_API Value +struct CODA_OSS_API Value final { -public: - Value() - { - } + Value() = default; template explicit Value(std::vector value) @@ -57,12 +55,6 @@ class CODA_OSS_API Value set(value); } - template - Value(T* value, size_t size, bool own = false) - { - set(value, size, own); - } - ~Value() { cleanup(); @@ -75,17 +67,6 @@ class CODA_OSS_API Value mValues.push_back(str::toString(value)); } - template - void set(T* value, size_t size, bool own = false) - { - cleanup(); - mValues.reserve(size); - for (size_t i = 0; i < size; ++i) - add(value[i]); - if (own) - delete[] value; - } - template void setContainer(const std::vector& c) { @@ -106,7 +87,7 @@ class CODA_OSS_API Value { if (index >= mValues.size()) throw except::IndexOutOfRangeException( - Ctxt(FmtX("Invalid index: %d", index))); + Ctxt(str::Format("Invalid index: %d", index))); return str::toType(mValues[index]); } diff --git a/modules/c++/cli/source/Argument.cpp b/modules/c++/cli/source/Argument.cpp index 01b2563233..8b924f30a7 100644 --- a/modules/c++/cli/source/Argument.cpp +++ b/modules/c++/cli/source/Argument.cpp @@ -54,7 +54,7 @@ cli::Argument::~Argument() cli::Argument* cli::Argument::addFlag(const std::string& flag) { char p = mParser->mPrefixChar; - std::string p2 = FmtX("%c%c", p, p); + std::string p2 = str::Format("%c%c", p, p); if (flag.size() > 2 && str::startsWith(flag, p2) && flag[2] != p) mLongFlags.push_back(validateFlag(flag.substr(2))); else if (flag.size() > 1 && flag[0] == p && flag[1] != p) diff --git a/modules/c++/cli/source/ArgumentParser.cpp b/modules/c++/cli/source/ArgumentParser.cpp index e7763e4aae..367ff9edce 100644 --- a/modules/c++/cli/source/ArgumentParser.cpp +++ b/modules/c++/cli/source/ArgumentParser.cpp @@ -28,10 +28,11 @@ #include #include +#include namespace { -static const size_t MAX_ARG_LINE_LENGTH = 21; +constexpr size_t MAX_ARG_LINE_LENGTH = 21; bool containsOnly(const std::string& str, const std::map& flags) @@ -284,6 +285,20 @@ cli::Results* cli::ArgumentParser::parse(const std::vector& args) return parse("cli::ArgumentParser::parse" /*program*/, args).release(); // provide a "meaningful" default program name } } + +static auto put(cli::Results& currentResults, const std::string& argVar, + cli::Value* v, std::unique_ptr&& v_) +{ + if (v == v_.get()) // no existing value, using the newly created std::unique_ptr + { + currentResults.put(argVar, std::move(v_)); + } + else + { + currentResults.put(argVar, v); // already had a value, just update + } +} + std::unique_ptr cli::ArgumentParser::parse(const std::string& program, const std::vector& args) { if (!program.empty()) @@ -318,7 +333,7 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog std::map& flagMap = (subOption ? shortOptionsFlags : shortFlags); if (flagMap.find(op) != flagMap.end()) - parseError(FmtX("Conflicting option: %c%s", mPrefixChar, op)); + parseError(str::Format("Conflicting option: %c%s", mPrefixChar, op)); flagMap[op] = arg; } for (std::vector::const_iterator it = @@ -328,7 +343,7 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog std::map& flagMap = (subOption ? longOptionsFlags : longFlags); if (flagMap.find(op) != flagMap.end()) - parseError(FmtX("Conflicting option: %c%c%s", mPrefixChar, mPrefixChar, op)); + parseError(str::Format("Conflicting option: %c%c%s", mPrefixChar, mPrefixChar, op)); flagMap[op] = arg; } } @@ -457,7 +472,7 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog } else { - throw except::Exception(Ctxt(FmtX("Invalid option: [%s]", argStr))); + throw except::Exception(Ctxt(str::Format("Invalid option: [%s]", argStr))); } } } @@ -498,7 +513,7 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog } else { - throw except::Exception(Ctxt(FmtX("Invalid option: [%s]", argStr))); + throw except::Exception(Ctxt(str::Format("Invalid option: [%s]", argStr))); } } @@ -513,9 +528,8 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog { case cli::STORE: { - cli::Value* v = currentResults->hasValue(argVar) - ? currentResults->getValue(argVar) - : new cli::Value; + auto v_ = std::make_unique(); + auto v = currentResults->hasValue(argVar) ? currentResults->getValue(argVar) : v_.get(); int maxArgs = arg->getMaxArgs(); // risky, I know... bool added = false; @@ -545,16 +559,16 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog } if (!added) - parseError(FmtX("option requires value or has exceeded its max: [%s]", argVar)); + parseError(str::Format("option requires value or has exceeded its max: [%s]", argVar)); - currentResults->put(argVar, v); + put(*currentResults, argVar, v, std::move(v_)); break; } case cli::STORE_TRUE: - currentResults->put(argVar, new cli::Value(true)); + currentResults->put(argVar, std::make_unique(true)); break; case cli::STORE_FALSE: - currentResults->put(argVar, new cli::Value(false)); + currentResults->put(argVar, std::make_unique(false)); break; case cli::STORE_CONST: { @@ -565,10 +579,10 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog case cli::SUB_OPTIONS: { if (optionsStr.empty()) - parseError(FmtX("invalid sub option: [%s]", argVar.c_str())); - cli::Value* v = currentResults->hasValue(optionsStr) - ? currentResults->getValue(optionsStr) - : new cli::Value; + parseError(str::Format("invalid sub option: [%s]", argVar.c_str())); + + auto v_ = std::make_unique(); + auto v = currentResults->hasValue(optionsStr) ? currentResults->getValue(optionsStr) : v_.get(); if (i < s - 1) { std::string nextArg = explodedArgs[i + 1]; @@ -589,7 +603,7 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog // this indicates the sub op is a bool v->add(true); } - currentResults->put(optionsStr, v); + put(*currentResults, optionsStr, v, std::move(v_)); break; } case cli::VERSION: @@ -601,24 +615,22 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog { // it's a positional argument cli::Value* lastPosVal = nullptr; - for (std::vector::iterator it = - positionalArgs.begin(); it != positionalArgs.end(); ++it) + for (auto&& posArg : positionalArgs) { - cli::Argument *posArg = *it; std::string argVar = posArg->getVariable(); int maxArgs = posArg->getMaxArgs(); if (currentResults->hasValue(argVar)) { - cli::Value *posVal = lastPosVal - = currentResults->getValue(argVar); - if (static_cast(posVal->size()) >= maxArgs) + lastPosVal = currentResults->getValue(argVar); + if (gsl::narrow(lastPosVal->size()) >= maxArgs) continue; break; } else if (maxArgs != 0) { - lastPosVal = new cli::Value; - currentResults->put(argVar, lastPosVal); + auto lastPosVal_ = std::make_unique(); + lastPosVal = lastPosVal_.get(); + currentResults->put(argVar, std::move(lastPosVal_)); break; } } @@ -651,12 +663,11 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog if (defaultVal != nullptr) results->put(argVar, defaultVal->clone()); else if (arg->getAction() == cli::STORE_FALSE) - results->put(argVar, new cli::Value(true)); + results->put(argVar, std::make_unique(true)); else if (arg->getAction() == cli::STORE_TRUE) - results->put(argVar, new cli::Value(false)); + results->put(argVar, std::make_unique(false)); else if (arg->isRequired()) - parseError(FmtX("missing required argument: [%s]", - argVar.c_str())); + parseError(str::Format("missing required argument: [%s]", argVar)); } @@ -670,9 +681,9 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog if (arg->isRequired() || numGiven > 0) { if (minArgs > 0 && numGiven < static_cast(minArgs)) - parseError(FmtX("not enough arguments, %d required: [%s]", minArgs, argId)); + parseError(str::Format("not enough arguments, %d required: [%s]", minArgs, argId)); if (maxArgs >= 0 && numGiven > static_cast(maxArgs)) - parseError(FmtX("too many arguments, %d supported: [%s]", maxArgs, argId)); + parseError(str::Format("too many arguments, %d supported: [%s]", maxArgs, argId)); } @@ -682,7 +693,7 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog if (numGiven > 0 && !choices.empty()) { bool isValid = false; - cli::Value *vals = results->getValue(argVar); + auto vals = results->getValue(argVar); for (size_t ii = 0; ii < numGiven; ++ii) { @@ -695,7 +706,7 @@ std::unique_ptr cli::ArgumentParser::parse(const std::string& prog } if (!isValid) { - parseError(FmtX("invalid option for [%s]", argVar.c_str())); + parseError(str::Format("invalid option for [%s]", argVar.c_str())); } } } diff --git a/modules/c++/cli/unittests/test_cli.cpp b/modules/c++/cli/unittests/test_cli.cpp index ade63b34ac..d8d1578ff9 100644 --- a/modules/c++/cli/unittests/test_cli.cpp +++ b/modules/c++/cli/unittests/test_cli.cpp @@ -160,7 +160,7 @@ TEST_CASE(testIterate) std::unique_ptr results(parser.parse(str::split("-v -c config.xml"))); std::vector keys; - for(cli::Results::const_iterator it = results->begin(); it != results->end(); ++it) + for(auto it = results->begin(); it != results->end(); ++it) keys.push_back(it->first); TEST_ASSERT_EQ(std::ssize(keys), 2); // std::map returns keys in alphabetical order... @@ -186,14 +186,9 @@ TEST_CASE(testRequiredThrows) parser.addArgument("-c --config", "Specify a config file", cli::STORE) ->setRequired(true); - // The exceptions leak memory which causes an ASAN diagnostic on Linux. - #if CODA_OSS_POSIX_SOURCE && __SANITIZE_ADDRESS__ - TEST_SUCCESS; - #else const std::string program(testName); TEST_EXCEPTION(parser.parse(program, str::split(""))); TEST_EXCEPTION(parser.parse(program, str::split("-c"))); - #endif } TEST_CASE(testUnknownArgumentsOptions) diff --git a/modules/c++/except/include/except/Error.h b/modules/c++/except/include/except/Error.h index 56fcdb78ca..02e50fa8bd 100644 --- a/modules/c++/except/include/except/Error.h +++ b/modules/c++/except/include/except/Error.h @@ -44,16 +44,12 @@ _Name##Error_(const except::Context& c) : _Base(c){} \ _Name##Error_(const std::string& msg) : _Base(msg){} \ _Name##Error_(const except::Throwable& t, const except::Context& c) : _Base(t, c){} \ - _Name##Error_(const except::ThrowableEx& t, const except::Context& c) : _Base(t, c){} \ std::string getType() const noexcept override { return #_Name; } \ } #define DECLARE_EXTENDED_ERROR(_Name, _Base) DECLARE_EXTENDED_ERROR_(_Name, Error, _Base) -#define DECLARE_EXTENDED_ERROREX(_Name, _Base) DECLARE_EXTENDED_ERROR_(_Name, ErrorEx, _Base) // Need to keep this around for existing code -#define DECLARE_ERROR(_Name) \ - DECLARE_EXTENDED_ERROR(_Name, except::Error); \ - DECLARE_EXTENDED_ERROREX(_Name, except::ErrorEx) +#define DECLARE_ERROR(_Name) DECLARE_EXTENDED_ERROR(_Name, except::Error) namespace except { @@ -98,9 +94,6 @@ struct Error : public Throwable Throwable(t, c) { } - Error(const ThrowableEx& t, const Context& c) : Throwable(t, c) - { - } std::string getType() const override { @@ -110,46 +103,9 @@ struct Error : public Throwable // Use this in new code: name is FooErrror (not FooErrorEx), base is except::ErrorEx (not except::Error). #define CODA_OSS_DECLARE_EXTENDED_ERROR(name_, base_) DECLARE_EXTENDED_ERROR_(name_, Error, base_) -#define CODA_OSS_DECLARE_ERROR(name_) CODA_OSS_DECLARE_EXTENDED_ERROR(name_, except::ErrorEx) - -struct ErrorEx : public ThrowableEx -{ - ErrorEx() = default; - virtual ~ErrorEx() = default; - - /*! - * Constructor. Takes a Context - * \param c The Context - */ - ErrorEx(const Context& c) : ThrowableEx(c) - { - } +#define CODA_OSS_DECLARE_ERROR(name_) CODA_OSS_DECLARE_EXTENDED_ERROR(name_, except::Error) - /*! - * Constructor. Takes a message - * \param message The message - */ - ErrorEx(const std::string& message) : ThrowableEx(message) - { - } - - /*! - * Constructor. Takes an Throwable and a Context - * \param t The Throwable - * \param c The Context - */ - ErrorEx(const ThrowableEx& t, const Context& c) : ThrowableEx(t, c) - { - } - ErrorEx(const Throwable& t, const Context& c) : ThrowableEx(t, c) - { - } - - std::string getType() const noexcept override - { - return "ErrorEx"; - } -}; +using ErrorEx = Error; using Error11 = ErrorEx; // keep old name around for other projects /*! diff --git a/modules/c++/except/include/except/Exception.h b/modules/c++/except/include/except/Exception.h index 33a8563632..95ddad022d 100644 --- a/modules/c++/except/include/except/Exception.h +++ b/modules/c++/except/include/except/Exception.h @@ -60,17 +60,14 @@ _Name##Exception_(const except::Context& c) : _Base(c){} \ _Name##Exception_(const std::string& msg) : _Base(msg){} \ _Name##Exception_(const except::Throwable& t, const except::Context& c) : _Base(t, c){} \ - _Name##Exception_(const except::ThrowableEx& t, const except::Context& c) : _Base(t, c){} \ CODA_OSS_except_Exception_suppress_26447_BEGIN_ \ std::string getType() const noexcept override { return #_Name #Exception_; } \ CODA_OSS_except_Exception_suppress_26447_END_ } #define DECLARE_EXTENDED_EXCEPTION(_Name, _Base) DECLARE_EXTENDED_EXCEPTION_(_Name, Exception, _Base) -#define DECLARE_EXTENDED_EXCEPTIONEX(_Name, _Base) DECLARE_EXTENDED_EXCEPTION_(_Name, ExceptionEx, _Base) // Need to keep this around for existing code -#define DECLARE_EXCEPTION(_Name) \ - DECLARE_EXTENDED_EXCEPTION(_Name, except::Exception); \ - DECLARE_EXTENDED_EXCEPTIONEX(_Name, except::ExceptionEx) +#define DECLARE_EXCEPTION(_Name) DECLARE_EXTENDED_EXCEPTION(_Name, except::Exception); \ + using _Name ## ExceptionEx = _Name ##Exception namespace except { @@ -104,9 +101,6 @@ struct Exception : public Throwable Throwable(t, c) { } - Exception(const ThrowableEx& t, const Context& c) : Throwable(t, c) - { - } /*! * Constructor. Takes a message @@ -126,46 +120,9 @@ struct Exception : public Throwable // Use this in new code: name is FooException (not FooExceptionEx), base is except::ExceptionEx (not except::Exception). #define CODA_OSS_DECLARE_EXTENDED_EXCEPTION(name_, base_) DECLARE_EXTENDED_EXCEPTION_(name_, Exception, base_) -#define CODA_OSS_DECLARE_EXCEPTION(name_) CODA_OSS_DECLARE_EXTENDED_EXCEPTION(name_, except::ExceptionEx) - -struct ExceptionEx : public ThrowableEx -{ - ExceptionEx() = default; - virtual ~ExceptionEx() = default; - - /*! - * Constructor. Takes a Context - * \param c The Context - */ - ExceptionEx(const Context& c) : ThrowableEx(c) - { - } +#define CODA_OSS_DECLARE_EXCEPTION(name_) CODA_OSS_DECLARE_EXTENDED_EXCEPTION(name_, except::Exception) - /*! - * Constructor. Takes an Throwable and a Context - * \param t The Throwable - * \param c The Context - */ - ExceptionEx(const ThrowableEx& t, const Context& c) : ThrowableEx(t, c) - { - } - ExceptionEx(const Throwable& t, const Context& c) : ThrowableEx(t, c) - { - } - - /*! - * Constructor. Takes a message - * \param message The message - */ - ExceptionEx(const std::string& message) : ThrowableEx(message) - { - } - - std::string getType() const noexcept override - { - return "ExceptionEx"; - } -}; +using ExceptionEx = Exception; using Exception11 = ExceptionEx; // keep old name around for other projects /*! diff --git a/modules/c++/except/include/except/Throwable.h b/modules/c++/except/include/except/Throwable.h index 5067ab02a0..ffb2ea7b0f 100644 --- a/modules/c++/except/include/except/Throwable.h +++ b/modules/c++/except/include/except/Throwable.h @@ -20,9 +20,9 @@ * */ +#pragma once #ifndef CODA_OSS_except_Throwable_h_INCLUDED_ #define CODA_OSS_except_Throwable_h_INCLUDED_ -#pragma once #include #include @@ -47,14 +47,11 @@ * A lot of existing code has "catch (std::exception)" BEFORE "catch (except::Throwable)" * making it difficult to change without the risk of breaking something. :-( */ -#ifdef CODA_OSS_THROWABLE_ISA_STD_EXCEPTION // -DCODA_OSS_THROWABLE_ISA_STD_EXCEPTION -#ifdef CODA_OSS_except_Throwable_ISA_std_exception -#error "CODA_OSS_except_Throwable_ISA_std_exception is already #define'd." +#ifdef CODA_OSS_THROWABLE_ISA_STD_EXCEPTION +#error "No longer supported, please update your code." #endif -#define CODA_OSS_except_Throwable_ISA_std_exception 1 -#endif -#ifndef CODA_OSS_except_Throwable_ISA_std_exception // or, -DCODA_OSS_except_Throwable_ISA_std_exception=1 -#define CODA_OSS_except_Throwable_ISA_std_exception 0 +#ifdef CODA_OSS_except_Throwable_ISA_std_exception +#error "No longer supported, please update your code." #endif /*! @@ -74,18 +71,21 @@ namespace except * This class provides the base interface for exceptions and errors. */ -class ThrowableEx; -class CODA_OSS_API Throwable -#if CODA_OSS_except_Throwable_ISA_std_exception - : public std::exception +#ifndef SWIGPYTHON +CODA_OSS_disable_warning_push +#if _MSC_VER +#pragma warning(disable: 4275) // non dll-interface class '...' used as base for dll-interface class '...' #endif -{ - void doGetBacktrace(); - template - Throwable(const Context*, const TThrowable* pT, const std::string* pMessage, bool callGetBacktrace, std::nullptr_t); +class CODA_OSS_API Throwable : public std::exception { +CODA_OSS_disable_warning_pop +#else +class Throwable { +#endif // SWIGPYTHON + + void doGetBacktrace(); + Throwable(const Context*, const Throwable* pT, const std::string* pMessage, bool callGetBacktrace, std::nullptr_t); protected: Throwable(const Context*, const Throwable* pT = nullptr, const std::string* pMessage = nullptr, bool callGetBacktrace = false); - Throwable(const Context*, const ThrowableEx* pT, const std::string* pMessage = nullptr, bool callGetBacktrace = false); public: Throwable() = default; @@ -94,7 +94,6 @@ class CODA_OSS_API Throwable Throwable(Throwable&&) = default; Throwable& operator=(Throwable&&) = default; - Throwable(const ThrowableEx&); /*! * Constructor. Takes a message @@ -114,7 +113,6 @@ class CODA_OSS_API Throwable * \param c The Context */ Throwable(const Throwable&, Context); - Throwable(const ThrowableEx&, Context); /*! * Destructor @@ -184,7 +182,7 @@ class CODA_OSS_API Throwable virtual std::string toString(bool includeBacktrace) const { - // Adding the backtrace to existing toString() output could substantally alter existing strings. + // Adding the backtrace to existing toString() output could substantially alter existing strings. std::string backtrace; if (includeBacktrace) { @@ -193,11 +191,10 @@ class CODA_OSS_API Throwable } return toString() + backtrace; } - + #ifndef SWIGPYTHON + const char* what() const noexcept override final // derived classes override toString() + #else const char* what() const noexcept - #if CODA_OSS_except_Throwable_ISA_std_exception - // can't use "final" unless what() is virtual - final // derived classes override toString() #endif { // adding this to toString() output could (significantly) alter existing display @@ -222,57 +219,8 @@ class CODA_OSS_API Throwable * * This class provides the base interface for exceptions and errors. */ +using ThrowableEx = Throwable; -/* - * It can be quite convenient to derive from std::exception as often one less - * "catch" will be needed and we'll have standard what(). But doing so could - * break existing code as "catch (const std::exception&)" will catch - * except::Throwable when it didn't before. - */ -// Use multiple-inheritance :-( to reduce duplicated boilerplate code. -class ThrowableEx : public Throwable // "ThrowableEx" = "Throwable exception" -#if !CODA_OSS_except_Throwable_ISA_std_exception - , public std::exception -#endif -{ -public: - ThrowableEx() = default; - virtual ~ThrowableEx() = default; - ThrowableEx(const ThrowableEx&) = default; - ThrowableEx& operator=(const ThrowableEx&) = default; - ThrowableEx(ThrowableEx&&) = default; - ThrowableEx& operator=(ThrowableEx&&) = default; - - ThrowableEx(const Throwable& t) : Throwable(t){} - - /*! - * Constructor. Takes a message - * \param message The message - */ - ThrowableEx(const std::string& message) : Throwable(message) {} - - /*! - * Constructor. Takes a Context. - * \param c The Context - */ - ThrowableEx(const Context& ctx) : Throwable(ctx) {} - - /*! - * Constructor. Takes a Throwable and a Context - * \param t The throwable - * \param c The Context - */ - ThrowableEx(const ThrowableEx& t, const Context& ctx) : Throwable(t, ctx) {} - ThrowableEx(const Throwable& t, const Context& ctx) : Throwable(t, ctx) {} - - #if !CODA_OSS_except_Throwable_ISA_std_exception - const char* what() const noexcept override final // derived classes override toString() - { - const Throwable* pThrowable = this; - return pThrowable->what(); - } - #endif -}; using Throwable11 = ThrowableEx; // keep old name around for other projects } diff --git a/modules/c++/except/source/Throwable.cpp b/modules/c++/except/source/Throwable.cpp index a30d484dfb..85c2c3f55b 100644 --- a/modules/c++/except/source/Throwable.cpp +++ b/modules/c++/except/source/Throwable.cpp @@ -34,8 +34,7 @@ void except::Throwable::doGetBacktrace() (void)except::getBacktrace(supported, mBacktrace); } -template -except::Throwable::Throwable(const Context* pContext, const TThrowable* pThrowable, const std::string* pMessage, bool callGetBacktrace, std::nullptr_t) +except::Throwable::Throwable(const Context* pContext, const Throwable* pThrowable, const std::string* pMessage, bool callGetBacktrace, std::nullptr_t) { if (pThrowable != nullptr) { @@ -62,7 +61,7 @@ except::Throwable::Throwable(const Context* pContext, const TThrowable* pThrowab // This will record a back-trace from where the Throwable object was instantiated. // That's not necessarily where the "throw" will occur, but it's often the case; Throwable - // instances ususally aren't passed around. That is, hardly anybody does: + // instances usually aren't passed around. That is, hardly anybody does: // Exception e; // Throwable instance // might_throw(e); // rather, the idiom is usually @@ -76,10 +75,6 @@ except::Throwable::Throwable(const Context* pContext, const Throwable* pThrowabl : Throwable(pContext, pThrowable, pMessage, callGetBacktrace, nullptr) { } -except::Throwable::Throwable(const Context* pContext, const ThrowableEx* pThrowable, const std::string* pMessage, bool callGetBacktrace) -: Throwable(pContext, pThrowable, pMessage, callGetBacktrace, nullptr) -{ -} except::Throwable::Throwable(const std::string& message) : Throwable(nullptr, static_cast(nullptr), &message) { @@ -92,9 +87,4 @@ except::Throwable::Throwable(except::Context c) : Throwable(&c) except::Throwable::Throwable(const except::Throwable& t, except::Context c) : Throwable(&c, &t) { } -except::Throwable::Throwable(const except::ThrowableEx& t, except::Context c) : Throwable(&c, &t) -{ -} -except::Throwable::Throwable(const except::ThrowableEx& t) : Throwable(nullptr, &t) -{ -} + diff --git a/modules/c++/io/source/FileInputStreamIOS.cpp b/modules/c++/io/source/FileInputStreamIOS.cpp index e1585d16ba..5ff403893d 100644 --- a/modules/c++/io/source/FileInputStreamIOS.cpp +++ b/modules/c++/io/source/FileInputStreamIOS.cpp @@ -91,7 +91,7 @@ void io::FileInputStreamIOS::open(const char *file, mFStream.open(file, mode); if (!isOpen()) throw except::IOException(Ctxt( - FmtX( + str::Format( "File %s could not be opened for reading", file) ) diff --git a/modules/c++/io/source/FileUtils.cpp b/modules/c++/io/source/FileUtils.cpp index 9de78af1f1..5c61540576 100644 --- a/modules/c++/io/source/FileUtils.cpp +++ b/modules/c++/io/source/FileUtils.cpp @@ -133,7 +133,7 @@ std::string io::FileUtils::createFile(std::string dirname, sys::OS os; if (!os.exists(dirname)) - throw except::IOException(Ctxt(FmtX("Directory does not exist: %s", + throw except::IOException(Ctxt(str::Format("Directory does not exist: %s", dirname.c_str()))); str::trim(filename); diff --git a/modules/c++/io/source/MMapInputStream.cpp b/modules/c++/io/source/MMapInputStream.cpp index d942ac2231..e1391083a5 100644 --- a/modules/c++/io/source/MMapInputStream.cpp +++ b/modules/c++/io/source/MMapInputStream.cpp @@ -28,7 +28,7 @@ void io::MMapInputStream::open(const std::string& fname, char* flags) // std::cout << mLength << std::endl; mFile = fopen(fname.c_str(), "r"); if (!mFile) - throw sys::SystemException(FmtX("Failure while opening file: %s", fname.c_str())); + throw sys::SystemException(str::Format("Failure while opening file: %s", fname.c_str())); _map(); diff --git a/modules/c++/io/source/StandardStreams.cpp b/modules/c++/io/source/StandardStreams.cpp index 2323613990..3e374d09d5 100644 --- a/modules/c++/io/source/StandardStreams.cpp +++ b/modules/c++/io/source/StandardStreams.cpp @@ -33,7 +33,7 @@ void io::StandardOutStream::write(const void* buffer, sys::Size_T len) if (!std::cout.good()) throw except::IOException( Ctxt( - FmtX("std::cout stream is bad after requested write: (%d)", + str::Format("std::cout stream is bad after requested write: (%d)", len)) ); } @@ -53,7 +53,7 @@ void io::StandardErrStream::write(const void* buffer, sys::Size_T len) if (!std::cerr.good()) throw except::IOException( Ctxt( - FmtX("std::cerr stream is bad after requested write: (%d)", + str::Format("std::cerr stream is bad after requested write: (%d)", len) ) ); } diff --git a/modules/c++/logging/include/logging/Enums.h b/modules/c++/logging/include/logging/Enums.h index f7d30fb1ea..8569a8c49f 100644 --- a/modules/c++/logging/include/logging/Enums.h +++ b/modules/c++/logging/include/logging/Enums.h @@ -93,7 +93,7 @@ struct LogLevel final else if (s == "SEVERE") value = LOG_CRITICAL; else - throw except::InvalidFormatException(Ctxt(FmtX("Invalid enum value: %s", s.c_str()))); + throw except::InvalidFormatException(Ctxt(str::Format("Invalid enum value: %s", s.c_str()))); } //! int constructor @@ -120,7 +120,7 @@ struct LogLevel final value = LOG_CRITICAL; break; default: - throw except::InvalidFormatException(Ctxt(FmtX("Invalid enum value: %d", i))); + throw except::InvalidFormatException(Ctxt(str::Format("Invalid enum value: %d", i))); } } @@ -145,7 +145,7 @@ struct LogLevel final case 5: return std::string("CRITICAL"); default: - throw except::InvalidFormatException(Ctxt(FmtX("Invalid enum value: %d", value))); + throw except::InvalidFormatException(Ctxt(str::Format("Invalid enum value: %d", value))); } } diff --git a/modules/c++/math.poly/include/math/poly/Fixed1D.h b/modules/c++/math.poly/include/math/poly/Fixed1D.h index 5f14c6fcd4..03024de73f 100644 --- a/modules/c++/math.poly/include/math/poly/Fixed1D.h +++ b/modules/c++/math.poly/include/math/poly/Fixed1D.h @@ -203,14 +203,14 @@ template class Fixed1D _T& operator [] (size_t i) { if (i > _Order) - throw except::IndexOutOfRangeException(Ctxt(FmtX("index [%d] is not in range [0..%d]", i, _Order))); + throw except::IndexOutOfRangeException(Ctxt(str::Format("index [%d] is not in range [0..%d]", i, _Order))); return mCoef[i]; } _T operator [] (size_t i) const { if (i > _Order) - throw except::IndexOutOfRangeException(Ctxt(FmtX("index [%d] is not in range [0..%d]", i, _Order))); + throw except::IndexOutOfRangeException(Ctxt(str::Format("index [%d] is not in range [0..%d]", i, _Order))); return mCoef[i]; diff --git a/modules/c++/net.ssl/source/SSLConnection.cpp b/modules/c++/net.ssl/source/SSLConnection.cpp index 903fa077dc..433d737489 100644 --- a/modules/c++/net.ssl/source/SSLConnection.cpp +++ b/modules/c++/net.ssl/source/SSLConnection.cpp @@ -37,7 +37,7 @@ net::ssl::SSLConnection::SSLConnection(std::unique_ptr&& socket, mSSL = SSL_new(ctx); if (mSSL == nullptr) { - throw net::ssl::SSLException(Ctxt(FmtX("SSL_new failed"))); + throw net::ssl::SSLException(Ctxt(str::Format("SSL_new failed"))); } setupSocket(host); @@ -87,7 +87,7 @@ void net::ssl::SSLConnection::setupSocket(const std::string& hostName) #endif throw net::ssl::SSLException - (Ctxt(FmtX("SSL_connect failed: %d", SSL_get_error(mSSL, val)))); + (Ctxt(str::Format("SSL_connect failed: %d", SSL_get_error(mSSL, val)))); } if(mServerAuthentication) @@ -143,7 +143,7 @@ sys::SSize_T net::ssl::SSLConnection::read(sys::byte* b, sys::Size_T len) std::cout << "=============================================" << std::endl << std::endl; #endif - throw net::ssl::SSLException(Ctxt(FmtX("When receiving %d bytes", + throw net::ssl::SSLException(Ctxt(str::Format("When receiving %d bytes", len)) ); } else if (numBytes == 0) @@ -155,7 +155,7 @@ sys::SSize_T net::ssl::SSLConnection::read(sys::byte* b, sys::Size_T len) return -1; } #if defined(__DEBUG_SOCKET) - std::cout << FmtX("Read %d bytes from socket:", numBytes) << std::endl; + std::cout << str::Format("Read %d bytes from socket:", numBytes) << std::endl; std::cout << "---------------------------------------------" << std::endl; std::cout << std::string(b, numBytes) << std::endl; std::cout << "---------------------------------------------" << std::endl; @@ -172,7 +172,7 @@ void net::ssl::SSLConnection::write(const sys::byte* b, sys::Size_T len) const auto numBytes = SSL_write(mSSL, (const char*)b, len); if (static_cast(numBytes) != len) { - throw net::ssl::SSLException(Ctxt(FmtX("Tried sending %d bytes, %d sent", + throw net::ssl::SSLException(Ctxt(str::Format("Tried sending %d bytes, %d sent", len, numBytes)) ); } diff --git a/modules/c++/net.ssl/source/SSLConnectionClientFactory.cpp b/modules/c++/net.ssl/source/SSLConnectionClientFactory.cpp index 5a9d441480..63a721a818 100644 --- a/modules/c++/net.ssl/source/SSLConnectionClientFactory.cpp +++ b/modules/c++/net.ssl/source/SSLConnectionClientFactory.cpp @@ -60,12 +60,12 @@ void net::ssl::SSLConnectionClientFactory::initializeContext() if(method == nullptr) { - throw net::ssl::SSLException(Ctxt(FmtX("SSLv23_client_method failed"))); + throw net::ssl::SSLException(Ctxt(str::Format("SSLv23_client_method failed"))); } mCtx = SSL_CTX_new(method); if(mCtx == nullptr) { - throw net::ssl::SSLException(Ctxt(FmtX("SSL_CTX_new failed"))); + throw net::ssl::SSLException(Ctxt(str::Format("SSL_CTX_new failed"))); } if(mClientAuthentication) diff --git a/modules/c++/net/source/DaemonUnix.cpp b/modules/c++/net/source/DaemonUnix.cpp index 8683816f95..797f515788 100644 --- a/modules/c++/net/source/DaemonUnix.cpp +++ b/modules/c++/net/source/DaemonUnix.cpp @@ -258,12 +258,12 @@ void DaemonUnix::redirectStreamsTo(const std::string& filename) if (openFileFor(STDOUT_FILENO, filename, O_WRONLY|O_CREAT|O_TRUNC) < 0) { throw except::Exception( - Ctxt(FmtX("Failed to open file %s for STDOUT.", filename.c_str()))); + Ctxt(str::Format("Failed to open file %s for STDOUT.", filename.c_str()))); } if (openFileFor(STDERR_FILENO, filename, O_WRONLY|O_CREAT|O_TRUNC) < 0) { throw except::Exception( - Ctxt(FmtX("Failed to open file %s for STDERR.", filename.c_str()))); + Ctxt(str::Format("Failed to open file %s for STDERR.", filename.c_str()))); } } diff --git a/modules/c++/net/source/Socket.cpp b/modules/c++/net/source/Socket.cpp index 9c0c791b90..4fa436453c 100644 --- a/modules/c++/net/source/Socket.cpp +++ b/modules/c++/net/source/Socket.cpp @@ -115,7 +115,7 @@ size_t net::Socket::recv(void* b, size_t len, int flags) return static_cast(-1); } #if defined(__DEBUG_SOCKET) - std::cout << FmtX("Read %d bytes from socket:", numBytes) << std::endl; + std::cout << str::Format("Read %d bytes from socket:", numBytes) << std::endl; std::cout << "---------------------------------------------" << std::endl; std::cout << std::string(b, numBytes) << std::endl; std::cout << "---------------------------------------------" << std::endl; diff --git a/modules/c++/plugin/include/plugin/BasicPluginManager.h b/modules/c++/plugin/include/plugin/BasicPluginManager.h index c421beea9e..6e65ea9eb4 100644 --- a/modules/c++/plugin/include/plugin/BasicPluginManager.h +++ b/modules/c++/plugin/include/plugin/BasicPluginManager.h @@ -278,8 +278,8 @@ template class BasicPluginManager for (unsigned int i = 0; ops[i] != nullptr; i++) oss << ops[i] << ":"; - auto unsupported = FmtX("For plugin supporting ops %s version ", oss.str()); - unsupported += FmtX("[%d.%d] not supported (%d.%d)", majorVersion, minorVersion, mMajorVersion, mMinorVersion); + auto unsupported = str::Format("For plugin supporting ops %s version ", oss.str()); + unsupported += str::Format("[%d.%d] not supported (%d.%d)", majorVersion, minorVersion, mMajorVersion, mMinorVersion); eh->onPluginVersionUnsupported(unsupported); return; } @@ -290,7 +290,7 @@ template class BasicPluginManager if (! pluginHandler ) { eh->onPluginLoadFailed( - FmtX("Failed to spawn handler for op %s", ops[i])); + str::Format("Failed to spawn handler for op %s", ops[i])); // Keep going } mHandlers[ops[i]].first = pluginHandler; diff --git a/modules/c++/sio.lite/include/sio/lite/SioFileWriter.h b/modules/c++/sio.lite/include/sio/lite/SioFileWriter.h index 8d75266d52..72a895b00c 100644 --- a/modules/c++/sio.lite/include/sio/lite/SioFileWriter.h +++ b/modules/c++/sio.lite/include/sio/lite/SioFileWriter.h @@ -173,7 +173,7 @@ template void writeSIO(const T* image, size_t rows, size_t cols, et = FileHeader::UNSIGNED; break; default: - throw except::Exception(Ctxt(FmtX("Unexpected es: %d", es))); + throw except::Exception(Ctxt(str::Format("Unexpected es: %d", es))); } } diff --git a/modules/c++/str/include/str/Format.h b/modules/c++/str/include/str/Format.h index 44808100f1..6261e204df 100644 --- a/modules/c++/str/include/str/Format.h +++ b/modules/c++/str/include/str/Format.h @@ -65,152 +65,155 @@ inline auto Format(const char* format, const T1& t1, const T2& t2, const T3& t3, return Format_(format, t1, t2, t3, t4); } } -} + /*! * \param format The format * \param ... Any printf like thing */ -inline auto FmtX(const char* format) +inline auto Format(const char* format) { - return str::details::Format(format); + return details::Format(format); } -inline auto FmtX(const char* format, const char* pStr) +inline auto Format(const char* format, const char* pStr) { - return str::details::Format(format, pStr); + return details::Format(format, pStr); } -inline auto FmtX(const char* format, const std::string& s) +inline auto Format(const char* format, const std::string& s) { - return FmtX(format, s.c_str()); + return Format(format, s.c_str()); } -inline auto FmtX(const char* format, int i) +inline auto Format(const char* format, int i) { - return str::details::Format(format, i); + return details::Format(format, i); } -inline auto FmtX(const char* format, uint32_t i) +inline auto Format(const char* format, uint32_t i) { - return str::details::Format(format, i); + return details::Format(format, i); } -inline auto FmtX(const char* format, ptrdiff_t l) +inline auto Format(const char* format, ptrdiff_t l) { - return str::details::Format(format, l); + return details::Format(format, l); } -inline auto FmtX(const char* format, size_t ul) +inline auto Format(const char* format, size_t ul) { - return str::details::Format(format, ul); + return details::Format(format, ul); } -inline auto FmtX(const char* format, float f) +inline auto Format(const char* format, float f) { - return str::details::Format(format, f); + return details::Format(format, f); } -inline auto FmtX(const char* format, double d) +inline auto Format(const char* format, double d) { - return str::details::Format(format, d); + return details::Format(format, d); } -inline auto FmtX(const char* format, const void* p) +inline auto Format(const char* format, const void* p) { - return str::details::Format(format, p); + return details::Format(format, p); } -inline auto FmtX(const char* format, char ch, const char* pStr) +inline auto Format(const char* format, char ch, const char* pStr) { - return str::details::Format(format, ch, pStr); + return details::Format(format, ch, pStr); } -inline auto FmtX(const char* format, char ch, const std::string& s) +inline auto Format(const char* format, char ch, const std::string& s) { - return FmtX(format, ch, s.c_str()); + return Format(format, ch, s.c_str()); } -inline auto FmtX(const char* format, const std::string& s1, const std::string& s2) +inline auto Format(const char* format, const std::string& s1, const std::string& s2) { - return str::details::Format(format, s1.c_str(), s2.c_str()); + return details::Format(format, s1.c_str(), s2.c_str()); } -inline auto FmtX(const char* format, const std::string& s, size_t ul) +inline auto Format(const char* format, const std::string& s, size_t ul) { - return str::details::Format(format, s.c_str(), ul); + return details::Format(format, s.c_str(), ul); } -inline auto FmtX(const char* format, char ch1, char ch2) +inline auto Format(const char* format, char ch1, char ch2) { - return str::details::Format(format, ch1, ch2); + return details::Format(format, ch1, ch2); } -inline auto FmtX(const char* format, int i1, int i2) +inline auto Format(const char* format, int i1, int i2) { - return str::details::Format(format, i1, i2); + return details::Format(format, i1, i2); } -inline auto FmtX(const char* format, long l1, long l2) +inline auto Format(const char* format, long l1, long l2) { - return str::details::Format(format, l1, l2); + return details::Format(format, l1, l2); } -inline auto FmtX(const char* format, uint32_t ui1, uint32_t ui2) +inline auto Format(const char* format, uint32_t ui1, uint32_t ui2) { - return str::details::Format(format, ui1, ui2); + return details::Format(format, ui1, ui2); } -inline auto FmtX(const char* format, size_t ul1, size_t ul2) +inline auto Format(const char* format, size_t ul1, size_t ul2) { - return str::details::Format(format, ul1, ul2); + return details::Format(format, ul1, ul2); } -inline auto FmtX(const char* format, size_t ul1, int i2) +inline auto Format(const char* format, size_t ul1, int i2) { - return str::details::Format(format, ul1, i2); + return details::Format(format, ul1, i2); } -inline auto FmtX(const char* format, double d1, double d2) +inline auto Format(const char* format, double d1, double d2) { - return str::details::Format(format, d1, d2); + return details::Format(format, d1, d2); } -inline auto FmtX(const char* format, int i, const char* pStr) +inline auto Format(const char* format, int i, const char* pStr) { - return str::details::Format(format, i, pStr); + return details::Format(format, i, pStr); } -inline auto FmtX(const char* fmt, int i, const std::string& s) +inline auto Format(const char* fmt, int i, const std::string& s) { - return FmtX(fmt, i, s.c_str()); + return Format(fmt, i, s.c_str()); } -inline auto FmtX(const char* format, char ch1, char ch2, const std::string& s) +inline auto Format(const char* format, char ch1, char ch2, const std::string& s) { - return str::details::Format(format, ch1, ch2, s.c_str()); + return details::Format(format, ch1, ch2, s.c_str()); } -inline auto FmtX(const char* format, int i1, int i2, unsigned long ul) +inline auto Format(const char* format, int i1, int i2, unsigned long ul) { - return str::details::Format(format, i1, i2, ul); + return details::Format(format, i1, i2, ul); } -inline auto FmtX(const char* format, int i1, int i2, int i3) +inline auto Format(const char* format, int i1, int i2, int i3) { - return str::details::Format(format, i1, i2, i3); + return details::Format(format, i1, i2, i3); } -inline auto FmtX(const char* format, float f1, float f2, float f3) +inline auto Format(const char* format, float f1, float f2, float f3) { - return str::details::Format(format, f1, f2, f3); + return details::Format(format, f1, f2, f3); } -inline std::string FmtX(const char* format, const std::string& s1, int i2, int i3) +inline std::string Format(const char* format, const std::string& s1, int i2, int i3) { - return str::details::Format(format, s1.c_str(), i2, i3); + return details::Format(format, s1.c_str(), i2, i3); } -inline auto FmtX(const char* format, const std::string& s1, const std::string& s2, uint32_t ui) +inline auto Format(const char* format, const std::string& s1, const std::string& s2, uint32_t ui) { - return str::details::Format(format, s1.c_str(), s2.c_str(), ui); + return details::Format(format, s1.c_str(), s2.c_str(), ui); } -inline auto FmtX(const char* format, const std::string& s1, const std::string& s2, const std::string& s3) +inline auto Format(const char* format, const std::string& s1, const std::string& s2, const std::string& s3) { - return str::details::Format(format, s1.c_str(), s2.c_str(), s3.c_str()); + return details::Format(format, s1.c_str(), s2.c_str(), s3.c_str()); } -inline auto FmtX(const char* format, int i1, int i2, int i3, int i4) +inline auto Format(const char* format, int i1, int i2, int i3, int i4) { - return str::details::Format(format, i1, i2, i3, i4); + return details::Format(format, i1, i2, i3, i4); } -inline auto FmtX(const char* format, uint32_t ui1, uint32_t ui2, uint32_t ui3, uint32_t ui4) +inline auto Format(const char* format, uint32_t ui1, uint32_t ui2, uint32_t ui3, uint32_t ui4) { - return str::details::Format(format, ui1, ui2, ui3, ui4); + return details::Format(format, ui1, ui2, ui3, ui4); } -inline auto FmtX(const char* format, const char* pStr1, const std::string& s2, const char* pStr3, const std::string& s4) +inline auto Format(const char* format, const char* pStr1, const std::string& s2, const char* pStr3, const std::string& s4) { - return str::details::Format(format, pStr1, s2.c_str(), pStr3, s4.c_str()); + return details::Format(format, pStr1, s2.c_str(), pStr3, s4.c_str()); } -inline auto FmtX(const char* format, const std::string& s1, int i2, const std::string& s3, int i4) +inline auto Format(const char* format, const std::string& s1, int i2, const std::string& s3, int i4) { - return str::details::Format(format, s1.c_str(), i2, s3.c_str(), i4); + return details::Format(format, s1.c_str(), i2, s3.c_str(), i4); } +} + +#define FmtX(format, ...) str::Format(format, __VA_ARGS__) #endif // CODA_OSS_str_Format_h_INCLUDED_ diff --git a/modules/c++/sys/include/sys/ThreadInterface.h b/modules/c++/sys/include/sys/ThreadInterface.h index e536fe48e7..cefbf8dcc4 100644 --- a/modules/c++/sys/include/sys/ThreadInterface.h +++ b/modules/c++/sys/include/sys/ThreadInterface.h @@ -132,7 +132,7 @@ struct CODA_OSS_API ThreadInterface : public Runnable // of nasty issues that could pop up (execution in freed memory, etc). if (isRunning()) { - std::cerr << Ctxt(FmtX("Thread object [%s] destructed before " \ + std::cerr << Ctxt(str::Format("Thread object [%s] destructed before " \ "thread terminated, aborting program.", getName().c_str())) << std::endl; abort(); diff --git a/modules/c++/sys/source/DLLWin32.cpp b/modules/c++/sys/source/DLLWin32.cpp index 466f7d2952..6fc96611fc 100644 --- a/modules/c++/sys/source/DLLWin32.cpp +++ b/modules/c++/sys/source/DLLWin32.cpp @@ -45,7 +45,7 @@ void sys::DLL::load(const std::string& libName) // Now we check the return value if (!mLib) - throw sys::DLLException(FmtX("Failed to load() DLL: %s", + throw sys::DLLException(str::Format("Failed to load() DLL: %s", mLibName.c_str() ) ); } @@ -75,7 +75,7 @@ retrieve(const std::string& functionName) // Now we check the ptr value if (ptr == nullptr) - throw sys::DLLException(FmtX("Failed to load function: %s", + throw sys::DLLException(str::Format("Failed to load function: %s", functionName.c_str())); return ptr; } diff --git a/modules/c++/sys/source/FileUnix.cpp b/modules/c++/sys/source/FileUnix.cpp index e603835a32..07d6c5ff94 100644 --- a/modules/c++/sys/source/FileUnix.cpp +++ b/modules/c++/sys/source/FileUnix.cpp @@ -43,7 +43,7 @@ void sys::File::create(const std::string& str, int accessFlags, if (mHandle < 0) { throw sys::SystemException(Ctxt( - FmtX("Error opening file [%d]: [%s]", mHandle, str.c_str()))); + str::Format("Error opening file [%d]: [%s]", mHandle, str.c_str()))); } } diff --git a/modules/c++/sys/source/FileWin32.cpp b/modules/c++/sys/source/FileWin32.cpp index 5e2f2b23d0..cf30c9f6cc 100644 --- a/modules/c++/sys/source/FileWin32.cpp +++ b/modules/c++/sys/source/FileWin32.cpp @@ -59,7 +59,7 @@ void sys::File::create(const std::string& str, if (mHandle == INVALID_HANDLE_VALUE) { throw sys::SystemException( - Ctxt(FmtX("Error opening file: [%s]", str.c_str()))); + Ctxt(str::Format("Error opening file: [%s]", str.c_str()))); } } @@ -169,7 +169,7 @@ sys::Off_T sys::File::lastModifiedTime() return (sys::Off_T)stInMillis; } throw sys::SystemException(Ctxt( - FmtX("Error getting last modified time for path %s", + str::Format("Error getting last modified time for path %s", mPath.c_str()))); } diff --git a/modules/c++/sys/source/OSUnix.cpp b/modules/c++/sys/source/OSUnix.cpp index 5d5685ba92..d1806010d8 100644 --- a/modules/c++/sys/source/OSUnix.cpp +++ b/modules/c++/sys/source/OSUnix.cpp @@ -144,8 +144,8 @@ std::string sys::OSUnix::getPlatformName() const throw sys::SystemException("Uname failed"); std::string retval = name.sysname; - retval += FmtX(" (%s): %s", name.machine, name.release); - retval += FmtX(" [build: %s]", name.version); + retval += str::Format(" (%s): %s", name.machine, name.release); + retval += str::Format(" [build: %s]", name.version); return retval; } diff --git a/modules/c++/sys/source/OSWin32.cpp b/modules/c++/sys/source/OSWin32.cpp index 62ac0cda35..bde4cb5d28 100644 --- a/modules/c++/sys/source/OSWin32.cpp +++ b/modules/c++/sys/source/OSWin32.cpp @@ -59,7 +59,7 @@ std::string sys::OSWin32::getPlatformName() const platform = "Unknown Windows OS"; } auto retval = platform + ": "; - retval += FmtX("%d.%d [build: %d], ", info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber); + retval += str::Format("%d.%d [build: %d], ", info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber); retval += info.szCSDVersion; return retval; } diff --git a/modules/c++/tiff/include/tiff/IFD.h b/modules/c++/tiff/include/tiff/IFD.h index 706309e8f4..4975bdaaa8 100644 --- a/modules/c++/tiff/include/tiff/IFD.h +++ b/modules/c++/tiff/include/tiff/IFD.h @@ -155,7 +155,7 @@ class IFD : public io::Serializable const tiff::IFDEntry *mapEntry = tiff::KnownTagsRegistry::getInstance()[name]; //we can't add it if we don't know about it if (!mapEntry) - throw except::Exception(Ctxt(FmtX( + throw except::Exception(Ctxt(str::Format( "Unable to add IFD Entry: unknown tag [%s]", name.c_str()))); const auto id = mapEntry->getTagID(); diff --git a/modules/c++/tiff/include/tiff/TiffFileWriter.h b/modules/c++/tiff/include/tiff/TiffFileWriter.h index fb495a6766..f6474c6e2e 100644 --- a/modules/c++/tiff/include/tiff/TiffFileWriter.h +++ b/modules/c++/tiff/include/tiff/TiffFileWriter.h @@ -191,7 +191,7 @@ template void writeTIFF(const T* image, size_t rows, size_t cols, et = ::tiff::Const::SampleFormatType::UNSIGNED_INT; break; default: - throw except::Exception(Ctxt(FmtX("Unexpected es: %d", es))); + throw except::Exception(Ctxt(str::Format("Unexpected es: %d", es))); } } unsigned short alpha(0); diff --git a/modules/c++/tiff/source/IFD.cpp b/modules/c++/tiff/source/IFD.cpp index fb145461db..bb9d80e7dc 100644 --- a/modules/c++/tiff/source/IFD.cpp +++ b/modules/c++/tiff/source/IFD.cpp @@ -90,7 +90,7 @@ void tiff::IFD::addEntry(const std::string& name) tiff::IFDEntry *mapEntry = tiff::KnownTagsRegistry::getInstance()[name]; // we can't add it b/c we don't know about this tag if (!mapEntry) - throw except::Exception(Ctxt(FmtX( + throw except::Exception(Ctxt(str::Format( "Unable to add IFD Entry: unknown tag [%s]", name.c_str()))); unsigned short id = mapEntry->getTagID(); diff --git a/modules/c++/tiff/source/ImageReader.cpp b/modules/c++/tiff/source/ImageReader.cpp index 5fd42c6adc..9e5c1cbf7e 100644 --- a/modules/c++/tiff/source/ImageReader.cpp +++ b/modules/c++/tiff/source/ImageReader.cpp @@ -65,7 +65,7 @@ void tiff::ImageReader::getData(unsigned char *buffer, { unsigned short c = *(tiff::GenericType *)(*compression)[0]; if (c != tiff::Const::CompressionType::NO_COMPRESSION) - throw except::Exception(Ctxt(FmtX("Unsupported compression type: %d", c))); + throw except::Exception(Ctxt(str::Format("Unsupported compression type: %d", c))); } if (mIFD["StripOffsets"]) diff --git a/modules/c++/tiff/source/TiffFileReader.cpp b/modules/c++/tiff/source/TiffFileReader.cpp index e0d78f8417..812a1bb9ce 100644 --- a/modules/c++/tiff/source/TiffFileReader.cpp +++ b/modules/c++/tiff/source/TiffFileReader.cpp @@ -79,7 +79,7 @@ void tiff::FileReader::close() tiff::ImageReader *tiff::FileReader::operator[](const sys::Uint32_T index) const { if (index >= mImages.size()) - throw except::Exception(Ctxt(FmtX("Index out of range: %d", index))); + throw except::Exception(Ctxt(str::Format("Index out of range: %d", index))); return mImages[index]; } @@ -106,7 +106,7 @@ void tiff::FileReader::getData(unsigned char *buffer, const sys::Uint32_T numElementsToRead, const sys::Uint32_T imageIndex) { if (imageIndex >= mImages.size()) - throw except::Exception(Ctxt(FmtX("Index out of range", imageIndex))); + throw except::Exception(Ctxt(str::Format("Index out of range", imageIndex))); mImages[imageIndex]->getData(buffer, numElementsToRead); } diff --git a/modules/c++/xml.lite/source/Attributes.cpp b/modules/c++/xml.lite/source/Attributes.cpp index 2628ca7c84..4032f86fc0 100644 --- a/modules/c++/xml.lite/source/Attributes.cpp +++ b/modules/c++/xml.lite/source/Attributes.cpp @@ -81,7 +81,7 @@ std::string xml::lite::Attributes::getValue(int i) const } catch (const std::out_of_range& ex) { - throw except::NoSuchKeyException(Ctxt(FmtX("attributes[%d] not found, %s", i, ex.what()))); + throw except::NoSuchKeyException(Ctxt(str::Format("attributes[%d] not found, %s", i, ex.what()))); } } bool xml::lite::Attributes::getValue(int i, std::string& result) const @@ -125,7 +125,7 @@ std::string xml::lite::Attributes::getValue(const std::string& qname) const std::string retval; if (!getValue(qname, retval)) { - throw except::NoSuchKeyException(Ctxt(FmtX("QName '%s' could not be found", + throw except::NoSuchKeyException(Ctxt(str::Format("QName '%s' could not be found", qname.c_str()))); } @@ -152,7 +152,7 @@ std::string xml::lite::Attributes::getValue(const QName& qname) const { const auto uri = qname.getUri().value; const auto localName = qname.getName(); - throw except::NoSuchKeyException(Ctxt(FmtX("(uri: %s, localName: %s", uri, localName))); + throw except::NoSuchKeyException(Ctxt(str::Format("(uri: %s, localName: %s", uri, localName))); } return retval; } diff --git a/modules/c++/zip/source/ZipEntry.cpp b/modules/c++/zip/source/ZipEntry.cpp index 4c93526729..14bc85be17 100644 --- a/modules/c++/zip/source/ZipEntry.cpp +++ b/modules/c++/zip/source/ZipEntry.cpp @@ -50,7 +50,7 @@ void ZipEntry::inflate(sys::ubyte* out, sys::Size_T outLen, sys::ubyte* in, int zerr = inflateInit2(&zstream, -MAX_WBITS); if (zerr != Z_OK) { - throw except::IOException(Ctxt(FmtX("inflateInit2 failed [%d]", zerr))); + throw except::IOException(Ctxt(str::Format("inflateInit2 failed [%d]", zerr))); } // decompress @@ -58,7 +58,7 @@ void ZipEntry::inflate(sys::ubyte* out, sys::Size_T outLen, sys::ubyte* in, if (zerr != Z_STREAM_END) { - throw except::IOException(Ctxt(FmtX( + throw except::IOException(Ctxt(str::Format( "inflate failed [%d]: wanted: %d, got: %lu", zerr, Z_STREAM_END, zstream.total_out))); }