Bayes Filters Library
any.h
Go to the documentation of this file.
1 
52 #ifndef ANY_H
53 #define ANY_H
54 
55 #include <algorithm>
56 #include <memory>
57 #include <stdexcept>
58 #include <typeinfo>
59 #include <type_traits>
60 
61 
62 namespace bfl
63 {
64 namespace any
65 {
66 
77 class any
78 {
79 public:
85  any() noexcept :
86  content(nullptr)
87  { }
88 
89 
98  any(const any& other) :
99  content(other.content ? other.content->clone() : nullptr)
100  { }
101 
102 
110  any(any&& other) noexcept :
111  content(other.content)
112  {
113  other.content = nullptr;
114  }
115 
116 
124  template<typename ValueType>
125  any(const ValueType& value) :
126  content(new holder<typename std::remove_cv<typename std::decay<const ValueType>::type>::type>(value))
127  { }
128 
129 
137  template<typename ValueType>
138  any(ValueType&& value, typename std::enable_if<!std::is_same<any&, ValueType>::value>::type* = nullptr, typename std::enable_if<!std::is_const<ValueType>::value>::type* = nullptr) :
139  content(new holder<typename std::decay<ValueType>::type>(static_cast<ValueType&&>(value)))
140  { }
141 
142 
152  any& operator=(const any& rhs)
153  {
154  any(rhs).swap(*this);
155  return *this;
156  }
157 
158 
168  any& operator=(any&& rhs) noexcept
169  {
170  if (this == &rhs)
171  return *this;
172 
173  rhs.swap(*this);
174  any().swap(rhs);
175 
176  return *this;
177  }
178 
179 
191  template <class ValueType>
192  any& operator=(ValueType&& rhs)
193  {
194  any(static_cast<ValueType&&>(rhs)).swap(*this);
195 
196  return *this;
197  }
198 
199 
203  ~any() noexcept
204  {
205  delete content;
206  }
207 
208 
212  void reset() noexcept
213  {
214  any().swap(*this);
215  }
216 
217 
223  any& swap(any& rhs) noexcept
224  {
225  std::swap(content, rhs.content);
226 
227  return *this;
228  }
229 
235  bool has_value() const noexcept
236  {
237  return content;
238  }
239 
240 
246  const std::type_info& type() const noexcept
247  {
248  return content ? content->type() : typeid(void);
249  }
250 
251 
252 private:
254  {
255  public:
256  virtual ~placeholder()
257  { }
258 
259 
260  virtual const std::type_info& type() const noexcept = 0;
261 
262 
263  virtual placeholder* clone() const = 0;
264 
265  };
266 
267 
268  template<typename ValueType>
269  class holder : public placeholder
270  {
271  public:
272  holder(const ValueType& value) :
273  held(value)
274  { }
275 
276 
277  holder(ValueType&& value) :
278  held(static_cast<ValueType&&>(value))
279  { }
280 
281 
282  virtual const std::type_info& type() const noexcept
283  {
284  return typeid(ValueType);
285  }
286 
287 
288  virtual placeholder* clone() const
289  {
290  return new holder(held);
291  }
292 
293 
294  ValueType held;
295 
296 
297  private:
298  /* Intentionally left unimplemented. */
299  holder& operator=(const holder&);
300  };
301 
302 
303  template<typename ValueType>
304  friend ValueType* any_cast(any* operand) noexcept;
305 
306 
308 };
309 
310 
317 inline void swap(any& lhs, any& rhs) noexcept
318 {
319  lhs.swap(rhs);
320 }
321 
322 
326 class bad_any_cast : public std::bad_cast
327 {
328 public:
336  virtual const char* what() const noexcept override
337  {
338  return "bad any_cast";
339  }
340 };
341 
342 
351 template<typename ValueType>
352 ValueType* any_cast(any* operand) noexcept
353 {
354  return operand && operand->type() == typeid(ValueType) ? std::addressof(static_cast<any::holder<typename std::remove_cv<ValueType>::type>*>(operand->content)->held) : nullptr;
355 }
356 
357 
366 template<typename ValueType>
367 inline const ValueType* any_cast(const any* operand) noexcept
368 {
369  return any_cast<ValueType>(const_cast<any*>(operand));
370 }
371 
372 
381 template<typename ValueType>
382 ValueType any_cast(any& operand)
383 {
384  typedef typename std::remove_reference<ValueType>::type nonref;
385 
386  nonref* result = any_cast<nonref>(std::addressof(operand));
387  if(!result)
388  throw bad_any_cast();
389 
390  typedef typename std::conditional<std::is_reference<ValueType>::value, ValueType, typename std::add_lvalue_reference<ValueType>::type>::type ref_type;
391 
392  return static_cast<ref_type>(*result);
393 }
394 
395 
404 template<typename ValueType>
405 inline ValueType any_cast(const any& operand)
406 {
407  typedef typename std::remove_reference<ValueType>::type nonref;
408 
409  return any_cast<const nonref&>(const_cast<any&>(operand));
410 }
411 
412 
421 template<typename ValueType>
422 inline ValueType any_cast(any&& operand)
423 {
424  static_assert(std::is_rvalue_reference<ValueType&&>::value || std::is_const<typename std::remove_reference<ValueType>::type>::value, "any_cast shall not be used for getting nonconst references to temporary objects");
425 
426  return any_cast<ValueType>(operand);
427 }
428 
429 }
430 }
431 
432 #endif /* ANY_H */
bfl::any::any::has_value
bool has_value() const noexcept
Checks whether the object contains a value.
Definition: any.h:235
bfl::any::any::type
const std::type_info & type() const noexcept
Queries the contained type.
Definition: any.h:246
bfl::any::any::any
any(const any &other)
Copies content of other into a new instance, so that any content is equivalent in both type and value...
Definition: any.h:98
bfl::any::swap
void swap(any &lhs, any &rhs) noexcept
Overloads the std::swap algorithm for std::any.
Definition: any.h:317
bfl::any::any::holder::clone
virtual placeholder * clone() const
Definition: any.h:288
bfl::any::any::holder
Definition: any.h:269
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
bfl::any::any::any
any(any &&other) noexcept
Moves content of other into a new instance, so that any content is equivalent in both type and value ...
Definition: any.h:110
bfl::any::any::placeholder::~placeholder
virtual ~placeholder()
Definition: any.h:256
bfl::any::any::any
any(const ValueType &value)
Constructs an object with initial content an object of type std::decay_t<ValueType>,...
Definition: any.h:125
bfl::any::any::holder::held
ValueType held
Definition: any.h:294
bfl::any::bad_any_cast
Defines a type of object to be thrown by the value-returning forms of blf::any::any_cast on failure.
Definition: any.h:326
bfl::any::any::operator=
any & operator=(ValueType &&rhs)
Assigns contents to the contained value.
Definition: any.h:192
bfl::any::any::operator=
any & operator=(any &&rhs) noexcept
Assigns contents to the contained value.
Definition: any.h:168
bfl::any::any::holder::holder
holder(const ValueType &value)
Definition: any.h:272
bfl::any::any::placeholder
Definition: any.h:253
bfl::any::any::operator=
any & operator=(const any &rhs)
Assigns contents to the contained value.
Definition: any.h:152
bfl::any::any::content
placeholder * content
Definition: any.h:307
bfl::any::any::reset
void reset() noexcept
If not empty, destroys the contained object.
Definition: any.h:212
bfl::any::any::any
any(ValueType &&value, typename std::enable_if<!std::is_same< any &, ValueType >::value >::type *=nullptr, typename std::enable_if<!std::is_const< ValueType >::value >::type *=nullptr)
Constructs an object with initial content an object of type std::decay_t<ValueType>,...
Definition: any.h:138
bfl::any::any::placeholder::type
virtual const std::type_info & type() const noexcept=0
bfl::any::any::any
any() noexcept
Constructs an empty object.
Definition: any.h:85
bfl::any::any::placeholder::clone
virtual placeholder * clone() const =0
bfl::any::any::any_cast
friend ValueType * any_cast(any *operand) noexcept
Performs type-safe access to the contained object.
Definition: any.h:352
bfl::any::any::swap
any & swap(any &rhs) noexcept
Swaps the content of two any objects.
Definition: any.h:223
bfl::any::any::holder::type
virtual const std::type_info & type() const noexcept
Definition: any.h:282
bfl::any::any::holder::holder
holder(ValueType &&value)
Definition: any.h:277
bfl::any::any
The class any describes a type-safe container for single values of any type.
Definition: any.h:77
bfl::any::any::~any
~any() noexcept
Destruct the object.
Definition: any.h:203
bfl::any::bad_any_cast::what
virtual const char * what() const noexcept override
Returns the explanatory string.
Definition: any.h:336