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 | |
29 | custom() : m_value(0) {} |
30 | explicit custom(T const& value) : m_value(value) {} |
31 | |
32 | bool operator<(self const& other) const |
33 | { |
34 | return m_value < other.m_value; |
35 | } |
36 | |
37 | self operator-() const |
38 | { |
39 | return self(-m_value); |
40 | } |
41 | |
42 | self operator-(self const& other) const |
43 | { |
44 | return self(m_value - other.m_value); |
45 | } |
46 | }; |
47 | |
48 | template <typename T> |
49 | inline custom<T> sqrt(custom<T> const& c) |
50 | { |
51 | return custom<T>(std::sqrt(c.m_value)); |
52 | } |
53 | |
54 | template <typename T> |
55 | inline 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 | |
78 | custom_with_global_sqrt() : m_value(0) {} |
79 | explicit custom_with_global_sqrt(T const& value) : m_value(value) {} |
80 | |
81 | bool operator<(self const& other) const |
82 | { |
83 | return m_value < other.m_value; |
84 | } |
85 | |
86 | self operator-() const |
87 | { |
88 | return self(-m_value); |
89 | } |
90 | |
91 | 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> |
101 | sqrt(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> |
108 | fabs(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 | |
127 | custom_global() : m_value(0) {} |
128 | explicit custom_global(T const& value) : m_value(value) {} |
129 | |
130 | bool operator<(self const& other) const |
131 | { |
132 | return m_value < other.m_value; |
133 | } |
134 | |
135 | self operator-() const |
136 | { |
137 | return self(-m_value); |
138 | } |
139 | |
140 | self operator-(self const& other) const |
141 | { |
142 | return self(m_value - other.m_value); |
143 | } |
144 | }; |
145 | |
146 | template <typename T> |
147 | inline 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> |
153 | inline 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 | |
168 | custom_raw() : m_value(0) {} |
169 | explicit custom_raw(T const& value) : m_value(value) {} |
170 | |
171 | bool operator<(self const& other) const |
172 | { |
173 | return m_value < other.m_value; |
174 | } |
175 | |
176 | self operator-() const |
177 | { |
178 | return self(-m_value); |
179 | } |
180 | |
181 | 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 | |