1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7 // This file was modified by Oracle on 2014.
8 // Modifications copyright (c) 2014 Oracle and/or its affiliates.
10 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
13 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15 // Use, modification and distribution is subject to the Boost Software License,
16 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_GEOMETRY_UTIL_SELECT_MOST_PRECISE_HPP
20 #define BOOST_GEOMETRY_UTIL_SELECT_MOST_PRECISE_HPP
22 #include <boost/mpl/if.hpp>
23 #include <boost/type_traits/is_floating_point.hpp>
24 #include <boost/type_traits/is_fundamental.hpp>
27 namespace boost { namespace geometry
30 #ifndef DOXYGEN_NO_DETAIL
32 namespace detail { namespace select_most_precise
36 // At least one of the types is non-fundamental. Take that one.
37 // if both are non-fundamental, the type-to-be-selected
38 // is unknown, it should be defined by explicit specialization.
39 template <bool Fundamental1, bool Fundamental2, typename T1, typename T2>
40 struct select_non_fundamental
45 template <typename T1, typename T2>
46 struct select_non_fundamental<true, false, T1, T2>
51 template <typename T1, typename T2>
52 struct select_non_fundamental<false, true, T1, T2>
58 // Selection of largest type (e.g. int of <short int,int>
59 // It defaults takes the first one, if second is larger, take the second one
60 template <bool SecondLarger, typename T1, typename T2>
66 template <typename T1, typename T2>
67 struct select_largest<true, T1, T2>
74 // Selection of floating point and specializations:
75 // both FP or both !FP does never occur...
76 template <bool FP1, bool FP2, typename T1, typename T2>
77 struct select_floating_point
83 // ... so if ONE but not both of these types is floating point, take that one
84 template <typename T1, typename T2>
85 struct select_floating_point<true, false, T1, T2>
91 template <typename T1, typename T2>
92 struct select_floating_point<false, true, T1, T2>
98 }} // namespace detail::select_most_precise
99 #endif // DOXYGEN_NO_DETAIL
103 \brief Meta-function to select, of two types, the most accurate type for
106 \details select_most_precise classes, compares two types on compile time.
107 For example, if an addition must be done with a double and an integer, the
108 result must be a double.
109 If both types are integer, the result can be an integer.
110 \note It is different from the "promote" class, already in boost. That
111 class promotes e.g. a (one) float to a double. This class selects a
112 type from two types. It takes the most accurate, but does not promote
114 \note This traits class is completely independant from GGL and might be a
115 separate addition to Boost
116 \note If the input is a non-fundamental type, it might be a calculation
117 type such as a GMP-value or another high precision value. Therefore,
118 if one is non-fundamental, that one is chosen.
119 \note If both types are non-fundamental, the result is indeterminate and
120 currently the first one is chosen.
122 template <typename T1, typename T2 = void, typename T3 = void>
123 struct select_most_precise
125 typedef typename select_most_precise
127 typename select_most_precise<T1, T2>::type,
132 template <typename T1, typename T2>
133 struct select_most_precise<T1, T2, void>
135 static const bool second_larger = sizeof(T2) > sizeof(T1);
136 static const bool one_not_fundamental = !
137 (boost::is_fundamental<T1>::type::value
138 && boost::is_fundamental<T2>::type::value);
140 static const bool both_same =
141 boost::is_floating_point<T1>::type::value
142 == boost::is_floating_point<T2>::type::value;
144 typedef typename boost::mpl::if_c
147 typename detail::select_most_precise::select_non_fundamental
149 boost::is_fundamental<T1>::type::value,
150 boost::is_fundamental<T2>::type::value,
154 typename boost::mpl::if_c
157 typename detail::select_most_precise::select_largest
163 typename detail::select_most_precise::select_floating_point
165 boost::is_floating_point<T1>::type::value,
166 boost::is_floating_point<T2>::type::value,
174 template <typename T1>
175 struct select_most_precise<T1, void, void>
180 }} // namespace boost::geometry
182 #endif // BOOST_GEOMETRY_UTIL_SELECT_MOST_PRECISE_HPP