Template Class PoorMansHeap

Class Documentation

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

Reserves space on stack for placement new instatiation.

#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;
}

Param Interface:

base type of all classes which should be stored in here

Param TypeSize:

maximum size of a child of Interface

Param TypeAlignment:

alignment which is required for the types

Public Functions

PoorMansHeap() = default
~PoorMansHeap() noexcept
template<typename Type, typename ...CTorArgs>
PoorMansHeap(PoorMansHeapType<Type>, CTorArgs&&... ctorArgs) noexcept

Constructor for immediate construction of an instance

Parameters:
  • Type[in] the type to instantiate, wrapped in PoorMansHeapType

  • ctorArgs[in] ctor arguments for the type to instantiate

PoorMansHeap(PoorMansHeap &&other) = delete
PoorMansHeap &operator=(PoorMansHeap &&rhs) = delete
PoorMansHeap(const PoorMansHeap&) = delete
PoorMansHeap &operator=(const PoorMansHeap&) = delete
template<typename Type, typename ...CTorArgs>
void newInstance(CTorArgs&&... ctorArgs) noexcept

Create a new instance of the Type

Parameters:
  • Type[in] the type to instantiate, wrapped in PoorMansHeapType

  • ctorArgs[in] ctor arguments for the type to instantiate

void deleteInstance() noexcept

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

bool hasInstance() const noexcept

Checks is there is a valid instance

Returns:

true if there is a valid instance

Interface *operator->() const noexcept

Returns a pointer to the underlying instance

Returns:

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

Interface &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