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