SeqAn3  3.0.1
The Modern C++ library for sequence analysis.
misc.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 <variant>
16 
20 #include <seqan3/io/exception.hpp>
22 #include <seqan3/std/algorithm>
23 #include <seqan3/std/filesystem>
24 #include <seqan3/std/iterator>
25 
26 namespace seqan3::detail
27 {
28 
31 template <typename list_t, template <typename ...> typename output_t>
32 struct variant_from_tags;
33 
36 template <template <typename...> typename output_t, typename ...ts>
37 struct variant_from_tags<type_list<ts...>, output_t>
38 {
40  using type = std::variant<output_t<ts>...>;
41 };
42 
49 template <std::output_iterator<char> it_t>
50 constexpr void write_eol(it_t & it, bool const add_cr)
51 {
52  if (add_cr)
53  it = '\r';
54 
55  it = '\n';
56 }
57 
68 template <typename format_variant_type>
69 void set_format(format_variant_type & format,
70  std::filesystem::path const & file_name)
71 {
72  using valid_formats = detail::transfer_template_args_onto_t<format_variant_type, type_list>;
73 
74  bool format_found = false;
75  std::string extension = file_name.extension().string();
76  if (extension.size() > 1)
77  {
78  extension = extension.substr(1); // drop leading "."
79  detail::for_each<valid_formats>([&] (auto fmt)
80  {
81  using fm_type = typename decltype(fmt)::type; // remove type_identity wrapper
82 
83  for (auto const & ext : fm_type::file_extensions)
84  {
85  if (std::ranges::equal(ext, extension))
86  {
87  format = fm_type{};
88  format_found = true;
89  return;
90  }
91  }
92  });
93  }
94 
95  if (!format_found)
96  throw unhandled_extension_error("No valid format found for this extension.");
97 }
98 
103 template <typename list_t>
104 inline constexpr bool has_member_file_extensions = false;
105 
107 template <template <typename ...> typename list_t, typename ...ts>
108  requires (requires { ts::file_extensions; }, ..., true)
109 inline constexpr bool has_member_file_extensions<list_t<ts...>> = true;
111 
116 template <typename query_t>
117 inline constexpr bool has_type_valid_formats = false;
118 
120 template <typename query_t>
121  requires requires { typename query_t::valid_formats; }
122 inline constexpr bool has_type_valid_formats<query_t> = true;
124 
144 template <typename formats_t>
145 inline std::vector<std::string> valid_file_extensions()
146 {
147  static_assert(has_member_file_extensions<formats_t>,
148  "Expects that all formats have a static member file_extensions storing the extensions in a range");
149 
150  std::vector<std::string> extensions;
151  detail::for_each<formats_t>([&extensions] (auto t_identity)
152  {
153  using format_t = typename decltype(t_identity)::type;
154  std::ranges::copy(format_t::file_extensions, std::ranges::back_inserter(extensions));
155  });
156 
157  return extensions;
158 }
159 } // namespace seqan3::detail
input_format_concept.hpp
Provides seqan3::sequence_file_input_format and auxiliary classes.
std::string
pack_algorithm.hpp
Provides algorithms for meta programming, parameter packs and seqan3::type_list.
seqan3::type_list
meta::list< types... > type_list
Type that contains multiple types, an alias for meta::list.
Definition: type_list.hpp:31
std::vector< std::string >
std::string::size
T size(T... args)
iterator
Provides C++20 additions to the <iterator> header.
template_inspection.hpp
Provides seqan3::type_list and auxiliary type traits.
filesystem
This header includes C++17 filesystem support and imports it into namespace seqan3::filesystem (indep...
std::filesystem::path
algorithm
Adaptations of algorithms from the Ranges TS.
exception.hpp
Provides exceptions used in the I/O module.
std::string::substr
T substr(T... args)
std::filesystem::path::extension
T extension(T... args)
traits.hpp
Provides traits for seqan3::type_list.
variant