SeqAn3  3.0.3
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 
19 #include <seqan3/io/exception.hpp>
20 #include <seqan3/std/algorithm>
21 #include <seqan3/std/filesystem>
22 #include <seqan3/std/iterator>
23 
24 namespace seqan3::detail
25 {
26 
29 template <typename list_t, template <typename ...> typename output_t>
31 
34 template <template <typename...> typename output_t, typename ...ts>
35 struct variant_from_tags<type_list<ts...>, output_t>
36 {
39 };
40 
47 template <std::output_iterator<char> it_t>
48 constexpr void write_eol(it_t & it, bool const add_cr)
49 {
50  if (add_cr)
51  it = '\r';
52 
53  it = '\n';
54 }
55 
66 template <typename format_variant_type>
67 void set_format(format_variant_type & format,
68  std::filesystem::path const & file_name)
69 {
71 
72  bool format_found = false;
73  std::string extension = file_name.extension().string();
74  if (extension.size() > 1)
75  {
76  extension = extension.substr(1); // drop leading "."
77  detail::for_each<valid_formats>([&] (auto fmt)
78  {
79  using fm_type = typename decltype(fmt)::type; // remove type_identity wrapper
80 
81  for (auto const & ext : fm_type::file_extensions)
82  {
83  if (std::ranges::equal(ext, extension))
84  {
85  format.template emplace<fm_type>();
86  format_found = true;
87  return;
88  }
89  }
90  });
91  }
92 
93  if (!format_found)
94  throw unhandled_extension_error("No valid format found for this extension.");
95 }
96 
101 template <typename list_t>
102 inline constexpr bool has_member_file_extensions = false;
103 
105 template <template <typename ...> typename list_t, typename ...ts>
106  requires (requires { ts::file_extensions; }, ..., true)
107 inline constexpr bool has_member_file_extensions<list_t<ts...>> = true;
109 
114 template <typename query_t>
115 inline constexpr bool has_type_valid_formats = false;
116 
118 template <typename query_t>
119  requires requires { typename query_t::valid_formats; }
120 inline constexpr bool has_type_valid_formats<query_t> = true;
122 
142 template <typename formats_t>
144 {
145  static_assert(has_member_file_extensions<formats_t>,
146  "Expects that all formats have a static member file_extensions storing the extensions in a range");
147 
148  std::vector<std::string> extensions;
149  detail::for_each<formats_t>([&extensions] (auto t_identity)
150  {
151  using format_t = typename decltype(t_identity)::type;
152  std::ranges::copy(format_t::file_extensions, std::cpp20::back_inserter(extensions));
153  });
154 
155  return extensions;
156 }
157 } // namespace seqan3::detail
Adaptations of algorithms from the Ranges TS.
Provides type traits for working with templates.
T extension(T... args)
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
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
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:67
std::vector< std::string > valid_file_extensions()
Returns a list of valid file extensions.
Definition: misc.hpp:143
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:48
meta::list< types... > type_list
Type that contains multiple types, an alias for meta::list.
Definition: type_list.hpp:31
Provides exceptions used in the I/O module.
Provides C++20 additions to the <iterator> header.
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
constexpr bool has_type_valid_formats
Helper function to determine if a type has a static member valid_formats.
Definition: misc.hpp:115
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:102
Provides algorithms for meta programming, parameter packs and seqan3::type_list.
T size(T... args)
Base class to deduce the std::variant type from format tags.
Definition: misc.hpp:30
Thrown if there is no format that accepts a given file extension.
Definition: exception.hpp:32
T substr(T... args)