diff --git a/simgear/io/iostreams/zlibstream.cxx b/simgear/io/iostreams/zlibstream.cxx index 6898d068..eaf997f8 100644 --- a/simgear/io/iostreams/zlibstream.cxx +++ b/simgear/io/iostreams/zlibstream.cxx @@ -31,7 +31,7 @@ #include #include #include // std::numeric_limits -#include // std::make_unsigned() +#include // std::make_unsigned(), std::underlying_type #include // std::size_t, std::ptrdiff_t #include @@ -46,6 +46,11 @@ using std::string; using traits = std::char_traits; +// Cast an enum value to its underlying type +template +static constexpr typename std::underlying_type::type enumValue(T e) { + return static_cast::type>(e); +} // Private utility function static string zlibErrorMessage(const z_stream& zstream, int errorCode) @@ -264,7 +269,7 @@ ZlibAbstractIStreambuf::fOB_remainingSpace(unsigned char* nextOutPtr) const [[ noreturn ]] void ZlibAbstractIStreambuf::handleZ_BUF_ERROR() const { switch (operationType()) { - case OPERATION_TYPE_DECOMPRESSION: + case OperationType::DECOMPRESSION: { string message = (_path.isNull()) ? "Got Z_BUF_ERROR from zlib while decompressing a stream. The stream " @@ -275,15 +280,15 @@ ZlibAbstractIStreambuf::fOB_remainingSpace(unsigned char* nextOutPtr) const // When _path.isNull(), sg_location(_path) is equivalent to sg_location() throw sg_io_exception(message, sg_location(_path)); } - case OPERATION_TYPE_COMPRESSION: + case OperationType::COMPRESSION: throw std::logic_error( "Called ZlibAbstractIStreambuf::handleZ_BUF_ERROR() with " - "operationType() == OPERATION_TYPE_DECOMPRESSION"); + "operationType() == ZlibAbstractIStreambuf::OperationType::COMPRESSION"); default: throw std::logic_error( "Unexpected operationType() in " "ZlibAbstractIStreambuf::handleZ_BUF_ERROR(): " + - std::to_string(operationType())); + std::to_string(enumValue(operationType()))); } } @@ -607,7 +612,7 @@ ZlibCompressorIStreambuf::~ZlibCompressorIStreambuf() ZlibAbstractIStreambuf::OperationType ZlibCompressorIStreambuf::operationType() const { - return OPERATION_TYPE_COMPRESSION; + return OperationType::COMPRESSION; } void ZlibCompressorIStreambuf::zStreamInit(int compressionLevel, @@ -616,30 +621,30 @@ void ZlibCompressorIStreambuf::zStreamInit(int compressionLevel, { int windowBits, memLevel; - // Intentionally not listing ZLIB_COMPRESSION_FORMAT_AUTODETECT here (it is + // Intentionally not listing ZLibCompressionFormat::AUTODETECT here (it is // only for decompression!) switch (format) { - case ZLIB_COMPRESSION_FORMAT_ZLIB: + case ZLibCompressionFormat::ZLIB: windowBits = 15; break; - case ZLIB_COMPRESSION_FORMAT_GZIP: + case ZLibCompressionFormat::GZIP: windowBits = 31; break; default: throw std::logic_error("Unexpected compression format: " + - std::to_string(format)); + std::to_string(enumValue(format))); } switch (memStrategy) { - case ZLIB_FAVOR_MEMORY_OVER_SPEED: + case ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED: memLevel = 8; break; - case ZLIB_FAVOR_SPEED_OVER_MEMORY: + case ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY: memLevel = 9; break; default: throw std::logic_error("Unexpected memory strategy: " + - std::to_string(memStrategy)); + std::to_string(enumValue(memStrategy))); } _zstream.zalloc = Z_NULL; // No custom memory allocation routines @@ -710,7 +715,7 @@ ZlibDecompressorIStreambuf::~ZlibDecompressorIStreambuf() ZlibAbstractIStreambuf::OperationType ZlibDecompressorIStreambuf::operationType() const { - return OPERATION_TYPE_DECOMPRESSION; + return OperationType::DECOMPRESSION; } void ZlibDecompressorIStreambuf::zStreamInit(ZLibCompressionFormat format) @@ -718,18 +723,18 @@ void ZlibDecompressorIStreambuf::zStreamInit(ZLibCompressionFormat format) int windowBits; switch (format) { - case ZLIB_COMPRESSION_FORMAT_ZLIB: + case ZLibCompressionFormat::ZLIB: windowBits = 15; break; - case ZLIB_COMPRESSION_FORMAT_GZIP: + case ZLibCompressionFormat::GZIP: windowBits = 31; break; - case ZLIB_COMPRESSION_FORMAT_AUTODETECT: + case ZLibCompressionFormat::AUTODETECT: windowBits = 47; // 47 = 32 + 15 break; default: throw std::logic_error("Unexpected compression format: " + - std::to_string(format)); + std::to_string(enumValue(format))); } _zstream.zalloc = Z_NULL; // No custom memory allocation routines diff --git a/simgear/io/iostreams/zlibstream.hxx b/simgear/io/iostreams/zlibstream.hxx index bd264a1b..690b65f3 100644 --- a/simgear/io/iostreams/zlibstream.hxx +++ b/simgear/io/iostreams/zlibstream.hxx @@ -99,15 +99,15 @@ namespace simgear { -enum ZLibCompressionFormat { - ZLIB_COMPRESSION_FORMAT_ZLIB = 0, - ZLIB_COMPRESSION_FORMAT_GZIP, - ZLIB_COMPRESSION_FORMAT_AUTODETECT +enum class ZLibCompressionFormat { + ZLIB = 0, + GZIP, + AUTODETECT }; -enum ZLibMemoryStrategy { - ZLIB_FAVOR_MEMORY_OVER_SPEED = 0, - ZLIB_FAVOR_SPEED_OVER_MEMORY +enum class ZLibMemoryStrategy { + FAVOR_MEMORY_OVER_SPEED = 0, + FAVOR_SPEED_OVER_MEMORY }; // Abstract base class for both the compressor and decompressor stream buffers. @@ -164,9 +164,9 @@ public: ~ZlibAbstractIStreambuf(); protected: - enum OperationType { - OPERATION_TYPE_COMPRESSION = 0, - OPERATION_TYPE_DECOMPRESSION + enum class OperationType { + COMPRESSION = 0, + DECOMPRESSION }; virtual OperationType operationType() const = 0; @@ -290,16 +290,16 @@ public: // the highest compression speed but worst compression // ratio, and 9 the highest compression ratio but lowest // compression speed. - // format either ZLIB_COMPRESSION_FORMAT_ZLIB or - // ZLIB_COMPRESSION_FORMAT_GZIP - // memStrategy either ZLIB_FAVOR_MEMORY_OVER_SPEED or - // ZLIB_FAVOR_SPEED_OVER_MEMORY + // format either ZLibCompressionFormat::ZLIB or + // ZLibCompressionFormat::GZIP + // memStrategy either ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED or + // ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY explicit ZlibCompressorIStreambuf( std::istream& iStream, const SGPath& path = SGPath(), int compressionLevel = Z_DEFAULT_COMPRESSION, - ZLibCompressionFormat format = ZLIB_COMPRESSION_FORMAT_ZLIB, - ZLibMemoryStrategy memStrategy = ZLIB_FAVOR_SPEED_OVER_MEMORY, + ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB, + ZLibMemoryStrategy memStrategy = ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY, char* inBuf = nullptr, std::size_t inBufSize = 262144, char* outBuf = nullptr, @@ -311,8 +311,8 @@ public: std::unique_ptr _iStream_p, const SGPath& path = SGPath(), int compressionLevel = Z_DEFAULT_COMPRESSION, - ZLibCompressionFormat format = ZLIB_COMPRESSION_FORMAT_ZLIB, - ZLibMemoryStrategy memStrategy = ZLIB_FAVOR_SPEED_OVER_MEMORY, + ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB, + ZLibMemoryStrategy memStrategy = ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY, char* inBuf = nullptr, std::size_t inBufSize = 262144, char* outBuf = nullptr, @@ -344,13 +344,13 @@ class ZlibDecompressorIStreambuf: public ZlibAbstractIStreambuf public: // Same parameters as for ZlibAbstractIStreambuf, except: // - // format ZLIB_COMPRESSION_FORMAT_ZLIB, - // ZLIB_COMPRESSION_FORMAT_GZIP or - // ZLIB_COMPRESSION_FORMAT_AUTODETECT + // format ZLibCompressionFormat::ZLIB, + // ZLibCompressionFormat::GZIP or + // ZLibCompressionFormat::AUTODETECT explicit ZlibDecompressorIStreambuf( std::istream& iStream, const SGPath& path = SGPath(), - ZLibCompressionFormat format = ZLIB_COMPRESSION_FORMAT_ZLIB, + ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB, char* inBuf = nullptr, std::size_t inBufSize = 262144, char* outBuf = nullptr, @@ -361,7 +361,7 @@ public: explicit ZlibDecompressorIStreambuf( std::unique_ptr _iStream_p, const SGPath& path = SGPath(), - ZLibCompressionFormat format = ZLIB_COMPRESSION_FORMAT_ZLIB, + ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB, char* inBuf = nullptr, std::size_t inBufSize = 262144, char* outBuf = nullptr, @@ -398,8 +398,8 @@ public: std::istream& iStream, const SGPath& path = SGPath(), int compressionLevel = Z_DEFAULT_COMPRESSION, - ZLibCompressionFormat format = ZLIB_COMPRESSION_FORMAT_ZLIB, - ZLibMemoryStrategy memStrategy = ZLIB_FAVOR_SPEED_OVER_MEMORY, + ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB, + ZLibMemoryStrategy memStrategy = ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY, char* inBuf = nullptr, std::size_t inBufSize = 262144, char* outBuf = nullptr, @@ -411,8 +411,8 @@ public: std::unique_ptr _iStream_p, const SGPath& path = SGPath(), int compressionLevel = Z_DEFAULT_COMPRESSION, - ZLibCompressionFormat format = ZLIB_COMPRESSION_FORMAT_ZLIB, - ZLibMemoryStrategy memStrategy = ZLIB_FAVOR_SPEED_OVER_MEMORY, + ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB, + ZLibMemoryStrategy memStrategy = ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY, char* inBuf = nullptr, std::size_t inBufSize = 262144, char* outBuf = nullptr, @@ -445,7 +445,7 @@ public: explicit ZlibDecompressorIStream( std::istream& iStream, const SGPath& path = SGPath(), - ZLibCompressionFormat format = ZLIB_COMPRESSION_FORMAT_ZLIB, + ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB, char* inBuf = nullptr, std::size_t inBufSize = 262144, char* outBuf = nullptr, @@ -456,7 +456,7 @@ public: explicit ZlibDecompressorIStream( std::unique_ptr _iStream_p, const SGPath& path = SGPath(), - ZLibCompressionFormat format = ZLIB_COMPRESSION_FORMAT_ZLIB, + ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB, char* inBuf = nullptr, std::size_t inBufSize = 262144, char* outBuf = nullptr, diff --git a/simgear/io/iostreams/zlibstream_test.cxx b/simgear/io/iostreams/zlibstream_test.cxx index 9f1da25b..e455b9b9 100644 --- a/simgear/io/iostreams/zlibstream_test.cxx +++ b/simgear/io/iostreams/zlibstream_test.cxx @@ -35,6 +35,8 @@ #include // std::size_t #include // strcmp() +#include // Z_BEST_COMPRESSION + #include #include #include @@ -195,9 +197,9 @@ void test_StreambufBasicOperations() static constexpr std::size_t compOutBufSize = 4; static constexpr std::size_t compPutbackSize = 0; simgear::ZlibCompressorIStreambuf compSBuf( - text_ss, SGPath(), 8, simgear::ZLIB_COMPRESSION_FORMAT_ZLIB, - simgear::ZLIB_FAVOR_SPEED_OVER_MEMORY, nullptr, compInBufSize, nullptr, - compOutBufSize, compPutbackSize); + text_ss, SGPath(), 8, simgear::ZLibCompressionFormat::ZLIB, + simgear::ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY, + nullptr, compInBufSize, nullptr, compOutBufSize, compPutbackSize); std::stringstream compressedOutput_ss; compressedOutput_ss << &compSBuf; @@ -205,7 +207,7 @@ void test_StreambufBasicOperations() static constexpr std::size_t decompOutBufSize = 4; static constexpr std::size_t decompPutbackSize = 2; simgear::ZlibDecompressorIStreambuf decompSBuf( - compressedOutput_ss, SGPath(), simgear::ZLIB_COMPRESSION_FORMAT_ZLIB, + compressedOutput_ss, SGPath(), simgear::ZLibCompressionFormat::ZLIB, nullptr, decompInBufSize, nullptr, decompOutBufSize, decompPutbackSize); int ch = decompSBuf.sgetc(); @@ -303,8 +305,8 @@ void test_ZlibDecompressorIStreambuf_readLargestPossibleAmount() input_ss, // input stream SGPath(), // this stream is not associated to a file 9, // compression level - simgear::ZLIB_COMPRESSION_FORMAT_ZLIB, - simgear::ZLIB_FAVOR_SPEED_OVER_MEMORY, + simgear::ZLibCompressionFormat::ZLIB, + simgear::ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY, nullptr, // dynamically allocate the input buffer 230, // input buffer size nullptr, // dynamically allocate the output buffer @@ -315,7 +317,7 @@ void test_ZlibDecompressorIStreambuf_readLargestPossibleAmount() // Decompressor stream buffer (std::streambuf subclass) that gets input data // from our compressor 'compIStream' (std::istream subclass) simgear::ZlibDecompressorIStreambuf decompSBuf( - compIStream, SGPath(), simgear::ZLIB_COMPRESSION_FORMAT_ZLIB, + compIStream, SGPath(), simgear::ZLibCompressionFormat::ZLIB, nullptr, 150, nullptr, 175, 2); std::unique_ptr buf(new char[maxDataSize]); @@ -351,12 +353,12 @@ void test_formattedInputFromDecompressor() static char inBuf[6]; static char outBuf[15]; string compressed = compress( - lipsum, simgear::ZLIB_COMPRESSION_FORMAT_ZLIB, Z_BEST_COMPRESSION, - simgear::ZLIB_FAVOR_MEMORY_OVER_SPEED, /* putback size */ 0); + lipsum, simgear::ZLibCompressionFormat::ZLIB, Z_BEST_COMPRESSION, + simgear::ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED, /* putback size */ 0); std::istringstream compressed_ss(compressed); simgear::ZlibDecompressorIStream decompressor( - compressed_ss, SGPath(), simgear::ZLIB_COMPRESSION_FORMAT_ZLIB, + compressed_ss, SGPath(), simgear::ZLibCompressionFormat::ZLIB, inBuf, sizeof(inBuf), outBuf, sizeof(outBuf), /* putback size */ 1); decompressor.exceptions(std::ios_base::badbit); // throw if badbit is set @@ -395,15 +397,15 @@ void test_ZlibDecompressorIStream_readPutbackEtc() simgear::ZlibCompressorIStream compressor( text_ss, SGPath(), Z_BEST_COMPRESSION, - simgear::ZLIB_COMPRESSION_FORMAT_ZLIB, - simgear::ZLIB_FAVOR_MEMORY_OVER_SPEED, + simgear::ZLibCompressionFormat::ZLIB, + simgear::ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED, compInBuf, sizeof(compInBuf), compOutBuf, sizeof(compOutBuf), /* putback size */ 0); compressor.exceptions(std::ios_base::badbit); // throw if badbit is set // Use the compressor (subclass of std::istream) as input to the decompressor simgear::ZlibDecompressorIStream decompressor( - compressor, SGPath(), simgear::ZLIB_COMPRESSION_FORMAT_ZLIB, + compressor, SGPath(), simgear::ZLibCompressionFormat::ZLIB, decompInBuf, sizeof(decompInBuf), decompOutBuf, sizeof(decompOutBuf), /* putback size */ 3); decompressor.exceptions(std::ios_base::badbit); @@ -528,7 +530,7 @@ void roundTripWithIStreams( { const simgear::ZLibCompressionFormat decompFormat = (useAutoFormatForDecompression) ? - simgear::ZLIB_COMPRESSION_FORMAT_AUTODETECT : compressionFormat; + simgear::ZLibCompressionFormat::AUTODETECT : compressionFormat; std::istringstream lipsum_ss(lipsum); // This tests the optional dynamic buffer allocation in ZlibAbstractIStreambuf @@ -568,11 +570,12 @@ void test_RoundTripMultiWithIStreams() const std::size_t compPutbackSize = 1; const std::size_t decompPutbackSize = 1; - for (auto format: {simgear::ZLIB_COMPRESSION_FORMAT_ZLIB, - simgear::ZLIB_COMPRESSION_FORMAT_GZIP}) { + for (auto format: {simgear::ZLibCompressionFormat::ZLIB, + simgear::ZLibCompressionFormat::GZIP}) { for (int compressionLevel: {1, 4, 7, 9}) { - for (auto memStrategy: {simgear::ZLIB_FAVOR_MEMORY_OVER_SPEED, - simgear::ZLIB_FAVOR_SPEED_OVER_MEMORY}) { + for (auto memStrategy: { + simgear::ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED, + simgear::ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY}) { for (std::size_t compInBufSize: {3, 4}) { for (std::size_t compOutBufSize: {3, 5}) { for (std::size_t decompInBufSize: {3, 4}) { @@ -591,9 +594,10 @@ void test_RoundTripMultiWithIStreams() } { - const auto format = simgear::ZLIB_COMPRESSION_FORMAT_ZLIB; + const auto format = simgear::ZLibCompressionFormat::ZLIB; const int compressionLevel = Z_DEFAULT_COMPRESSION; - const auto memStrategy = simgear::ZLIB_FAVOR_SPEED_OVER_MEMORY; + const auto memStrategy = + simgear::ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY; for (std::size_t compInBufSize: {3, 4, 31, 256, 19475}) { for (std::size_t compOutBufSize: {3, 5, 9, 74, 4568}) { @@ -623,11 +627,12 @@ void test_RoundTripMultiWithIStreams() for (std::size_t compPutbackSize: {25, 40, 105}) { for (std::size_t decompPutbackSize: {30, 60, 81}) { const simgear::ZLibCompressionFormat compFormat = (i++ % 2) ? - simgear::ZLIB_COMPRESSION_FORMAT_ZLIB : - simgear::ZLIB_COMPRESSION_FORMAT_GZIP; + simgear::ZLibCompressionFormat::ZLIB : + simgear::ZLibCompressionFormat::GZIP; roundTripWithIStreams( - compFormat, Z_BEST_COMPRESSION, simgear::ZLIB_FAVOR_MEMORY_OVER_SPEED, + compFormat, Z_BEST_COMPRESSION, + simgear::ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED, compInBufSize, compOutBufSize, decompInBufSize, decompOutBufSize, compPutbackSize, decompPutbackSize, /* automatic format detection for decompression */ true);