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)
82  delete (* reinterpret_cast<T**>(x));
83  *x = NULL;
84  }
85  virtual void copy_from_value(void const* src, void** dest)
86  {
87  *dest = new T(*reinterpret_cast<T const*>(src));
88  }
89  virtual void clone(void* const* src, void** dest)
90  {
91  *dest = new T(**reinterpret_cast<T* const*>(src));
92  }
93  virtual void move(void* const* src, void** dest)
94  {
95  (*reinterpret_cast<T**>(dest))->~T();
96  **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
97  }
98  virtual void* get_value(void** src) { return *src; }
99  virtual const void* get_value(void* const * src) { return *src; }
100  virtual void print(std::ostream& out, void* const* src) { out << *reinterpret_cast<T const*>(*src); }
101 };
102 
103 template<typename T>
105 {
107 };
108 
109 template<typename T>
110 struct choose_policy<T*>
111 {
113 };
114 
115 struct any;
116 
119 template<>
121 {
122  typedef void type;
123 };
124 
126 #define SMALL_POLICY(TYPE) \
127  template<> \
128  struct choose_policy<TYPE> { typedef small_any_policy<TYPE> type; \
129  }
130 
131 SMALL_POLICY(signed char);
132 SMALL_POLICY(unsigned char);
133 SMALL_POLICY(signed short);
134 SMALL_POLICY(unsigned short);
135 SMALL_POLICY(signed int);
136 SMALL_POLICY(unsigned int);
137 SMALL_POLICY(signed long);
138 SMALL_POLICY(unsigned long);
139 SMALL_POLICY(float);
140 SMALL_POLICY(bool);
141 
142 //#undef SMALL_POLICY
143 
145 template<typename T>
147 {
148  static typename choose_policy<T>::type policy;
149  return &policy;
150 }
151 } // namespace anyimpl
152 
153 class any
154 {
155  typedef any any_t; // workaround for the NVCC compiler under windows
156 private:
157  // fields
159  void* object;
160 
161 public:
163  template <typename T>
164  any(const T& x)
165  : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
166  {
167  assign(x);
168  }
169 
171  any()
172  : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
173  { }
174 
176  any(const char* x)
177  : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
178  {
179  assign(x);
180  }
181 
183  any(const any& x)
184  : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
185  {
186  assign(x);
187  }
188 
191  {
192  policy->static_delete(&object);
193  }
194 
196  any& assign(const any& x)
197  {
198  reset();
199  policy = x.policy;
200  policy->clone(&x.object, &object);
201  return *this;
202  }
203 
205  template <typename T>
206  any_t& assign(const T& x)
207  {
208  reset();
209  policy = anyimpl::get_policy<T>();
210  policy->copy_from_value(&x, &object);
211  return *this;
212  }
213 
215  template<typename T>
216  any_t& operator=(const T& x)
217  {
218  return assign(x);
219  }
220 
223  any& operator=(const char* x)
224  {
225  return assign(x);
226  }
227 
229  any& swap(any& x)
230  {
231  std::swap(policy, x.policy);
232  std::swap(object, x.object);
233  return *this;
234  }
235 
237  template<typename T>
238  T& cast()
239  {
240  if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
241  T* r = reinterpret_cast<T*>(policy->get_value(&object));
242  return *r;
243  }
244 
246  template<typename T>
247  const T& cast() const
248  {
249  if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
250  const T* r = reinterpret_cast<const T*>(policy->get_value(&object));
251  return *r;
252  }
253 
255  bool empty() const
256  {
257  return policy->type() == typeid(anyimpl::empty_any);
258  }
259 
261  void reset()
262  {
263  policy->static_delete(&object);
264  policy = anyimpl::get_policy<anyimpl::empty_any>();
265  }
266 
268  bool compatible(const any& x) const
269  {
270  return policy->type() == x.policy->type();
271  }
272 
274  template<typename T>
275  bool has_type()
276  {
277  return policy->type() == typeid(T);
278  }
279 
280  const std::type_info& type() const
281  {
282  return policy->type();
283  }
284 
285  friend std::ostream& operator <<(std::ostream& out, const any& any_val);
286 };
287 
288 inline std::ostream& operator <<(std::ostream& out, const any& any_val)
289 {
290  any_val.policy->print(out,&any_val.object);
291  return out;
292 }
293 
294 }
295 
296 #endif // FLANN_ANY_H_
rtflann::anyimpl::operator<<
std::ostream & operator<<(std::ostream &out, const empty_any &)
Definition: any.h:34
rtflann::anyimpl::get_policy
base_any_policy * get_policy()
This function will return a different policy for each type.
Definition: any.h:146
rtflann::any::any_t
any any_t
Definition: any.h:155
rtflann::anyimpl::base_any_policy::clone
virtual void clone(void *const *src, void **dest)=0
rtflann::any::type
const std::type_info & type() const
Definition: any.h:280
rtflann::any::swap
any & swap(any &x)
Utility functions.
Definition: any.h:229
rtflann::anyimpl::base_any_policy::static_delete
virtual void static_delete(void **x)=0
rtflann::anyimpl::base_any_policy::get_value
virtual void * get_value(void **src)=0
rtflann::any::any
any()
Empty constructor.
Definition: any.h:171
rtflann::anyimpl::big_any_policy::get_value
virtual void * get_value(void **src)
Definition: any.h:98
rtflann::anyimpl::base_any_policy::print
virtual void print(std::ostream &out, void *const *src)=0
rtflann::anyimpl::small_any_policy::get_value
virtual void * get_value(void **src)
Definition: any.h:71
rtflann::anyimpl::typed_base_any_policy::type
virtual const std::type_info & type()
Definition: any.h:57
rtflann::anyimpl::big_any_policy::static_delete
virtual void static_delete(void **x)
Definition: any.h:79
rtflann::anyimpl::big_any_policy::clone
virtual void clone(void *const *src, void **dest)
Definition: any.h:89
rtflann::anyimpl::SMALL_POLICY
SMALL_POLICY(signed char)
rtflann::anyimpl::big_any_policy
Definition: any.h:77
rtflann::anyimpl::small_any_policy::static_delete
virtual void static_delete(void **)
Definition: any.h:64
rtflann::anyimpl::base_any_policy::type
virtual const std::type_info & type()=0
rtflann::anyimpl::choose_policy::type
big_any_policy< T > type
Definition: any.h:106
rtflann::anyimpl::bad_any_cast::bad_any_cast
bad_any_cast()
Definition: any.h:27
rtflann::any::compatible
bool compatible(const any &x) const
Returns true if the two types are the same.
Definition: any.h:268
rtflann::anyimpl::base_any_policy
Definition: any.h:40
rtflann::anyimpl::small_any_policy::move
virtual void move(void *const *src, void **dest)
Definition: any.h:70
rtflann::anyimpl::bad_any_cast
Definition: any.h:25
rtflann::anyimpl::base_any_policy::move
virtual void move(void *const *src, void **dest)=0
rtflann::any::object
void * object
Definition: any.h:159
rtflann::any
Definition: any.h:153
rtflann::any::operator<<
friend std::ostream & operator<<(std::ostream &out, const any &any_val)
Definition: any.h:288
rtflann::any::has_type
bool has_type()
Returns if the type is compatible with the policy.
Definition: any.h:275
rtflann::anyimpl::big_any_policy::copy_from_value
virtual void copy_from_value(void const *src, void **dest)
Definition: any.h:85
rtflann::any::reset
void reset()
Frees any allocated memory, and sets the value to NULL.
Definition: any.h:261
std::swap
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)
rtflann::any::any
any(const any &x)
Copy constructor.
Definition: any.h:183
rtflann::anyimpl::small_any_policy::get_value
virtual const void * get_value(void *const *src)
Definition: any.h:72
object
x
x
Eigen::Triplet< double >
rtflann::anyimpl::typed_base_any_policy
Definition: any.h:54
out
std::ofstream out("Result.txt")
rtflann::any::policy
anyimpl::base_any_policy * policy
Definition: any.h:158
rtflann::anyimpl::big_any_policy::print
virtual void print(std::ostream &out, void *const *src)
Definition: any.h:100
rtflann::anyimpl::big_any_policy::move
virtual void move(void *const *src, void **dest)
Definition: any.h:93
rtflann::operator<<
std::ostream & operator<<(std::ostream &out, const any &any_val)
Definition: any.h:288
rtflann::anyimpl::small_any_policy::print
virtual void print(std::ostream &out, void *const *src)
Definition: any.h:73
rtflann::anyimpl::choose_policy< T * >::type
small_any_policy< T * > type
Definition: any.h:112
rtflann::anyimpl::big_any_policy::get_value
virtual const void * get_value(void *const *src)
Definition: any.h:99
rtflann::anyimpl::empty_any
Definition: any.h:30
rtflann::any::cast
T & cast()
Cast operator. You can only cast to the original type.
Definition: any.h:238
rtflann::anyimpl::small_any_policy
Definition: any.h:62
dest
const char char * dest
Definition: lz4hc.h:181
rtflann::anyimpl::choose_policy< any >::type
void type
Definition: any.h:122
rtflann::any::operator=
any & operator=(const char *x)
Definition: any.h:223
rtflann::any::assign
any & assign(const any &x)
Assignment function from another any.
Definition: any.h:196
std
rtflann::any::operator=
any_t & operator=(const T &x)
Assignment operator.
Definition: any.h:216
rtflann::any::cast
const T & cast() const
Cast operator. You can only cast to the original type.
Definition: any.h:247
rtflann::any::any
any(const char *x)
Special initializing constructor for string literals.
Definition: any.h:176
rtflann::any::empty
bool empty() const
Returns true if the any contains no value.
Definition: any.h:255
rtflann::anyimpl::small_any_policy::copy_from_value
virtual void copy_from_value(void const *src, void **dest)
Definition: any.h:65
rtflann::anyimpl::base_any_policy::copy_from_value
virtual void copy_from_value(void const *src, void **dest)=0
rtflann::any::any
any(const T &x)
Initializing constructor.
Definition: any.h:164
NULL
#define NULL
rtflann::any::~any
~any()
Destructor.
Definition: any.h:190
rtflann::anyimpl::small_any_policy::clone
virtual void clone(void *const *src, void **dest)
Definition: any.h:69
rtflann::anyimpl::choose_policy
Definition: any.h:104
rtflann
Definition: all_indices.h:49
rtflann::any::assign
any_t & assign(const T &x)
Assignment function.
Definition: any.h:206
rtflann::anyimpl::typed_base_any_policy::get_size
virtual ::size_t get_size()
Definition: any.h:56
rtflann::anyimpl::base_any_policy::get_size
virtual ::size_t get_size()=0


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:06