Audacity 3.2.0
Classes | Macros | Typedefs | Functions
MemoryX.h File Reference
#include <iterator>
#include <memory>
#include <new>
#include <cstdlib>
#include <functional>
#include <limits>
#include <algorithm>
#include <type_traits>
#include <variant>
#include <stdexcept>
Include dependency graph for MemoryX.h:

Go to the source code of this file.

Classes

class  ArrayOf< 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. More...
 
class  ArraysOf< X >
 
struct  freer
 
struct  Destroyer< T >
 A deleter class to supply the second template parameter of unique_ptr for classes like wxWindow that should be sent a message called Destroy rather than be deleted directly. More...
 
struct  Finally< F >
 "finally" as in The C++ Programming Language, 4th ed., p. 358 Useful for defining ad-hoc RAII actions. typical usage: auto cleanup = finally([&]{ ... code; ... }); More...
 
struct  RestoreValue< T >
 Structure used by ValueRestorer. More...
 
class  ValueRestorer< T >
 Set a variable temporarily in a scope. More...
 
struct  ValueIterator< Value, Category >
 A convenience for defining iterators that return rvalue types, so that they cooperate correctly with stl algorithms and std::reverse_iterator. More...
 
struct  IteratorRange< Iterator >
 A convenience for use with range-for. More...
 
struct  IteratorRange< Iterator >::identity< T >
 
struct  SharedNonInterfering< T >
 Workaround for std::make_shared not working on macOs with over-alignment. More...
 
struct  NonInterfering< T >
 
struct  VisitHelperReturn< Visitor, Variant >
 Help to define Visit() below. More...
 
struct  AtomicUniquePointer< T >
 

Macros

#define safenew   new
 
#define QUANTIZED_TIME(time, rate)   (floor(((double)(time) * (rate)) + 0.5) / (rate))
 
#define DB_TO_LINEAR(x)   (pow(10.0, (x) / 20.0))
 
#define LINEAR_TO_DB(x)   (20.0 * log10(x))
 
#define MAX_AUDIO   (1. - 1./(1<<15))
 

Typedefs

template<typename T >
using MallocPtr = std::unique_ptr< T, freer >
 
template<typename Character = char>
using MallocString = std::unique_ptr< Character[], freer >
 
template<typename T >
using Destroy_ptr = std::unique_ptr< T, Destroyer< T > >
 a convenience for using Destroyer More...
 

Functions

template<typename F >
Finally< F > finally (F f)
 Function template with type deduction lets you construct Finally without typing any angle brackets. More...
 
template<typename F >
 Finally (F) -> Finally< F >
 
template<typename T >
ValueRestorer< T > valueRestorer (T &var)
 inline functions provide convenient parameter type deduction More...
 
template<typename T >
ValueRestorer< T > valueRestorer (T &var, const T &newValue)
 
template<typename Iterator >
IteratorRange< Iterator > make_iterator_range (const Iterator &i1, const Iterator &i2)
 
template<typename Container >
IteratorRange< typename Container::iterator > make_iterator_range (Container &container)
 
template<typename Container >
IteratorRange< typename Container::const_iterator > make_iterator_range (const Container &container)
 
template<typename Container , typename Iterator , typename Function >
Container transform_range (Iterator first, Iterator last, Function &&fn)
 
template<typename OutContainer , typename InContainer , typename Function >
OutContainer transform_container (InContainer &inContainer, Function &&fn)
 
struct UTILITY_API alignas (64) NonInterferingBase
 Non-template helper for class template NonInterfering. More...
 
template<typename Visitor , typename Variant >
auto VisitHelper (Visitor &&, Variant &&) -> typename VisitHelperReturn< Visitor, Variant >::type
 Help to define Visit() below. More...
 
template<size_t Index, size_t... Indices, typename Visitor , typename Variant >
auto VisitHelper (Visitor &&vis, Variant &&var)
 Help to define Visit() below. More...
 
template<size_t... Indices, typename Visitor , typename Variant >
auto VisitHelper (std::index_sequence< Indices... >, Visitor &&vis, Variant &&var)
 Help to define Visit() below. More...
 
template<typename Visitor , typename Variant >
auto Visit (Visitor &&vis, Variant &&var)
 Mimic some of std::visit, for the case of one visitor only. More...
 

Macro Definition Documentation

◆ DB_TO_LINEAR

#define DB_TO_LINEAR (   x)    (pow(10.0, (x) / 20.0))

Definition at line 543 of file MemoryX.h.

◆ LINEAR_TO_DB

#define LINEAR_TO_DB (   x)    (20.0 * log10(x))

Definition at line 544 of file MemoryX.h.

◆ MAX_AUDIO

#define MAX_AUDIO   (1. - 1./(1<<15))

Definition at line 546 of file MemoryX.h.

◆ QUANTIZED_TIME

#define QUANTIZED_TIME (   time,
  rate 
)    (floor(((double)(time) * (rate)) + 0.5) / (rate))

Definition at line 541 of file MemoryX.h.

◆ safenew

#define safenew   new

Definition at line 10 of file MemoryX.h.

Typedef Documentation

◆ Destroy_ptr

template<typename T >
using Destroy_ptr = std::unique_ptr<T, Destroyer<T> >

a convenience for using Destroyer

Definition at line 162 of file MemoryX.h.

◆ MallocPtr

template<typename T >
using MallocPtr = std::unique_ptr< T, freer >

A useful alias for holding the result of malloc

Definition at line 140 of file MemoryX.h.

◆ MallocString

template<typename Character = char>
using MallocString = std::unique_ptr< Character[], freer >

A useful alias for holding the result of strup and similar

Definition at line 146 of file MemoryX.h.

Function Documentation

◆ alignas()

struct UTILITY_API alignas ( 64  )

Non-template helper for class template NonInterfering.

If a structure contains any members with large alignment, this base class may also allow it to work in macOS builds under current limitations of the C++17 standard implementation.

Definition at line 471 of file MemoryX.h.

480 {
481 static void *operator new(std::size_t count, std::align_val_t al);
482 static void operator delete(void *ptr, std::align_val_t al);
483
484#if defined (_MSC_VER) && defined(_DEBUG)
485 // Versions that work in the presence of the DEBUG_NEW macro.
486 // Ignore the arguments supplied by the macro and forward to the
487 // other overloads.
488 static void *operator new(
489 std::size_t count, std::align_val_t al, int, const char *, int)
490 { return operator new(count, al); }
491 static void operator delete(
492 void *ptr, std::align_val_t al, int, const char *, int)
493 { return operator delete(ptr, al); }
494#endif
495};

◆ finally()

template<typename F >
Finally< F > finally ( f)

Function template with type deduction lets you construct Finally without typing any angle brackets.

Definition at line 182 of file MemoryX.h.

183{
184 return Finally<F>(f);
185}
"finally" as in The C++ Programming Language, 4th ed., p. 358 Useful for defining ad-hoc RAII actions...
Definition: MemoryX.h:173

◆ Finally()

template<typename F >
Finally ( ) -> Finally< F >

C++17 deduction guide allows even simpler syntax: Finally Do{[&]{ Stuff(); }};

Don't omit Do or some other variable name! Otherwise, the execution of the body is immediate, not delayed to the end of the enclosing scope.

◆ make_iterator_range() [1/3]

template<typename Container >
IteratorRange< typename Container::const_iterator > make_iterator_range ( const Container &  container)

Definition at line 445 of file MemoryX.h.

446{
447 return { container.begin(), container.end() };
448}

◆ make_iterator_range() [2/3]

template<typename Iterator >
IteratorRange< Iterator > make_iterator_range ( const Iterator &  i1,
const Iterator &  i2 
)

Definition at line 431 of file MemoryX.h.

432{
433 return { i1, i2 };
434}

Referenced by ShuttleGuiBase::AddChoice(), ProjectFileManager::AddImportedTracks(), AudioIO::AllocateBuffers(), ChoiceEditor::BeginEdit(), PluginManager::CheckPluginUpdates(), TrackList::Contains(), ProjectAudioManager::DoRecord(), ExportMP3::Export(), ExportFFmpegOptions::FetchCompatibleCodecList(), AudioSetupToolBar::FillHosts(), DeviceToolBar::FillHosts(), ExportFFmpegOptions::FindSelectedCodec(), ExportFFmpegOptions::FindSelectedFormat(), AudioIO::GetBestRate(), CommandManager::GetCategories(), DevicePrefs::GetNamesAndLabels(), AudioIOBase::GetOptimalSupportedSampleRate(), PluginManager::GetPlugin(), AudioIOBase::GetSupportedCaptureRates(), AudioIOBase::GetSupportedPlaybackRates(), AudioIOBase::GetSupportedSampleRates(), MacroCommands::IsFixed(), LangChoiceDialog::LangChoiceDialog(), Effect::LoadSettingsFromString(), MacroCommands::MacroCommands(), LabelDialog::OnChangeTrack(), LabelDialog::OnInsert(), LangChoiceDialog::OnOk(), PluginRegistrationDialog::OnRescan(), EffectUIHost::OnSaveAs(), KeyConfigPrefs::OnSet(), LabelTrackMenuTable::OnSetFont(), NyquistEffect::Parse(), ModuleManager::Providers(), ChoiceEditor::Reset(), KeyConfigPrefs::SetKeyForSelected(), ShuttleGuiBase::TieChoice(), ShuttleGuiBase::TieNumberAsChoice(), NyquistEffect::Tokenizer::Tokenize(), ShuttleGuiBase::TranslateToIndex(), and OptionValidator::Validate().

Here is the caller graph for this function:

◆ make_iterator_range() [3/3]

template<typename Container >
IteratorRange< typename Container::iterator > make_iterator_range ( Container &  container)

Definition at line 438 of file MemoryX.h.

439{
440 return { container.begin(), container.end() };
441}

◆ transform_container()

template<typename OutContainer , typename InContainer , typename Function >
OutContainer transform_container ( InContainer &  inContainer,
Function &&  fn 
)

Definition at line 460 of file MemoryX.h.

461{
462 return transform_range<OutContainer>(
463 inContainer.begin(), inContainer.end(), fn );
464}
static const auto fn

References fn.

◆ transform_range()

template<typename Container , typename Iterator , typename Function >
Container transform_range ( Iterator  first,
Iterator  last,
Function &&  fn 
)

Definition at line 452 of file MemoryX.h.

453{
454 Container result;
455 std::transform( first, last, std::back_inserter( result ), fn );
456 return result;
457}

References fn.

◆ valueRestorer() [1/2]

template<typename T >
ValueRestorer< T > valueRestorer ( T &  var)

inline functions provide convenient parameter type deduction

Definition at line 234 of file MemoryX.h.

235{ return ValueRestorer< T >{ var }; }
Set a variable temporarily in a scope.
Definition: MemoryX.h:212

Referenced by MacroCommands::ApplyMacro(), AudacityCommand::DoAudacityCommand(), EffectBase::DoEffect(), CommandManager::FilterKeyEvent(), Importer::Import(), EffectNoiseReduction::Dialog::OnPreview(), EffectAmplify::Preview(), EffectBase::Preview(), EffectTimeScale::Preview(), NyquistEffect::Process(), NyquistEffect::ProcessOne(), and AudacityCommand::ShowInterface().

Here is the caller graph for this function:

◆ valueRestorer() [2/2]

template<typename T >
ValueRestorer< T > valueRestorer ( T &  var,
const T &  newValue 
)

Definition at line 238 of file MemoryX.h.

239{ return ValueRestorer< T >{ var, newValue }; }

◆ Visit()

template<typename Visitor , typename Variant >
auto Visit ( Visitor &&  vis,
Variant &&  var 
)

Mimic some of std::visit, for the case of one visitor only.

This is necessary because of limitations of the macOS implementation of some of the C++17 standard library without a minimum version of 10.13, and so let's use this even when not needed on the other platforms, instead of having too much conditional compilation

Definition at line 611 of file MemoryX.h.

612{
613 constexpr auto size = std::variant_size_v<std::remove_reference_t<Variant>>;
614 return VisitHelper( std::make_index_sequence<size>{},
615 std::forward<Visitor>(vis), std::forward<Variant>(var) );
616}
auto VisitHelper(Visitor &&, Variant &&) -> typename VisitHelperReturn< Visitor, Variant >::type
Help to define Visit() below.
Definition: MemoryX.h:570

References size, and VisitHelper().

Referenced by EffectTruncSilence::DoRemoval(), Exporter::Exporter(), PluginManager::GetConfigValue(), Importer::Initialize(), ToolDock::LayoutToolBars(), EditActions::Handler::OnCut(), ToolDock::PositionBar(), and PluginManager::SetConfigValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ VisitHelper() [1/3]

template<size_t... Indices, typename Visitor , typename Variant >
auto VisitHelper ( std::index_sequence< Indices... >  ,
Visitor &&  vis,
Variant &&  var 
)

Help to define Visit() below.

Definition at line 597 of file MemoryX.h.

598{
599 // Non-template parameters were deduced and are passed on as non-deduced
600 return VisitHelper<Indices...>(
601 std::forward<Visitor>(vis), std::forward<Variant>(var) );
602}

References VisitHelper().

Here is the call graph for this function:

◆ VisitHelper() [2/3]

template<typename Visitor , typename Variant >
auto VisitHelper ( Visitor &&  ,
Variant &&   
) -> typename VisitHelperReturn<Visitor, Variant>::type

Help to define Visit() below.

Definition at line 570 of file MemoryX.h.

572{
573 // Fall through here when the variant holds no value
574 // Should really throw std::bad_variant_access but that may not be available
575 throw std::invalid_argument{"Bad variant"};
576}

Referenced by Visit(), and VisitHelper().

Here is the caller graph for this function:

◆ VisitHelper() [3/3]

template<size_t Index, size_t... Indices, typename Visitor , typename Variant >
auto VisitHelper ( Visitor &&  vis,
Variant &&  var 
)

Help to define Visit() below.

Definition at line 580 of file MemoryX.h.

581{
582 // Invoke vis at most once after no-throw testing for presence of
583 // alternatives in the variant
584 if (const auto pValue = std::get_if<Index>(&var)) {
585 if constexpr (std::is_lvalue_reference_v<Variant>)
586 return std::invoke( std::forward<Visitor>(vis), (*pValue) );
587 else
588 return std::invoke( std::forward<Visitor>(vis), std::move(*pValue) );
589 }
590 // Recur down the index value pack
591 return VisitHelper<Indices...>(
592 std::forward<Visitor>(vis), std::forward<Variant>(var));
593}

References VisitHelper().

Here is the call graph for this function: