diff --git a/CMakeLists.txt b/CMakeLists.txt index 7488a885..540619ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -604,6 +604,7 @@ list(APPEND LIBDWARFS_COMMON_SRC src/dwarfs/block_compressor.cpp src/dwarfs/block_compressor_parser.cpp src/dwarfs/checksum.cpp + src/dwarfs/conv.cpp src/dwarfs/error.cpp src/dwarfs/features.cpp src/dwarfs/file_access_generic.cpp diff --git a/include/dwarfs/conv.h b/include/dwarfs/conv.h index a58b8ca6..2e74a438 100644 --- a/include/dwarfs/conv.h +++ b/include/dwarfs/conv.h @@ -21,17 +21,63 @@ #pragma once +#include +#include +#include #include -#include -// #include +#include +#include namespace dwarfs { +namespace detail { + +std::optional str_to_bool(std::string_view s); + +} // namespace detail + +template +std::optional tryTo(U&& s) + requires(!std::same_as && !std::convertible_to) +{ +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + if (auto r = boost::convert(std::forward(s), boost::cnv::spirit())) { + return r.value(); + } +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif + return std::nullopt; +} + +template +std::optional tryTo(U&& s) + requires(std::same_as && std::is_arithmetic_v) +{ + return s != U{}; +} + +template +std::optional tryTo(std::string_view s) + requires std::same_as +{ + return detail::str_to_bool(s); +} + +template +std::optional tryTo(U&& s) + requires(std::convertible_to) +{ + return std::forward(s); +} + template T to(U&& s) { - return folly::to(std::forward(s)); - // return boost::lexical_cast(std::forward(s)); + return tryTo(std::forward(s)).value(); } } // namespace dwarfs diff --git a/src/dwarfs/compression/lz4.cpp b/src/dwarfs/compression/lz4.cpp index 123ddc49..dd324f3a 100644 --- a/src/dwarfs/compression/lz4.cpp +++ b/src/dwarfs/compression/lz4.cpp @@ -24,9 +24,8 @@ #include -#include - #include +#include #include #include #include @@ -38,9 +37,9 @@ namespace { struct lz4_compression_policy { static size_t compress(const void* src, void* dest, size_t size, size_t destsize, int /*level*/) { - return folly::to(LZ4_compress_default( - reinterpret_cast(src), reinterpret_cast(dest), - folly::to(size), folly::to(destsize))); + return to(LZ4_compress_default(reinterpret_cast(src), + reinterpret_cast(dest), + to(size), to(destsize))); } static std::string describe(int /*level*/) { return "lz4"; } @@ -49,9 +48,9 @@ struct lz4_compression_policy { struct lz4hc_compression_policy { static size_t compress(const void* src, void* dest, size_t size, size_t destsize, int level) { - return folly::to(LZ4_compress_HC( - reinterpret_cast(src), reinterpret_cast(dest), - folly::to(size), folly::to(destsize), level)); + return to(LZ4_compress_HC(reinterpret_cast(src), + reinterpret_cast(dest), + to(size), to(destsize), level)); } static std::string describe(int level) { @@ -73,8 +72,8 @@ class lz4_block_compressor final : public block_compressor::impl { std::vector compress(const std::vector& data, std::string const* /*metadata*/) const override { - std::vector compressed( - sizeof(uint32_t) + LZ4_compressBound(folly::to(data.size()))); + std::vector compressed(sizeof(uint32_t) + + LZ4_compressBound(to(data.size()))); *reinterpret_cast(&compressed[0]) = data.size(); auto csize = Policy::compress(&data[0], &compressed[sizeof(uint32_t)], data.size(), diff --git a/src/dwarfs/conv.cpp b/src/dwarfs/conv.cpp new file mode 100644 index 00000000..d58a9ffb --- /dev/null +++ b/src/dwarfs/conv.cpp @@ -0,0 +1,37 @@ +/* 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 . + */ + +#include + +#include + +namespace dwarfs { +namespace detail { + +std::optional str_to_bool(std::string_view s) { + if (auto r = folly::tryTo(s)) { + return *r; + } + return std::nullopt; +} + +} // namespace detail +} // namespace dwarfs diff --git a/src/dwarfs/internal/worker_group.cpp b/src/dwarfs/internal/worker_group.cpp index 0a24e0e8..b10d6ab6 100644 --- a/src/dwarfs/internal/worker_group.cpp +++ b/src/dwarfs/internal/worker_group.cpp @@ -34,7 +34,8 @@ #include #include -#include +#include + #include #include #include @@ -73,7 +74,7 @@ class basic_worker_group final : public worker_group::impl, private Policy { for (size_t i = 0; i < num_workers; ++i) { workers_.emplace_back([this, niceness, group_name, i] { - folly::setThreadName(folly::to(group_name, i + 1)); + folly::setThreadName(fmt::format("{}{}", group_name, i + 1)); set_thread_niceness(niceness); do_work(niceness > 10); }); diff --git a/src/dwarfs_main.cpp b/src/dwarfs_main.cpp index 284afa9d..1d75b8ea 100644 --- a/src/dwarfs_main.cpp +++ b/src/dwarfs_main.cpp @@ -45,7 +45,6 @@ #include -#include #include #include #include @@ -83,6 +82,7 @@ #define DWARFS_FSP_COMPAT #endif +#include #include #include #include @@ -1553,12 +1553,11 @@ int dwarfs_main(int argc, sys_char** argv, iolayer const& iol) { : kDefaultBlockSize; opts.readahead = opts.readahead_str ? parse_size_with_unit(opts.readahead_str) : 0; - opts.workers = opts.workers_str ? folly::to(opts.workers_str) : 2; + opts.workers = opts.workers_str ? to(opts.workers_str) : 2; opts.lock_mode = opts.mlock_str ? parse_mlock_mode(opts.mlock_str) : mlock_mode::NONE; - opts.decompress_ratio = opts.decompress_ratio_str - ? folly::to(opts.decompress_ratio_str) - : 0.8; + opts.decompress_ratio = + opts.decompress_ratio_str ? to(opts.decompress_ratio_str) : 0.8; if (opts.cache_tidy_strategy_str) { if (auto it = cache_tidy_strategy_map.find(opts.cache_tidy_strategy_str); @@ -1593,10 +1592,9 @@ int dwarfs_main(int argc, sys_char** argv, iolayer const& iol) { return 1; } - opts.seq_detector_threshold = - opts.seq_detector_thresh_str - ? folly::to(opts.seq_detector_thresh_str) - : kDefaultSeqDetectorThreshold; + opts.seq_detector_threshold = opts.seq_detector_thresh_str + ? to(opts.seq_detector_thresh_str) + : kDefaultSeqDetectorThreshold; #ifdef DWARFS_BUILTIN_MANPAGE if (userdata.opts.is_man) { diff --git a/src/dwarfsbench_main.cpp b/src/dwarfsbench_main.cpp index cad3a0a3..cee050e8 100644 --- a/src/dwarfsbench_main.cpp +++ b/src/dwarfsbench_main.cpp @@ -23,9 +23,9 @@ #include -#include #include +#include #include #include #include @@ -99,8 +99,7 @@ int dwarfsbench_main(int argc, sys_char** argv, iolayer const& iol) { fsopts.lock_mode = parse_mlock_mode(lock_mode_str); fsopts.block_cache.max_bytes = parse_size_with_unit(cache_size_str); fsopts.block_cache.num_workers = num_workers; - fsopts.block_cache.decompress_ratio = - folly::to(decompress_ratio_str); + fsopts.block_cache.decompress_ratio = to(decompress_ratio_str); dwarfs::filesystem_v2 fs( lgr, *iol.os, std::make_shared(filesystem), fsopts); diff --git a/src/mkdwarfs_main.cpp b/src/mkdwarfs_main.cpp index 438c67c2..fecb96ae 100644 --- a/src/mkdwarfs_main.cpp +++ b/src/mkdwarfs_main.cpp @@ -47,7 +47,6 @@ #include #include -#include #include #include #include @@ -66,6 +65,7 @@ #include #include #include +#include #include #include #include @@ -986,7 +986,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) { if (vm.count("set-time")) { if (timestamp == "now") { options.timestamp = std::time(nullptr); - } else if (auto val = folly::tryTo(timestamp)) { + } else if (auto val = tryTo(timestamp)) { options.timestamp = *val; } else { try { @@ -1004,7 +1004,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) { if (auto it = time_resolutions.find(time_resolution); it != time_resolutions.end()) { options.time_resolution_sec = it->second; - } else if (auto val = folly::tryTo(time_resolution)) { + } else if (auto val = tryTo(time_resolution)) { options.time_resolution_sec = *val; if (options.time_resolution_sec == 0) { iol.err << "error: the argument to '--time-resolution' must be nonzero\n"; diff --git a/test/integral_value_parser_test.cpp b/test/integral_value_parser_test.cpp index 1bbfcc88..2c5409ae 100644 --- a/test/integral_value_parser_test.cpp +++ b/test/integral_value_parser_test.cpp @@ -34,7 +34,8 @@ namespace { auto throws_conversion_error() { return testing::AnyOf(testing::Throws(), - testing::Throws()); + testing::Throws(), + testing::Throws()); } } // namespace diff --git a/vcpkg.json b/vcpkg.json index b789b8c6..52d9157e 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -4,6 +4,7 @@ "boost-asio", "boost-chrono", "boost-context", + "boost-convert", "boost-crc", "boost-filesystem", "boost-iostreams",