NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
ne_stl_any.h
浏览该文件的文档.
1#ifndef NE_STL_INCLUDENE_STL_ANY_H
2#define NE_STL_INCLUDENE_STL_ANY_H
3#pragma warning(disable : 4503)
4
5#include "ne_stl_build.h"
6
7namespace nstd {
8
9struct type_id_t {
10 const char* name;
11 type_id_t(const char* _name)
12 : name(_name) {}
13 type_id_t(const type_id_t& other)
14 : name(other.name) {}
15 bool operator<(const type_id_t& other) const { return name < other.name; }
16 bool operator==(const type_id_t& other) const { return (name == other.name || strcmp(name, other.name) == 0); }
17 bool operator!=(const type_id_t& other) const { return !operator==(other); }
18};
19
20class any {
21public:
22 template <typename T>
24 static type_id_t id() { return type_id_t{typeid(T).name()}; };
25 };
26#define TypeIDForT(TClass) nstd::any::type_id_for_t<typename std::decay<TClass>::type>::id()
27public: // structors
29 : content(0) {}
30
31 template <typename _ValueType>
32 any(const _ValueType& value)
33 : content(new holder<_ValueType>(value)) {}
34
35 any(const any& other)
36 : content(other.content ? other.content->clone() : 0) {}
37
38 ~any() { delete content; }
39
40public:
41 template <typename _Ty>
42 static const type_id_t type_id() {
43 return TypeIDForT(_Ty);
44 }
45
46public: // modifiers
47 any& swap(any& rhs) {
48 placeholder* _temp = content;
49 content = rhs.content;
50 rhs.content = _temp;
51 return *this;
52 }
53
54 template <typename _ValueType>
55 any& operator=(const _ValueType& rhs) {
56 any(rhs).swap(*this);
57 return *this;
58 }
59
61 rhs.swap(*this);
62 return *this;
63 }
64
65public: // queries
66 bool empty() const { return !content; }
67
68private: // types
70 public: // structors
72 : type_id_index_(index) {}
73 virtual ~placeholder() {}
74
75 public: // queries
76 virtual placeholder* clone() const = 0;
77
79 };
80
81 template <typename _ValueType>
82 class holder : public placeholder {
83 public: // structors
84 holder(const _ValueType& value)
85 : placeholder(TypeIDForT(_ValueType))
86 , held(value) {}
87
88 public: // queries
89 virtual placeholder* clone() const { return new holder(held); }
90
91 public: // representation
92 _ValueType held;
93
94 private: // intentionally left unimplemented
96 };
97
98private: // representation
99 template <typename _ValueType>
100 friend _ValueType* any_cast(any*);
101
102 template <typename _ValueType>
103 friend _ValueType* UnsafeanyCast(any*);
104
106};
107
109public:
110 const char* what() const throw() {
111 return "bad_any_cast: "
112 "failed conversion any_cast";
113 }
114};
115
116template <typename _ValueType>
117_ValueType* any_cast(any* operand) {
118 _ValueType* ret = nullptr;
119 do {
120 if (operand == nullptr)
121 break;
122 if (TypeIDForT(_ValueType).name != operand->content->type_id_index_.name)
123 break;
124 ret = &static_cast<any::holder<_ValueType>*>(operand->content)->held;
125 } while (false);
126
127 return ret;
128}
129
130template <typename _ValueType>
131inline const _ValueType* any_cast(const any* operand) {
132 return any_cast<_ValueType>(const_cast<any*>(operand));
133}
134
135template <typename _ValueType>
136_ValueType any_cast(any& operand) {
137 typedef typename std::decay<_ValueType>::type nonref;
138 static_assert(!(std::is_reference<nonref>::value), "still right reference");
139 nonref* result = any_cast<nonref>(&operand);
140 return *result;
141}
142template <typename _ValueType>
143inline _ValueType any_cast(const any& operand) {
144 typedef typename std::decay<_ValueType>::type nonref;
145 static_assert(!std::is_reference<nonref>::value, "still right reference");
146 return any_cast<const nonref&>(const_cast<any&>(operand));
147}
148template <typename _ValueType>
149inline _ValueType* UnsafeanyCast(any* operand) {
150 return &static_cast<any::holder<_ValueType>*>(operand->content)->held;
151}
152
153template <typename _ValueType>
154inline const _ValueType* UnsafeanyCast(const any* operand) {
155 return UnsafeanyCast<_ValueType>(const_cast<any*>(operand));
156}
157
158} // namespace nstd
159
160#endif // !NE_STL_INCLUDENE_STL_ANY_H
Definition: ne_stl_any.h:82
holder & operator=(const holder &)
holder(const _ValueType &value)
Definition: ne_stl_any.h:84
_ValueType held
Definition: ne_stl_any.h:92
virtual placeholder * clone() const
Definition: ne_stl_any.h:89
Definition: ne_stl_any.h:69
virtual ~placeholder()
Definition: ne_stl_any.h:73
type_id_t type_id_index_
Definition: ne_stl_any.h:78
virtual placeholder * clone() const =0
placeholder(const type_id_t index)
Definition: ne_stl_any.h:71
Definition: ne_stl_any.h:20
static const type_id_t type_id()
Definition: ne_stl_any.h:42
friend _ValueType * UnsafeanyCast(any *)
Definition: ne_stl_any.h:149
any(const any &other)
Definition: ne_stl_any.h:35
bool empty() const
Definition: ne_stl_any.h:66
any & operator=(any rhs)
Definition: ne_stl_any.h:60
~any()
Definition: ne_stl_any.h:38
any()
Definition: ne_stl_any.h:28
any & swap(any &rhs)
Definition: ne_stl_any.h:47
friend _ValueType * any_cast(any *)
Definition: ne_stl_any.h:117
any(const _ValueType &value)
Definition: ne_stl_any.h:32
placeholder * content
Definition: ne_stl_any.h:105
any & operator=(const _ValueType &rhs)
Definition: ne_stl_any.h:55
Definition: ne_stl_any.h:108
const char * what() const
Definition: ne_stl_any.h:110
Definition: ne_stl_any.h:7
_ValueType * any_cast(any *operand)
Definition: ne_stl_any.h:117
_ValueType * UnsafeanyCast(any *operand)
Definition: ne_stl_any.h:149
#define TypeIDForT(TClass)
Definition: ne_stl_any.h:26
Definition: ne_stl_any.h:23
static type_id_t id()
Definition: ne_stl_any.h:24
Definition: ne_stl_any.h:9
type_id_t(const type_id_t &other)
Definition: ne_stl_any.h:13
const char * name
Definition: ne_stl_any.h:10
bool operator!=(const type_id_t &other) const
Definition: ne_stl_any.h:17
bool operator==(const type_id_t &other) const
Definition: ne_stl_any.h:16
bool operator<(const type_id_t &other) const
Definition: ne_stl_any.h:15
type_id_t(const char *_name)
Definition: ne_stl_any.h:11