function_model.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright 2016-2018 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. template<typename Allocator>
  74. using segment_backend=detail::segment_backend<function_model,Allocator>;
  75. template<typename Callable,typename Allocator>
  76. using segment_backend_implementation=
  77. split_segment<function_model,Callable,Allocator>;
  78. static base_iterator nonconst_iterator(const_base_iterator it)
  79. {
  80. return base_iterator{
  81. const_cast<value_type*>(static_cast<const value_type*>(it))};
  82. }
  83. template<typename T>
  84. static iterator<T> nonconst_iterator(const_iterator<T> it)
  85. {
  86. return const_cast<iterator<T>>(it);
  87. }
  88. private:
  89. template<typename,typename,typename>
  90. friend class split_segment;
  91. template<typename Callable>
  92. static value_type make_value_type(Callable& x){return value_type{x};}
  93. };
  94. } /* namespace poly_collection::detail */
  95. } /* namespace poly_collection */
  96. } /* namespace boost */
  97. #endif