Factory helper for creating objects with polymorphic allocators using proper RAII semantics.
Uses the cetl::pmr::PolymorphicDeleter type to ensure proper deallocation.
Example usage:
using MyAllocator = cetl::pmr::polymorphic_allocator<MyObject>;
MyAllocator alloc{cetl::pmr::get_default_resource()};
if (nullptr != object_0)
{
alloc.construct(object_0.get(), "object_0", 8U);
objects.
emplace(object_0->name(), std::move(object_0));
}
if (nullptr != object_1)
{
alloc.construct(object_1.get(), "object_1", 8U);
objects.
emplace(object_1->name(), std::move(object_1));
}
if (nullptr != object_2)
{
alloc.construct(object_2.get(), "object_2", 8U);
objects.
emplace(object_2->name(), std::move(object_2));
}
for (const auto& pair : objects)
{
}
cetl::pmr::polymorphic_allocator<MyObject> alloc{cetl::pmr::get_default_resource()};
auto object_0 = cetl::pmr::Factory::make_unique(alloc, "object_0", 8U);
objects.
emplace(object_0->name(), std::move(object_0));
auto object_1 = cetl::pmr::Factory::make_unique(alloc, "object_1", 8U);
objects.
emplace(object_1->name(), std::move(object_1));
auto object_2 = cetl::pmr::Factory::make_unique(alloc, "object_2", 8U);
objects.
emplace(object_2->name(), std::move(object_2));
auto emplacer = [&objects, &alloc](
const char* name,
std::size_t name_length) {
auto object = cetl::pmr::Factory::make_unique(alloc, name, name_length);
objects.
emplace(object->name(), std::move(
object));
};
emplacer("object_3", 8U);
emplacer("object_4", 8U);
emplacer("object_5", 8U);
for (const auto& pair : objects)
{
}
(See full example here...)