refactor: remove folly/Conv.h from public API

This commit is contained in:
Marcus Holland-Moritz 2024-07-27 23:03:33 +02:00
parent fbba876c18
commit f8efbdc4a8
10 changed files with 115 additions and 32 deletions

View File

@ -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

View File

@ -21,17 +21,63 @@
#pragma once
#include <concepts>
#include <optional>
#include <type_traits>
#include <utility>
#include <folly/Conv.h>
// #include <boost/lexical_cast.hpp>
#include <boost/convert.hpp>
#include <boost/convert/spirit.hpp>
namespace dwarfs {
namespace detail {
std::optional<bool> str_to_bool(std::string_view s);
} // namespace detail
template <typename T, typename U>
std::optional<T> tryTo(U&& s)
requires(!std::same_as<T, bool> && !std::convertible_to<U, T>)
{
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
if (auto r = boost::convert<T>(std::forward<U>(s), boost::cnv::spirit())) {
return r.value();
}
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
return std::nullopt;
}
template <typename T, typename U>
std::optional<bool> tryTo(U&& s)
requires(std::same_as<T, bool> && std::is_arithmetic_v<U>)
{
return s != U{};
}
template <typename T>
std::optional<bool> tryTo(std::string_view s)
requires std::same_as<T, bool>
{
return detail::str_to_bool(s);
}
template <typename T, typename U>
std::optional<T> tryTo(U&& s)
requires(std::convertible_to<U, T>)
{
return std::forward<U>(s);
}
template <typename T, typename U>
T to(U&& s) {
return folly::to<T>(std::forward<U>(s));
// return boost::lexical_cast<T>(std::forward<U>(s));
return tryTo<T>(std::forward<U>(s)).value();
}
} // namespace dwarfs

View File

@ -24,9 +24,8 @@
#include <fmt/format.h>
#include <folly/Conv.h>
#include <dwarfs/block_compressor.h>
#include <dwarfs/conv.h>
#include <dwarfs/error.h>
#include <dwarfs/fstypes.h>
#include <dwarfs/option_map.h>
@ -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<size_t>(LZ4_compress_default(
reinterpret_cast<const char*>(src), reinterpret_cast<char*>(dest),
folly::to<int>(size), folly::to<int>(destsize)));
return to<size_t>(LZ4_compress_default(reinterpret_cast<const char*>(src),
reinterpret_cast<char*>(dest),
to<int>(size), to<int>(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<size_t>(LZ4_compress_HC(
reinterpret_cast<const char*>(src), reinterpret_cast<char*>(dest),
folly::to<int>(size), folly::to<int>(destsize), level));
return to<size_t>(LZ4_compress_HC(reinterpret_cast<const char*>(src),
reinterpret_cast<char*>(dest),
to<int>(size), to<int>(destsize), level));
}
static std::string describe(int level) {
@ -73,8 +72,8 @@ class lz4_block_compressor final : public block_compressor::impl {
std::vector<uint8_t>
compress(const std::vector<uint8_t>& data,
std::string const* /*metadata*/) const override {
std::vector<uint8_t> compressed(
sizeof(uint32_t) + LZ4_compressBound(folly::to<int>(data.size())));
std::vector<uint8_t> compressed(sizeof(uint32_t) +
LZ4_compressBound(to<int>(data.size())));
*reinterpret_cast<uint32_t*>(&compressed[0]) = data.size();
auto csize =
Policy::compress(&data[0], &compressed[sizeof(uint32_t)], data.size(),

37
src/dwarfs/conv.cpp Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <folly/Conv.h>
#include <dwarfs/conv.h>
namespace dwarfs {
namespace detail {
std::optional<bool> str_to_bool(std::string_view s) {
if (auto r = folly::tryTo<bool>(s)) {
return *r;
}
return std::nullopt;
}
} // namespace detail
} // namespace dwarfs

View File

@ -34,7 +34,8 @@
#include <variant>
#include <vector>
#include <folly/Conv.h>
#include <fmt/format.h>
#include <folly/String.h>
#include <folly/portability/Windows.h>
#include <folly/system/HardwareConcurrency.h>
@ -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<std::string>(group_name, i + 1));
folly::setThreadName(fmt::format("{}{}", group_name, i + 1));
set_thread_niceness(niceness);
do_work(niceness > 10);
});

View File

@ -45,7 +45,6 @@
#include <fmt/format.h>
#include <folly/Conv.h>
#include <folly/String.h>
#include <folly/experimental/symbolizer/SignalHandler.h>
#include <folly/portability/Fcntl.h>
@ -83,6 +82,7 @@
#define DWARFS_FSP_COMPAT
#endif
#include <dwarfs/conv.h>
#include <dwarfs/error.h>
#include <dwarfs/file_stat.h>
#include <dwarfs/filesystem_v2.h>
@ -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<size_t>(opts.workers_str) : 2;
opts.workers = opts.workers_str ? to<size_t>(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<double>(opts.decompress_ratio_str)
: 0.8;
opts.decompress_ratio =
opts.decompress_ratio_str ? to<double>(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<size_t>(opts.seq_detector_thresh_str)
: kDefaultSeqDetectorThreshold;
opts.seq_detector_threshold = opts.seq_detector_thresh_str
? to<size_t>(opts.seq_detector_thresh_str)
: kDefaultSeqDetectorThreshold;
#ifdef DWARFS_BUILTIN_MANPAGE
if (userdata.opts.is_man) {

View File

@ -23,9 +23,9 @@
#include <boost/program_options.hpp>
#include <folly/Conv.h>
#include <folly/String.h>
#include <dwarfs/conv.h>
#include <dwarfs/file_stat.h>
#include <dwarfs/filesystem_v2.h>
#include <dwarfs/fstypes.h>
@ -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<double>(decompress_ratio_str);
fsopts.block_cache.decompress_ratio = to<double>(decompress_ratio_str);
dwarfs::filesystem_v2 fs(
lgr, *iol.os, std::make_shared<dwarfs::mmap>(filesystem), fsopts);

View File

@ -47,7 +47,6 @@
#include <boost/algorithm/string/join.hpp>
#include <boost/program_options.hpp>
#include <folly/Conv.h>
#include <folly/FileUtil.h>
#include <folly/String.h>
#include <folly/container/Enumerate.h>
@ -66,6 +65,7 @@
#include <dwarfs/category_parser.h>
#include <dwarfs/chmod_entry_transformer.h>
#include <dwarfs/console_writer.h>
#include <dwarfs/conv.h>
#include <dwarfs/entry.h>
#include <dwarfs/error.h>
#include <dwarfs/file_access.h>
@ -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<uint64_t>(timestamp)) {
} else if (auto val = tryTo<uint64_t>(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<uint32_t>(time_resolution)) {
} else if (auto val = tryTo<uint32_t>(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";

View File

@ -34,7 +34,8 @@ namespace {
auto throws_conversion_error() {
return testing::AnyOf(testing::Throws<folly::ConversionError>(),
testing::Throws<boost::bad_lexical_cast>());
testing::Throws<boost::bad_lexical_cast>(),
testing::Throws<std::bad_optional_access>());
}
} // namespace

View File

@ -4,6 +4,7 @@
"boost-asio",
"boost-chrono",
"boost-context",
"boost-convert",
"boost-crc",
"boost-filesystem",
"boost-iostreams",