function_model.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright 2016-2024 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. using type_index=std::type_info;
  40. template<typename Callable>
  41. using is_implementation=is_invocable_r<R,Callable&,Args...>;
  42. template<typename T>
  43. using is_terminal=function_model_is_terminal<T>;
  44. template<typename T>
  45. static const std::type_info& index(){return typeid(T);}
  46. template<typename T>
  47. static const std::type_info& subindex(const T&){return typeid(T);}
  48. template<typename Signature>
  49. static const std::type_info& subindex(
  50. const callable_wrapper<Signature>& f)
  51. {
  52. return f.target_type();
  53. }
  54. template<typename T>
  55. static void* subaddress(T& x){return boost::addressof(x);}
  56. template<typename T>
  57. static const void* subaddress(const T& x){return boost::addressof(x);}
  58. template<typename Signature>
  59. static void* subaddress(callable_wrapper<Signature>& f)
  60. {
  61. return f.data();
  62. }
  63. template<typename Signature>
  64. static const void* subaddress(const callable_wrapper<Signature>& f)
  65. {
  66. return f.data();
  67. }
  68. using base_iterator=callable_wrapper_iterator<value_type>;
  69. using const_base_iterator=callable_wrapper_iterator<const value_type>;
  70. using base_sentinel=value_type*;
  71. using const_base_sentinel=const value_type*;
  72. template<typename Callable>
  73. using iterator=Callable*;
  74. template<typename Callable>
  75. using const_iterator=const Callable*;
  76. template<typename Allocator>
  77. using segment_backend=detail::segment_backend<function_model,Allocator>;
  78. template<typename Callable,typename Allocator>
  79. using segment_backend_implementation=
  80. split_segment<function_model,Callable,Allocator>;
  81. static base_iterator nonconst_iterator(const_base_iterator it)
  82. {
  83. return base_iterator{
  84. const_cast<value_type*>(static_cast<const value_type*>(it))};
  85. }
  86. template<typename T>
  87. static iterator<T> nonconst_iterator(const_iterator<T> it)
  88. {
  89. return const_cast<iterator<T>>(it);
  90. }
  91. private:
  92. template<typename,typename,typename>
  93. friend class split_segment;
  94. template<typename Callable>
  95. static value_type make_value_type(Callable& x){return value_type{x};}
  96. };
  97. } /* namespace poly_collection::detail */
  98. } /* namespace poly_collection */
  99. } /* namespace boost */
  100. #endif