1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-22 23:30:07 +02:00

Define ArrayOf<X>, ArraysOf<X> to hide uses of new[] and delete[]...

... but it's still unused.
This commit is contained in:
Paul Licameli 2016-03-25 11:26:00 -04:00
parent b5c5d55d2f
commit 1aa9ddc76d

View File

@ -348,6 +348,68 @@ namespace std {
}
#endif
/*
* ArrayOf<X>
* Not to be confused with std::array (which takes a fixed size) or std::vector
* This maintains a pointer allocated by NEW X[]. It's cheap: only one pointer,
* with no size and capacity information for resizing as for vector, and if X is
* a built-in numeric or pointer type, by default there is no zero filling at
* allocation time.
*/
template<typename X>
class ArrayOf : public std::unique_ptr<X[]>
{
public:
ArrayOf() {}
explicit ArrayOf(size_t count, bool initialize = false)
{
if (initialize)
reset(safenew X[count]{});
else
reset(safenew X[count]);
}
ArrayOf(const ArrayOf&) = delete;
ArrayOf& operator= (ArrayOf &&that)
{
std::unique_ptr<X[]>::operator=(std::move(that));
return *this;
}
ArrayOf& operator= (std::unique_ptr<X[]> &&that)
{
std::unique_ptr<X[]>::operator=(std::move(that));
return *this;
}
};
/*
* ArraysOf<X>
* This simplifies arrays of arrays, each array separately allocated with NEW[]
* But it might be better to use std::Array<ArrayOf<X>, N> for some small constant N
* Or use just one array when sub-arrays have a common size and are not large.
*/
template<typename X>
class ArraysOf : public ArrayOf<ArrayOf<X>>
{
public:
ArraysOf() {}
explicit ArraysOf(size_t N)
: ArrayOf<ArrayOf<X>>( N )
{}
ArraysOf(size_t N, size_t M, bool initialize = false)
: ArrayOf<ArrayOf<X>>( N )
{
for (size_t ii = 0; ii < N; ++ii)
(*this)[ii] = ArrayOf<X>{ M, initialize };
}
ArraysOf(const ArraysOf&) = delete;
ArraysOf& operator= (ArraysOf&& that)
{
ArrayOf<ArrayOf<X>>::operator=(std::move(that));
return *this;
}
};
/*
* template class Maybe<X>
* Can be used for monomorphic objects that are stack-allocable, but only conditionally constructed.