Audacity  3.0.3
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Optional< X > Class Template Reference

Like a smart pointer, allows for object to not exist (nullptr) More...

#include <MemoryX.h>

Inheritance diagram for Optional< X >:
[legend]

Public Types

using value_type = X
 

Public Member Functions

 Optional ()
 
 Optional (const Optional &that)
 
Optionaloperator= (const Optional &that)
 
 Optional (Optional &&that)
 
Optionaloperator= (Optional &&that)
 
template<typename... Args>
X & emplace (Args &&... args)
 
 ~Optional ()
 
X & operator* () const
 Dereference, with the usual bad consequences if NULL. More...
 
X * operator-> () const
 
void reset ()
 
 operator bool () const
 
bool has_value () const
 

Private Member Functions

X * address ()
 

Private Attributes

union {
   double   d
 
   char   storage [sizeof(X)]
 
}; 
 
X * pp { nullptr }
 

Detailed Description

template<typename X>
class Optional< X >

Like a smart pointer, allows for object to not exist (nullptr)

emulating some of std::optional of C++17

template class Optional<X> Can be used for monomorphic objects that are stack-allocable, but only conditionally constructed. You might also use it as a member. Initialize with emplace(), then use like a smart pointer, with *, ->, reset(), or in if()

Definition at line 143 of file MemoryX.h.

Member Typedef Documentation

◆ value_type

template<typename X >
using Optional< X >::value_type = X

Definition at line 146 of file MemoryX.h.

Constructor & Destructor Documentation

◆ Optional() [1/3]

template<typename X >
Optional< X >::Optional ( )
inline

Definition at line 149 of file MemoryX.h.

149 {}

◆ Optional() [2/3]

template<typename X >
Optional< X >::Optional ( const Optional< X > &  that)
inline

Definition at line 152 of file MemoryX.h.

153  {
154  if (that)
155  emplace(*that);
156  }

◆ Optional() [3/3]

template<typename X >
Optional< X >::Optional ( Optional< X > &&  that)
inline

Definition at line 169 of file MemoryX.h.

170  {
171  if (that)
172  emplace(::std::move(*that));
173  }

◆ ~Optional()

template<typename X >
Optional< X >::~Optional ( )
inline

Definition at line 202 of file MemoryX.h.

203  {
204  reset();
205  }

Member Function Documentation

◆ address()

template<typename X >
X* Optional< X >::address ( )
inlineprivate

Definition at line 238 of file MemoryX.h.

239  {
240  return reinterpret_cast<X*>(&storage);
241  }

Referenced by Optional< CommandSignature >::emplace().

Here is the caller graph for this function:

◆ emplace()

template<typename X >
template<typename... Args>
X& Optional< X >::emplace ( Args &&...  args)
inline

Make an object in the buffer, passing constructor arguments, but destroying any previous object first Note that if constructor throws, we remain in a consistent NULL state – giving exception safety but only weakly (previous value was lost if present)

Definition at line 192 of file MemoryX.h.

193  {
194  // Lose any old value
195  reset();
196  // emplace NEW value
197  pp = safenew(address()) X(std::forward<Args>(args)...);
198  return **this;
199  }

Referenced by ProjectFileManager::DoSave(), OverlayPanel::DrawOverlays(), AudioIO::FillBuffers(), OldStyleCommandType::GetSignature(), import_ffmpeg_read_next_frame(), Optional< CommandSignature >::operator=(), Optional< CommandSignature >::Optional(), EffectFindClipping::Process(), NyquistEffect::Process(), LegacyBlockFile::ReadSummary(), SimpleBlockFile::ReadSummary(), FrequencyPlotDialog::Recalc(), UndoManager::RemoveStates(), ShuttleGuiBase::StartRadioButtonGroup(), and AudioIO::StopStream().

Here is the caller graph for this function:

◆ has_value()

template<typename X >
bool Optional< X >::has_value ( ) const
inline

Definition at line 232 of file MemoryX.h.

233  {
234  return pp != nullptr;
235  }

◆ operator bool()

template<typename X >
Optional< X >::operator bool ( ) const
inlineexplicit

Definition at line 227 of file MemoryX.h.

228  {
229  return pp != nullptr;
230  }

◆ operator*()

template<typename X >
X& Optional< X >::operator* ( ) const
inline

Dereference, with the usual bad consequences if NULL.

Definition at line 210 of file MemoryX.h.

211  {
212  return *pp;
213  }

◆ operator->()

template<typename X >
X* Optional< X >::operator-> ( ) const
inline

Definition at line 215 of file MemoryX.h.

216  {
217  return pp;
218  }

◆ operator=() [1/2]

template<typename X >
Optional& Optional< X >::operator= ( const Optional< X > &  that)
inline

Definition at line 158 of file MemoryX.h.

159  {
160  if (this != &that) {
161  if (that)
162  emplace(*that);
163  else
164  reset();
165  }
166  return *this;
167  }

◆ operator=() [2/2]

template<typename X >
Optional& Optional< X >::operator= ( Optional< X > &&  that)
inline

Definition at line 175 of file MemoryX.h.

176  {
177  if (this != &that) {
178  if (that)
179  emplace(::std::move(*that));
180  else
181  reset();
182  }
183  return *this;
184  }

◆ reset()

template<typename X >
void Optional< X >::reset ( )
inline

Definition at line 220 of file MemoryX.h.

221  {
222  if (pp)
223  pp->~X(), pp = nullptr;
224  }

Referenced by Optional< CommandSignature >::emplace(), ShuttleGuiBase::EndRadioButtonGroup(), Optional< CommandSignature >::operator=(), and Optional< CommandSignature >::~Optional().

Here is the caller graph for this function:

Member Data Documentation

◆ @1

union { ... }

◆ d

template<typename X >
double Optional< X >::d

Definition at line 251 of file MemoryX.h.

◆ pp

template<typename X >
X* Optional< X >::pp { nullptr }
private

◆ storage

template<typename X >
char Optional< X >::storage[sizeof(X)]

Definition at line 252 of file MemoryX.h.

Referenced by Optional< CommandSignature >::address().


The documentation for this class was generated from the following file:
Optional::emplace
X & emplace(Args &&... args)
Definition: MemoryX.h:192
Optional::address
X * address()
Definition: MemoryX.h:238
Optional::storage
char storage[sizeof(X)]
Definition: MemoryX.h:252
Optional::reset
void reset()
Definition: MemoryX.h:220
Optional::pp
X * pp
Definition: MemoryX.h:255
safenew
#define safenew
Definition: MemoryX.h:10