iceoryx_hoofs 2.0.5
Loading...
Searching...
No Matches
Public Member Functions | List of all members
iox::cxx::PoorMansHeap< Interface, TypeSize, TypeAlignment > Class Template Reference

Reserves space on stack for placement new instatiation. More...

#include <iceoryx_hoofs/cxx/poor_mans_heap.hpp>

Public Member Functions

template<typename Type , typename... CTorArgs>
 PoorMansHeap (PoorMansHeapType< Type >, CTorArgs &&... ctorArgs) noexcept
 
 PoorMansHeap (PoorMansHeap &&other)=delete
 
PoorMansHeapoperator= (PoorMansHeap &&rhs)=delete
 
 PoorMansHeap (const PoorMansHeap &)=delete
 
PoorMansHeapoperator= (const PoorMansHeap &)=delete
 
template<typename Type , typename... CTorArgs>
void newInstance (CTorArgs &&... ctorArgs) noexcept
 
void deleteInstance () noexcept
 Calls the destructor if there is a valid instance, otherwise nothing happens.
 
bool hasInstance () const noexcept
 
Interface * operator-> () const noexcept
 
Interface & operator* () const noexcept
 

Detailed Description

template<typename Interface, size_t TypeSize, size_t TypeAlignment = 8>
class iox::cxx::PoorMansHeap< Interface, TypeSize, TypeAlignment >

Reserves space on stack for placement new instatiation.

Parameters
Interfacebase type of all classes which should be stored in here
TypeSizemaximum size of a child of Interface
TypeAlignmentalignment which is required for the types
#include "iceoryx_hoofs/cxx/poor_mans_heap.hpp"
#include "iceoryx_hoofs/cxx/helplets.hpp"
#include <iostream>
class Base
{
public:
virtual ~Base() = default;
virtual void doStuff() = 0;
};
class Foo : public Base
{
public:
Foo(int stuff)
: m_stuff(stuff)
{
}
void doStuff() override
{
std::cout << __PRETTY_FUNCTION__ << ": " << m_stuff << std::endl;
}
private:
int m_stuff;
};
class Bar : public Base
{
public:
void doStuff() override
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
int main()
{
constexpr auto MaxSize = cxx::maxSize<Foo, Bar>();
constexpr auto MaxAlignment = cxx::maxAlignment<Foo, Bar>();
using FooBar = cxx::PoorMansHeap<Base, MaxSize, MaxAlignment>;
FooBar fooBar1{cxx::PoorMansHeapType<Foo>(), 42};
fooBar1->doStuff();
fooBar1.newInstance<Bar>();
fooBar1->doStuff();
fooBar1.newInstance<Foo>(13);
fooBar1->doStuff();
FooBar fooBar2;
if (!fooBar2.hasInstance())
{
std::cout << "There is no instance!" << std::endl;
}
fooBar2.newInstance<Bar>();
fooBar2->doStuff();
fooBar2.deleteInstance();
if (!fooBar2.hasInstance())
{
std::cout << "There is again no instance!" << std::endl;
}
return 0;
}

Constructor & Destructor Documentation

◆ PoorMansHeap()

template<typename Interface , size_t TypeSize, size_t TypeAlignment = 8>
template<typename Type , typename... CTorArgs>
iox::cxx::PoorMansHeap< Interface, TypeSize, TypeAlignment >::PoorMansHeap ( PoorMansHeapType< Type >  ,
CTorArgs &&...  ctorArgs 
)
noexcept

Constructor for immediate construction of an instance

Parameters
[in]Typethe type to instantiate, wrapped in PoorMansHeapType
[in]ctorArgsctor arguments for the type to instantiate

Member Function Documentation

◆ hasInstance()

template<typename Interface , size_t TypeSize, size_t TypeAlignment = 8>
bool iox::cxx::PoorMansHeap< Interface, TypeSize, TypeAlignment >::hasInstance ( ) const
noexcept

Checks is there is a valid instance

Returns
true if there is a valid instance

◆ newInstance()

template<typename Interface , size_t TypeSize, size_t TypeAlignment = 8>
template<typename Type , typename... CTorArgs>
void iox::cxx::PoorMansHeap< Interface, TypeSize, TypeAlignment >::newInstance ( CTorArgs &&...  ctorArgs)
noexcept

Create a new instance of the Type

Parameters
[in]Typethe type to instantiate, wrapped in PoorMansHeapType
[in]ctorArgsctor arguments for the type to instantiate

◆ operator*()

template<typename Interface , size_t TypeSize, size_t TypeAlignment = 8>
Interface & iox::cxx::PoorMansHeap< Interface, TypeSize, TypeAlignment >::operator* ( ) const
noexcept

Returns a reference to the underlying instance. If there is no valid instance, the behaviour is undefined

Returns
reference to the underlying instance

◆ operator->()

template<typename Interface , size_t TypeSize, size_t TypeAlignment = 8>
Interface * iox::cxx::PoorMansHeap< Interface, TypeSize, TypeAlignment >::operator-> ( ) const
noexcept

Returns a pointer to the underlying instance

Returns
pointer to the underlying instance or nullptr if there is no valid instance

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