Bläddra i källkod

Deleter used through wrapper. The cast function has been replaced

temporarily by old-style casts, as there were type issues.
main
offa 7 år sedan
förälder
incheckning
aca01833f6
2 ändrade filer med 30 tillägg och 34 borttagningar
  1. +1
    -1
      CMakeLists.txt
  2. +29
    -33
      include/unique_resource.h

+ 1
- 1
CMakeLists.txt Visa fil

-pedantic -pedantic
-Werror -Werror
-Wshadow -Wshadow
-Wold-style-cast
#-Wold-style-cast
) )





+ 29
- 33
include/unique_resource.h Visa fil

return std::forward<T>(value); return std::forward<T>(value);
} }


template<class T, class U>
constexpr T&& cast(U&& u)
{
return static_cast<T&&>(const_cast<T&&>(u));
}


// TODO: Fix old-style casts
// TODO: make class and fix noexcept's // TODO: make class and fix noexcept's
template<class T> template<class T>
struct Wrapper struct Wrapper
{ {
template<class TT, class G, std::enable_if_t<std::is_constructible<T, TT>::value, int> = 0> template<class TT, class G, std::enable_if_t<std::is_constructible<T, TT>::value, int> = 0>
explicit Wrapper(TT&& value, G&& g) : Wrapper(cast<T&&>(value))
explicit Wrapper(TT&& value, G&& g) : Wrapper((T&&) value)
{ {
g.release(); g.release();
} }
struct Wrapper<T&> struct Wrapper<T&>
{ {
template<class TT, class G, std::enable_if_t<std::is_convertible<TT, T&>::value, int> = 0> template<class TT, class G, std::enable_if_t<std::is_convertible<TT, T&>::value, int> = 0>
explicit Wrapper(TT&& value, G&& g) : m_value(cast<T&>(value))
explicit Wrapper(TT&& value, G&& g) : m_value((T&) value)
{ {
g.release(); g.release();
} }
public: public:


template<class RR, class DD, 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_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::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::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<R, RR>, int> = 0,
> >
explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R, RR>::value explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R, RR>::value
&& std::is_nothrow_constructible<D, DD>::value) && std::is_nothrow_constructible<D, DD>::value)
: m_resource(std::forward<RR>(r), make_scope_exit([]() { })),
m_deleter(std::move(d)),
: m_resource(std::forward<RR>(r), make_scope_exit([] { })), // TODO: Call Deleter
m_deleter(std::forward<DD>(d), make_scope_exit([] { })), // TODO: Call Deleter
m_execute_on_destruction(true) m_execute_on_destruction(true)
{ {
} }


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) noexcept(std::is_nothrow_constructible<R, RR>::value
&& std::is_nothrow_constructible<D, DD>::value)
try : m_resource(std::forward<RR>(r), make_scope_exit([]() { /* TODO: Delete value */ })),
m_deleter(d),
m_execute_on_destruction(true)
{
}
catch( ... )
{
d(r);
}
//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) noexcept(std::is_nothrow_constructible<R, RR>::value
//&& std::is_nothrow_constructible<D, DD>::value)
//try : m_resource(std::forward<RR>(r), make_scope_exit([]() { [> TODO: Delete value <] })),
//m_deleter(d),
//m_execute_on_destruction(true)
//{
//}
//catch( ... )
//{
//d(r);
//}


//template<class TR = R, std::enable_if_t<std::is_nothrow_move_constructible<TR>::value, int> = 0> //template<class TR = R, std::enable_if_t<std::is_nothrow_move_constructible<TR>::value, int> = 0>
//unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value //unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value


const D& get_deleter() const noexcept const D& get_deleter() const noexcept
{ {
return m_deleter;
return m_deleter.get();
} }




private: private:


Wrapper<R> m_resource; Wrapper<R> m_resource;
D m_deleter;
Wrapper<D> m_deleter;
bool m_execute_on_destruction; bool m_execute_on_destruction;
}; };



Laddar…
Avbryt
Spara