From 87271666ac4102b548c4d45f6fbfa66e95f2f23f Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sat, 5 Nov 2022 18:45:48 +0100 Subject: [PATCH] Autogenerate compression type code --- include/dwarfs/block_compressor.h | 11 ++------ include/dwarfs/compression.h | 46 +++++++++++++++++++++++++++++++ src/dwarfs/fstypes.cpp | 10 ++++--- 3 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 include/dwarfs/compression.h diff --git a/include/dwarfs/block_compressor.h b/include/dwarfs/block_compressor.h index c8c74b24..8184cada 100644 --- a/include/dwarfs/block_compressor.h +++ b/include/dwarfs/block_compressor.h @@ -33,19 +33,12 @@ #include #include +#include "dwarfs/compression.h" + namespace dwarfs { class option_map; -// TODO: move this to fstypes.h? -enum class compression_type : uint8_t { - NONE = 0, - LZMA = 1, - ZSTD = 2, - LZ4 = 3, - LZ4HC = 4, -}; - class bad_compression_ratio_error : public std::runtime_error { public: bad_compression_ratio_error() diff --git a/include/dwarfs/compression.h b/include/dwarfs/compression.h new file mode 100644 index 00000000..d8f9f9cf --- /dev/null +++ b/include/dwarfs/compression.h @@ -0,0 +1,46 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dwarfs is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dwarfs. If not, see . + */ + +#pragma once + +#include + +// clang-format off +#define DWARFS_COMPRESSION_TYPE_LIST(DWARFS_COMPRESSION_TYPE, SEPARATOR) \ + DWARFS_COMPRESSION_TYPE(NONE, 0) SEPARATOR \ + DWARFS_COMPRESSION_TYPE(LZMA, 1) SEPARATOR \ + DWARFS_COMPRESSION_TYPE(ZSTD, 2) SEPARATOR \ + DWARFS_COMPRESSION_TYPE(LZ4, 3) SEPARATOR \ + DWARFS_COMPRESSION_TYPE(LZ4HC, 4) +// clang-format on + +namespace dwarfs { + +enum class compression_type : uint8_t { +#define DWARFS_COMPRESSION_TYPE_ENUMERATION_(name, value) name = value +#define DWARFS_COMMA_ , + DWARFS_COMPRESSION_TYPE_LIST(DWARFS_COMPRESSION_TYPE_ENUMERATION_, + DWARFS_COMMA_) +#undef DWARFS_COMPRESSION_TYPE_ENUMERATION_ +#undef DWARFS_COMMA_ +}; + +} // namespace dwarfs diff --git a/src/dwarfs/fstypes.cpp b/src/dwarfs/fstypes.cpp index 93cade0e..992b7941 100644 --- a/src/dwarfs/fstypes.cpp +++ b/src/dwarfs/fstypes.cpp @@ -28,6 +28,7 @@ #include +#include "dwarfs/compression.h" #include "dwarfs/fstypes.h" namespace dwarfs { @@ -44,10 +45,11 @@ const std::map sections{ }; const std::map compressions{ -#define COMPRESSION_TYPE_(x) {compression_type::x, #x} - COMPRESSION_TYPE_(NONE), COMPRESSION_TYPE_(LZMA), COMPRESSION_TYPE_(ZSTD), - COMPRESSION_TYPE_(LZ4), COMPRESSION_TYPE_(LZ4HC) -#undef COMPRESSION_TYPE_ +#define DWARFS_COMPRESSION_TYPE_(name, value) {compression_type::name, #name} +#define DWARFS_COMMA_ , + DWARFS_COMPRESSION_TYPE_LIST(DWARFS_COMPRESSION_TYPE_, DWARFS_COMMA_) +#undef DWARFS_COMPRESSION_TYPE_ +#undef DWARFS_COMMA_ }; template