callable_wrapper_iterator.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright 2016 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. #ifndef BOOST_POLY_COLLECTION_DETAIL_CALLABLE_WRAPPER_ITERATOR_HPP
  9. #define BOOST_POLY_COLLECTION_DETAIL_CALLABLE_WRAPPER_ITERATOR_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/iterator/iterator_adaptor.hpp>
  14. #include <type_traits>
  15. namespace boost{
  16. namespace poly_collection{
  17. namespace detail{
  18. /* callable_wrapper<Sig>* adaptor convertible to pointer to wrapped entity */
  19. template<typename CWrapper>
  20. class callable_wrapper_iterator:public boost::iterator_adaptor<
  21. callable_wrapper_iterator<CWrapper>,CWrapper*
  22. >
  23. {
  24. public:
  25. callable_wrapper_iterator()=default;
  26. explicit callable_wrapper_iterator(CWrapper* p)noexcept:
  27. callable_wrapper_iterator::iterator_adaptor_{p}{}
  28. callable_wrapper_iterator(const callable_wrapper_iterator&)=default;
  29. callable_wrapper_iterator& operator=(
  30. const callable_wrapper_iterator&)=default;
  31. template<
  32. typename NonConstCWrapper,
  33. typename std::enable_if<
  34. std::is_same<CWrapper,const NonConstCWrapper>::value>::type* =nullptr
  35. >
  36. callable_wrapper_iterator(
  37. const callable_wrapper_iterator<NonConstCWrapper>& x)noexcept:
  38. callable_wrapper_iterator::iterator_adaptor_{x.base()}{}
  39. template<
  40. typename NonConstCWrapper,
  41. typename std::enable_if<
  42. std::is_same<CWrapper,const NonConstCWrapper>::value>::type* =nullptr
  43. >
  44. callable_wrapper_iterator& operator=(
  45. const callable_wrapper_iterator<NonConstCWrapper>& x)noexcept
  46. {
  47. this->base_reference()=x.base();
  48. return *this;
  49. }
  50. /* interoperability with CWrapper* */
  51. callable_wrapper_iterator& operator=(CWrapper* p)noexcept
  52. {this->base_reference()=p;return *this;}
  53. operator CWrapper*()const noexcept{return this->base();}
  54. /* interoperability with Callable* */
  55. template<
  56. typename Callable,
  57. typename std::enable_if<
  58. std::is_constructible<CWrapper,Callable&>::value&&
  59. (!std::is_const<CWrapper>::value||std::is_const<Callable>::value)
  60. >::type* =nullptr
  61. >
  62. explicit operator Callable*()const noexcept
  63. {
  64. return const_cast<Callable*>(
  65. static_cast<const Callable*>(
  66. const_cast<const void*>(
  67. this->base()->data())));
  68. }
  69. private:
  70. template<typename>
  71. friend class callable_wrapper_iterator;
  72. };
  73. } /* namespace poly_collection::detail */
  74. } /* namespace poly_collection */
  75. } /* namespace boost */
  76. #endif