SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
io/detail/misc.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <algorithm>
13#include <filesystem>
14#include <iterator>
15#include <variant>
16#include <vector>
17
21
22namespace seqan3::detail
23{
24
27template <typename list_t, template <typename...> typename output_t>
28struct variant_from_tags;
29
32template <template <typename...> typename output_t, typename... ts>
33struct variant_from_tags<type_list<ts...>, output_t>
34{
36 using type = std::variant<output_t<ts>...>;
37};
38
45template <std::output_iterator<char> it_t>
46constexpr void write_eol(it_t & it, bool const add_cr)
47{
48 if (add_cr)
49 it = '\r';
50
51 it = '\n';
52}
53
64template <typename format_variant_type>
65void set_format(format_variant_type & format, std::filesystem::path const & file_name)
66{
67 using valid_formats = detail::transfer_template_args_onto_t<format_variant_type, type_list>;
68
69 bool format_found = false;
70 std::string extension = file_name.extension().string();
71 if (extension.size() > 1)
72 {
73 extension = extension.substr(1); // drop leading "."
74 detail::for_each<valid_formats>(
75 [&](auto fmt)
76 {
77 using fm_type = typename decltype(fmt)::type; // remove type_identity wrapper
78
79 for (auto const & ext : fm_type::file_extensions)
80 {
81 if (std::ranges::equal(ext, extension))
82 {
83 format.template emplace<fm_type>();
84 format_found = true;
85 return;
86 }
87 }
88 });
89 }
90
91 if (!format_found)
92 throw unhandled_extension_error("No valid format found for this extension.");
93}
94
100template <typename list_t>
101inline constexpr bool has_member_file_extensions = false;
102
104template <template <typename...> typename list_t, typename... ts>
105 requires (
106 requires { ts::file_extensions; },
107 ...,
108 true)
109inline constexpr bool has_member_file_extensions<list_t<ts...>> = true;
111
117template <typename query_t>
118inline constexpr bool has_type_valid_formats = false;
119
121template <typename query_t>
122 requires requires { typename query_t::valid_formats; }
123inline constexpr bool has_type_valid_formats<query_t> = true;
125
146template <typename formats_t>
147inline std::vector<std::string> valid_file_extensions()
148{
149 static_assert(has_member_file_extensions<formats_t>,
150 "Expects that all formats have a static member file_extensions storing the extensions in a range");
151
152 std::vector<std::string> extensions;
153 detail::for_each<formats_t>(
154 [&extensions](auto t_identity)
155 {
156 using format_t = typename decltype(t_identity)::type;
157 std::ranges::copy(format_t::file_extensions, std::back_inserter(extensions));
158 });
159
160 return extensions;
161}
162} // namespace seqan3::detail
T back_inserter(T... args)
T copy(T... args)
T equal(T... args)
T extension(T... args)
T format(T... args)
Provides exceptions used in the I/O module.
T size(T... args)
T substr(T... args)
Provides type traits for working with templates.
Provides algorithms for meta programming, parameter packs and seqan3::type_list.
Hide me