-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTemplateUtil.h
173 lines (158 loc) · 4.17 KB
/
TemplateUtil.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
#ifndef _TEMPLATEUTIL_H_
#define _TEMPLATEUTIL_H_
#include <vector>
#include <string>
using namespace std;
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
TypeName(); \
DISALLOW_COPY_AND_ASSIGN(TypeName)
#ifdef WIN32
#define STRING2(x) #x
#define STRING(x) STRING2(x)
#define QP_DEPRECATED(msg) __declspec(deprecated(msg))
#define QP_COMPILE_MSG1(msg) __pragma(message(msg))
#define QP_COMPILE_MSG(msg) QP_COMPILE_MSG1(__FILE__ "(" STRING(__LINE__) "): "msg)
#endif
enum TU_RC{
TU_RC_OK,
TU_RC_FAILED,
TU_RC_ALREADY_EXISTED,
};
template<typename T,typename C, typename A=vector<T>>
class CTemplateUtil{
public:
template<typename R, typename Z>
static bool Compare(const R &a,const Z &b){
return a.compare(b);
};
template<>
static bool Compare<int,int>(const int &a,const int &b){
return (a == b);
}
template<>
static bool Compare<unsigned long,unsigned long>(const unsigned long &a,const unsigned long &b){
return (a == b);
}
template<>
static bool Compare<string, string>(const string &a,const string &b){
return (a.compare(b) == 0)?true:false;
}
typedef bool (*pFuncWithReturnBool)(const T &a,const C &b);
typedef bool (*pFunc2WithReturnBool)(const T &a,const C &b,const C &c);
typedef void (*pFuncWithNoReturn)(T &a,const C &b);
static int GetFirstIndex(const A &vec,const T &data,pFuncWithReturnBool pCompare){
int index = -1;
A::const_iterator itor = vec.begin();
int i = 0;
while(itor != vec.end()){
if(pCompare((*itor),data)){
index = i;
break;
}
itor++;
i++;
}
return index;
}
static T *FindItem(A &vec,const C &data,pFuncWithReturnBool pCompare){
T *p = NULL;
A::iterator itor = vec.begin();
while(itor != vec.end()){
if(pCompare((*itor),data)){
p = &(*itor);
break;
}
itor++;
}
return p;
}
static T *FindItem(A &vec,const C &data1,const C &data2,pFunc2WithReturnBool pCompare){
T *p = NULL;
A::iterator itor = vec.begin();
while(itor != vec.end()){
if(pCompare((*itor),data1,data2)){
p = &(*itor);
break;
}
itor++;
}
return p;
}
static void RemoveItem(A &vec,const C &data,pFuncWithReturnBool pCompare){
A::iterator itor = vec.begin();
while(itor != vec.end()){
if(pCompare((*itor),data)){
vec.erase(itor);
break;
}
itor++;
}
};
static TU_RC PushItem(A &vec,const C &data, T item,pFuncWithReturnBool pCompare){
TU_RC rc = TU_RC_OK;
A::iterator itor = vec.begin();
while(itor != vec.end()){
if(pCompare((*itor),data)){
rc = TU_RC_ALREADY_EXISTED;
break;
}
itor++;
}
if(rc != TU_RC_ALREADY_EXISTED){
vec.push_back(item);
rc = TU_RC_OK;
}
return rc;
}
static void DoSomeThingOnItems(A &vec,const C &data,pFuncWithNoReturn pDoSomeThing){
A::iterator itor = vec.begin();
while(itor != vec.end()){
pDoSomeThing(*itor,data);
itor++;
}
};
static void DoSomeThingOnItems(A &vec,const C &CompareDat,const C &DoSomeThingDat,pFuncWithReturnBool pCompare,pFuncWithNoReturn pDoSomeThing){
A::iterator itor = vec.begin();
while(itor != vec.end()){
if(pCompare((*itor),CompareDat)){
pDoSomeThing(*itor,DoSomeThingDat);
break;
}
itor++;
}
};
private:
DISALLOW_COPY_AND_ASSIGN(CTemplateUtil);
};
template <typename T,typename C, size_t count>
class CTemplateArray {
public:
typedef bool (*pFuncWithReturnBool)(const T &a,const C &b);
static const T *FindItem(const T arry[],const C &data,pFuncWithReturnBool pCompare){
const T *p = NULL;
for(size_t i = 0; i< count; i++){
if(pCompare(arry[i],data)){
p = &arry[i];
break;
}
}
return p;
};
private:
DISALLOW_COPY_AND_ASSIGN(CTemplateArray);
};
template<typename T>
void ClearVector(vector<T*> &vec){
vector<T*>::iterator itor = vec.begin();
while(itor != vec.end()){
T* pTemp = *itor;
if(pTemp)
delete pTemp;
pTemp = NULL;
itor = vec.erase(itor);
}
};
#endif