| 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 | |
| 9 | #ifndef BOOST_MULTI_INDEX_DETAIL_ANY_CONTAINER_VIEW_HPP |
| 10 | #define BOOST_MULTI_INDEX_DETAIL_ANY_CONTAINER_VIEW_HPP |
| 11 | |
| 12 | #if defined(_MSC_VER) |
| 13 | #pragma once |
| 14 | #endif |
| 15 | |
| 16 | namespace boost{ |
| 17 | |
| 18 | namespace multi_index{ |
| 19 | |
| 20 | namespace detail{ |
| 21 | |
| 22 | /* type-erased, non-owning view over a ConstIterator-container's range */ |
| 23 | |
| 24 | template<typename ConstIterator> |
| 25 | class any_container_view |
| 26 | { |
| 27 | public: |
| 28 | template<typename Container> |
| 29 | any_container_view(const Container& x):px(&x),pt(vtable_for<Container>()){} |
| 30 | |
| 31 | const void* container()const{return px;} |
| 32 | ConstIterator begin()const{return pt->begin(px);} |
| 33 | ConstIterator end()const{return pt->end(px);} |
| 34 | |
| 35 | private: |
| 36 | struct vtable |
| 37 | { |
| 38 | ConstIterator (*begin)(const void*); |
| 39 | ConstIterator (*end)(const void*); |
| 40 | }; |
| 41 | |
| 42 | template<typename Container> |
| 43 | static ConstIterator begin_for(const void* px) |
| 44 | { |
| 45 | return static_cast<const Container*>(px)->begin(); |
| 46 | } |
| 47 | |
| 48 | template<typename Container> |
| 49 | static ConstIterator end_for(const void* px) |
| 50 | { |
| 51 | return static_cast<const Container*>(px)->end(); |
| 52 | } |
| 53 | |
| 54 | template<typename Container> |
| 55 | vtable* vtable_for() |
| 56 | { |
| 57 | static vtable v= |
| 58 | { |
| 59 | &begin_for<Container>, |
| 60 | &end_for<Container> |
| 61 | }; |
| 62 | |
| 63 | return &v; |
| 64 | } |
| 65 | |
| 66 | const void* px; |
| 67 | vtable* pt; |
| 68 | }; |
| 69 | |
| 70 | } /* namespace multi_index::detail */ |
| 71 | |
| 72 | } /* namespace multi_index */ |
| 73 | |
| 74 | } /* namespace boost */ |
| 75 | |
| 76 | #endif |
| 77 | |