Otclient  14/8/2020
any.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef STDEXT_ANY_H
24 #define STDEXT_ANY_H
25 
26 #include <algorithm>
27 #include <cassert>
28 #include <typeinfo>
29 
30 namespace stdext {
31 
32 class any {
33 public:
34  struct placeholder {
35  virtual ~placeholder() { }
36  virtual const std::type_info& type() const = 0;
37  virtual placeholder* clone() const = 0;
38  };
39 
40  template<typename T>
41  struct holder : public placeholder {
42  holder(const T& value) : held(value) { }
43  const std::type_info& type() const { return typeid(T); }
44  placeholder* clone() const { return new holder(held); }
45  T held;
46  private:
47  holder& operator=(const holder &);
48  };
49 
51 
52  any() : content(nullptr) { }
53  any(const any& other) : content(other.content ? other.content->clone() : nullptr) { }
54  template<typename T> any(const T& value) : content(new holder<T>(value)) { }
55  ~any() { delete content; }
56 
57  any& swap(any& rhs) { std::swap(content, rhs.content); return *this; }
58 
59  template<typename T> any& operator=(const T& rhs) { any(rhs).swap(*this); return *this; }
60  any& operator=(any rhs) { rhs.swap(*this); return *this; }
61 
62  bool empty() const { return !content; }
63  template<typename T> const T& cast() const;
64  const std::type_info & type() const { return content ? content->type() : typeid(void); }
65 };
66 
67 template<typename T>
68 const T& any_cast(const any& operand) {
69  assert(operand.type() == typeid(T));
70  return static_cast<any::holder<T>*>(operand.content)->held;
71 }
72 
73 template<typename T> const T& any::cast() const { return any_cast<T>(*this); }
74 
75 }
76 
77 #endif
stdext::any::holder::type
const std::type_info & type() const
Definition: any.h:43
stdext::any::content
placeholder * content
Definition: any.h:50
std::swap
void swap(stdext::packed_vector< T, U > &lhs, stdext::packed_vector< T, U > &rhs)
Definition: packed_vector.h:158
stdext::any::swap
any & swap(any &rhs)
Definition: any.h:57
stdext::any::holder
Definition: any.h:41
stdext::any::placeholder::~placeholder
virtual ~placeholder()
Definition: any.h:35
stdext::any::operator=
any & operator=(any rhs)
Definition: any.h:60
stdext::any::holder::holder
holder(const T &value)
Definition: any.h:42
stdext::any_cast
const T & any_cast(const any &operand)
Definition: any.h:68
stdext::any::holder::clone
placeholder * clone() const
Definition: any.h:44
stdext::any::any
any(const T &value)
Definition: any.h:54
stdext::any::placeholder
Definition: any.h:34
stdext::any::~any
~any()
Definition: any.h:55
stdext::any::any
any(const any &other)
Definition: any.h:53
stdext::any::cast
const T & cast() const
Definition: any.h:73
stdext::any::any
any()
Definition: any.h:52
stdext::any
Definition: any.h:32
stdext::any::type
const std::type_info & type() const
Definition: any.h:64
stdext::any::empty
bool empty() const
Definition: any.h:62
stdext::any::placeholder::type
virtual const std::type_info & type() const =0
stdext::any::holder::held
T held
Definition: any.h:45
stdext::any::operator=
any & operator=(const T &rhs)
Definition: any.h:59
stdext::any::placeholder::clone
virtual placeholder * clone() const =0
stdext
Definition: any.h:30