COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
comms::util::alloc::InPlaceSingle< TInterface, TAllTypes > Class Template Reference

#include "alloc.h"

Detailed Description

template<typename TInterface, typename TAllTypes>
class comms::util::alloc::InPlaceSingle< TInterface, TAllTypes >

In-place single object allocator.

May allocate only single object at a time. In order to be able to allocate new object, previous one must be destructed first. The allocator contains uninitialised storage area in its private data, which is used to contain allocated object.

Template Parameters
TInterfaceCommon interface class for all objects being allocated with this allocator.
TAllTypesAll the possible types that can be allocated with this allocator bundled in std::tuple. They are used to identify the size required to allocate any of the provided objects.

Public Types

using Ptr = std::unique_ptr< TInterface, details::InPlaceDeleter< TInterface > >
 Smart pointer (std::unique_ptr) to the allocated object.
 

Public Member Functions

 ~InPlaceSingle ()
 Destructor.
 
template<typename TObj , typename... TArgs>
Ptr alloc (TArgs &&... args)
 Allocation function.
 
const void * allocAddr () const
 Get address of the objects being allocated using this allocator.
 
bool allocated () const
 Inquire whether the object is already allocated.
 
bool canAllocate () const
 Inquiry whether allocation is possible.
 
template<typename TObj >
Ptr wrap (TObj *obj)
 Function used to wrap raw pointer into a smart one.
 

Member Typedef Documentation

◆ Ptr

template<typename TInterface , typename TAllTypes >
using comms::util::alloc::InPlaceSingle< TInterface, TAllTypes >::Ptr = std::unique_ptr<TInterface, details::InPlaceDeleter<TInterface> >

Smart pointer (std::unique_ptr) to the allocated object.

The custom deleter makes sure the destructor of the allocated object is called.

Member Function Documentation

◆ alloc()

template<typename TInterface , typename TAllTypes >
template<typename TObj , typename... TArgs>
Ptr comms::util::alloc::InPlaceSingle< TInterface, TAllTypes >::alloc ( TArgs &&...  args)

Allocation function.

Template Parameters
TObjType of the object being allocated, expected to be the same as or derived from TInterface.
TArgstypes of arguments to be passed to the constructor.
Parameters
[in]argsExtra arguments to be passed to allocated object's constructor.
Returns
Smart pointer to the allocated object.
Precondition
If TObj is NOT the same as TInterface, i.e. TInterface is a base class of TObj, then TInterface must have virtual destructor.

◆ wrap()

template<typename TInterface , typename TAllTypes >
template<typename TObj >
Ptr comms::util::alloc::InPlaceSingle< TInterface, TAllTypes >::wrap ( TObj *  obj)

Function used to wrap raw pointer into a smart one.

Template Parameters
Typeof the object, expected to be the same as or derived from TInterface.
Parameters
[in]objPointer to previously allocated object.
Returns
Smart pointer to the wrapped object.

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