Nugget
Public Types | Public Member Functions | Protected Attributes | List of all members
eastl::late_constructed< T, autoConstruct, autoDestruct > Class Template Reference

#include <memory.h>

Public Types

using this_type = late_constructed< T, autoConstruct, autoDestruct >
 
using value_type = T
 
using storage_type = eastl::aligned_storage_t< sizeof(value_type), eastl::alignment_of_v< value_type > >
 

Public Member Functions

template<typename... Args>
void construct (Args &&... args)
 
bool is_constructed () const EA_NOEXCEPT
 
void destruct ()
 
value_type & operator* () EA_NOEXCEPT
 
const value_type & operator* () const EA_NOEXCEPT
 
value_type * operator-> () EA_NOEXCEPT
 
const value_type * operator-> () const EA_NOEXCEPT
 
value_type * get () EA_NOEXCEPT
 
const value_type * get () const EA_NOEXCEPT
 

Protected Attributes

storage_type mStorage
 
value_type * mpValue
 

Detailed Description

template<typename T, bool autoConstruct = true, bool autoDestruct = true>
class eastl::late_constructed< T, autoConstruct, autoDestruct >

late_constructed

Implements a smart pointer type which separates the memory allocation of an object from the object's construction. The primary use case is to declare a global variable of the late_construction type, which allows the memory to be global but the constructor executes at some point after main() begins as opposed to before main, which is often dangerous for non-trivial types.

The autoConstruct template parameter controls whether the object is automatically default constructed upon first reference or must be manually constructed upon the first use of operator * or ->. autoConstruct is convenient but it causes * and -> to be slightly slower and may result in construction at an inconvenient time.

The autoDestruct template parameter controls whether the object, if constructed, is automatically destructed when ~late_constructed() is called or must be manually destructed via a call to destruct().

While construction can be automatic or manual, automatic destruction support is always present. Thus you aren't required in any case to manually call destruct. However, you may safely manually destruct the object at any time before the late_constructed destructor is executed.

You may still use late_constructed after calling destruct(), including calling construct() again to reconstruct the instance. destruct returns the late_constructed instance to a state equivalent to before construct was called.

Caveat: While late_constructed instances can be declared in global scope and initialize prior to main() executing, you cannot otherwise use such globally declared instances prior to main with guaranteed behavior unless you can ensure that the late_constructed instance is itself constructed prior to your use of it.

Example usage (demonstrating manual-construction): late_constructed<Widget, false> gWidget;

void main(){ gWidget.construct(kScrollbarType, kVertical, "MyScrollbar"); gWidget->SetValue(15); gWidget.destruct(); }

Example usage (demonstrating auto-construction): late_constructed<Widget, true> gWidget;

void main(){ gWidget->SetValue(15); // You may want to call destruct here, but aren't required to do so unless the Widget type requires it. }


The documentation for this class was generated from the following file: