postconstructor_invoker<T>
postconstructor_invoker<T>
const A1 &
postconstructor_invoker<T>
const A1 &
const A2 &
postconstructor_invoker<T>
const A1 &
const A2 &
...
const AN &
Create a shared_ptr
with support for post-constructors and pre-destructors.
Creates an object and its owning shared_ptr<T>
(wrapped inside a postconstructor_invoker)
using only a single allocation,
in a manner similar
to that of boost::make_shared(). In addition, deconstruct
supports postconstructors and predestructors. The returned
shared_ptr is wrapped inside a postconstructor_invoker
in order to provide the user with an opportunity to pass arguments to a postconstructor,
while insuring the postconstructor is run before the wrapped
shared_ptr is accessible.
In order to use deconstruct
you must define a postconstructor for your class.
More specifically, you must define
an adl_postconstruct
function which can be found via argument-dependent
lookup. Typically, this means defining an adl_postconstruct
function
in the same namespace as its associated class. See the reference for
postconstructor_invoker
for a specification of what arguments are passed to the adl_postconstruct
call.
Optionally, you may define a predestructor for your class. This is done by
defining an adl_predestruct
function which may be found
by argument-dependent lookup. The deleter of the shared_ptr
created by deconstruct
will make an unqualified call to
adl_predestruct
with a single
argument: a pointer to the object which is about to be deleted.
As a convenience, the pointer will always be cast to point to a non-const type
before being passed to adl_predestruct
.
If no user-defined adl_predestruct
function is found via
argument-dependent lookup, a default function (which does nothing) will
be used. After adl_predestruct
is called, the deleter
will delete the object with
checked_delete.
Any arguments passed to a
deconstruct()
call are forwarded to the matching constructor of the
template type
T
. Arguments may also be passed to the class' associated
adl_postconstruct
function by using the
postconstructor_invoker::postconstruct() methods.
If your compiler supports the C++11 features of rvalue references
and variadic templates, then deconstruct
will perform perfect
forwarding of arguments to the T
constructor, using
a prototype of:
template< typename T, typename... Args > postconstructor_invoker< T > deconstruct( Args && ... args );
Otherwise, argument forwarding is performed via const references, as specified in
the synopsis. In order to pass non-const references to a constructor, you will need
to wrap them in reference wrappers using boost::ref.
You may give all the deconstruct
overloads access to your class'
private and protected constructors by
declaring deconstruct_access a friend. Using private
constructors in conjunction with deconstruct_access
can be useful to
ensure your objects are only created by deconstruct
, and thus
their postconstructors or predestructors will always be called.
A postconstructor_invoker<T>
owning a newly allocated object of
type T
.
Gives deconstruct access to private/protected constructors.
Declaring deconstruct_access
a friend to your class will give the
deconstruct factory function access to your class' private and
protected constructors. Using private
constructors in conjunction with deconstruct_access
can be useful to
ensure postconstructible or predestructible
objects are always created
properly using deconstruct
.
const shared_ptr<T> &
The conversion operator has the same effect as explicitly calling
the postconstruct method with no arguments.
const shared_ptr<T> &
const shared_ptr<T> &
A1
const shared_ptr<T> &
A1
A1
const shared_ptr<T> &
A1
A1
...
A1
The postconstruct
methods make an unqualified call to
adl_postconstruct()
and then return the shared_ptr
which was wrapped inside the postconstructor_invoker
object by deconstruct().
The first two arguments passed to the
adl_postconstruct()
call are always the shared_ptr
owning the object created by deconstruct(),
followed by a ordinary pointer to the same object. As a convenience,
the ordinary pointer
will always be cast to point to a non-const type before being passed
to adl_postconstruct
. The remaining arguments passed to
adl_postconstruct
are whatever arguments the user may have
passed to the postconstruct
method.
Pass arguments to and run postconstructors for objects created with deconstruct().
Objects of type postconstructor_invoker
are returned by calls to the
deconstruct() factory function. These objects are intended
to either be immediately assigned to a shared_ptr (in which case the
class' conversion operator will perform the conversion by calling the
postconstruct with no arguments), or to be converted
to shared_ptr explicitly by the user calling one of
the postconstruct methods.