1 // Boost.Geometry (aka GGL, Generic Geometry Library) 2 // Unit Test 3 4 // Copyright (c) 2014-2015 Oracle and/or its affiliates. 5 6 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle 7 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle 8 9 // Licensed under the Boost Software License version 1.0. 10 // http://www.boost.org/users/license.html 11 12 #ifndef BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP 13 #define BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP 14 15 #include <cmath> 16 17 18 // define a custom number type and its sqrt in own namespace 19 namespace number_types 20 { 21 22 template <typename T> 23 struct custom 24 { 25 typedef custom<T> self; 26 27 T m_value; 28 customnumber_types::custom29 custom() : m_value(0) {} customnumber_types::custom30 explicit custom(T const& value) : m_value(value) {} 31 operator <number_types::custom32 bool operator<(self const& other) const 33 { 34 return m_value < other.m_value; 35 } 36 operator -number_types::custom37 self operator-() const 38 { 39 return self(-m_value); 40 } 41 operator -number_types::custom42 self operator-(self const& other) const 43 { 44 return self(m_value - other.m_value); 45 } 46 }; 47 48 template <typename T> sqrt(custom<T> const & c)49inline custom<T> sqrt(custom<T> const& c) 50 { 51 return custom<T>(std::sqrt(c.m_value)); 52 } 53 54 template <typename T> fabs(custom<T> const & c)55inline custom<T> fabs(custom<T> const& c) 56 { 57 return custom<T>(c.m_value < T(0) ? c.m_value : -c.m_value); 58 } 59 60 } // namespace number_types 61 62 63 64 65 66 67 // define a custom number type with sqrt in global namespace 68 namespace number_types 69 { 70 71 template <typename T> 72 struct custom_with_global_sqrt 73 { 74 typedef custom_with_global_sqrt<T> self; 75 76 T m_value; 77 custom_with_global_sqrtnumber_types::custom_with_global_sqrt78 custom_with_global_sqrt() : m_value(0) {} custom_with_global_sqrtnumber_types::custom_with_global_sqrt79 explicit custom_with_global_sqrt(T const& value) : m_value(value) {} 80 operator <number_types::custom_with_global_sqrt81 bool operator<(self const& other) const 82 { 83 return m_value < other.m_value; 84 } 85 operator -number_types::custom_with_global_sqrt86 self operator-() const 87 { 88 return self(-m_value); 89 } 90 operator -number_types::custom_with_global_sqrt91 self operator-(self const& other) const 92 { 93 return self(m_value - other.m_value); 94 } 95 }; 96 97 } // namespace number_types 98 99 template <typename T> 100 inline number_types::custom_with_global_sqrt<T> sqrt(number_types::custom_with_global_sqrt<T> const & c)101sqrt(number_types::custom_with_global_sqrt<T> const& c) 102 { 103 return number_types::custom_with_global_sqrt<T>(std::sqrt(c.m_value)); 104 } 105 106 template <typename T> 107 inline number_types::custom_with_global_sqrt<T> fabs(number_types::custom_with_global_sqrt<T> const & c)108fabs(number_types::custom_with_global_sqrt<T> const& c) 109 { 110 return number_types::custom_with_global_sqrt<T> 111 (c.m_value < T(0) ? c.m_value : -c.m_value); 112 } 113 114 115 116 117 118 119 // define a custom number type and its sqrt in global namespace 120 template <typename T> 121 struct custom_global 122 { 123 typedef custom_global<T> self; 124 125 T m_value; 126 custom_globalcustom_global127 custom_global() : m_value(0) {} custom_globalcustom_global128 explicit custom_global(T const& value) : m_value(value) {} 129 operator <custom_global130 bool operator<(self const& other) const 131 { 132 return m_value < other.m_value; 133 } 134 operator -custom_global135 self operator-() const 136 { 137 return self(-m_value); 138 } 139 operator -custom_global140 self operator-(self const& other) const 141 { 142 return self(m_value - other.m_value); 143 } 144 }; 145 146 template <typename T> sqrt(custom_global<T> const & c)147inline custom_global<T> sqrt(custom_global<T> const& c) 148 { 149 return custom_global<T>(std::sqrt(c.m_value)); 150 } 151 152 template <typename T> fabs(custom_global<T> const & c)153inline custom_global<T> fabs(custom_global<T> const& c) 154 { 155 return custom_global<T>(c.m_value < T(0) ? c.m_value : -c.m_value); 156 } 157 158 159 160 // custom number type without functions definition 161 template <typename T> 162 struct custom_raw 163 { 164 typedef custom_raw<T> self; 165 166 T m_value; 167 custom_rawcustom_raw168 custom_raw() : m_value(0) {} custom_rawcustom_raw169 explicit custom_raw(T const& value) : m_value(value) {} 170 operator <custom_raw171 bool operator<(self const& other) const 172 { 173 return m_value < other.m_value; 174 } 175 operator -custom_raw176 self operator-() const 177 { 178 return self(-m_value); 179 } 180 operator -custom_raw181 self operator-(self const& other) const 182 { 183 return self(m_value - other.m_value); 184 } 185 }; 186 187 #endif // BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP 188