1 /* Copyright 2016-2017 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)
6 * See http://www.boost.org/libs/poly_collection for library home page.
9 #ifndef BOOST_POLY_COLLECTION_DETAIL_FUNCTION_MODEL_HPP
10 #define BOOST_POLY_COLLECTION_DETAIL_FUNCTION_MODEL_HPP
16 #include <boost/core/addressof.hpp>
17 #include <boost/poly_collection/detail/callable_wrapper.hpp>
18 #include <boost/poly_collection/detail/callable_wrapper_iterator.hpp>
19 #include <boost/poly_collection/detail/is_invocable.hpp>
20 #include <boost/poly_collection/detail/segment_backend.hpp>
21 #include <boost/poly_collection/detail/split_segment.hpp>
23 #include <type_traits>
29 namespace poly_collection{
33 /* model for function_collection */
35 template<typename Signature>
36 struct function_model;
38 /* is_terminal defined out-class to allow for partial specialization */
41 struct function_model_is_terminal:std::true_type{};
43 template<typename Signature>
44 struct function_model_is_terminal<callable_wrapper<Signature>>:
47 template<typename R,typename... Args>
48 struct function_model<R(Args...)>
50 using value_type=callable_wrapper<R(Args...)>;
52 template<typename Callable>
53 using is_implementation=is_invocable_r<R,Callable&,Args...>;
56 using is_terminal=function_model_is_terminal<T>;
59 static const std::type_info& subtypeid(const T&){return typeid(T);}
61 template<typename Signature>
62 static const std::type_info& subtypeid(
63 const callable_wrapper<Signature>& f)
65 return f.target_type();
69 static void* subaddress(T& x){return boost::addressof(x);}
72 static const void* subaddress(const T& x){return boost::addressof(x);}
74 template<typename Signature>
75 static void* subaddress(callable_wrapper<Signature>& f)
80 template<typename Signature>
81 static const void* subaddress(const callable_wrapper<Signature>& f)
86 using base_iterator=callable_wrapper_iterator<value_type>;
87 using const_base_iterator=callable_wrapper_iterator<const value_type>;
88 using base_sentinel=value_type*;
89 using const_base_sentinel=const value_type*;
90 template<typename Callable>
91 using iterator=Callable*;
92 template<typename Callable>
93 using const_iterator=const Callable*;
94 using segment_backend=detail::segment_backend<function_model>;
95 template<typename Callable,typename Allocator>
96 using segment_backend_implementation=split_segment<
99 typename std::allocator_traits<Allocator>::
100 template rebind_alloc<Callable>
102 using segment_backend_unique_ptr=
103 typename segment_backend::segment_backend_unique_ptr;
105 static base_iterator nonconst_iterator(const_base_iterator it)
107 return base_iterator{
108 const_cast<value_type*>(static_cast<const value_type*>(it))};
112 static iterator<T> nonconst_iterator(const_iterator<T> it)
114 return const_cast<iterator<T>>(it);
117 template<typename Callable,typename Allocator>
118 static segment_backend_unique_ptr make(const Allocator& al)
120 return segment_backend_implementation<Callable,Allocator>::new_(al,al);
124 template<typename,typename,typename>
125 friend class split_segment;
127 template<typename Callable>
128 static value_type make_value_type(Callable& x){return value_type{x};}
131 } /* namespace poly_collection::detail */
133 } /* namespace poly_collection */
135 } /* namespace boost */