| 1 | #pragma once |
| 2 | |
| 3 | #include <memory> |
| 4 | |
| 5 | namespace ext |
| 6 | { |
| 7 | |
| 8 | /** Allows to make std::shared_ptr from T with protected constructor. |
| 9 | * |
| 10 | * Derive your T class from shared_ptr_helper<T> and add shared_ptr_helper<T> as a friend |
| 11 | * and you will have static 'create' method in your class. |
| 12 | */ |
| 13 | template <typename T> |
| 14 | struct shared_ptr_helper |
| 15 | { |
| 16 | template <typename... TArgs> |
| 17 | static std::shared_ptr<T> create(TArgs &&... args) |
| 18 | { |
| 19 | return std::shared_ptr<T>(new T(std::forward<TArgs>(args)...)); |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | template <typename T> |
| 25 | struct is_shared_ptr |
| 26 | { |
| 27 | static constexpr bool value = false; |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | template <typename T> |
| 32 | struct is_shared_ptr<std::shared_ptr<T>> |
| 33 | { |
| 34 | static constexpr bool value = true; |
| 35 | }; |
| 36 | |
| 37 | template <typename T> |
| 38 | inline constexpr bool is_shared_ptr_v = is_shared_ptr<T>::value; |
| 39 | } |
| 40 | |