function_types.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_TEST_FUNCTION_TYPES_HPP
  9. #define BOOST_POLY_COLLECTION_TEST_FUNCTION_TYPES_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/poly_collection/function_collection.hpp>
  14. #include <typeinfo>
  15. namespace function_types{
  16. struct function1 final
  17. {
  18. function1(int n):n{n}{}
  19. function1(function1&&)=default;
  20. function1(const function1&)=delete;
  21. function1& operator=(function1&&)=default;
  22. function1& operator=(const function1&)=delete;
  23. int operator()(int)const{return n;}
  24. friend bool operator==(
  25. const function1& x,const function1& y){return x.n==y.n;}
  26. int n;
  27. };
  28. struct function2
  29. {
  30. function2(int n):n{n}{}
  31. int operator()(int x)const{return x*n;}
  32. bool operator==(const function2& x)const{return n==x.n;}
  33. int n;
  34. };
  35. struct function3
  36. {
  37. function3():n{-1}{}
  38. function3(int n):n{n}{}
  39. int operator()(int x)const{return x*x*n;}
  40. int n;
  41. };
  42. struct function4:function3
  43. {
  44. using function3::function3;
  45. int operator()(int x)const{return -(this->function3::operator()(x));}
  46. bool operator==(const function4& x)const{return n==x.n;}
  47. };
  48. struct function5
  49. {
  50. function5(int n):n{n}{}
  51. int operator()(int x)const{return x*x*x*n;}
  52. int n;
  53. };
  54. struct int_alias /* brings this namespace into ADL for operator== below */
  55. {
  56. int_alias(int n):n{n}{}
  57. operator int()const{return n;}
  58. int n;
  59. };
  60. using signature=int_alias(int);
  61. using collection=boost::function_collection<signature>;
  62. using t1=function1;
  63. using t2=function2;
  64. using t3=function3;
  65. using t4=function4;
  66. using t5=function5;
  67. inline bool operator==(
  68. const collection::value_type& x,const collection::value_type& y)
  69. {
  70. const std::type_info& xi=x.target_type();
  71. const std::type_info& yi=y.target_type();
  72. if(xi==yi){
  73. if(xi==typeid(t1))return (*x.target<t1>())==(*y.target<t1>());
  74. if(xi==typeid(t2))return (*x.target<t2>()).operator==(*y.target<t2>());
  75. if(xi==typeid(t4))return (*x.target<t4>()).operator==(*y.target<t4>());
  76. }
  77. return false;
  78. }
  79. inline bool operator==(const collection::value_type& x,const t1& y)
  80. {
  81. const std::type_info& xi=x.target_type();
  82. if(xi==typeid(t1))return (*x.target<t1>())==y;
  83. return false;
  84. }
  85. inline bool operator==(const t1& x,const collection::value_type& y)
  86. {
  87. return y==x;
  88. }
  89. inline bool operator==(const collection::value_type& x,const t2& y)
  90. {
  91. const std::type_info& xi=x.target_type();
  92. if(xi==typeid(t2))return (*x.target<t2>())==y;
  93. return false;
  94. }
  95. inline bool operator==(const t2& x,const collection::value_type& y)
  96. {
  97. return y==x;
  98. }
  99. inline bool operator==(const collection::value_type& x,const t4& y)
  100. {
  101. const std::type_info& xi=x.target_type();
  102. if(xi==typeid(t4))return (*x.target<t4>())==y;
  103. return false;
  104. }
  105. inline bool operator==(const t4& x,const collection::value_type& y)
  106. {
  107. return y==x;
  108. }
  109. inline bool operator==(const t1&,const t2&){return false;}
  110. inline bool operator==(const t1&,const t4&){return false;}
  111. inline bool operator==(const t2&,const t1&){return false;}
  112. inline bool operator==(const t2&,const t4&){return false;}
  113. inline bool operator==(const t4&,const t1&){return false;}
  114. inline bool operator==(const t4&,const t2&){return false;}
  115. struct to_int
  116. {
  117. template<typename F>
  118. int operator()(const F& f)const{return f(1);}
  119. };
  120. } /* namespace function_types */
  121. #endif