Move unique_span to d_uspan.h
This commit is contained in:
parent
5700cc28e7
commit
05eea350a0
55
common/main/d_uspan.h
Normal file
55
common/main/d_uspan.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
|
||||
* It is copyright by its individual contributors, as recorded in the
|
||||
* project's Git history. See COPYING.txt at the top level for license
|
||||
* terms and a link to the Git history.
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <span>
|
||||
|
||||
namespace dcx {
|
||||
|
||||
/* `unique_span` wraps `unique_ptr<T[]>`, but also remembers the extent of the
|
||||
* array `T[]`. The method `span()` returns a
|
||||
* `std::span<T, std::dynamic_extent>` that covers the memory region managed by
|
||||
* `unique_span`. There is no support for a `std::span<T, static_length>`,
|
||||
* because that could be more simply handled by
|
||||
* `std::unique_ptr<std::array<T, static_length>>`, which would avoid
|
||||
* storing the extent in the `unique_span`.
|
||||
*/
|
||||
/* Set a default deleter from the underlying unique_ptr, but allow it to be
|
||||
* overridden if the caller needs special handling.
|
||||
*/
|
||||
template <typename T, typename Deleter = typename std::unique_ptr<T[]>::deleter_type>
|
||||
class unique_span : std::unique_ptr<T[], Deleter>
|
||||
{
|
||||
using base_type = std::unique_ptr<T[], Deleter>;
|
||||
std::size_t extent;
|
||||
public:
|
||||
unique_span(const std::size_t e) :
|
||||
base_type(new T[e]()),
|
||||
extent(e)
|
||||
{
|
||||
}
|
||||
unique_span(unique_span &&) = default;
|
||||
using base_type::get;
|
||||
/* Require an lvalue input, since the returned pointer is borrowed from
|
||||
* this object. If the method is called on an rvalue input, then the
|
||||
* unique_ptr would be destroyed and free the memory before the returned
|
||||
* span was destroyed, which would leave the span dangling.
|
||||
*/
|
||||
[[nodiscard]]
|
||||
std::span<T> span() &
|
||||
{
|
||||
return {get(), extent};
|
||||
}
|
||||
[[nodiscard]]
|
||||
std::span<const T> span() const &
|
||||
{
|
||||
return {get(), extent};
|
||||
}
|
||||
std::span<const T> span() const && = delete;
|
||||
};
|
||||
|
||||
}
|
|
@ -41,6 +41,7 @@
|
|||
#include "d_bitset.h"
|
||||
#include "d_range.h"
|
||||
#include "d_underlying_value.h"
|
||||
#include "d_uspan.h"
|
||||
|
||||
#define MIX_DIGI_DEBUG 0
|
||||
#define MIX_OUTPUT_FORMAT AUDIO_S16
|
||||
|
@ -56,37 +57,6 @@ namespace dcx {
|
|||
|
||||
namespace {
|
||||
|
||||
template <typename T>
|
||||
class unique_span : std::unique_ptr<T[]>
|
||||
{
|
||||
using base_type = std::unique_ptr<T[]>;
|
||||
std::size_t extent;
|
||||
public:
|
||||
unique_span(const std::size_t e) :
|
||||
base_type(std::make_unique<T[]>(e)),
|
||||
extent(e)
|
||||
{
|
||||
}
|
||||
unique_span(unique_span &&) = default;
|
||||
using base_type::get;
|
||||
/* Require an lvalue input, since the returned pointer is borrowed from
|
||||
* this object. If the method is called on an rvalue input, then the
|
||||
* unique_ptr would be destroyed and free the memory before the returned
|
||||
* span was destroyed, which would leave the span dangling.
|
||||
*/
|
||||
[[nodiscard]]
|
||||
std::span<T> span() &
|
||||
{
|
||||
return {get(), extent};
|
||||
}
|
||||
[[nodiscard]]
|
||||
std::span<const T> span() const &
|
||||
{
|
||||
return {get(), extent};
|
||||
}
|
||||
std::span<const T> span() const && = delete;
|
||||
};
|
||||
|
||||
enumerated_bitset<64, sound_channel> channels;
|
||||
|
||||
/* channel management */
|
||||
|
|
Loading…
Reference in a new issue