Line | Hotness | Optimization | Source | Inline Context |
---|---|---|---|---|
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 | { | |||
38 | out << "[empty_any]"; | |||
39 | return out; | |||
40 | } | |||
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 | virtual ~base_any_policy() {} | |||
54 | }; | |||
55 | ||||
56 | template<typename T> | |||
57 | struct typed_base_any_policy : base_any_policy | |||
58 | { | |||
59 | virtual ::size_t get_size() CV_OVERRIDE { return sizeof(T); } | |||
60 | virtual const std::type_info& type() CV_OVERRIDE { return typeid(T); } | |||
61 | ||||
62 | }; | |||
63 | ||||
64 | template<typename T> | |||
65 | struct small_any_policy CV_FINAL : typed_base_any_policy<T> | |||
66 | { | |||
67 | virtual void static_delete(void**) CV_OVERRIDE { } | |||
68 | virtual void copy_from_value(void const* src, void** dest) CV_OVERRIDE | |||
69 | { | |||
70 | new (dest) T(* reinterpret_cast<T const*>(src)); | |||
71 | } | |||
72 | virtual void clone(void* const* src, void** dest) CV_OVERRIDE { *dest = *src; } | |||
73 | virtual void move(void* const* src, void** dest) CV_OVERRIDE { *dest = *src; } | |||
74 | virtual void* get_value(void** src) CV_OVERRIDE { return reinterpret_cast<void*>(src); } | |||
75 | virtual const void* get_value(void* const * src) CV_OVERRIDE { return reinterpret_cast<const void*>(src); } | |||
76 | virtual void print(std::ostream& out, void* const* src) CV_OVERRIDE { out << *reinterpret_cast<T const*>(src); } | |||
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 | { | |||
84 | if (* x) delete (* reinterpret_cast<T**>(x)); | |||
85 | *x = NULL; | |||
86 | } | |||
87 | virtual void copy_from_value(void const* src, void** dest) CV_OVERRIDE | |||
88 | { | |||
89 | *dest = new T(*reinterpret_cast<T const*>(src)); | |||
90 | } | |||
91 | virtual void clone(void* const* src, void** dest) CV_OVERRIDE | |||
92 | { | |||
93 | *dest = new T(**reinterpret_cast<T* const*>(src)); | |||
0 | ^ | |||
gvn | • load of type double not eliminated because it is clobbered by call | cvflann::anyimpl::big_any_policy | ||
gvn | • load of type i32 not eliminated because it is clobbered by call | cvflann::anyimpl::big_any_policy | ||
gvn | • load of type i32 not eliminated because it is clobbered by call | cvflann::anyimpl::big_any_policy | ||
94 | } | |||
95 | virtual void move(void* const* src, void** dest) CV_OVERRIDE | |||
96 | { | |||
97 | (*reinterpret_cast<T**>(dest))->~T(); | |||
98 | **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src); | |||
gvn | • load of type %"class.std::__cxx11::basic_string"* not eliminated because it is clobbered by call | cvflann::anyimpl::big_any_policy | ||
gvn | • load of type %"class.std::__cxx11::basic_string"* not eliminated in favor of load because it is clobbered by call | cvflann::anyimpl::big_any_policy | ||
99 | } | |||
100 | virtual void* get_value(void** src) CV_OVERRIDE { return *src; } | |||
101 | virtual const void* get_value(void* const * src) CV_OVERRIDE { return *src; } | |||
102 | 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 | { | |||
107 | out << int(*reinterpret_cast<flann_centers_init_t const*>(*src)); | |||
108 | } | |||
109 | ||||
110 | template<> inline void big_any_policy<flann_algorithm_t>::print(std::ostream& out, void* const* src) | |||
111 | { | |||
112 | out << int(*reinterpret_cast<flann_algorithm_t const*>(*src)); | |||
113 | } | |||
114 | ||||
115 | template<> inline void big_any_policy<cv::String>::print(std::ostream& out, void* const* src) | |||
116 | { | |||
117 | out << (*reinterpret_cast<cv::String const*>(*src)).c_str(); | |||
118 | } | |||
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 | { | |||
176 | static typename choose_policy<T>::type policy; | |||
177 | return &policy; | |||
178 | } | |||
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 | { } | |||
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 | { | |||
207 | assign(x); | |||
208 | } | |||
209 | ||||
210 | /// Copy constructor. | |||
211 | any(const any& x) | |||
212 | : policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL) | |||
213 | { | |||
214 | assign(x); | |||
215 | } | |||
216 | ||||
217 | /// Destructor. | |||
218 | ~any() | |||
219 | { | |||
220 | policy->static_delete(&object); | |||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by call | std::_Rb_tree | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8**)** not eliminated because it is clobbered by call | std::_Rb_tree | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8**)* not eliminated because it is clobbered by call | std::_Rb_tree | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by invoke | std::_Rb_tree | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated in favor of store because it is clobbered by invoke | std::_Rb_tree | ||
0 | ...1 similar remarks omitted. | |||
221 | } | |||
222 | ||||
223 | /// Assignment function from another any. | |||
224 | any& assign(const any& x) | |||
225 | { | |||
226 | reset(); | |||
227 | policy = x.policy; | |||
0 | ^ | |||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by store | cvflann::any::assign | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by store | cvflann::any::any | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by store | std::pair | ||
228 | policy->clone(&x.object, &object); | |||
0 | ^ | |||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8**, i8**)* not eliminated because it is clobbered by store | cvflann::any::assign | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8**, i8**)* not eliminated because it is clobbered by store | cvflann::any::any | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8**, i8**)* not eliminated because it is clobbered by store | std::pair | ||
229 | return *this; | |||
230 | } | |||
231 | ||||
232 | /// Assignment function. | |||
233 | template <typename T> | |||
234 | any& assign(const T& x) | |||
235 | { | |||
236 | reset(); | |||
237 | policy = anyimpl::SinglePolicy<T>::get_policy(); | |||
238 | policy->copy_from_value(&x, &object); | |||
0 | ^ | |||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8*, i8**)** not eliminated because it is clobbered by call | cvflann::any::assign | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8*, i8**)** not eliminated because it is clobbered by load | cvflann::any::assign | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8*, i8**)* not eliminated because it is clobbered by store | cvflann::any::assign | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8*, i8**)** not eliminated because it is clobbered by call | cvflann::any::operator= | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8*, i8**)** not eliminated because it is clobbered by load | cvflann::any::operator= | ||
0 | ...263 similar remarks omitted. | |||
0 | ^ | |||
licm | • failed to move load with loop-invariant address because the loop may invalidate its value | cvflann::AutotunedIndex | ||
licm | • failed to move load with loop-invariant address because the loop may invalidate its value | cvflann::AutotunedIndex | ||
licm | • failed to move load with loop-invariant address because the loop may invalidate its value | cvflann::AutotunedIndex | ||
licm | • failed to move load with loop-invariant address because the loop may invalidate its value | cvflann::AutotunedIndex | ||
239 | return *this; | |||
240 | } | |||
241 | ||||
242 | /// Assignment operator. | |||
243 | template<typename T> | |||
244 | any& operator=(const T& x) | |||
245 | { | |||
246 | return assign(x); | |||
247 | } | |||
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 | { | |||
252 | return assign(x); | |||
253 | } | |||
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 | { | |||
259 | return assign(x); | |||
260 | } | |||
261 | ||||
262 | /// Utility functions | |||
263 | any& swap(any& x) | |||
264 | { | |||
265 | std::swap(policy, x.policy); | |||
266 | std::swap(object, x.object); | |||
267 | return *this; | |||
268 | } | |||
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(); | |||
0 | ^ | |||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by call | cv::flann::getParam | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by call | cv::flann::getParam | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by call | cv::flann::IndexParams::getInt | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by call | cv::flann::getParam | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by call | cv::flann::IndexParams::getDouble | ||
0 | ...10 similar remarks omitted. | |||
284 | const T* r = reinterpret_cast<const T*>(policy->get_value(&object)); | |||
0 | ^ | |||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated in favor of load because it is clobbered by call | cvflann::any::cast | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated in favor of load because it is clobbered by call | cv::flann::getParam | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated in favor of load because it is clobbered by call | cvflann::any::cast | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated in favor of load because it is clobbered by call | cv::flann::getParam | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated in favor of load because it is clobbered by call | cv::flann::IndexParams::getInt | ||
0 | ...65 similar remarks omitted. | |||
285 | return *r; | |||
286 | } | |||
287 | ||||
288 | /// Returns true if the any contains no value. | |||
289 | bool empty() const | |||
290 | { | |||
291 | return policy->type() == typeid(anyimpl::empty_any); | |||
292 | } | |||
293 | ||||
294 | /// Frees any allocated memory, and sets the value to NULL. | |||
295 | void reset() | |||
296 | { | |||
297 | policy->static_delete(&object); | |||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by call | cv::flann::setParam | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8**)** not eliminated because it is clobbered by call | cv::flann::setParam | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8**)* not eliminated because it is clobbered by call | cv::flann::setParam | ||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by call | cv::flann::setParam | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, i8**)** not eliminated because it is clobbered by call | cv::flann::setParam | ||
0 | ...165 similar remarks omitted. | |||
298 | policy = anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy(); | |||
299 | } | |||
300 | ||||
301 | /// Returns true if the two types are the same. | |||
302 | bool compatible(const any& x) const | |||
303 | { | |||
304 | return policy->type() == x.policy->type(); | |||
305 | } | |||
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 | { | |||
316 | return policy->type(); | |||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated in favor of load because it is clobbered by call | cv::flann::IndexParams::getAll | ||
gvn | • load of type %"class.std::type_info"* (%"struct.cvflann::anyimpl::base_any_policy"*)** not eliminated because it is clobbered by call | cv::flann::IndexParams::getAll | ||
gvn | • load of type %"class.std::type_info"* (%"struct.cvflann::anyimpl::base_any_policy"*)* not eliminated because it is clobbered by call | cv::flann::IndexParams::getAll | ||
317 | } | |||
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 | { | |||
324 | any_val.policy->print(out,&any_val.object); | |||
gvn | • load of type %"struct.cvflann::anyimpl::base_any_policy"* not eliminated because it is clobbered by call | cvflann::print_params | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, %"class.std::basic_ostream"*, i8**)** not eliminated because it is clobbered by call | cvflann::print_params | ||
gvn | • load of type void (%"struct.cvflann::anyimpl::base_any_policy"*, %"class.std::basic_ostream"*, i8**)* not eliminated because it is clobbered by call | cvflann::print_params | ||
325 | return out; | |||
326 | } | |||
327 | ||||
328 | } | |||
329 | ||||
330 | //! @endcond | |||
331 | ||||
332 | #endif // OPENCV_FLANN_ANY_H_ | |||
333 | ||||
334 |