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