mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-10 04:50:31 -04:00
refactor: remove folly/Conv.h from public API
This commit is contained in:
parent
fbba876c18
commit
f8efbdc4a8
@ -604,6 +604,7 @@ list(APPEND LIBDWARFS_COMMON_SRC
|
|||||||
src/dwarfs/block_compressor.cpp
|
src/dwarfs/block_compressor.cpp
|
||||||
src/dwarfs/block_compressor_parser.cpp
|
src/dwarfs/block_compressor_parser.cpp
|
||||||
src/dwarfs/checksum.cpp
|
src/dwarfs/checksum.cpp
|
||||||
|
src/dwarfs/conv.cpp
|
||||||
src/dwarfs/error.cpp
|
src/dwarfs/error.cpp
|
||||||
src/dwarfs/features.cpp
|
src/dwarfs/features.cpp
|
||||||
src/dwarfs/file_access_generic.cpp
|
src/dwarfs/file_access_generic.cpp
|
||||||
|
@ -21,17 +21,63 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <concepts>
|
||||||
|
#include <optional>
|
||||||
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include <folly/Conv.h>
|
#include <boost/convert.hpp>
|
||||||
// #include <boost/lexical_cast.hpp>
|
#include <boost/convert/spirit.hpp>
|
||||||
|
|
||||||
namespace dwarfs {
|
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>
|
template <typename T, typename U>
|
||||||
T to(U&& s) {
|
T to(U&& s) {
|
||||||
return folly::to<T>(std::forward<U>(s));
|
return tryTo<T>(std::forward<U>(s)).value();
|
||||||
// return boost::lexical_cast<T>(std::forward<U>(s));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dwarfs
|
} // namespace dwarfs
|
||||||
|
@ -24,9 +24,8 @@
|
|||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
|
|
||||||
#include <folly/Conv.h>
|
|
||||||
|
|
||||||
#include <dwarfs/block_compressor.h>
|
#include <dwarfs/block_compressor.h>
|
||||||
|
#include <dwarfs/conv.h>
|
||||||
#include <dwarfs/error.h>
|
#include <dwarfs/error.h>
|
||||||
#include <dwarfs/fstypes.h>
|
#include <dwarfs/fstypes.h>
|
||||||
#include <dwarfs/option_map.h>
|
#include <dwarfs/option_map.h>
|
||||||
@ -38,9 +37,9 @@ namespace {
|
|||||||
struct lz4_compression_policy {
|
struct lz4_compression_policy {
|
||||||
static size_t compress(const void* src, void* dest, size_t size,
|
static size_t compress(const void* src, void* dest, size_t size,
|
||||||
size_t destsize, int /*level*/) {
|
size_t destsize, int /*level*/) {
|
||||||
return folly::to<size_t>(LZ4_compress_default(
|
return to<size_t>(LZ4_compress_default(reinterpret_cast<const char*>(src),
|
||||||
reinterpret_cast<const char*>(src), reinterpret_cast<char*>(dest),
|
reinterpret_cast<char*>(dest),
|
||||||
folly::to<int>(size), folly::to<int>(destsize)));
|
to<int>(size), to<int>(destsize)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string describe(int /*level*/) { return "lz4"; }
|
static std::string describe(int /*level*/) { return "lz4"; }
|
||||||
@ -49,9 +48,9 @@ struct lz4_compression_policy {
|
|||||||
struct lz4hc_compression_policy {
|
struct lz4hc_compression_policy {
|
||||||
static size_t compress(const void* src, void* dest, size_t size,
|
static size_t compress(const void* src, void* dest, size_t size,
|
||||||
size_t destsize, int level) {
|
size_t destsize, int level) {
|
||||||
return folly::to<size_t>(LZ4_compress_HC(
|
return to<size_t>(LZ4_compress_HC(reinterpret_cast<const char*>(src),
|
||||||
reinterpret_cast<const char*>(src), reinterpret_cast<char*>(dest),
|
reinterpret_cast<char*>(dest),
|
||||||
folly::to<int>(size), folly::to<int>(destsize), level));
|
to<int>(size), to<int>(destsize), level));
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string describe(int level) {
|
static std::string describe(int level) {
|
||||||
@ -73,8 +72,8 @@ class lz4_block_compressor final : public block_compressor::impl {
|
|||||||
std::vector<uint8_t>
|
std::vector<uint8_t>
|
||||||
compress(const std::vector<uint8_t>& data,
|
compress(const std::vector<uint8_t>& data,
|
||||||
std::string const* /*metadata*/) const override {
|
std::string const* /*metadata*/) const override {
|
||||||
std::vector<uint8_t> compressed(
|
std::vector<uint8_t> compressed(sizeof(uint32_t) +
|
||||||
sizeof(uint32_t) + LZ4_compressBound(folly::to<int>(data.size())));
|
LZ4_compressBound(to<int>(data.size())));
|
||||||
*reinterpret_cast<uint32_t*>(&compressed[0]) = data.size();
|
*reinterpret_cast<uint32_t*>(&compressed[0]) = data.size();
|
||||||
auto csize =
|
auto csize =
|
||||||
Policy::compress(&data[0], &compressed[sizeof(uint32_t)], data.size(),
|
Policy::compress(&data[0], &compressed[sizeof(uint32_t)], data.size(),
|
||||||
|
37
src/dwarfs/conv.cpp
Normal file
37
src/dwarfs/conv.cpp
Normal 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
|
@ -34,7 +34,8 @@
|
|||||||
#include <variant>
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <folly/Conv.h>
|
#include <fmt/format.h>
|
||||||
|
|
||||||
#include <folly/String.h>
|
#include <folly/String.h>
|
||||||
#include <folly/portability/Windows.h>
|
#include <folly/portability/Windows.h>
|
||||||
#include <folly/system/HardwareConcurrency.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) {
|
for (size_t i = 0; i < num_workers; ++i) {
|
||||||
workers_.emplace_back([this, niceness, group_name, 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);
|
set_thread_niceness(niceness);
|
||||||
do_work(niceness > 10);
|
do_work(niceness > 10);
|
||||||
});
|
});
|
||||||
|
@ -45,7 +45,6 @@
|
|||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
|
|
||||||
#include <folly/Conv.h>
|
|
||||||
#include <folly/String.h>
|
#include <folly/String.h>
|
||||||
#include <folly/experimental/symbolizer/SignalHandler.h>
|
#include <folly/experimental/symbolizer/SignalHandler.h>
|
||||||
#include <folly/portability/Fcntl.h>
|
#include <folly/portability/Fcntl.h>
|
||||||
@ -83,6 +82,7 @@
|
|||||||
#define DWARFS_FSP_COMPAT
|
#define DWARFS_FSP_COMPAT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <dwarfs/conv.h>
|
||||||
#include <dwarfs/error.h>
|
#include <dwarfs/error.h>
|
||||||
#include <dwarfs/file_stat.h>
|
#include <dwarfs/file_stat.h>
|
||||||
#include <dwarfs/filesystem_v2.h>
|
#include <dwarfs/filesystem_v2.h>
|
||||||
@ -1553,12 +1553,11 @@ int dwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
|
|||||||
: kDefaultBlockSize;
|
: kDefaultBlockSize;
|
||||||
opts.readahead =
|
opts.readahead =
|
||||||
opts.readahead_str ? parse_size_with_unit(opts.readahead_str) : 0;
|
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.lock_mode =
|
||||||
opts.mlock_str ? parse_mlock_mode(opts.mlock_str) : mlock_mode::NONE;
|
opts.mlock_str ? parse_mlock_mode(opts.mlock_str) : mlock_mode::NONE;
|
||||||
opts.decompress_ratio = opts.decompress_ratio_str
|
opts.decompress_ratio =
|
||||||
? folly::to<double>(opts.decompress_ratio_str)
|
opts.decompress_ratio_str ? to<double>(opts.decompress_ratio_str) : 0.8;
|
||||||
: 0.8;
|
|
||||||
|
|
||||||
if (opts.cache_tidy_strategy_str) {
|
if (opts.cache_tidy_strategy_str) {
|
||||||
if (auto it = cache_tidy_strategy_map.find(opts.cache_tidy_strategy_str);
|
if (auto it = cache_tidy_strategy_map.find(opts.cache_tidy_strategy_str);
|
||||||
@ -1593,9 +1592,8 @@ int dwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.seq_detector_threshold =
|
opts.seq_detector_threshold = opts.seq_detector_thresh_str
|
||||||
opts.seq_detector_thresh_str
|
? to<size_t>(opts.seq_detector_thresh_str)
|
||||||
? folly::to<size_t>(opts.seq_detector_thresh_str)
|
|
||||||
: kDefaultSeqDetectorThreshold;
|
: kDefaultSeqDetectorThreshold;
|
||||||
|
|
||||||
#ifdef DWARFS_BUILTIN_MANPAGE
|
#ifdef DWARFS_BUILTIN_MANPAGE
|
||||||
|
@ -23,9 +23,9 @@
|
|||||||
|
|
||||||
#include <boost/program_options.hpp>
|
#include <boost/program_options.hpp>
|
||||||
|
|
||||||
#include <folly/Conv.h>
|
|
||||||
#include <folly/String.h>
|
#include <folly/String.h>
|
||||||
|
|
||||||
|
#include <dwarfs/conv.h>
|
||||||
#include <dwarfs/file_stat.h>
|
#include <dwarfs/file_stat.h>
|
||||||
#include <dwarfs/filesystem_v2.h>
|
#include <dwarfs/filesystem_v2.h>
|
||||||
#include <dwarfs/fstypes.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.lock_mode = parse_mlock_mode(lock_mode_str);
|
||||||
fsopts.block_cache.max_bytes = parse_size_with_unit(cache_size_str);
|
fsopts.block_cache.max_bytes = parse_size_with_unit(cache_size_str);
|
||||||
fsopts.block_cache.num_workers = num_workers;
|
fsopts.block_cache.num_workers = num_workers;
|
||||||
fsopts.block_cache.decompress_ratio =
|
fsopts.block_cache.decompress_ratio = to<double>(decompress_ratio_str);
|
||||||
folly::to<double>(decompress_ratio_str);
|
|
||||||
|
|
||||||
dwarfs::filesystem_v2 fs(
|
dwarfs::filesystem_v2 fs(
|
||||||
lgr, *iol.os, std::make_shared<dwarfs::mmap>(filesystem), fsopts);
|
lgr, *iol.os, std::make_shared<dwarfs::mmap>(filesystem), fsopts);
|
||||||
|
@ -47,7 +47,6 @@
|
|||||||
#include <boost/algorithm/string/join.hpp>
|
#include <boost/algorithm/string/join.hpp>
|
||||||
#include <boost/program_options.hpp>
|
#include <boost/program_options.hpp>
|
||||||
|
|
||||||
#include <folly/Conv.h>
|
|
||||||
#include <folly/FileUtil.h>
|
#include <folly/FileUtil.h>
|
||||||
#include <folly/String.h>
|
#include <folly/String.h>
|
||||||
#include <folly/container/Enumerate.h>
|
#include <folly/container/Enumerate.h>
|
||||||
@ -66,6 +65,7 @@
|
|||||||
#include <dwarfs/category_parser.h>
|
#include <dwarfs/category_parser.h>
|
||||||
#include <dwarfs/chmod_entry_transformer.h>
|
#include <dwarfs/chmod_entry_transformer.h>
|
||||||
#include <dwarfs/console_writer.h>
|
#include <dwarfs/console_writer.h>
|
||||||
|
#include <dwarfs/conv.h>
|
||||||
#include <dwarfs/entry.h>
|
#include <dwarfs/entry.h>
|
||||||
#include <dwarfs/error.h>
|
#include <dwarfs/error.h>
|
||||||
#include <dwarfs/file_access.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 (vm.count("set-time")) {
|
||||||
if (timestamp == "now") {
|
if (timestamp == "now") {
|
||||||
options.timestamp = std::time(nullptr);
|
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;
|
options.timestamp = *val;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
@ -1004,7 +1004,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
|
|||||||
if (auto it = time_resolutions.find(time_resolution);
|
if (auto it = time_resolutions.find(time_resolution);
|
||||||
it != time_resolutions.end()) {
|
it != time_resolutions.end()) {
|
||||||
options.time_resolution_sec = it->second;
|
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;
|
options.time_resolution_sec = *val;
|
||||||
if (options.time_resolution_sec == 0) {
|
if (options.time_resolution_sec == 0) {
|
||||||
iol.err << "error: the argument to '--time-resolution' must be nonzero\n";
|
iol.err << "error: the argument to '--time-resolution' must be nonzero\n";
|
||||||
|
@ -34,7 +34,8 @@ namespace {
|
|||||||
|
|
||||||
auto throws_conversion_error() {
|
auto throws_conversion_error() {
|
||||||
return testing::AnyOf(testing::Throws<folly::ConversionError>(),
|
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
|
} // namespace
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
"boost-asio",
|
"boost-asio",
|
||||||
"boost-chrono",
|
"boost-chrono",
|
||||||
"boost-context",
|
"boost-context",
|
||||||
|
"boost-convert",
|
||||||
"boost-crc",
|
"boost-crc",
|
||||||
"boost-filesystem",
|
"boost-filesystem",
|
||||||
"boost-iostreams",
|
"boost-iostreams",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user