function_model.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. #ifndef BOOST_POLY_COLLECTION_DETAIL_FUNCTION_MODEL_HPP
  9. #define BOOST_POLY_COLLECTION_DETAIL_FUNCTION_MODEL_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/core/addressof.hpp>
  14. #include <boost/poly_collection/detail/callable_wrapper.hpp>
  15. #include <boost/poly_collection/detail/callable_wrapper_iterator.hpp>
  16. #include <boost/poly_collection/detail/is_invocable.hpp>
  17. #include <boost/poly_collection/detail/segment_backend.hpp>
  18. #include <boost/poly_collection/detail/split_segment.hpp>
  19. #include <memory>
  20. #include <type_traits>
  21. #include <typeinfo>
  22. #include <utility>
  23. namespace boost{
  24. namespace poly_collection{
  25. namespace detail{
  26. /* model for function_collection */
  27. template<typename Signature>
  28. struct function_model;
  29. /* is_terminal defined out-class to allow for partial specialization */
  30. template<typename T>
  31. struct function_model_is_terminal:std::true_type{};
  32. template<typename Signature>
  33. struct function_model_is_terminal<callable_wrapper<Signature>>:
  34. std::false_type{};
  35. template<typename R,typename... Args>
  36. struct function_model<R(Args...)>
  37. {
  38. using value_type=callable_wrapper<R(Args...)>;
  39. template<typename Callable>
  40. using is_implementation=is_invocable_r<R,Callable&,Args...>;
  41. template<typename T>
  42. using is_terminal=function_model_is_terminal<T>;
  43. template<typename T>
  44. static const std::type_info& subtypeid(const T&){return typeid(T);}
  45. template<typename Signature>
  46. static const std::type_info& subtypeid(
  47. const callable_wrapper<Signature>& f)
  48. {
  49. return f.target_type();
  50. }
  51. template<typename T>
  52. static void* subaddress(T& x){return boost::addressof(x);}
  53. template<typename T>
  54. static const void* subaddress(const T& x){return boost::addressof(x);}
  55. template<typename Signature>
  56. static void* subaddress(callable_wrapper<Signature>& f)
  57. {
  58. return f.data();
  59. }
  60. template<typename Signature>
  61. static const void* subaddress(const callable_wrapper<Signature>& f)
  62. {
  63. return f.data();
  64. }
  65. using base_iterator=callable_wrapper_iterator<value_type>;
  66. using const_base_iterator=callable_wrapper_iterator<const value_type>;
  67. using base_sentinel=value_type*;
  68. using const_base_sentinel=const value_type*;
  69. template<typename Callable>
  70. using iterator=Callable*;
  71. template<typename Callable>
  72. using const_iterator=const Callable*;
  73. using segment_backend=detail::segment_backend<function_model>;
  74. template<typename Callable,typename Allocator>
  75. using segment_backend_implementation=split_segment<
  76. function_model,
  77. Callable,
  78. typename std::allocator_traits<Allocator>::
  79. template rebind_alloc<Callable>
  80. >;
  81. using segment_backend_unique_ptr=
  82. typename segment_backend::segment_backend_unique_ptr;
  83. static base_iterator nonconst_iterator(const_base_iterator it)
  84. {
  85. return base_iterator{
  86. const_cast<value_type*>(static_cast<const value_type*>(it))};
  87. }
  88. template<typename T>
  89. static iterator<T> nonconst_iterator(const_iterator<T> it)
  90. {
  91. return const_cast<iterator<T>>(it);
  92. }
  93. template<typename Callable,typename Allocator>
  94. static segment_backend_unique_ptr make(const Allocator& al)
  95. {
  96. return segment_backend_implementation<Callable,Allocator>::new_(al,al);
  97. }
  98. private:
  99. template<typename,typename,typename>
  100. friend class split_segment;
  101. template<typename Callable>
  102. static value_type make_value_type(Callable& x){return value_type{x};}
  103. };
  104. } /* namespace poly_collection::detail */
  105. } /* namespace poly_collection */
  106. } /* namespace boost */
  107. #endif