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