SeqAn3 3.2.0-rc.1
The Modern C++ library for sequence analysis.
misc.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2021, Knut Reinert & MPI für molekulare Genetik
4// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6// -----------------------------------------------------------------------------------------------------
7
13#pragma once
14
15#include <algorithm>
16#include <filesystem>
17#include <iterator>
18#include <variant>
19#include <vector>
20
24
25namespace seqan3::detail
26{
27
30template <typename list_t, template <typename ...> typename output_t>
32
35template <template <typename...> typename output_t, typename ...ts>
36struct variant_from_tags<type_list<ts...>, output_t>
37{
40};
41
48template <std::output_iterator<char> it_t>
49constexpr void write_eol(it_t & it, bool const add_cr)
50{
51 if (add_cr)
52 it = '\r';
53
54 it = '\n';
55}
56
67template <typename format_variant_type>
68void set_format(format_variant_type & format,
69 std::filesystem::path const & file_name)
70{
72
73 bool format_found = false;
74 std::string extension = file_name.extension().string();
75 if (extension.size() > 1)
76 {
77 extension = extension.substr(1); // drop leading "."
78 detail::for_each<valid_formats>([&] (auto fmt)
79 {
80 using fm_type = typename decltype(fmt)::type; // remove type_identity wrapper
81
82 for (auto const & ext : fm_type::file_extensions)
83 {
84 if (std::ranges::equal(ext, extension))
85 {
86 format.template emplace<fm_type>();
87 format_found = true;
88 return;
89 }
90 }
91 });
92 }
93
94 if (!format_found)
95 throw unhandled_extension_error("No valid format found for this extension.");
96}
97
103template <typename list_t>
104inline constexpr bool has_member_file_extensions = false;
105
107template <template <typename ...> typename list_t, typename ...ts>
108 requires (requires { ts::file_extensions; }, ..., 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>([&extensions] (auto t_identity)
154 {
155 using format_t = typename decltype(t_identity)::type;
156 std::ranges::copy(format_t::file_extensions, std::back_inserter(extensions));
157 });
158
159 return extensions;
160}
161} // namespace seqan3::detail
T back_inserter(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:77
std::vector< std::string > valid_file_extensions()
Returns a list of valid file extensions.
Definition: 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: misc.hpp:68
constexpr bool has_type_valid_formats
Helper function to determine if a type has a static member valid_formats.
Definition: 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: misc.hpp:49
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: misc.hpp:104
Provides exceptions used in the I/O module.
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
T size(T... args)
Base class to deduce the std::variant type from format tags.
Definition: misc.hpp:31
Type that contains multiple types.
Definition: type_list.hpp:29
Thrown if there is no format that accepts a given file extension.
Definition: exception.hpp:30
T substr(T... args)
Provides type traits for working with templates.
Provides algorithms for meta programming, parameter packs and seqan3::type_list.