Skip to content

iox::cxx::PoorMansHeap🔗

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

#include <iceoryx_hoofs/cxx/poor_mans_heap.hpp>

Public Functions🔗

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

Detailed Description🔗

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

Reserves space on stack for placement new instatiation.

Parameters:

  • Interface base type of all classes which should be stored in here
  • TypeSize maximum size of a child of Interface
  • TypeAlignment alignment 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;
}

Public Functions Documentation🔗

function PoorMansHeap🔗

PoorMansHeap() =default

function ~PoorMansHeap🔗

~PoorMansHeap()

function PoorMansHeap🔗

template <typename Type ,
typename... CTorArgs>
PoorMansHeap(
    PoorMansHeapType< Type > ,
    CTorArgs &&... ctorArgs
)

Parameters:

  • Type the type to instantiate, wrapped in PoorMansHeapType
  • ctorArgs ctor arguments for the type to instantiate

Constructor for immediate construction of an instance

function PoorMansHeap🔗

PoorMansHeap(
    PoorMansHeap && other
)

function operator=🔗

PoorMansHeap & operator=(
    PoorMansHeap && rhs
)

function PoorMansHeap🔗

PoorMansHeap(
    const PoorMansHeap & 
)

function operator=🔗

PoorMansHeap & operator=(
    const PoorMansHeap & 
)

function newInstance🔗

template <typename Type ,
typename... CTorArgs>
void newInstance(
    CTorArgs &&... ctorArgs
)

Parameters:

  • Type the type to instantiate, wrapped in PoorMansHeapType
  • ctorArgs ctor arguments for the type to instantiate

Create a new instance of the Type

function deleteInstance🔗

void deleteInstance()

Calls the destructor if there is a valid instance, otherwise nothing happens.

function hasInstance🔗

bool hasInstance() const

Return: true if there is a valid instance

Checks is there is a valid instance

function operator->🔗

Interface * operator->() const

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

Returns a pointer to the underlying instance

function operator*🔗

Interface & operator*() const

Return: reference to the underlying instance

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


Updated on 31 May 2022 at 11:34:55 CEST