| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- /***************************************************************************
- * Copyright (c) Sylvain Corlay and Johan Mabille and Wolf Vollprecht *
- * Copyright (c) QuantStack *
- * *
- * Distributed under the terms of the BSD 3-Clause License. *
- * *
- * The full license is in the file LICENSE, distributed with this software. *
- ****************************************************************************/
- #ifndef XTL_CLOSURE_HPP
- #define XTL_CLOSURE_HPP
- #include <memory>
- #include <type_traits>
- #include <utility>
- #include "xtl_config.hpp"
- namespace xtl
- {
- #ifdef __cpp_lib_as_const
- using std::as_const;
- #else
- template <class T>
- constexpr std::add_const_t<T>& as_const(T& t) noexcept
- {
- return t;
- }
- template <class T>
- constexpr std::add_const_t<T&&>& as_const(T&& t) noexcept = delete;
- #endif
- /****************
- * closure_type *
- ****************/
- template <class S>
- struct closure_type
- {
- using underlying_type = std::conditional_t<std::is_const<std::remove_reference_t<S>>::value,
- const std::decay_t<S>,
- std::decay_t<S>>;
- using type = typename std::conditional<std::is_lvalue_reference<S>::value,
- underlying_type&,
- underlying_type>::type;
- };
- template <class S>
- using closure_type_t = typename closure_type<S>::type;
- template <class S>
- struct const_closure_type
- {
- using underlying_type = std::decay_t<S>;
- using type = typename std::conditional<std::is_lvalue_reference<S>::value,
- std::add_const_t<underlying_type>&,
- underlying_type>::type;
- };
- template <class S>
- using const_closure_type_t = typename const_closure_type<S>::type;
- /********************
- * ptr_closure_type *
- ********************/
- template <class S>
- struct ptr_closure_type
- {
- using underlying_type = std::conditional_t<std::is_const<std::remove_reference_t<S>>::value,
- const std::decay_t<S>,
- std::decay_t<S>>;
- using type = std::conditional_t<std::is_lvalue_reference<S>::value,
- underlying_type*,
- underlying_type>;
- };
- template <class S>
- using ptr_closure_type_t = typename ptr_closure_type<S>::type;
- template <class S>
- struct const_ptr_closure_type
- {
- using underlying_type = const std::decay_t<S>;
- using type = std::conditional_t<std::is_lvalue_reference<S>::value,
- underlying_type*,
- underlying_type>;
- };
- template <class S>
- using const_ptr_closure_type_t = typename const_ptr_closure_type<S>::type;
- /********************
- * xclosure_wrapper *
- ********************/
- template <class CT>
- class xclosure_wrapper
- {
- public:
- using self_type = xclosure_wrapper<CT>;
- using closure_type = CT;
- using const_closure_type = std::add_const_t<CT>;
- using value_type = std::decay_t<CT>;
- using reference = std::conditional_t<
- std::is_const<std::remove_reference_t<CT>>::value,
- const value_type&, value_type&
- >;
- using pointer = std::conditional_t<
- std::is_const<std::remove_reference_t<CT>>::value,
- const value_type*, value_type*
- >;
- xclosure_wrapper(value_type&& e);
- xclosure_wrapper(reference e);
- xclosure_wrapper(const self_type& rhs) = default;
- xclosure_wrapper(self_type&& rhs) = default;
- self_type& operator=(const self_type& rhs);
- self_type& operator=(self_type&& rhs);
-
- template <class T>
- self_type& operator=(T&&);
- operator closure_type() noexcept;
- operator const_closure_type() const noexcept;
- std::add_lvalue_reference_t<closure_type> get() & noexcept;
- std::add_lvalue_reference_t<std::add_const_t<closure_type>> get() const & noexcept;
- closure_type get() && noexcept;
- pointer operator&() noexcept;
- bool equal(const self_type& rhs) const;
- void swap(self_type& rhs);
- private:
- using storing_type = ptr_closure_type_t<CT>;
- storing_type m_wrappee;
- template <class T>
- std::enable_if_t<std::is_lvalue_reference<CT>::value, std::add_lvalue_reference_t<std::remove_pointer_t<T>>>
- deref(T val) const;
- template <class T>
- std::enable_if_t<!std::is_lvalue_reference<CT>::value, std::add_lvalue_reference_t<T>>
- deref(T& val) const;
- template <class T>
- std::enable_if_t<std::is_lvalue_reference<CT>::value, T>
- get_pointer(T val) const;
- template <class T>
- std::enable_if_t<!std::is_lvalue_reference<CT>::value, std::add_pointer_t<T>>
- get_pointer(T& val) const;
- template <class T, class CTA>
- std::enable_if_t<std::is_lvalue_reference<CT>::value, T>
- get_storage_init(CTA&& e) const;
- template <class T, class CTA>
- std::enable_if_t<!std::is_lvalue_reference<CT>::value, T>
- get_storage_init(CTA&& e) const;
- };
- // TODO: remove this (backward compatibility)
- template <class CT>
- using closure_wrapper = xclosure_wrapper<CT>;
- /********************
- * xclosure_pointer *
- ********************/
- template <class CT>
- class xclosure_pointer
- {
- public:
- using self_type = xclosure_pointer<CT>;
- using closure_type = CT;
- using value_type = std::decay_t<CT>;
- using reference = std::conditional_t<
- std::is_const<std::remove_reference_t<CT>>::value,
- const value_type&, value_type&
- >;
- using const_reference = const value_type&;
- using pointer = std::conditional_t<
- std::is_const<std::remove_reference_t<CT>>::value,
- const value_type*, value_type*
- >;
- xclosure_pointer(value_type&& e);
- xclosure_pointer(reference e);
- reference operator*() noexcept;
- const_reference operator*() const noexcept;
- pointer operator->() const noexcept;
- private:
- using storing_type = closure_type_t<CT>;
- storing_type m_wrappee;
- };
- /***********************************
- * xclosure_wrapper implementation *
- ***********************************/
- template <class CT>
- inline xclosure_wrapper<CT>::xclosure_wrapper(value_type&& e)
- : m_wrappee(get_storage_init<storing_type>(std::move(e)))
- {
- }
- template <class CT>
- inline xclosure_wrapper<CT>::xclosure_wrapper(reference e)
- : m_wrappee(get_storage_init<storing_type>(e))
- {
- }
- template <class CT>
- inline auto xclosure_wrapper<CT>::operator=(const self_type& rhs) -> self_type&
- {
- deref(m_wrappee) = deref(rhs.m_wrappee);
- return *this;
- }
- template <class CT>
- inline auto xclosure_wrapper<CT>::operator=(self_type&& rhs) -> self_type&
- {
- swap(rhs);
- return *this;
- }
- template <class CT>
- template <class T>
- inline auto xclosure_wrapper<CT>::operator=(T&& t) -> self_type&
- {
- deref(m_wrappee) = std::forward<T>(t);
- return *this;
- }
- template <class CT>
- inline xclosure_wrapper<CT>::operator typename xclosure_wrapper<CT>::closure_type() noexcept
- {
- return deref(m_wrappee);
- }
- template <class CT>
- inline xclosure_wrapper<CT>::operator typename xclosure_wrapper<CT>::const_closure_type() const noexcept
- {
- return deref(m_wrappee);
- }
- template <class CT>
- inline auto xclosure_wrapper<CT>::get() & noexcept -> std::add_lvalue_reference_t<closure_type>
- {
- return deref(m_wrappee);
- }
- template <class CT>
- inline auto xclosure_wrapper<CT>::get() const & noexcept -> std::add_lvalue_reference_t<std::add_const_t<closure_type>>
- {
- return deref(m_wrappee);
- }
- template <class CT>
- inline auto xclosure_wrapper<CT>::get() && noexcept -> closure_type
- {
- return deref(m_wrappee);
- }
- template <class CT>
- inline auto xclosure_wrapper<CT>::operator&() noexcept -> pointer
- {
- return get_pointer(m_wrappee);
- }
- template <class CT>
- template <class T>
- inline std::enable_if_t<std::is_lvalue_reference<CT>::value, std::add_lvalue_reference_t<std::remove_pointer_t<T>>>
- xclosure_wrapper<CT>::deref(T val) const
- {
- return *val;
- }
- template <class CT>
- template <class T>
- inline std::enable_if_t<!std::is_lvalue_reference<CT>::value, std::add_lvalue_reference_t<T>>
- xclosure_wrapper<CT>::deref(T& val) const
- {
- return val;
- }
- template <class CT>
- template <class T>
- inline std::enable_if_t<std::is_lvalue_reference<CT>::value, T>
- xclosure_wrapper<CT>::get_pointer(T val) const
- {
- return val;
- }
- template <class CT>
- template <class T>
- inline std::enable_if_t<!std::is_lvalue_reference<CT>::value, std::add_pointer_t<T>>
- xclosure_wrapper<CT>::get_pointer(T& val) const
- {
- return &val;
- }
- template <class CT>
- template <class T, class CTA>
- inline std::enable_if_t<std::is_lvalue_reference<CT>::value, T>
- xclosure_wrapper<CT>::get_storage_init(CTA&& e) const
- {
- return &e;
- }
- template <class CT>
- template <class T, class CTA>
- inline std::enable_if_t<!std::is_lvalue_reference<CT>::value, T>
- xclosure_wrapper<CT>::get_storage_init(CTA&& e) const
- {
- return e;
- }
- template <class CT>
- inline bool xclosure_wrapper<CT>::equal(const self_type& rhs) const
- {
- return deref(m_wrappee) == rhs.deref(rhs.m_wrappee);
- }
- template <class CT>
- inline void xclosure_wrapper<CT>::swap(self_type& rhs)
- {
- using std::swap;
- swap(deref(m_wrappee), deref(rhs.m_wrappee));
- }
- template <class CT>
- inline bool operator==(const xclosure_wrapper<CT>& lhs, const xclosure_wrapper<CT>& rhs)
- {
- return lhs.equal(rhs);
- }
- template <class CT>
- inline bool operator!=(const xclosure_wrapper<CT>& lhs, const xclosure_wrapper<CT>& rhs)
- {
- return !(lhs == rhs);
- }
- template <class CT>
- inline void swap(xclosure_wrapper<CT>& lhs, xclosure_wrapper<CT>& rhs)
- {
- lhs.swap(rhs);
- }
- /***********************************
- * xclosure_pointer implementation *
- ***********************************/
- template <class CT>
- inline xclosure_pointer<CT>::xclosure_pointer(value_type&& e)
- : m_wrappee(std::move(e))
- {
- }
- template <class CT>
- inline xclosure_pointer<CT>::xclosure_pointer(reference e)
- : m_wrappee(e)
- {
- }
- template <class CT>
- inline auto xclosure_pointer<CT>::operator*() noexcept -> reference
- {
- return m_wrappee;
- }
- template <class CT>
- inline auto xclosure_pointer<CT>::operator*() const noexcept -> const_reference
- {
- return m_wrappee;
- }
- template <class CT>
- inline auto xclosure_pointer<CT>::operator->() const noexcept -> pointer
- {
- return const_cast<pointer>(std::addressof(m_wrappee));
- }
- /*****************************
- * closure and const_closure *
- *****************************/
- template <class T>
- inline decltype(auto) closure(T&& t)
- {
- return xclosure_wrapper<closure_type_t<T>>(std::forward<T>(t));
- }
- template <class T>
- inline decltype(auto) const_closure(T&& t)
- {
- return xclosure_wrapper<const_closure_type_t<T>>(std::forward<T>(t));
- }
- /*********************************************
- * closure_pointer and const_closure_pointer *
- *********************************************/
- template <class T>
- inline auto closure_pointer(T&& t)
- {
- return xclosure_pointer<closure_type_t<T>>(std::forward<T>(t));
- }
- template <class T>
- inline auto const_closure_pointer(T&& t)
- {
- return xclosure_pointer<const_closure_type_t<T>>(std::forward<T>(t));
- }
- }
- #endif
|