Libmagicxx v9.0.2
A C++ wrapper library over the Magic Number Recognition Library.
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2025 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
4#ifndef UTILITY_HPP
5#define UTILITY_HPP
6
7#include <algorithm>
8#include <concepts>
9#include <filesystem>
10#include <functional>
11#include <ranges>
12#include <string>
13
14namespace recognition {
15
16namespace utility {
17
23template <typename ContainerType>
24concept range_container = std::ranges::range<ContainerType>
25 && requires(ContainerType c) {
26 typename ContainerType::value_type;
27 };
28
35template <typename ValueType, typename StringConverterType>
36concept string_converter = std::same_as<
37 std::invoke_result_t<StringConverterType, ValueType>,
38 std::string>;
39
52template <range_container ContainerType, typename StringConverterType>
53requires string_converter<
54 typename ContainerType::value_type,
55 StringConverterType>
56[[nodiscard]] inline std::string to_string(
57 const ContainerType& container,
58 const std::string& value_separator,
59 StringConverterType string_converter
60)
61{
62 if (container.empty()) {
63 return {};
64 }
65 return std::ranges::fold_left(
66 std::ranges::next(std::ranges::begin(container)),
67 std::ranges::end(container),
68 std::invoke(string_converter, *std::ranges::begin(container)),
69 [&](const auto& left, const auto& right) {
70 return left + value_separator
71 + std::invoke(string_converter, right);
72 }
73 );
74}
75
80template <typename ContainerType>
81concept file_container = std::ranges::range<ContainerType>
82 && std::default_initializable<ContainerType>
83 && std::same_as<
84 typename ContainerType::value_type,
85 std::filesystem::path>
86 && requires(ContainerType c, std::filesystem::path p) {
87 c.push_back(p);
88 c.empty();
89 typename ContainerType::value_type;
90 };
91
100[[nodiscard]] inline std::string to_string(
101 const file_container auto& container,
102 const std::string& separator = ", "
103)
104{
105 return to_string(
106 container,
107 separator,
108 [](const std::filesystem::path& path) {
109 return path.string();
110 }
111 );
112}
113
114} /* namespace utility */
115
116} /* namespace recognition */
117
118#endif /* UTILITY_HPP */
The file_container concept specifies the requirements of a container which can be used as a file cont...
Definition utility.hpp:81
Define requirements for a range container.
Definition utility.hpp:24
Define requirements for a string converter.
Definition utility.hpp:36
std::string to_string(const ContainerType &container, const std::string &value_separator, StringConverterType string_converter)
Convert any container to string using the string_converter.
Definition utility.hpp:56
Definition magic.hpp:19