15 #include <type_traits>
61 template<
typename T,
typename ...ArgTypes>
62 T*
New(
const char* allocationTag, ArgTypes&&... args)
64 void *rawMemory =
Malloc(allocationTag,
sizeof(T));
66 T *constructedMemory =
new (rawMemory) T(std::forward<ArgTypes>(args)...);
67 return constructedMemory;
77 #if defined(_MSC_VER) && !defined(_CPPRTTI)
81 if (pointerToT ==
nullptr)
94 typename std::enable_if<!std::is_polymorphic<T>::value>::type
Delete(T* pointerToT)
96 if (pointerToT ==
nullptr)
106 typename std::enable_if<std::is_polymorphic<T>::value>::type
Delete(T* pointerToT)
108 if (pointerToT ==
nullptr)
117 void* mostDerivedT =
dynamic_cast<void*
>(pointerToT);
126 return std::is_class<T>::value;
132 return !std::is_trivially_destructible<T>::value;
140 T*
NewArray(std::size_t amount,
const char* allocationTag)
144 bool constructMembers = ShouldConstructArrayMembers<T>();
145 bool trackMemberCount = ShouldDestroyArrayMembers<T>();
148 std::size_t allocationSize = amount *
sizeof(T);
149 #if defined(_MSC_VER) && _MSC_VER < 1900
150 std::size_t headerSize = (std::max)(
sizeof(std::size_t), __alignof(T));
152 std::size_t headerSize = (std::max)(
sizeof(std::size_t),
alignof(T));
155 if (trackMemberCount)
157 allocationSize += headerSize;
160 void* rawMemory =
Malloc(allocationTag, allocationSize);
161 T* pointerToT =
nullptr;
163 if (trackMemberCount)
165 std::size_t* pointerToAmount =
reinterpret_cast<std::size_t*
>(rawMemory);
166 *pointerToAmount = amount;
167 pointerToT =
reinterpret_cast<T*
>(
reinterpret_cast<char*
>(pointerToAmount) + headerSize);
172 pointerToT =
reinterpret_cast<T*
>(rawMemory);
175 if (constructMembers)
177 for (std::size_t i = 0; i < amount; ++i)
179 new (pointerToT + i) T;
196 if (pointerToTArray ==
nullptr)
201 bool destroyMembers = ShouldDestroyArrayMembers<T>();
202 void* rawMemory =
nullptr;
206 #if defined(_MSC_VER) && _MSC_VER < 1900
207 std::size_t headerSize = (std::max)(
sizeof(std::size_t), __alignof(T));
209 std::size_t headerSize = (std::max)(
sizeof(std::size_t),
alignof(T));
212 std::size_t *pointerToAmount =
reinterpret_cast<std::size_t*
>(
reinterpret_cast<char*
>(pointerToTArray) - headerSize);
213 std::size_t amount = *pointerToAmount;
215 for (std::size_t i = amount; i > 0; --i)
217 (pointerToTArray + i - 1)->~T();
219 rawMemory =
reinterpret_cast<void *
>(pointerToAmount);
223 rawMemory =
reinterpret_cast<void *
>(pointerToTArray);
237 template<class U, class = typename std::enable_if<std::is_convertible<U *, T *>::value,
void>::type>
244 static_assert(0 <
sizeof(T),
"can't delete an incomplete type");
249 template<
typename T >
using UniquePtr = std::unique_ptr< T, Deleter< T > >;
255 template<
typename T,
typename ...ArgTypes>
258 return UniquePtr<T>(Aws::New<T>(allocationTag, std::forward<ArgTypes>(args)...));
266 template<class U, class = typename std::enable_if<std::is_convertible<U *, T *>::value,
void>::type>
273 static_assert(0 <
sizeof(T),
"can't delete an incomplete type");
278 template<
typename T >
using UniqueArrayPtr = std::unique_ptr< T, ArrayDeleter< T > >;
284 template<
typename T,
typename ...ArgTypes>
287 return UniqueArrayPtr<T>(Aws::NewArray<T>(amount, allocationTag, std::forward<ArgTypes>(args)...));