-
Notifications
You must be signed in to change notification settings - Fork 49
/
utility.h
135 lines (120 loc) · 3.19 KB
/
utility.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
/*
Copyright (C) <2012> <huangweilook@21cn.com>
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 _UTILITY
#define _UTILITY
#include "TypeList.h"
#include "Trait.h"
template<typename T>
struct GetReplaceType
{
typedef T type;
//typedef typename refTraits<T>::RefType type;
};
template<>
struct GetReplaceType<const char *>
{
typedef std::string type;
};
template<>
struct GetReplaceType<char *>
{
typedef std::string type;
};
template<typename T>
struct GetRawType
{
typedef T type;
};
template<typename T>
struct GetRawType<T&>
{
typedef T &type;
};
template<>
struct GetRawType<std::string>
{
typedef const char * type;
};
template<typename T>
inline typename GetRawType<T>::type GetRawValue(T &in)
{
return in;
}
template<>
inline GetRawType<std::string>::type GetRawValue(std::string &in)
{
return in.c_str();
}
static inline const char *stack_value_tostr(lua_State *L,int index) {
static char buff[1024];
int type = lua_type(L,index);
switch(type) {
case LUA_TNIL:{
snprintf(buff,1023,"nil");
}break;
case LUA_TUSERDATA:{
snprintf(buff,1023,"userdata");
}break;
case LUA_TLIGHTUSERDATA:{
snprintf(buff,1023,"lightuserdata");
}break;
case LUA_TNUMBER:{
lua_Number v = lua_tonumber(L,index);
if(v != (lua_Integer)v){
snprintf(buff,1023,"%f",v);
} else {
snprintf(buff,1023,"%lld",lua_tointeger(L,index));
}
}break;
case LUA_TBOOLEAN:{
lua_Integer b = lua_tointeger(L,index);
snprintf(buff,1023,"%s",b == 1 ? "true":"false");
}break;
case LUA_TTABLE:{
snprintf(buff,1023,"table");
}break;
case LUA_TTHREAD:{
snprintf(buff,1023,"thread");
}break;
case LUA_TFUNCTION:{
snprintf(buff,1023,"function");
}break;
case LUA_TSTRING:{
snprintf(buff,1023,"%s",lua_tostring(L,index));
}
break;
default:{
snprintf(buff,1023,"unknow");
}
}
buff[1023] = 0;
return buff;
}
static inline void show_stack(lua_State *L,const char *msg = NULL) {
int top = lua_gettop(L);
if(msg) {
printf("top:%d,%s\n",top,msg);
}else {
printf("top:%d\n",top);
}
int i;
for(i = top;i > 0; --i) {
if(i == top) {
printf("(stack index:%d) %s <- top\n",i,stack_value_tostr(L,i));
} else {
printf("(stack index:%d) %s\n",i,stack_value_tostr(L,i));
}
}
}
#endif