CatapultServer  v0.5.0.1 (Elephant)
MemoryUtils.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "catapult/exceptions.h"
23 #include <memory>
24 
25 namespace catapult { namespace utils {
26 
28  template<typename T>
29  std::unique_ptr<T> MakeUniqueWithSize(size_t size) {
30  if (size < sizeof(T))
31  CATAPULT_THROW_INVALID_ARGUMENT("size is insufficient");
32 
33  return std::unique_ptr<T>(reinterpret_cast<T*>(::operator new(size)));
34  }
35 
37  template<typename T>
38  std::shared_ptr<T> MakeSharedWithSize(size_t size) {
39  if (size < sizeof(T))
40  CATAPULT_THROW_INVALID_ARGUMENT("size is insufficient");
41 
42  return std::shared_ptr<T>(reinterpret_cast<T*>(::operator new(size)));
43  }
44 
46  template<typename T>
47  std::shared_ptr<T> UniqueToShared(std::unique_ptr<T>&& pointer) {
48  return std::move(pointer);
49  }
50 }}
catapult::utils::MakeUniqueWithSize
std::unique_ptr< T > MakeUniqueWithSize(size_t size)
Creates a unique pointer of the specified type with custom size.
Definition: MemoryUtils.h:29
exceptions.h
catapult::utils::UniqueToShared
std::shared_ptr< T > UniqueToShared(std::unique_ptr< T > &&pointer)
Converts a unique pointer to a shared pointer of the same type.
Definition: MemoryUtils.h:47
size
uint64_t size
Definition: MemoryCounters.cpp:65
CATAPULT_THROW_INVALID_ARGUMENT
#define CATAPULT_THROW_INVALID_ARGUMENT(MESSAGE)
Macro used to throw a catapult invalid argument.
Definition: exceptions.h:179
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::MakeSharedWithSize
std::shared_ptr< T > MakeSharedWithSize(size_t size)
Creates a shared pointer of the specified type with custom size.
Definition: MemoryUtils.h:38