Libmagicxx v9.1.1
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 {
15namespace utility {
21template <typename ContainerType>
22concept range_container = std::ranges::range<ContainerType>
23 && requires(ContainerType c) {
24 typename ContainerType::value_type;
25 };
26
33template <typename ValueType, typename StringConverterType>
34concept string_converter = std::same_as<
35 std::invoke_result_t<StringConverterType, ValueType>,
36 std::string
37>;
38
51template <range_container ContainerType, typename StringConverterType>
52requires string_converter<
53 typename ContainerType::value_type,
54 StringConverterType
55>
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
71 + value_separator
72 + std::invoke(string_converter, right);
73 }
74 );
75}
76
81template <typename ContainerType>
82concept file_container = std::ranges::range<ContainerType>
83 && std::default_initializable<ContainerType>
84 && std::same_as<
85 typename ContainerType::value_type,
86 std::filesystem::path
87 >
88 && requires(ContainerType c, std::filesystem::path p) {
89 c.push_back(p);
90 c.empty();
91 typename ContainerType::value_type;
92 };
93
102[[nodiscard]] inline std::string to_string(
103 const file_container auto& container,
104 const std::string& separator = ", "
105)
106{
107 return to_string(
108 container,
109 separator,
110 [](const std::filesystem::path& path) {
111 return path.string();
112 }
113 );
114}
115} /* namespace utility */
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:82
Define requirements for a range container.
Definition utility.hpp:22
Define requirements for a string converter.
Definition utility.hpp:34
Definition percentage.hpp:12
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