any_iterator.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_ANY_ITERATOR_HPP
  9. #define BOOST_POLY_COLLECTION_DETAIL_ANY_ITERATOR_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/iterator/iterator_adaptor.hpp>
  14. #include <boost/type_erasure/any_cast.hpp>
  15. #include <type_traits>
  16. #include <utility>
  17. namespace boost{
  18. namespace poly_collection{
  19. namespace detail{
  20. /* type_erasure::any<Concept>* adaptor convertible to pointer to wrapped
  21. * entity.
  22. */
  23. template<typename Any>
  24. class any_iterator:public boost::iterator_adaptor<any_iterator<Any>,Any*>
  25. {
  26. public:
  27. any_iterator()=default;
  28. explicit any_iterator(Any* p)noexcept:any_iterator::iterator_adaptor_{p}{}
  29. any_iterator(const any_iterator&)=default;
  30. any_iterator& operator=(const any_iterator&)=default;
  31. template<
  32. typename NonConstAny,
  33. typename std::enable_if<
  34. std::is_same<Any,const NonConstAny>::value>::type* =nullptr
  35. >
  36. any_iterator(const any_iterator<NonConstAny>& x)noexcept:
  37. any_iterator::iterator_adaptor_{x.base()}{}
  38. template<
  39. typename NonConstAny,
  40. typename std::enable_if<
  41. std::is_same<Any,const NonConstAny>::value>::type* =nullptr
  42. >
  43. any_iterator& operator=(const any_iterator<NonConstAny>& x)noexcept
  44. {
  45. this->base_reference()=x.base();
  46. return *this;
  47. }
  48. /* interoperability with Any* */
  49. any_iterator& operator=(Any* p)noexcept
  50. {this->base_reference()=p;return *this;}
  51. operator Any*()const noexcept{return this->base();}
  52. /* interoperability with Concrete* */
  53. template<
  54. typename Concrete,
  55. typename std::enable_if<
  56. /* can't compile-time check concept compliance */
  57. !std::is_const<Any>::value||std::is_const<Concrete>::value
  58. >::type* =nullptr
  59. >
  60. explicit operator Concrete*()const noexcept
  61. {
  62. return const_cast<Concrete*>(
  63. static_cast<typename std::remove_const<Concrete>::type*>(
  64. type_erasure::any_cast<void*>(this->base())));
  65. }
  66. private:
  67. template<typename>
  68. friend class any_iterator;
  69. };
  70. } /* namespace poly_collection::detail */
  71. } /* namespace poly_collection */
  72. } /* namespace boost */
  73. #endif