1 #ifndef OPENCV_FLANN_ANY_H_
2 #define OPENCV_FLANN_ANY_H_
4 * (C) Copyright Christopher Diggins 2005-2011
5 * (C) Copyright Pablo Aguilar 2005
6 * (C) Copyright Kevlin Henney 2001
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
12 * Adapted for FLANN by Marius Muja
34 inline std::ostream& operator <<(std::ostream& out, const empty_any&)
40 struct base_any_policy
42 virtual void static_delete(void** x) = 0;
43 virtual void copy_from_value(void const* src, void** dest) = 0;
44 virtual void clone(void* const* src, void** dest) = 0;
45 virtual void move(void* const* src, void** dest) = 0;
46 virtual void* get_value(void** src) = 0;
47 virtual ::size_t get_size() = 0;
48 virtual const std::type_info& type() = 0;
49 virtual void print(std::ostream& out, void* const* src) = 0;
51 #ifdef OPENCV_CAN_BREAK_BINARY_COMPATIBILITY
52 virtual ~base_any_policy() {}
57 struct typed_base_any_policy : base_any_policy
59 virtual ::size_t get_size() { return sizeof(T); }
60 virtual const std::type_info& type() { return typeid(T); }
65 struct small_any_policy : typed_base_any_policy<T>
67 virtual void static_delete(void**) { }
68 virtual void copy_from_value(void const* src, void** dest)
70 new (dest) T(* reinterpret_cast<T const*>(src));
72 virtual void clone(void* const* src, void** dest) { *dest = *src; }
73 virtual void move(void* const* src, void** dest) { *dest = *src; }
74 virtual void* get_value(void** src) { return reinterpret_cast<void*>(src); }
75 virtual void print(std::ostream& out, void* const* src) { out << *reinterpret_cast<T const*>(src); }
79 struct big_any_policy : typed_base_any_policy<T>
81 virtual void static_delete(void** x)
83 if (* x) delete (* reinterpret_cast<T**>(x)); *x = NULL;
85 virtual void copy_from_value(void const* src, void** dest)
87 *dest = new T(*reinterpret_cast<T const*>(src));
89 virtual void clone(void* const* src, void** dest)
91 *dest = new T(**reinterpret_cast<T* const*>(src));
93 virtual void move(void* const* src, void** dest)
95 (*reinterpret_cast<T**>(dest))->~T();
96 **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
98 virtual void* get_value(void** src) { return *src; }
99 virtual void print(std::ostream& out, void* const* src) { out << *reinterpret_cast<T const*>(*src); }
102 template<> inline void big_any_policy<flann_centers_init_t>::print(std::ostream& out, void* const* src)
104 out << int(*reinterpret_cast<flann_centers_init_t const*>(*src));
107 template<> inline void big_any_policy<flann_algorithm_t>::print(std::ostream& out, void* const* src)
109 out << int(*reinterpret_cast<flann_algorithm_t const*>(*src));
115 typedef big_any_policy<T> type;
119 struct choose_policy<T*>
121 typedef small_any_policy<T*> type;
126 /// Choosing the policy for an any type is illegal, but should never happen.
127 /// This is designed to throw a compiler error.
129 struct choose_policy<any>
134 /// Specializations for small types.
135 #define SMALL_POLICY(TYPE) \
137 struct choose_policy<TYPE> { typedef small_any_policy<TYPE> type; \
140 SMALL_POLICY(signed char);
141 SMALL_POLICY(unsigned char);
142 SMALL_POLICY(signed short);
143 SMALL_POLICY(unsigned short);
144 SMALL_POLICY(signed int);
145 SMALL_POLICY(unsigned int);
146 SMALL_POLICY(signed long);
147 SMALL_POLICY(unsigned long);
153 /// This function will return a different policy for each type.
155 base_any_policy* get_policy()
157 static typename choose_policy<T>::type policy;
160 } // namespace anyimpl
166 anyimpl::base_any_policy* policy;
170 /// Initializing constructor.
171 template <typename T>
173 : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
178 /// Empty constructor.
180 : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
183 /// Special initializing constructor for string literals.
185 : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
190 /// Copy constructor.
192 : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
200 policy->static_delete(&object);
203 /// Assignment function from another any.
204 any& assign(const any& x)
208 policy->clone(&x.object, &object);
212 /// Assignment function.
213 template <typename T>
214 any& assign(const T& x)
217 policy = anyimpl::get_policy<T>();
218 policy->copy_from_value(&x, &object);
222 /// Assignment operator.
224 any& operator=(const T& x)
229 /// Assignment operator, specialed for literal strings.
230 /// They have types like const char [6] which don't work as expected.
231 any& operator=(const char* x)
236 /// Utility functions
239 std::swap(policy, x.policy);
240 std::swap(object, x.object);
244 /// Cast operator. You can only cast to the original type.
248 if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
249 T* r = reinterpret_cast<T*>(policy->get_value(&object));
253 /// Cast operator. You can only cast to the original type.
255 const T& cast() const
257 if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
258 void* obj = const_cast<void*>(object);
259 T* r = reinterpret_cast<T*>(policy->get_value(&obj));
263 /// Returns true if the any contains no value.
266 return policy->type() == typeid(anyimpl::empty_any);
269 /// Frees any allocated memory, and sets the value to NULL.
272 policy->static_delete(&object);
273 policy = anyimpl::get_policy<anyimpl::empty_any>();
276 /// Returns true if the two types are the same.
277 bool compatible(const any& x) const
279 return policy->type() == x.policy->type();
282 /// Returns if the type is compatible with the policy
286 return policy->type() == typeid(T);
289 const std::type_info& type() const
291 return policy->type();
294 friend std::ostream& operator <<(std::ostream& out, const any& any_val);
297 inline std::ostream& operator <<(std::ostream& out, const any& any_val)
299 any_val.policy->print(out,&any_val.object);
305 #endif // OPENCV_FLANN_ANY_H_