Skip to content

Commit

Permalink
[FIX] sam_file_header ref_ids ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Nov 7, 2024
1 parent a96c7f2 commit b2a4981
Showing 1 changed file with 13 additions and 30 deletions.
43 changes: 13 additions & 30 deletions include/seqan3/io/sam_file/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,17 @@ class sam_file_header
/*!\name Constructors, destructor and assignment
* \{
*/
sam_file_header() = default; //!< Defaulted.
sam_file_header(sam_file_header const &) = delete; //!< Deleted. Holds a unique_ptr.
sam_file_header & operator=(sam_file_header const &) = delete; //!< Deleted. Holds a unique_ptr.
sam_file_header(sam_file_header &&) = default; //!< Defaulted.
sam_file_header & operator=(sam_file_header &&) = default; //!< Defaulted.
~sam_file_header() = default; //!< Defaulted.

/*!\brief Construct from a range of reference ids which redirects the `ref_ids_ptr` member (non-owning).
* \param[in] ref_ids The range over reference ids to redirect the pointer at.
sam_file_header() = default; //!< Defaulted.
sam_file_header(sam_file_header const &) = default; //!< Defaulted.
sam_file_header & operator=(sam_file_header const &) = default; //!< Defaulted.
sam_file_header(sam_file_header &&) = default; //!< Defaulted.
sam_file_header & operator=(sam_file_header &&) = default; //!< Defaulted.
~sam_file_header() = default; //!< Defaulted.

/*!\brief Construct from a range of reference ids.
* \param[in] ref_ids The range over reference ids.
*/
sam_file_header(ref_ids_type & ref_ids) : ref_ids_ptr{&ref_ids, ref_ids_deleter_noop}
{}

/*!\brief Construct from a rvalue range of reference ids which is moved into the `ref_ids_ptr` (owning).
* \param[in] ref_ids The range over reference ids to own.
*/
sam_file_header(ref_ids_type && ref_ids) :
ref_ids_ptr{new ref_ids_type{std::move(ref_ids)}, ref_ids_deleter_default}
sam_file_header(ref_ids_type ref_ids) : reference_ids{std::move(ref_ids)}
{}
//!\}

Expand All @@ -83,22 +76,12 @@ class sam_file_header
std::vector<std::string> comments; //!< The list of comments.

private:
//!\brief The type of the internal ref_ids pointer. Allows dynamically setting ownership management.
using ref_ids_ptr_t = std::unique_ptr<ref_ids_type, std::function<void(ref_ids_type *)>>;
//!\brief Stream deleter that does nothing (no ownership assumed).
static void ref_ids_deleter_noop(ref_ids_type *)
{}
//!\brief Stream deleter with default behaviour (ownership assumed).
static void ref_ids_deleter_default(ref_ids_type * ptr)
{
delete ptr;
}
//!\brief The key's type of ref_dict.
using key_type = std::conditional_t<std::ranges::contiguous_range<std::ranges::range_reference_t<ref_ids_type>>,
std::span<range_innermost_value_t<ref_ids_type> const>,
type_reduce_t<std::ranges::range_reference_t<ref_ids_type>>>;
//!\brief The pointer to reference ids information (non-owning if reference information is given).
ref_ids_ptr_t ref_ids_ptr{new ref_ids_type{}, ref_ids_deleter_default};
//!\brief The reference ids.
ref_ids_type reference_ids{};

//!\brief Custom hash function since std::hash is not defined for all range types (e.g. std::span<char>).
struct key_hasher
Expand Down Expand Up @@ -140,7 +123,7 @@ class sam_file_header
*/
ref_ids_type & ref_ids()
{
return *ref_ids_ptr;
return reference_ids;
}

/*!\brief The reference information. (used by the SAM/BAM format)
Expand Down

0 comments on commit b2a4981

Please sign in to comment.