-
Notifications
You must be signed in to change notification settings - Fork 49
/
any.h
executable file
·270 lines (224 loc) · 5.7 KB
/
any.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Copyright (C) <2012> <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _ANY_H
#define _ANY_H
#include <vector>
#include <iostream>
#include <stdint.h>
#include <map>
#include <stdlib.h>
#include "TypeList.h"
#include "Trait.h"
#include "luacommon.h"
namespace luacpp{
class luaObject;
class any;
typedef std::vector<any> luatable;
typedef LOKI_TYPELIST_15(bool,char,unsigned char,short,unsigned short,int,unsigned int,long,unsigned long,float,double,std::string,luaObject,luatable,int64_t) SupportType;
typedef LOKI_TYPELIST_9(char,unsigned char,short,unsigned short,int,unsigned int,long,unsigned long,int64_t) intType;
//注册到lua中的用户数据类型信息
template<typename T>
class luaRegisterClass
{
public:
static bool isRegister()
{
return m_isRegister;
}
static void SetRegister()
{
m_isRegister = true;
}
static void ClearRegister()
{
m_isRegister = false;
}
static void SetClassName(const char *name)
{
classname = name;
}
static const char *GetClassName()
{
return classname.c_str();
}
private:
static bool m_isRegister;
static std::string classname;
};
template<typename T>
bool luaRegisterClass<T>::m_isRegister = false;
template<typename T>
std::string luaRegisterClass<T>::classname;
class any;
class any_pusher
{
public:
virtual ~any_pusher(){}
virtual void push(lua_State *L,any*) = 0;
};
template <typename T>
any_pusher *create_any_pusher();
//一个精简版的boost::any
class any
{
public: // structors
any(): content(0),counter(0){}
template<typename ValueType>
any(const ValueType & value)
:counter(new int(1)),luaRegisterClassName("")
{
content = create_holder<ValueType>(value,Int2Type<IndexOf<intType,ValueType>::value >= 0>());
luaRegisterClassName = luaRegisterClass<typename pointerTraits<ValueType>::PointeeType>::GetClassName();
_any_pusher = create_any_pusher<ValueType>();
}
any(const any & other)
{
if(other.counter && other.content)
{
content = other.content;
counter = other.counter;
++(*counter);
luaRegisterClassName = other.luaRegisterClassName;
_any_pusher = other._any_pusher;
}
else
{
content = NULL;
counter = NULL;
_any_pusher = NULL;
}
}
any & operator=(const any & rhs)
{
if(&rhs == this)
return *this;
else
{
_destroy();
if(rhs.counter && rhs.content)
{
content = rhs.content;
counter = rhs.counter;
_any_pusher = rhs._any_pusher;
++(*counter);
}
else
{
content = 0;
counter = 0;
}
return *this;
}
}
~any()
{
_destroy();
}
private:
void _destroy()
{
if(counter && content && --*counter <= 0)
{
delete counter;
delete content;
delete _any_pusher;
}
}
public: // queries
bool empty() const
{
return !content;
}
public:
class placeholder
{
public: // structors
virtual ~placeholder(){}
};
template<typename ValueType>
class holder : public placeholder
{
public: // structors
holder(const ValueType & value)
: held(value)
{}
public: // representation
ValueType held;
};
template<typename ValueType>
placeholder * create_holder(ValueType v,Int2Type<true>)
{
return new holder<int64_t>(v);
}
template<typename ValueType>
placeholder * create_holder(ValueType v,Int2Type<false>)
{
return new holder<ValueType>(v);
}
placeholder * content;
int * counter;
public: // representation (public so any_cast can be non-friend)
any_pusher * _any_pusher;
std::string luaRegisterClassName;//保存的类型在lua中注册的名字
};
template<typename ValueType>
inline ValueType _any_cast(const any &operand,Int2Type<true>)
{
ValueType ret = (ValueType)static_cast<any::holder<int64_t> *>(operand.content)->held;
return ret;
}
template<typename ValueType>
inline ValueType _any_cast(const any &operand,Int2Type<false>)
{
return static_cast<any::holder<ValueType> *>(operand.content)->held;
}
template<typename ValueType>
inline ValueType any_cast(const any & operand)
{
return _any_cast<ValueType>(operand,Int2Type<IndexOf<intType,ValueType>::value >= 0>());
}
template<>
inline std::string any_cast(const any & operand)
{
any::holder<std::string> *tmp = static_cast<any::holder<std::string> *>(operand.content);
return tmp->held;
}
template<>
inline const char *any_cast(const any &operand){
any::holder<std::string> *tmp = static_cast<any::holder<std::string> *>(operand.content);
return tmp->held.c_str();
}
template<>
inline char *any_cast(const any &operand){
any::holder<std::string> *tmp = static_cast<any::holder<std::string> *>(operand.content);
return const_cast<char*>(tmp->held.c_str());
}
template<>
inline double any_cast(const any & operand)
{
any::holder<double> *tmp = static_cast<any::holder<double> *>(operand.content);
return tmp->held;
}
template<>
inline float any_cast(const any & operand)
{
return (float)any_cast<double>(operand);
}
//unsupported
template<>
const char * any_cast(const any & operand);
template<>
char * any_cast(const any & operand);
}
#endif