|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
-
-
- #pragma once
-
- #include <utility>
- #include <type_traits>
-
- namespace sr
- {
-
- template<class T, class TT>
- using is_ntmocp_constructible = std::conditional_t<std::is_reference<TT>::value || !std::is_nothrow_move_constructible<TT>::value,
- typename std::is_constructible<T, TT const &>::type,
- typename std::is_constructible<T, TT>::type>;
-
- template<class T, class TT>
- constexpr auto is_nothrow_move_or_copy_constructible_from_v = is_ntmocp_constructible<T, TT>::value;
-
-
- template<class R, class D>
- class unique_resource
- {
- public:
-
- template<class RR, class DD,
- std::enable_if_t<(!std::is_lvalue_reference<RR>::value)
- && std::is_nothrow_constructible<R, RR>::value, int> = 0,
- std::enable_if_t<(!std::is_lvalue_reference<DD>::value)
- && std::is_nothrow_constructible<D, DD>::value, int> = 0,
-
- std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
- && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
- std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
- std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
- >
- explicit unique_resource(RR&& r, DD& d) : m_resource(std::move(r)), m_deleter(std::move(d))
- {
- }
-
- template<class RR, class DD,
- std::enable_if_t<std::is_lvalue_reference<RR>::value || std::is_lvalue_reference<DD>::value, int> = 0,
-
- std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
- && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
- std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
- std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
- >
- explicit unique_resource(RR&& r, DD& d) try : m_resource(r), m_deleter(d)
- {
- }
- catch( ... )
- {
- d(r);
- throw;
- }
-
- template<class TR = R, class TD = D,
- std::enable_if_t<(std::is_nothrow_move_constructible<TR>::value
- && std::is_nothrow_move_constructible<TD>::value), int> = 0
- >
- unique_resource(unique_resource&& other) : m_resource(std::forward<R>(other.m_resource)),
- m_deleter(std::forward<D>(other.m_deleter)),
- m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
- {
- }
-
- template<class TR = R, class TD = D,
- std::enable_if_t<(!std::is_nothrow_move_constructible<TR>::value
- || !std::is_nothrow_move_constructible<TD>::value), int> = 0
- >
- unique_resource(unique_resource&& other) : m_resource(other.m_resource),
- m_deleter(other.m_deleter),
- m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
- {
- }
-
- unique_resource(const unique_resource&) = delete;
-
-
-
- unique_resource& operator=(unique_resource&& other);
- unique_resource& operator=(const unique_resource&) = delete;
-
-
- private:
-
- R m_resource;
- D m_deleter;
- bool m_execute_on_destruction;
- };
-
-
- template<class Resource, class Deleter>
- unique_resource<std::decay_t<Resource>, std::decay_t<Deleter>> make_unique_resource(Resource&& r, Deleter&& d)
- {
- return unique_resource<std::decay_t<Resource>, std::decay_t<Deleter>>{std::forward<Resource>(r), std::forward<Deleter>(d)};
- }
-
-
- }
-
-
|