EstervQrCode 2.0.0
Library for qr code manipulation
Loading...
Searching...
No Matches
any.h
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
16
17#include "defines.h"
18#include <stdexcept>
19#include <ostream>
20#include <typeinfo>
21
22#include "opencv2/core/cvdef.h"
23#include "opencv2/core/utility.hpp"
24
25namespace cvflann
26{
27
28namespace anyimpl
29{
30
31struct bad_any_cast : public std::exception
32{
33 bad_any_cast() = default;
34
35 bad_any_cast(const char* src, const char* dst)
36 : message_(cv::format("cvflann::bad_any_cast(from %s to %s)", src, dst)) {}
37
38
39 const char* what() const noexcept override
40 {
41 return message_.c_str();
42 }
43
44private:
45 std::string message_{"cvflann::bad_any_cast"};
46};
47
48#ifndef CV_THROW_IF_TYPE_MISMATCH
49#define CV_THROW_IF_TYPE_MISMATCH(src_type_info, dst_type_info) \
50 if ((src_type_info) != (dst_type_info)) \
51 throw cvflann::anyimpl::bad_any_cast((src_type_info).name(), \
52 (dst_type_info).name())
53#endif
54
55struct empty_any
56{
57};
58
59inline std::ostream& operator <<(std::ostream& out, const empty_any&)
60{
61 out << "[empty_any]";
62 return out;
63}
64
65struct base_any_policy
66{
67 virtual void static_delete(void** x) = 0;
68 virtual void copy_from_value(void const* src, void** dest) = 0;
69 virtual void clone(void* const* src, void** dest) = 0;
70 virtual void move(void* const* src, void** dest) = 0;
71 virtual void* get_value(void** src) = 0;
72 virtual const void* get_value(void* const * src) = 0;
73 virtual ::size_t get_size() = 0;
74 virtual const std::type_info& type() = 0;
75 virtual void print(std::ostream& out, void* const* src) = 0;
76 virtual ~base_any_policy() {}
77};
78
79template<typename T>
80struct typed_base_any_policy : base_any_policy
81{
82 virtual ::size_t get_size() CV_OVERRIDE { return sizeof(T); }
83 virtual const std::type_info& type() CV_OVERRIDE { return typeid(T); }
84
85};
86
87template<typename T>
88struct small_any_policy CV_FINAL : typed_base_any_policy<T>
89{
90 virtual void static_delete(void**) CV_OVERRIDE { }
91 virtual void copy_from_value(void const* src, void** dest) CV_OVERRIDE
92 {
93 new (dest) T(* reinterpret_cast<T const*>(src));
94 }
95 virtual void clone(void* const* src, void** dest) CV_OVERRIDE { *dest = *src; }
96 virtual void move(void* const* src, void** dest) CV_OVERRIDE { *dest = *src; }
97 virtual void* get_value(void** src) CV_OVERRIDE { return reinterpret_cast<void*>(src); }
98 virtual const void* get_value(void* const * src) CV_OVERRIDE { return reinterpret_cast<const void*>(src); }
99 virtual void print(std::ostream& out, void* const* src) CV_OVERRIDE { out << *reinterpret_cast<T const*>(src); }
100};
101
102template<typename T>
103struct big_any_policy CV_FINAL : typed_base_any_policy<T>
104{
105 virtual void static_delete(void** x) CV_OVERRIDE
106 {
107 if (* x) delete (* reinterpret_cast<T**>(x));
108 *x = NULL;
109 }
110 virtual void copy_from_value(void const* src, void** dest) CV_OVERRIDE
111 {
112 *dest = new T(*reinterpret_cast<T const*>(src));
113 }
114 virtual void clone(void* const* src, void** dest) CV_OVERRIDE
115 {
116 *dest = new T(**reinterpret_cast<T* const*>(src));
117 }
118 virtual void move(void* const* src, void** dest) CV_OVERRIDE
119 {
120 (*reinterpret_cast<T**>(dest))->~T();
121 **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
122 }
123 virtual void* get_value(void** src) CV_OVERRIDE { return *src; }
124 virtual const void* get_value(void* const * src) CV_OVERRIDE { return *src; }
125 virtual void print(std::ostream& out, void* const* src) CV_OVERRIDE { out << *reinterpret_cast<T const*>(*src); }
126};
127
128template<> inline void big_any_policy<flann_centers_init_t>::print(std::ostream& out, void* const* src)
129{
130 out << int(*reinterpret_cast<flann_centers_init_t const*>(*src));
131}
132
133template<> inline void big_any_policy<flann_algorithm_t>::print(std::ostream& out, void* const* src)
134{
135 out << int(*reinterpret_cast<flann_algorithm_t const*>(*src));
136}
137
138template<> inline void big_any_policy<cv::String>::print(std::ostream& out, void* const* src)
139{
140 out << (*reinterpret_cast<cv::String const*>(*src)).c_str();
141}
142
143template<typename T>
144struct choose_policy
145{
146 typedef big_any_policy<T> type;
147};
148
149template<typename T>
150struct choose_policy<T*>
151{
152 typedef small_any_policy<T*> type;
153};
154
155struct any;
156
159template<>
160struct choose_policy<any>
161{
162 typedef void type;
163};
164
166#define SMALL_POLICY(TYPE) \
167 template<> \
168 struct choose_policy<TYPE> { typedef small_any_policy<TYPE> type; \
169 }
170
171SMALL_POLICY(signed char);
172SMALL_POLICY(unsigned char);
173SMALL_POLICY(signed short);
174SMALL_POLICY(unsigned short);
175SMALL_POLICY(signed int);
176SMALL_POLICY(unsigned int);
177SMALL_POLICY(signed long);
178SMALL_POLICY(unsigned long);
179SMALL_POLICY(float);
180SMALL_POLICY(bool);
181
182#undef SMALL_POLICY
183
184template <typename T>
185class SinglePolicy
186{
187 SinglePolicy();
188 SinglePolicy(const SinglePolicy& other);
189 SinglePolicy& operator=(const SinglePolicy& other);
190
191public:
192 static base_any_policy* get_policy();
193};
194
196template <typename T>
197inline base_any_policy* SinglePolicy<T>::get_policy()
198{
199 static typename choose_policy<T>::type policy;
200 return &policy;
201}
202
203} // namespace anyimpl
204
205struct any
206{
207private:
208 // fields
209 anyimpl::base_any_policy* policy;
210 void* object;
211
212public:
214 template <typename T>
215 any(const T& x)
216 : policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
217 {
218 assign(x);
219 }
220
222 any()
223 : policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
224 { }
225
227 any(const char* x)
228 : policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
229 {
230 assign(x);
231 }
232
234 any(const any& x)
235 : policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
236 {
237 assign(x);
238 }
239
241 ~any()
242 {
243 policy->static_delete(&object);
244 }
245
247 any& assign(const any& x)
248 {
249 reset();
250 policy = x.policy;
251 policy->clone(&x.object, &object);
252 return *this;
253 }
254
256 template <typename T>
257 any& assign(const T& x)
258 {
259 reset();
260 policy = anyimpl::SinglePolicy<T>::get_policy();
261 policy->copy_from_value(&x, &object);
262 return *this;
263 }
264
266 template<typename T>
267 any& operator=(const T& x)
268 {
269 return assign(x);
270 }
271
273 any& operator=(const any& x)
274 {
275 return assign(x);
276 }
277
280 any& operator=(const char* x)
281 {
282 return assign(x);
283 }
284
286 any& swap(any& x)
287 {
288 std::swap(policy, x.policy);
289 std::swap(object, x.object);
290 return *this;
291 }
292
294 template<typename T>
295 T& cast()
296 {
297 CV_THROW_IF_TYPE_MISMATCH(policy->type(), typeid(T));
298 T* r = reinterpret_cast<T*>(policy->get_value(&object));
299 return *r;
300 }
301
303 template<typename T>
304 const T& cast() const
305 {
306 CV_THROW_IF_TYPE_MISMATCH(policy->type(), typeid(T));
307 const T* r = reinterpret_cast<const T*>(policy->get_value(&object));
308 return *r;
309 }
310
312 bool empty() const
313 {
314 return policy->type() == typeid(anyimpl::empty_any);
315 }
316
318 void reset()
319 {
320 policy->static_delete(&object);
321 policy = anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy();
322 }
323
325 bool compatible(const any& x) const
326 {
327 return policy->type() == x.policy->type();
328 }
329
331 template<typename T>
332 bool has_type()
333 {
334 return policy->type() == typeid(T);
335 }
336
337 const std::type_info& type() const
338 {
339 return policy->type();
340 }
341
342 friend std::ostream& operator <<(std::ostream& out, const any& any_val);
343};
344
345inline std::ostream& operator <<(std::ostream& out, const any& any_val)
346{
347 any_val.policy->print(out,&any_val.object);
348 return out;
349}
350
351}
352
354
355#endif // OPENCV_FLANN_ANY_H_
InputArrayOfArrays InputArrayOfArrays InputOutputArray InputOutputArray InputOutputArray InputOutputArray Size InputOutputArray InputOutputArray T
Definition calib3d.hpp:1867
CvArr * dst
Definition core_c.h:875
int int type
Definition core_c.h:221
const CvArr CvArr * x
Definition core_c.h:1195
#define CV_OVERRIDE
Definition cvdef.h:792
#define CV_FINAL
Definition cvdef.h:796
CV_EXPORTS String format(const char *fmt,...) CV_FORMAT_PRINTF(1
Returns a text string formatted using the printf-like expression.
CvRect r
Definition imgproc_c.h:984
T move(T... args)
"black box" representation of the file storage associated with a file on disk.
Definition calib3d.hpp:441
Definition flann.hpp:60
QTextStream & reset(QTextStream &stream)
T swap(T... args)