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>
29
32template <template <typename...> typename output_t, typename... ts>
33struct variant_from_tags<type_list<ts...>, output_t>
34{
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{
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>
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)
typename transfer_template_args_onto< source_type, target_template >::type transfer_template_args_onto_t
Shortcut for seqan3::detail::transfer_template_args_onto (transformation_trait shortcut).
Definition template_inspection.hpp:70
std::vector< std::string > valid_file_extensions()
Returns a list of valid file extensions.
Definition io/detail/misc.hpp:147
void set_format(format_variant_type &format, std::filesystem::path const &file_name)
Sets the file format according to the file name extension.
Definition io/detail/misc.hpp:65
constexpr bool has_type_valid_formats
Helper function to determine if a type has a static member valid_formats.
Definition io/detail/misc.hpp:118
constexpr void write_eol(it_t &it, bool const add_cr)
Write "\n" or "\r\n" to the stream iterator, depending on arguments.
Definition io/detail/misc.hpp:46
constexpr bool has_member_file_extensions
Helper function to determine if all types in a format type list have a static member file_extensions.
Definition io/detail/misc.hpp:101
Provides exceptions used in the I/O module.
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
T size(T... args)
Base class to deduce the std::variant type from format tags.
Definition io/detail/misc.hpp:28
Type that contains multiple types.
Definition type_list.hpp:26
Thrown if there is no format that accepts a given file extension.
Definition io/exception.hpp:27
T substr(T... args)
Provides type traits for working with templates.
Provides algorithms for meta programming, parameter packs and seqan3::type_list.
Hide me