Audacity  3.2.0
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 144 of file MemoryX.h.

Member Typedef Documentation

◆ value_type

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

Definition at line 147 of file MemoryX.h.

Constructor & Destructor Documentation

◆ Optional() [1/3]

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

Definition at line 150 of file MemoryX.h.

150 {}

◆ Optional() [2/3]

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

Definition at line 153 of file MemoryX.h.

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

◆ Optional() [3/3]

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

Definition at line 170 of file MemoryX.h.

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

◆ ~Optional()

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

Definition at line 203 of file MemoryX.h.

204  {
205  reset();
206  }

Member Function Documentation

◆ address()

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

Definition at line 239 of file MemoryX.h.

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

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 193 of file MemoryX.h.

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

Referenced by ProjectFileManager::DoSave(), OverlayPanel::DrawOverlays(), OldStyleCommandType::GetSignature(), Optional< CommandSignature >::operator=(), Optional< CommandSignature >::Optional(), EffectFindClipping::Process(), NyquistEffect::Process(), FrequencyPlotDialog::Recalc(), and ShuttleGuiBase::StartRadioButtonGroup().

Here is the caller graph for this function:

◆ has_value()

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

Definition at line 233 of file MemoryX.h.

234  {
235  return pp != nullptr;
236  }

◆ operator bool()

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

Definition at line 228 of file MemoryX.h.

229  {
230  return pp != nullptr;
231  }

◆ operator*()

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

Dereference, with the usual bad consequences if NULL.

Definition at line 211 of file MemoryX.h.

212  {
213  return *pp;
214  }

◆ operator->()

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

Definition at line 216 of file MemoryX.h.

217  {
218  return pp;
219  }

◆ operator=() [1/2]

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

Definition at line 159 of file MemoryX.h.

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

◆ operator=() [2/2]

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

Definition at line 176 of file MemoryX.h.

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

◆ reset()

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

Definition at line 221 of file MemoryX.h.

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

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 252 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 253 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:193
Optional::address
X * address()
Definition: MemoryX.h:239
Optional::storage
char storage[sizeof(X)]
Definition: MemoryX.h:253
Optional::reset
void reset()
Definition: MemoryX.h:221
Optional::pp
X * pp
Definition: MemoryX.h:256
safenew
#define safenew
Definition: MemoryX.h:10