forked from sniperHW/luawrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
any.h
231 lines (197 loc) · 5.11 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
/*
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"
class luaObject;
class any;
typedef std::vector<any> luatable;
typedef LOKI_TYPELIST_14(char,unsigned char,short,unsigned short,int,unsigned int,long,unsigned long,float,double,std::string,luaObject,luatable,int64_t) SupportType;
typedef LOKI_TYPELIST_11(char,unsigned char,short,unsigned short,int,unsigned int,long,unsigned long,float,double,int64_t) numberType;
typedef LOKI_TYPELIST_12(char,unsigned char,short,unsigned short,int,unsigned int,long,unsigned long,float,double,void,int64_t) internalType;
//注册到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 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)
: /*content(new holder<ValueType>(value)create_holder<ValueType>),*/counter(new int(1)),luaRegisterClassName("")
{
content = create_holder<ValueType>(value,Int2Type<IndexOf<numberType,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;
//printf("destroy\n");
}
}
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 = 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<numberType,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;
}
#endif