1 #ifndef GRAIL_POOL_ALLOCATOR_H
2 #define GRAIL_POOL_ALLOCATOR_H
4 #include "../GrailCore/consts.h"
17 Emplacer(
typename std::enable_if<std::is_trivially_destructible<T>::value>::type* = 0)
21 template <
typename... Arguments>
22 T& operator()(
void* memoryCell, Arguments&&... arguments)
24 return *(
new(memoryCell) T{std::forward<Arguments>(arguments)...});
32 template <
typename... Arguments>
33 std::string& operator()(
void* memoryCell, Arguments&&... arguments)
35 if(StringLength(std::forward<Arguments>(arguments)...) > consts::MAX_STRING_SIZE)
37 throw std::bad_alloc{};
39 return *(
new(memoryCell) std::string{std::forward<Arguments>(arguments)...});
43 std::size_t StringLength(
const std::string&
string)
const
53 template <
typename... Arguments>
54 const std::string& operator()(
void* memoryCell, Arguments&&... arguments)
56 if(StringLength(std::forward<Arguments>(arguments)...) > consts::MAX_STRING_SIZE)
58 throw std::bad_alloc{};
60 return *(
new(memoryCell)
const std::string{std::forward<Arguments>(arguments)...});
64 std::size_t StringLength(
const std::string&
string)
const
81 : blocks{
new char[BYTES]},
82 currentMemoryCell{&blocks[0]},
93 template <
typename T,
typename... ConstructorArguments>
94 T& Emplace(ConstructorArguments&&... arguments)
96 std::size_t memoryRequired =
sizeof(T);
97 _size += memoryRequired;
101 throw std::bad_alloc{};
104 Emplacer<T> emplacer;
105 T& result = emplacer(currentMemoryCell, std::forward<ConstructorArguments>(arguments)...);
106 currentMemoryCell =
static_cast<char*
>(currentMemoryCell) + memoryRequired;
110 std::size_t Size()
const
117 currentMemoryCell = &blocks[0];
123 void* currentMemoryCell;
125 const std::size_t BYTES;
129 #endif // GRAIL_POOL_ALLOCATOR_H