any.h
Go to the documentation of this file.
1 #ifndef RTABMAP_FLANN_ANY_H_
2 #define RTABMAP_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 #include <stdexcept>
16 #include <ostream>
17 #include <typeinfo>
18 
19 namespace rtflann
20 {
21 
22 namespace anyimpl
23 {
24 
25 struct bad_any_cast : public std::runtime_error
26 {
27  bad_any_cast() : std::runtime_error("Cannot convert 'any' value") { }
28 };
29 
30 struct empty_any
31 {
32 };
33 
34 inline std::ostream& operator <<(std::ostream& out, const empty_any&)
35 {
36  out << "[empty_any]";
37  return out;
38 }
39 
41 {
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 const void* get_value(void* const * src) = 0;
48  virtual ::size_t get_size() = 0;
49  virtual const std::type_info& type() = 0;
50  virtual void print(std::ostream& out, void* const* src) = 0;
51 };
52 
53 template<typename T>
55 {
56  virtual ::size_t get_size() { return sizeof(T); }
57  virtual const std::type_info& type() { return typeid(T); }
58 
59 };
60 
61 template<typename T>
63 {
64  virtual void static_delete(void**) { }
65  virtual void copy_from_value(void const* src, void** dest)
66  {
67  new (dest) T(* reinterpret_cast<T const*>(src));
68  }
69  virtual void clone(void* const* src, void** dest) { *dest = *src; }
70  virtual void move(void* const* src, void** dest) { *dest = *src; }
71  virtual void* get_value(void** src) { return reinterpret_cast<void*>(src); }
72  virtual const void* get_value(void* const * src) { return reinterpret_cast<const void*>(src); }
73  virtual void print(std::ostream& out, void* const* src) { out << *reinterpret_cast<T const*>(src); }
74 };
75 
76 template<typename T>
78 {
79  virtual void static_delete(void** x)
80  {
81  if (* x) delete (* reinterpret_cast<T**>(x)); *x = NULL;
82  }
83  virtual void copy_from_value(void const* src, void** dest)
84  {
85  *dest = new T(*reinterpret_cast<T const*>(src));
86  }
87  virtual void clone(void* const* src, void** dest)
88  {
89  *dest = new T(**reinterpret_cast<T* const*>(src));
90  }
91  virtual void move(void* const* src, void** dest)
92  {
93  (*reinterpret_cast<T**>(dest))->~T();
94  **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
95  }
96  virtual void* get_value(void** src) { return *src; }
97  virtual const void* get_value(void* const * src) { return *src; }
98  virtual void print(std::ostream& out, void* const* src) { out << *reinterpret_cast<T const*>(*src); }
99 };
100 
101 template<typename T>
103 {
105 };
106 
107 template<typename T>
108 struct choose_policy<T*>
109 {
111 };
112 
113 struct any;
114 
117 template<>
119 {
120  typedef void type;
121 };
122 
124 #define SMALL_POLICY(TYPE) \
125  template<> \
126  struct choose_policy<TYPE> { typedef small_any_policy<TYPE> type; \
127  }
128 
129 SMALL_POLICY(signed char);
130 SMALL_POLICY(unsigned char);
131 SMALL_POLICY(signed short);
132 SMALL_POLICY(unsigned short);
133 SMALL_POLICY(signed int);
134 SMALL_POLICY(unsigned int);
135 SMALL_POLICY(signed long);
136 SMALL_POLICY(unsigned long);
137 SMALL_POLICY(float);
138 SMALL_POLICY(bool);
139 
140 //#undef SMALL_POLICY
141 
143 template<typename T>
145 {
146  static typename choose_policy<T>::type policy;
147  return &policy;
148 }
149 } // namespace anyimpl
150 
151 class any
152 {
153  typedef any any_t; // workaround for the NVCC compiler under windows
154 private:
155  // fields
157  void* object;
158 
159 public:
161  template <typename T>
162  any(const T& x)
163  : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
164  {
165  assign(x);
166  }
167 
169  any()
170  : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
171  { }
172 
174  any(const char* x)
175  : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
176  {
177  assign(x);
178  }
179 
181  any(const any& x)
182  : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
183  {
184  assign(x);
185  }
186 
189  {
190  policy->static_delete(&object);
191  }
192 
194  any& assign(const any& x)
195  {
196  reset();
197  policy = x.policy;
198  policy->clone(&x.object, &object);
199  return *this;
200  }
201 
203  template <typename T>
204  any_t& assign(const T& x)
205  {
206  reset();
207  policy = anyimpl::get_policy<T>();
208  policy->copy_from_value(&x, &object);
209  return *this;
210  }
211 
213  template<typename T>
214  any_t& operator=(const T& x)
215  {
216  return assign(x);
217  }
218 
221  any& operator=(const char* x)
222  {
223  return assign(x);
224  }
225 
227  any& swap(any& x)
228  {
229  std::swap(policy, x.policy);
230  std::swap(object, x.object);
231  return *this;
232  }
233 
235  template<typename T>
236  T& cast()
237  {
238  if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
239  T* r = reinterpret_cast<T*>(policy->get_value(&object));
240  return *r;
241  }
242 
244  template<typename T>
245  const T& cast() const
246  {
247  if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
248  const T* r = reinterpret_cast<const T*>(policy->get_value(&object));
249  return *r;
250  }
251 
253  bool empty() const
254  {
255  return policy->type() == typeid(anyimpl::empty_any);
256  }
257 
259  void reset()
260  {
261  policy->static_delete(&object);
262  policy = anyimpl::get_policy<anyimpl::empty_any>();
263  }
264 
266  bool compatible(const any& x) const
267  {
268  return policy->type() == x.policy->type();
269  }
270 
272  template<typename T>
273  bool has_type()
274  {
275  return policy->type() == typeid(T);
276  }
277 
278  const std::type_info& type() const
279  {
280  return policy->type();
281  }
282 
283  friend std::ostream& operator <<(std::ostream& out, const any& any_val);
284 };
285 
286 inline std::ostream& operator <<(std::ostream& out, const any& any_val)
287 {
288  any_val.policy->print(out,&any_val.object);
289  return out;
290 }
291 
292 }
293 
294 #endif // FLANN_ANY_H_
virtual void * get_value(void **src)=0
virtual void static_delete(void **x)
Definition: any.h:79
#define NULL
big_any_policy< T > type
Definition: any.h:104
any_t & assign(const T &x)
Assignment function.
Definition: any.h:204
any()
Empty constructor.
Definition: any.h:169
any(const T &x)
Initializing constructor.
Definition: any.h:162
virtual const void * get_value(void *const *src)
Definition: any.h:72
SMALL_POLICY(signed char)
base_any_policy * get_policy()
This function will return a different policy for each type.
Definition: any.h:144
virtual void * get_value(void **src)
Definition: any.h:96
virtual void copy_from_value(void const *src, void **dest)=0
virtual const std::type_info & type()
Definition: any.h:57
virtual void copy_from_value(void const *src, void **dest)
Definition: any.h:65
virtual void copy_from_value(void const *src, void **dest)
Definition: any.h:83
void * object
Definition: any.h:157
ROSCONSOLE_DECL void print(FilterBase *filter, void *logger, Level level, const char *file, int line, const char *function, const char *fmt,...) ROSCONSOLE_PRINTF_ATTRIBUTE(7
virtual void print(std::ostream &out, void *const *src)
Definition: any.h:73
void reset()
Frees any allocated memory, and sets the value to NULL.
Definition: any.h:259
virtual const void * get_value(void *const *src)
Definition: any.h:97
virtual void print(std::ostream &out, void *const *src)=0
const std::type_info & type() const
Definition: any.h:278
bool has_type()
Returns if the type is compatible with the policy.
Definition: any.h:273
std::ostream & operator<<(std::ostream &out, const empty_any &)
Definition: any.h:34
virtual const std::type_info & type()=0
virtual void static_delete(void **x)=0
virtual void * get_value(void **src)
Definition: any.h:71
bool compatible(const any &x) const
Returns true if the two types are the same.
Definition: any.h:266
~any()
Destructor.
Definition: any.h:188
virtual void move(void *const *src, void **dest)
Definition: any.h:91
any(const char *x)
Special initializing constructor for string literals.
Definition: any.h:174
virtual void clone(void *const *src, void **dest)=0
any(const any &x)
Copy constructor.
Definition: any.h:181
anyimpl::base_any_policy * policy
Definition: any.h:156
const T & cast() const
Cast operator. You can only cast to the original type.
Definition: any.h:245
const char char * dest
Definition: lz4hc.h:181
T & cast()
Cast operator. You can only cast to the original type.
Definition: any.h:236
virtual void clone(void *const *src, void **dest)
Definition: any.h:87
virtual ::size_t get_size()
Definition: any.h:56
any & operator=(const char *x)
Definition: any.h:221
any & assign(const any &x)
Assignment function from another any.
Definition: any.h:194
virtual void clone(void *const *src, void **dest)
Definition: any.h:69
virtual void move(void *const *src, void **dest)
Definition: any.h:70
virtual void print(std::ostream &out, void *const *src)
Definition: any.h:98
any_t & operator=(const T &x)
Assignment operator.
Definition: any.h:214
any & swap(any &x)
Utility functions.
Definition: any.h:227
virtual void static_delete(void **)
Definition: any.h:64
any any_t
Definition: any.h:153
small_any_policy< T * > type
Definition: any.h:110
bool empty() const
Returns true if the any contains no value.
Definition: any.h:253


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:30