BWAPI
|
00001 // Copyright (c) 2006-2007 Max-Planck-Institute Saarbruecken (Germany). 00002 // All rights reserved. 00003 // 00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public License as 00006 // published by the Free Software Foundation; version 2.1 of the License. 00007 // See the file LICENSE.LGPL distributed with CGAL. 00008 // 00009 // Licensees holding a valid commercial license may use this file in 00010 // accordance with the commercial license agreement provided with the software. 00011 // 00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00014 // 00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Algebraic_foundations/include/CGAL/Coercion_traits.h $ 00016 // $Id: Coercion_traits.h 47295 2008-12-09 10:05:42Z hemmer $ 00017 // 00018 // 00019 // Author(s) : Michael Hemmer <hemmer@mpi-inf.mpg.de> 00020 // 00021 // ============================================================================= 00022 00030 #ifndef CGAL_COERCION_TRAITS_H 00031 #define CGAL_COERCION_TRAITS_H 1 00032 00033 #include <CGAL/number_type_basic.h> 00034 00035 #include <iterator> 00036 00037 #include <boost/iterator/transform_iterator.hpp> 00038 #include <boost/type_traits/is_same.hpp> 00039 00040 // Makro to define an additional operator for binary functors which takes 00041 // two number types as parameters that are interoperable with the 00042 // number type 00043 #define CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR_WITH_RT( NT, Result_type ) \ 00044 template < class CT_Type_1, class CT_Type_2 > \ 00045 Result_type operator()( const CT_Type_1& x, const CT_Type_2& y ) const { \ 00046 BOOST_STATIC_ASSERT((::boost::is_same< \ 00047 typename Coercion_traits< CT_Type_1, CT_Type_2 >::Type, NT \ 00048 >::value)); \ 00049 \ 00050 typename Coercion_traits< CT_Type_1, CT_Type_2 >::Cast cast; \ 00051 return operator()( cast(x), cast(y) ); \ 00052 } 00053 00054 #define CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR( NT ) \ 00055 CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR_WITH_RT( NT, NT ) 00056 00057 #define CGAL_DEFINE_COERCION_TRAITS_FROM_TO(FROM,TO) \ 00058 template <> \ 00059 struct Coercion_traits< FROM , TO >{ \ 00060 typedef Tag_true Are_explicit_interoperable; \ 00061 typedef Tag_true Are_implicit_interoperable; \ 00062 typedef TO Type; \ 00063 struct Cast{ \ 00064 typedef Type result_type; \ 00065 Type operator()(const TO& x) const { return x;} \ 00066 Type operator()(const FROM& x) const { \ 00067 return Type(x);} \ 00068 }; \ 00069 }; \ 00070 template <> \ 00071 struct Coercion_traits< TO , FROM >{ \ 00072 typedef Tag_true Are_explicit_interoperable; \ 00073 typedef Tag_true Are_implicit_interoperable; \ 00074 typedef TO Type; \ 00075 struct Cast{ \ 00076 typedef Type result_type; \ 00077 Type operator()(const TO& x) const { return x;} \ 00078 Type operator()(const FROM& x) const { \ 00079 return Type(x);} \ 00080 }; \ 00081 }; 00082 00083 #define CGAL_DEFINE_COERCION_TRAITS_FROM_TO_TEM(FROM,TO,TEM) \ 00084 template <TEM> \ 00085 struct Coercion_traits< FROM , TO >{ \ 00086 typedef Tag_true Are_explicit_interoperable; \ 00087 typedef Tag_true Are_implicit_interoperable; \ 00088 typedef TO Type; \ 00089 struct Cast{ \ 00090 typedef Type result_type; \ 00091 Type operator()(const TO& x) const { return x;} \ 00092 Type operator()(const FROM& x) const { \ 00093 return Type(x);} \ 00094 }; \ 00095 }; \ 00096 template <TEM> \ 00097 struct Coercion_traits< TO , FROM >{ \ 00098 typedef Tag_true Are_explicit_interoperable; \ 00099 typedef Tag_true Are_implicit_interoperable; \ 00100 typedef TO Type; \ 00101 struct Cast{ \ 00102 typedef Type result_type; \ 00103 Type operator()(const TO& x) const { return x;} \ 00104 Type operator()(const FROM& x) const { \ 00105 return Type(x);} \ 00106 }; \ 00107 }; 00108 00109 00110 00111 #define CGAL_DEFINE_COERCION_TRAITS_FOR_SELF(A) \ 00112 template <> \ 00113 struct Coercion_traits< A , A >{ \ 00114 typedef Tag_true Are_explicit_interoperable; \ 00115 typedef Tag_true Are_implicit_interoperable; \ 00116 typedef A Type; \ 00117 struct Cast{ \ 00118 typedef Type result_type; \ 00119 Type operator()(const A& x) const { return x;} \ 00120 }; \ 00121 }; 00122 00123 #define CGAL_DEFINE_COERCION_TRAITS_FOR_SELF_TEM(A,TEM) \ 00124 template <TEM> \ 00125 struct Coercion_traits< A , A >{ \ 00126 typedef Tag_true Are_explicit_interoperable; \ 00127 typedef Tag_true Are_implicit_interoperable; \ 00128 typedef A Type; \ 00129 struct Cast{ \ 00130 typedef Type result_type; \ 00131 Type operator()(const A& x) const {return x;} \ 00132 }; \ 00133 }; 00134 00135 CGAL_BEGIN_NAMESPACE 00136 00137 00138 namespace INTERN_CT{ 00139 template< class FROM, class TO >struct Cast_from_to{ 00140 typedef TO result_type; 00141 TO operator()(const TO& x){return x;} 00142 TO operator()(const FROM& x){return TO(x);} 00143 }; 00144 template< class TO> 00145 struct Cast_from_to<TO,TO>{ 00146 typedef TO result_type; 00147 TO operator()(const TO& x){return x;} 00148 }; 00149 } 00150 00151 00152 template<class A , class B> struct Coercion_traits; 00153 template<class A , class B, int > struct Coercion_traits_for_level; 00154 00155 00156 00157 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(short,int) 00158 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(short,long) 00159 #ifdef CGAL_USE_LONG_LONG 00160 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(short,long long) 00161 #endif 00162 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(short,float) 00163 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(short,double) 00164 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(short,long double) 00165 00166 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(int,long) 00167 #ifdef CGAL_USE_LONG_LONG 00168 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(int,long long) 00169 #endif 00170 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(int,float) 00171 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(int,double) 00172 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(int,long double) 00173 00174 #ifdef CGAL_USE_LONG_LONG 00175 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(long,long long) 00176 #endif 00177 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(long,float) 00178 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(long,double) 00179 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(long,long double) 00180 00181 #ifdef CGAL_USE_LONG_LONG 00182 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(long long,float) 00183 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(long long,double) 00184 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(long long,long double) 00185 #endif 00186 00187 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(float,double) 00188 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(float,long double) 00189 00190 CGAL_DEFINE_COERCION_TRAITS_FROM_TO(double,long double) 00191 00193 template <class A> 00194 struct Coercion_traits<A,A>{ 00195 typedef Tag_true Are_explicit_interoperable; 00196 typedef Tag_true Are_implicit_interoperable; 00197 typedef A Type; 00198 struct Cast{ 00199 typedef Type result_type; 00200 Type inline operator()(const A& x) const { 00201 return x; 00202 } 00203 }; 00204 }; 00205 00206 CGAL_DEFINE_COERCION_TRAITS_FOR_SELF(short) 00207 CGAL_DEFINE_COERCION_TRAITS_FOR_SELF(int) 00208 CGAL_DEFINE_COERCION_TRAITS_FOR_SELF(long) 00209 #ifdef CGAL_USE_LONG_LONG 00210 CGAL_DEFINE_COERCION_TRAITS_FOR_SELF(long long) 00211 #endif 00212 CGAL_DEFINE_COERCION_TRAITS_FOR_SELF(float) 00213 CGAL_DEFINE_COERCION_TRAITS_FOR_SELF(double) 00214 CGAL_DEFINE_COERCION_TRAITS_FOR_SELF(long double) 00215 00216 enum COERCION_TRAITS_LEVEL { 00217 CTL_TOP = 4, 00218 CTL_POLYNOMIAL = 4, 00219 CTL_COMPLEX = 3, 00220 CTL_INTERVAL = 2, 00221 CTL_SQRT_EXT = 1 00222 }; 00223 00224 template <class A, class B, int i > 00225 struct Coercion_traits_for_level: public Coercion_traits_for_level<A,B,i-1>{}; 00226 00227 template <class A, class B> 00228 struct Coercion_traits_for_level<A,B,0> { 00229 typedef Tag_false Are_explicit_interoperable; 00230 typedef Tag_false Are_implicit_interoperable; 00231 // typedef Null_type Type; 00232 typedef Null_functor Cast; 00233 }; 00234 00235 template<class A , class B> 00236 struct Coercion_traits :public Coercion_traits_for_level<A,B,CTL_TOP>{}; 00237 00238 00239 CGAL_END_NAMESPACE 00240 00241 #endif //NiX_COERCION_TRAITS_H