/work/install-coverage/include/opencv4/opencv2/flann/any.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef OPENCV_FLANN_ANY_H_ |
2 | | #define OPENCV_FLANN_ANY_H_ |
3 | | /* |
4 | | * (C) Copyright Christopher Diggins 2005-2011 |
5 | | * (C) Copyright Pablo Aguilar 2005 |
6 | | * (C) Copyright Kevlin Henney 2001 |
7 | | * |
8 | | * Distributed under the Boost Software License, Version 1.0. (See |
9 | | * accompanying file LICENSE_1_0.txt or copy at |
10 | | * http://www.boost.org/LICENSE_1_0.txt |
11 | | * |
12 | | * Adapted for FLANN by Marius Muja |
13 | | */ |
14 | | |
15 | | //! @cond IGNORED |
16 | | |
17 | | #include "defines.h" |
18 | | #include <stdexcept> |
19 | | #include <ostream> |
20 | | #include <typeinfo> |
21 | | |
22 | | namespace cvflann |
23 | | { |
24 | | |
25 | | namespace anyimpl |
26 | | { |
27 | | |
28 | | struct bad_any_cast |
29 | | { |
30 | | }; |
31 | | |
32 | | struct empty_any |
33 | | { |
34 | | }; |
35 | | |
36 | | inline std::ostream& operator <<(std::ostream& out, const empty_any&) |
37 | 0 | { |
38 | 0 | out << "[empty_any]"; |
39 | 0 | return out; |
40 | 0 | } |
41 | | |
42 | | struct base_any_policy |
43 | | { |
44 | | virtual void static_delete(void** x) = 0; |
45 | | virtual void copy_from_value(void const* src, void** dest) = 0; |
46 | | virtual void clone(void* const* src, void** dest) = 0; |
47 | | virtual void move(void* const* src, void** dest) = 0; |
48 | | virtual void* get_value(void** src) = 0; |
49 | | virtual const void* get_value(void* const * src) = 0; |
50 | | virtual ::size_t get_size() = 0; |
51 | | virtual const std::type_info& type() = 0; |
52 | | virtual void print(std::ostream& out, void* const* src) = 0; |
53 | 0 | virtual ~base_any_policy() {} |
54 | | }; |
55 | | |
56 | | template<typename T> |
57 | | struct typed_base_any_policy : base_any_policy |
58 | | { |
59 | 0 | virtual ::size_t get_size() CV_OVERRIDE { return sizeof(T); } Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<cvflann::anyimpl::empty_any>::get_size() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<char const*>::get_size() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<int>::get_size() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<float>::get_size() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<bool>::get_size() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<cvflann::flann_algorithm_t>::get_size() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<cvflann::flann_centers_init_t>::get_size() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::get_size() |
60 | 0 | virtual const std::type_info& type() CV_OVERRIDE { return typeid(T); } Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<cvflann::anyimpl::empty_any>::type() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<char const*>::type() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<int>::type() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<float>::type() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<bool>::type() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<cvflann::flann_algorithm_t>::type() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<cvflann::flann_centers_init_t>::type() Unexecuted instantiation: cvflann::anyimpl::typed_base_any_policy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::type() |
61 | | |
62 | | }; |
63 | | |
64 | | template<typename T> |
65 | | struct small_any_policy CV_FINAL : typed_base_any_policy<T> |
66 | | { |
67 | 0 | virtual void static_delete(void**) CV_OVERRIDE { } Unexecuted instantiation: cvflann::anyimpl::small_any_policy<char const*>::static_delete(void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<int>::static_delete(void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<float>::static_delete(void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<bool>::static_delete(void**) |
68 | | virtual void copy_from_value(void const* src, void** dest) CV_OVERRIDE |
69 | 0 | { |
70 | 0 | new (dest) T(* reinterpret_cast<T const*>(src)); |
71 | 0 | } Unexecuted instantiation: cvflann::anyimpl::small_any_policy<char const*>::copy_from_value(void const*, void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<int>::copy_from_value(void const*, void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<float>::copy_from_value(void const*, void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<bool>::copy_from_value(void const*, void**) |
72 | 0 | virtual void clone(void* const* src, void** dest) CV_OVERRIDE { *dest = *src; } Unexecuted instantiation: cvflann::anyimpl::small_any_policy<char const*>::clone(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<int>::clone(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<float>::clone(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<bool>::clone(void* const*, void**) |
73 | 0 | virtual void move(void* const* src, void** dest) CV_OVERRIDE { *dest = *src; } Unexecuted instantiation: cvflann::anyimpl::small_any_policy<char const*>::move(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<int>::move(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<float>::move(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<bool>::move(void* const*, void**) |
74 | 0 | virtual void* get_value(void** src) CV_OVERRIDE { return reinterpret_cast<void*>(src); } Unexecuted instantiation: cvflann::anyimpl::small_any_policy<char const*>::get_value(void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<int>::get_value(void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<float>::get_value(void**) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<bool>::get_value(void**) |
75 | 0 | virtual const void* get_value(void* const * src) CV_OVERRIDE { return reinterpret_cast<const void*>(src); } Unexecuted instantiation: cvflann::anyimpl::small_any_policy<char const*>::get_value(void* const*) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<int>::get_value(void* const*) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<float>::get_value(void* const*) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<bool>::get_value(void* const*) |
76 | 0 | virtual void print(std::ostream& out, void* const* src) CV_OVERRIDE { out << *reinterpret_cast<T const*>(src); } Unexecuted instantiation: cvflann::anyimpl::small_any_policy<char const*>::print(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, void* const*) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<int>::print(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, void* const*) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<float>::print(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, void* const*) Unexecuted instantiation: cvflann::anyimpl::small_any_policy<bool>::print(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, void* const*) |
77 | | }; |
78 | | |
79 | | template<typename T> |
80 | | struct big_any_policy CV_FINAL : typed_base_any_policy<T> |
81 | | { |
82 | | virtual void static_delete(void** x) CV_OVERRIDE |
83 | 0 | { |
84 | 0 | if (* x) delete (* reinterpret_cast<T**>(x)); |
85 | 0 | *x = NULL; |
86 | 0 | } Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::anyimpl::empty_any>::static_delete(void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_algorithm_t>::static_delete(void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_centers_init_t>::static_delete(void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::static_delete(void**) |
87 | | virtual void copy_from_value(void const* src, void** dest) CV_OVERRIDE |
88 | 0 | { |
89 | 0 | *dest = new T(*reinterpret_cast<T const*>(src)); |
90 | 0 | } Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::anyimpl::empty_any>::copy_from_value(void const*, void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_algorithm_t>::copy_from_value(void const*, void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_centers_init_t>::copy_from_value(void const*, void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::copy_from_value(void const*, void**) |
91 | | virtual void clone(void* const* src, void** dest) CV_OVERRIDE |
92 | 0 | { |
93 | 0 | *dest = new T(**reinterpret_cast<T* const*>(src)); |
94 | 0 | } Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::anyimpl::empty_any>::clone(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_algorithm_t>::clone(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_centers_init_t>::clone(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::clone(void* const*, void**) |
95 | | virtual void move(void* const* src, void** dest) CV_OVERRIDE |
96 | 0 | { |
97 | 0 | (*reinterpret_cast<T**>(dest))->~T(); |
98 | 0 | **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src); |
99 | 0 | } Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::anyimpl::empty_any>::move(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_algorithm_t>::move(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_centers_init_t>::move(void* const*, void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::move(void* const*, void**) |
100 | 0 | virtual void* get_value(void** src) CV_OVERRIDE { return *src; } Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::anyimpl::empty_any>::get_value(void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_algorithm_t>::get_value(void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_centers_init_t>::get_value(void**) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::get_value(void**) |
101 | 0 | virtual const void* get_value(void* const * src) CV_OVERRIDE { return *src; } Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::anyimpl::empty_any>::get_value(void* const*) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_algorithm_t>::get_value(void* const*) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<cvflann::flann_centers_init_t>::get_value(void* const*) Unexecuted instantiation: cvflann::anyimpl::big_any_policy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::get_value(void* const*) |
102 | 0 | virtual void print(std::ostream& out, void* const* src) CV_OVERRIDE { out << *reinterpret_cast<T const*>(*src); } |
103 | | }; |
104 | | |
105 | | template<> inline void big_any_policy<flann_centers_init_t>::print(std::ostream& out, void* const* src) |
106 | 0 | { |
107 | 0 | out << int(*reinterpret_cast<flann_centers_init_t const*>(*src)); |
108 | 0 | } |
109 | | |
110 | | template<> inline void big_any_policy<flann_algorithm_t>::print(std::ostream& out, void* const* src) |
111 | 0 | { |
112 | 0 | out << int(*reinterpret_cast<flann_algorithm_t const*>(*src)); |
113 | 0 | } |
114 | | |
115 | | template<> inline void big_any_policy<cv::String>::print(std::ostream& out, void* const* src) |
116 | 0 | { |
117 | 0 | out << (*reinterpret_cast<cv::String const*>(*src)).c_str(); |
118 | 0 | } |
119 | | |
120 | | template<typename T> |
121 | | struct choose_policy |
122 | | { |
123 | | typedef big_any_policy<T> type; |
124 | | }; |
125 | | |
126 | | template<typename T> |
127 | | struct choose_policy<T*> |
128 | | { |
129 | | typedef small_any_policy<T*> type; |
130 | | }; |
131 | | |
132 | | struct any; |
133 | | |
134 | | /// Choosing the policy for an any type is illegal, but should never happen. |
135 | | /// This is designed to throw a compiler error. |
136 | | template<> |
137 | | struct choose_policy<any> |
138 | | { |
139 | | typedef void type; |
140 | | }; |
141 | | |
142 | | /// Specializations for small types. |
143 | | #define SMALL_POLICY(TYPE) \ |
144 | | template<> \ |
145 | | struct choose_policy<TYPE> { typedef small_any_policy<TYPE> type; \ |
146 | | } |
147 | | |
148 | | SMALL_POLICY(signed char); |
149 | | SMALL_POLICY(unsigned char); |
150 | | SMALL_POLICY(signed short); |
151 | | SMALL_POLICY(unsigned short); |
152 | | SMALL_POLICY(signed int); |
153 | | SMALL_POLICY(unsigned int); |
154 | | SMALL_POLICY(signed long); |
155 | | SMALL_POLICY(unsigned long); |
156 | | SMALL_POLICY(float); |
157 | | SMALL_POLICY(bool); |
158 | | |
159 | | #undef SMALL_POLICY |
160 | | |
161 | | template <typename T> |
162 | | class SinglePolicy |
163 | | { |
164 | | SinglePolicy(); |
165 | | SinglePolicy(const SinglePolicy& other); |
166 | | SinglePolicy& operator=(const SinglePolicy& other); |
167 | | |
168 | | public: |
169 | | static base_any_policy* get_policy(); |
170 | | }; |
171 | | |
172 | | /// This function will return a different policy for each type. |
173 | | template <typename T> |
174 | | inline base_any_policy* SinglePolicy<T>::get_policy() |
175 | 0 | { |
176 | 0 | static typename choose_policy<T>::type policy; |
177 | 0 | return &policy; |
178 | 0 | } Unexecuted instantiation: cvflann::anyimpl::SinglePolicy<cvflann::anyimpl::empty_any>::get_policy() Unexecuted instantiation: cvflann::anyimpl::SinglePolicy<char const*>::get_policy() Unexecuted instantiation: cvflann::anyimpl::SinglePolicy<int>::get_policy() Unexecuted instantiation: cvflann::anyimpl::SinglePolicy<float>::get_policy() Unexecuted instantiation: cvflann::anyimpl::SinglePolicy<bool>::get_policy() Unexecuted instantiation: cvflann::anyimpl::SinglePolicy<cvflann::flann_algorithm_t>::get_policy() Unexecuted instantiation: cvflann::anyimpl::SinglePolicy<cvflann::flann_centers_init_t>::get_policy() Unexecuted instantiation: cvflann::anyimpl::SinglePolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::get_policy() |
179 | | |
180 | | } // namespace anyimpl |
181 | | |
182 | | struct any |
183 | | { |
184 | | private: |
185 | | // fields |
186 | | anyimpl::base_any_policy* policy; |
187 | | void* object; |
188 | | |
189 | | public: |
190 | | /// Initializing constructor. |
191 | | template <typename T> |
192 | | any(const T& x) |
193 | | : policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL) |
194 | | { |
195 | | assign(x); |
196 | | } |
197 | | |
198 | | /// Empty constructor. |
199 | | any() |
200 | | : policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL) |
201 | 0 | { } |
202 | | |
203 | | /// Special initializing constructor for string literals. |
204 | | any(const char* x) |
205 | | : policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL) |
206 | 0 | { |
207 | 0 | assign(x); |
208 | 0 | } |
209 | | |
210 | | /// Copy constructor. |
211 | | any(const any& x) |
212 | | : policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL) |
213 | 0 | { |
214 | 0 | assign(x); |
215 | 0 | } |
216 | | |
217 | | /// Destructor. |
218 | | ~any() |
219 | 0 | { |
220 | 0 | policy->static_delete(&object); |
221 | 0 | } |
222 | | |
223 | | /// Assignment function from another any. |
224 | | any& assign(const any& x) |
225 | 0 | { |
226 | 0 | reset(); |
227 | 0 | policy = x.policy; |
228 | 0 | policy->clone(&x.object, &object); |
229 | 0 | return *this; |
230 | 0 | } |
231 | | |
232 | | /// Assignment function. |
233 | | template <typename T> |
234 | | any& assign(const T& x) |
235 | 0 | { |
236 | 0 | reset(); |
237 | 0 | policy = anyimpl::SinglePolicy<T>::get_policy(); |
238 | 0 | policy->copy_from_value(&x, &object); |
239 | 0 | return *this; |
240 | 0 | } Unexecuted instantiation: cvflann::any& cvflann::any::assign<char const*>(char const* const&) Unexecuted instantiation: cvflann::any& cvflann::any::assign<int>(int const&) Unexecuted instantiation: cvflann::any& cvflann::any::assign<float>(float const&) Unexecuted instantiation: cvflann::any& cvflann::any::assign<bool>(bool const&) Unexecuted instantiation: cvflann::any& cvflann::any::assign<cvflann::flann_algorithm_t>(cvflann::flann_algorithm_t const&) Unexecuted instantiation: cvflann::any& cvflann::any::assign<cvflann::flann_centers_init_t>(cvflann::flann_centers_init_t const&) Unexecuted instantiation: cvflann::any& cvflann::any::assign<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) |
241 | | |
242 | | /// Assignment operator. |
243 | | template<typename T> |
244 | | any& operator=(const T& x) |
245 | 0 | { |
246 | 0 | return assign(x); |
247 | 0 | } Unexecuted instantiation: cvflann::any& cvflann::any::operator=<int>(int const&) Unexecuted instantiation: cvflann::any& cvflann::any::operator=<float>(float const&) Unexecuted instantiation: cvflann::any& cvflann::any::operator=<bool>(bool const&) Unexecuted instantiation: cvflann::any& cvflann::any::operator=<cvflann::flann_algorithm_t>(cvflann::flann_algorithm_t const&) Unexecuted instantiation: cvflann::any& cvflann::any::operator=<cvflann::flann_centers_init_t>(cvflann::flann_centers_init_t const&) Unexecuted instantiation: cvflann::any& cvflann::any::operator=<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) |
248 | | |
249 | | /// Assignment operator. Template-based version above doesn't work as expected. We need regular assignment operator here. |
250 | | any& operator=(const any& x) |
251 | 0 | { |
252 | 0 | return assign(x); |
253 | 0 | } |
254 | | |
255 | | /// Assignment operator, specialed for literal strings. |
256 | | /// They have types like const char [6] which don't work as expected. |
257 | | any& operator=(const char* x) |
258 | 0 | { |
259 | 0 | return assign(x); |
260 | 0 | } |
261 | | |
262 | | /// Utility functions |
263 | | any& swap(any& x) |
264 | 0 | { |
265 | 0 | std::swap(policy, x.policy); |
266 | 0 | std::swap(object, x.object); |
267 | 0 | return *this; |
268 | 0 | } |
269 | | |
270 | | /// Cast operator. You can only cast to the original type. |
271 | | template<typename T> |
272 | | T& cast() |
273 | | { |
274 | | if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast(); |
275 | | T* r = reinterpret_cast<T*>(policy->get_value(&object)); |
276 | | return *r; |
277 | | } |
278 | | |
279 | | /// Cast operator. You can only cast to the original type. |
280 | | template<typename T> |
281 | | const T& cast() const |
282 | | { |
283 | | if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast(); |
284 | | const T* r = reinterpret_cast<const T*>(policy->get_value(&object)); |
285 | | return *r; |
286 | | } |
287 | | |
288 | | /// Returns true if the any contains no value. |
289 | | bool empty() const |
290 | 0 | { |
291 | 0 | return policy->type() == typeid(anyimpl::empty_any); |
292 | 0 | } |
293 | | |
294 | | /// Frees any allocated memory, and sets the value to NULL. |
295 | | void reset() |
296 | 0 | { |
297 | 0 | policy->static_delete(&object); |
298 | 0 | policy = anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy(); |
299 | 0 | } |
300 | | |
301 | | /// Returns true if the two types are the same. |
302 | | bool compatible(const any& x) const |
303 | 0 | { |
304 | 0 | return policy->type() == x.policy->type(); |
305 | 0 | } |
306 | | |
307 | | /// Returns if the type is compatible with the policy |
308 | | template<typename T> |
309 | | bool has_type() |
310 | | { |
311 | | return policy->type() == typeid(T); |
312 | | } |
313 | | |
314 | | const std::type_info& type() const |
315 | 0 | { |
316 | 0 | return policy->type(); |
317 | 0 | } |
318 | | |
319 | | friend std::ostream& operator <<(std::ostream& out, const any& any_val); |
320 | | }; |
321 | | |
322 | | inline std::ostream& operator <<(std::ostream& out, const any& any_val) |
323 | 0 | { |
324 | 0 | any_val.policy->print(out,&any_val.object); |
325 | 0 | return out; |
326 | 0 | } |
327 | | |
328 | | } |
329 | | |
330 | | //! @endcond |
331 | | |
332 | | #endif // OPENCV_FLANN_ANY_H_ |