ClickHouse/libs/libcommon/include/ext/shared_ptr_helper.hpp

45 lines
1.1 KiB
C++
Raw Normal View History

#pragma once
#include <memory>
namespace ext
{
/**
* Class AllocateShared allow to make std::shared_ptr<T> from T with private constructor.
2016-10-23 06:58:26 +00:00
* Derive your T class from shared_ptr_helper<T>, define him as friend and call allocate_shared()/make_shared() method.
**/
template <class T>
class shared_ptr_helper
{
protected:
2016-10-23 06:58:26 +00:00
typedef typename std::remove_const<T>::type TNoConst;
template <class TAlloc>
struct Deleter
{
2016-10-23 06:58:26 +00:00
void operator()(typename TAlloc::value_type * ptr)
{
std::allocator_traits<TAlloc>::destroy(alloc, ptr);
}
TAlloc alloc;
};
2016-10-23 06:58:26 +00:00
///see std::allocate_shared
template <class TAlloc, class ... TArgs>
static std::shared_ptr<T> allocate_shared(const TAlloc & alloc, TArgs && ... args)
{
TAlloc alloc_copy(alloc);
return std::shared_ptr<TNoConst>(new (std::allocator_traits<TAlloc>::allocate(alloc_copy, 1)) TNoConst(std::forward<TArgs>(args)...), Deleter<TAlloc>(), alloc_copy);
}
2016-10-23 06:58:26 +00:00
template <class ... TArgs>
static std::shared_ptr<T> make_shared(TArgs && ... args)
{
return allocate_shared(std::allocator<TNoConst>(), std::forward<TArgs>(args)...);
}
};
}