any_container_view.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright 2003-2021 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/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_ANY_CONTAINER_VIEW_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_ANY_CONTAINER_VIEW_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. namespace boost{
  14. namespace multi_index{
  15. namespace detail{
  16. /* type-erased, non-owning view over a ConstIterator-container's range */
  17. template<typename ConstIterator>
  18. class any_container_view
  19. {
  20. public:
  21. template<typename Container>
  22. any_container_view(const Container& x):px(&x),pt(vtable_for<Container>()){}
  23. const void* container()const{return px;}
  24. ConstIterator begin()const{return pt->begin(px);}
  25. ConstIterator end()const{return pt->end(px);}
  26. private:
  27. struct vtable
  28. {
  29. ConstIterator (*begin)(const void*);
  30. ConstIterator (*end)(const void*);
  31. };
  32. template<typename Container>
  33. static ConstIterator begin_for(const void* px)
  34. {
  35. return static_cast<const Container*>(px)->begin();
  36. }
  37. template<typename Container>
  38. static ConstIterator end_for(const void* px)
  39. {
  40. return static_cast<const Container*>(px)->end();
  41. }
  42. template<typename Container>
  43. vtable* vtable_for()
  44. {
  45. static vtable v=
  46. {
  47. &begin_for<Container>,
  48. &end_for<Container>
  49. };
  50. return &v;
  51. }
  52. const void* px;
  53. vtable* pt;
  54. };
  55. } /* namespace multi_index::detail */
  56. } /* namespace multi_index */
  57. } /* namespace boost */
  58. #endif