1#ifndef __AUDACITY_MEMORY_X_H__
2#define __AUDACITY_MEMORY_X_H__
26class ArrayOf :
public std::unique_ptr<X[]>
31 template<
typename Integral>
32 explicit ArrayOf(Integral count,
bool initialize =
false)
34 static_assert(std::is_unsigned<Integral>::value,
"Unsigned arguments only");
41 :
std::unique_ptr < X[] >
42 (
std::move((
std::unique_ptr < X[] >&)(that)))
47 std::unique_ptr<X[]>::operator=(std::move(that));
52 std::unique_ptr<X[]>::operator=(std::move(that));
56 template<
typename Integral >
58 bool initialize =
false)
60 static_assert(std::is_unsigned<Integral>::value,
"Unsigned arguments only");
63 std::unique_ptr<X[]>::reset(
safenew X[count]{});
66 std::unique_ptr<X[]>::reset(
safenew X[count]);
85 template<
typename Integral>
90 template<
typename Integral1,
typename Integral2 >
91 ArraysOf(Integral1 N, Integral2 M,
bool initialize =
false)
94 static_assert(std::is_unsigned<Integral1>::value,
"Unsigned arguments only");
95 static_assert(std::is_unsigned<Integral2>::value,
"Unsigned arguments only");
96 for (
size_t ii = 0; ii < N; ++ii)
108 template<
typename Integral >
114 template<
typename Integral >
115 void reinit(Integral count,
bool initialize)
120 template<
typename Integral1,
typename Integral2 >
121 void reinit(Integral1 countN, Integral2 countM,
bool initialize =
false)
123 static_assert(std::is_unsigned<Integral1>::value,
"Unsigned arguments only");
124 static_assert(std::is_unsigned<Integral2>::value,
"Unsigned arguments only");
126 for (
size_t ii = 0; ii < countN; ++ii)
127 (*
this)[ii].reinit(countM, initialize);
139template<
typename T >
145template <
typename Character =
char>
200template<
typename T >
210template<
typename T >
213 using std::unique_ptr< T, RestoreValue<T> >::reset;
227 std::unique_ptr < T, RestoreValue<T> >::operator=(std::move(that));
235template<
typename T >
250template<
typename T >
254template<
typename T >
262template<
typename Value,
typename Category = std::forward_iterator_tag >
276template <
typename Iterator>
282 :
std::pair<Iterator, Iterator> ( a, b ) {}
285 :
std::pair<Iterator, Iterator> (
std::move(a),
std::move(b) ) {}
290 Iterator
begin()
const {
return this->first; }
291 Iterator
end()
const {
return this->second; }
297 explicit operator bool ()
const {
return !this->
empty(); }
298 size_t size()
const {
return std::distance(this->
begin(), this->
end()); }
301 {
return std::find(this->
begin(), this->
end(), t); }
303 template <
typename T>
long index(
const T &t)
const
305 auto iter = this->
find(t);
306 if (iter == this->
end())
308 return std::distance(this->
begin(), iter);
311 template <
typename T>
bool contains(
const T &t)
const
312 {
return this->
end() != this->
find(t); }
315 {
return std::find_if(this->
begin(), this->
end(), f); }
317 template <
typename F>
long index_if(
const F &f)
const
320 if (iter == this->
end())
322 return std::distance(this->
begin(), iter);
326 template <
typename F>
bool all_of(
const F &f)
const
329 [&](
typename std::iterator_traits<Iterator>::reference v)
331 return !this->
any_of( notF );
334 template <
typename F>
bool any_of(
const F &f)
const
337 template <
typename F>
bool none_of(
const F &f)
const
338 {
return !this->
any_of(f); }
341 {
const T&&
operator () (T &&v)
const {
return std::forward(v); } };
347 typename Binary = std::plus< R >,
352 Binary binary_op = {},
357 for (
auto&& v : *
this)
358 result = binary_op(result, unary_op(v));
366 typename Binary = std::plus< R >,
367 typename R2,
typename C
372 R2 (C :: * pmf) ()
const
375 return this->
accumulate( init, binary_op, std::mem_fn( pmf ) );
380 typename Unary = identity< decltype( *std::declval<Iterator>() ) >,
381 typename R =
decltype( std::declval<Unary>()( *std::declval<Iterator>() ) )
383 R
min( Unary unary_op = {} )
const
386 std::numeric_limits< R >::max(),
387 (
const R&(*)(
const R&,
const R&))
std::min,
393 typename R2,
typename C,
396 R
min( R2 (C :: * pmf) ()
const )
const
398 return this->
min( std::mem_fn( pmf ) );
402 typename Unary = identity< decltype( *std::declval<Iterator>() ) >,
403 typename R =
decltype( std::declval<Unary>()( *std::declval<Iterator>() ) )
405 R
max( Unary unary_op = {} )
const
408 std::numeric_limits< R >::lowest(),
409 (
const R&(*)(
const R&,
const R&)) std::max,
415 typename R2,
typename C,
418 R
max( R2 (C :: * pmf) ()
const )
const
420 return this->
max( std::mem_fn( pmf ) );
424 typename Unary = identity< decltype( *std::declval<Iterator>() ) >,
425 typename R =
decltype( std::declval<Unary>()( *std::declval<Iterator>() ) )
427 R
sum( Unary unary_op = {} )
const
437 typename R2,
typename C,
440 R
sum( R2 (C :: * pmf) ()
const )
const
442 return this->
sum( std::mem_fn( pmf ) );
446template<
typename Iterator>
453template<
typename Container >
457 return { container.begin(), container.end() };
460template<
typename Container >
464 return { container.begin(), container.end() };
468template<
typename Container,
typename Iterator,
typename Function >
472 std::transform( first, last, std::back_inserter( result ),
fn );
476template<
typename OutContainer,
typename InContainer,
typename Function >
479 return transform_range<OutContainer>(
480 inContainer.begin(), inContainer.end(),
fn );
489#if defined(_WIN32) && defined(_MSC_VER)
491 std::hardware_destructive_interference_size
499 static void *
operator new(std::size_t count, std::align_val_t al);
500 static void operator delete(
void *ptr, std::align_val_t al);
502#if defined (_MSC_VER) && defined(_DEBUG)
506 static void *
operator new(
507 std::size_t count, std::align_val_t al, int,
const char *, int)
508 {
return operator new(count, al); }
509 static void operator delete(
510 void *ptr, std::align_val_t al, int,
const char *, int)
511 {
return operator delete(ptr, al); }
522 template<
typename... Args>
532 <T>(std::forward<Args>(args)...);
548 T::operator =(other);
554 T::operator =(std::move(other));
559#define QUANTIZED_TIME(time, rate) (floor(((double)(time) * (rate)) + 0.5) / (rate))
561#define DB_TO_LINEAR(x) (pow(10.0, (x) / 20.0))
562#define LINEAR_TO_DB(x) (20.0 * log10(x))
564#define MAX_AUDIO (1. - 1./(1<<15))
570 static_assert(AtomicUniquePointer::is_always_lock_free);
571 using std::atomic<T*>::atomic;
577 delete this->exchange(p, std::memory_order_release);
580 template<
typename... Args>
void emplace(Args &&... args) {
586 using std::atomic<T*>::fetch_add;
587 using std::atomic<T*>::fetch_sub;
ValueRestorer< T > valueRestorer(T &var)
inline functions provide convenient parameter type deduction
struct UTILITY_API alignas(64) NonInterferingBase
Non-template helper for class template NonInterfering.
std::unique_ptr< Character[], freer > MallocString
Container transform_range(Iterator first, Iterator last, Function &&fn)
IteratorRange< Iterator > make_iterator_range(const Iterator &i1, const Iterator &i2)
std::unique_ptr< T, Destroyer< T > > Destroy_ptr
a convenience for using Destroyer
Finally(F) -> Finally< F >
OutContainer transform_container(InContainer &inContainer, Function &&fn)
std::unique_ptr< T, freer > MallocPtr
This simplifies arrays of arrays, each array separately allocated with NEW[] But it might be better t...
ArrayOf(const ArrayOf &)=delete
ArrayOf & operator=(ArrayOf &&that)
void reinit(Integral count, bool initialize=false)
ArrayOf(Integral count, bool initialize=false)
void reinit(Integral count)
ArraysOf(Integral1 N, Integral2 M, bool initialize=false)
ArraysOf(const ArraysOf &)=delete
ArraysOf & operator=(ArraysOf &&that)
void reinit(Integral1 countN, Integral2 countM, bool initialize=false)
void reinit(Integral count, bool initialize)
Set a variable temporarily in a scope.
ValueRestorer(T &var, const T &newValue)
ValueRestorer & operator=(ValueRestorer &&that)
ValueRestorer(ValueRestorer &&that)
void emplace(Args &&... args)
reset to a pointer to a new object with given ctor arguments
std::shared_ptr< T > pointer
CopyableValueRestorer(T &var, const T &newValue)
CopyableValueRestorer(T &var)
A deleter class to supply the second template parameter of unique_ptr for classes like wxWindow that ...
void operator()(T *p) const
"finally" as in The C++ Programming Language, 4th ed., p. 358 Useful for defining ad-hoc RAII actions...
const T && operator()(T &&v) const
A convenience for use with range-for.
R accumulate(R init, Binary binary_op, R2(C ::*pmf)() const) const
bool contains(const T &t) const
R sum(R2(C ::*pmf)() const) const
R sum(Unary unary_op={}) const
R accumulate(R init, Binary binary_op={}, Unary unary_op={}) const
bool all_of(const F &f) const
std::reverse_iterator< Iterator > reverse_iterator
iterator find_if(const F &f) const
iterator find(const T &t) const
IteratorRange(Iterator &&a, Iterator &&b)
long index(const T &t) const
R max(Unary unary_op={}) const
IteratorRange< reverse_iterator > reversal() const
R min(R2(C ::*pmf)() const) const
bool any_of(const F &f) const
long index_if(const F &f) const
reverse_iterator rend() const
bool none_of(const F &f) const
R min(Unary unary_op={}) const
IteratorRange(const Iterator &a, const Iterator &b)
reverse_iterator rbegin() const
R max(R2(C ::*pmf)() const) const
void Set(T &&other)
Allow assignment from default-aligned base type.
void Set(const T &other)
Allow assignment from default-aligned base type.
Structure used by ValueRestorer.
void operator()(T *p) const
Workaround for std::make_shared not working on macOs with over-alignment.
static std::shared_ptr< T > make_shared(Args &&...args)
A convenience for defining iterators that return rvalue types, so that they cooperate correctly with ...
ptrdiff_t difference_type
Category iterator_category
void operator()(void *p) const