any_types.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_ANY_TYPES_HPP
  9. #define BOOST_POLY_COLLECTION_TEST_ANY_TYPES_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/mpl/vector/vector10.hpp>
  14. #include <boost/poly_collection/any_collection.hpp>
  15. #include <boost/type_erasure/any_cast.hpp>
  16. #include <boost/type_erasure/builtin.hpp>
  17. #include <boost/type_erasure/call.hpp>
  18. #include <boost/type_erasure/operators.hpp>
  19. namespace any_types{
  20. struct incrementable1
  21. {
  22. incrementable1(int n):n{n}{}
  23. incrementable1(incrementable1&&)=default;
  24. incrementable1(const incrementable1&)=delete;
  25. incrementable1& operator=(incrementable1&&)=default;
  26. incrementable1& operator=(const incrementable1&)=delete;
  27. bool operator==(const incrementable1& x)const{return n==x.n;}
  28. incrementable1& operator++(){++n;return *this;}
  29. int n;
  30. };
  31. struct incrementable3
  32. {
  33. incrementable3():n{-1}{}
  34. incrementable3(int n):n{(double)n}{}
  35. incrementable3& operator++(){++n;return *this;}
  36. double n;
  37. };
  38. using concept_=boost::type_erasure::incrementable<>;
  39. using collection=boost::any_collection<concept_>;
  40. template<typename T=boost::type_erasure::_self>
  41. struct convertible_to_int
  42. {
  43. static int apply(const T& x){return x;}
  44. };
  45. using t1=incrementable1;
  46. using t2=double;
  47. using t3=incrementable3;
  48. using t4=int;
  49. using t5=boost::type_erasure::any<
  50. boost::mpl::vector4<
  51. boost::type_erasure::copy_constructible<>,
  52. boost::type_erasure::assignable<>,
  53. concept_,
  54. convertible_to_int<>
  55. >
  56. >;
  57. struct to_int
  58. {
  59. template<typename Concept,typename Tag>
  60. int operator()(const boost::type_erasure::any<Concept,Tag>& x)const
  61. {
  62. using boost::type_erasure::any_cast;
  63. if(auto p=any_cast<t1*>(&x))return (*this)(*p);
  64. if(auto p=any_cast<t2*>(&x))return (*this)(*p);
  65. if(auto p=any_cast<t3*>(&x))return (*this)(*p);
  66. if(auto p=any_cast<t4*>(&x))return (*this)(*p);
  67. if(auto p=any_cast<t5*>(&x))return (*this)(*p);
  68. else return 0;
  69. }
  70. int operator()(const t1& x)const{return x.n;}
  71. int operator()(const t2& x)const{return static_cast<int>(x);};
  72. int operator()(const t3& x)const{return static_cast<int>(x.n);}
  73. int operator()(const t4& x)const{return x;}
  74. int operator()(const t5& x)const
  75. {
  76. return boost::type_erasure::call(convertible_to_int<>{},x);
  77. }
  78. };
  79. } /* namespace any_types */
  80. #endif