DNA Calib 1.1
Project brief
ManagedInstance.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "pma/PolyAllocator.h"
6
7#ifdef _MSC_VER
8 #pragma warning(push)
9 #pragma warning(disable : 4365 4987)
10#endif
11#include <functional>
12#include <memory>
13#include <utility>
14#ifdef _MSC_VER
15 #pragma warning(pop)
16#endif
17
18namespace pma {
19
20class MemoryResource;
21
22namespace impl {
23
24template<class TPointer, class TTarget, class TBase = TTarget>
26 public:
27 using PointerType = TPointer;
28
29 private:
30 explicit ManagedInstance(MemoryResource* memRes) : pMemRes{memRes} {
31 }
32
33 public:
35 return ManagedInstance{memRes};
36 }
37
38 template<typename ... Args>
39 PointerType create(Args&& ... args) {
41 auto deleter = [alloc](TBase* ptr) mutable {
42 alloc.deleteObject(static_cast<TTarget*>(ptr));
43 };
44 return {alloc.newObject(std::forward<Args>(args)...), deleter};
45 }
46
47 private:
49
50};
51
52} // namespace impl
53
54template<class TTarget, class TBase = TTarget>
55using UniqueInstance = impl::ManagedInstance<std::unique_ptr<TBase, std::function<void (TBase*)> >, TTarget, TBase>;
56
57template<class TTarget, class TBase = TTarget>
59
60} // namespace pma
MemoryResource is an abstract class that allows the implementation of polymorphic allocators.
Definition: MemoryResource.h:17
Definition: PolyAllocator.h:129
Definition: ManagedInstance.h:25
ManagedInstance(MemoryResource *memRes)
Definition: ManagedInstance.h:30
MemoryResource * pMemRes
Definition: ManagedInstance.h:48
static ManagedInstance with(MemoryResource *memRes)
Definition: ManagedInstance.h:34
PointerType create(Args &&... args)
Definition: ManagedInstance.h:39
TPointer PointerType
Definition: ManagedInstance.h:27
Definition: BinaryStreamReader.h:121