forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-array.h
211 lines (180 loc) · 8.64 KB
/
php-array.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
/*
* php-array.h
*
* Copyright (C) Roger P. Gee
*/
#ifndef PHPGIT2_ARRAY_H
#define PHPGIT2_ARRAY_H
#include "php-git2.h"
namespace php_git2
{
// Provide a type for wrapping common array operations.
class array_wrapper
{
public:
array_wrapper(zval& zv);
array_wrapper(zval* zvp);
~array_wrapper();
bool query(const char* key);
bool query(const char* key,size_t keysz);
bool query(int index);
int type() const
{
return Z_TYPE_P(zvp);
}
bool found() const
{
return zvp != nullptr;
}
const char* get_string() const;
const char* get_string_nullable() const;
size_t get_string_length() const;
zend_long get_long() const;
bool get_bool() const;
zval* get_value() const;
void get_oid(git_oid* out) const;
private:
HashTable* ht;
zval* zvp;
mutable zval tmp;
mutable bool usesTmp;
zval* copy_if_not_type(int type) const;
};
}
// Define convenience macros for working with array_wrapper instances. These
// should be used in a byval_git2() implementation.
#define GIT2_ARRAY_LOOKUP_STRING(wrapper,name,var) \
if (wrapper.query(#name,sizeof(#name)-1)) { \
var.name = wrapper.get_string(); \
}
#define GIT2_ARRAY_INDEX_STRING(wrapper,index,name,var) \
if (wrapper.query(index)) { \
var.name = wrapper.get_string(); \
}
#define GIT2_ARRAY_LOOKUP_STRING_NULLABLE(wrapper,name,var) \
if (wrapper.query(#name,sizeof(#name)-1)) { \
var.name = wrapper.get_string_nullable(); \
}
#define GIT2_ARRAY_INDEX_STRING_NULLABLE(wrapper,index,name,var) \
if (wrapper.query(index)) { \
var.name = wrapper.get_string_nullable(); \
}
#define GIT2_ARRAY_LOOKUP_STRING_WITH_LENGTH(wrapper,name,len,var) \
if (wrapper.query(#name,sizeof(#name)-1)) { \
var.name = wrapper.get_string(); \
var.len = wrapper.get_string_length(); \
}
#define GIT2_ARRAY_LOOKUP_LONG(wrapper,name,var) \
if (wrapper.query(#name,sizeof(#name)-1)) { \
var.name = (decltype(var.name))wrapper.get_long(); \
}
#define GIT2_ARRAY_INDEX_LONG(wrapper,index,name,var) \
if (wrapper.query(index)) { \
var.name = (decltype(var.name))wrapper.get_long(); \
}
#define GIT2_ARRAY_LOOKUP_BOOL(wrapper,name,var) \
if (wrapper.query(#name,sizeof(#name)-1)) { \
var.name = (decltype(var.name))wrapper.get_bool(); \
}
#define GIT2_ARRAY_INDEX_BOOL(wrapper,index,name,var) \
if (wrapper.query(index)) { \
var.name = (decltype(var.name))wrapper.get_bool(); \
}
#define GIT2_ARRAY_LOOKUP_OID(wrapper,name,var) \
if (wrapper.query(#name,sizeof(#name)-1)) { \
wrapper.get_oid(&var.name); \
}
#define GIT2_ARRAY_INDEX_OID(wrapper,index,name,var) \
if (wrapper.query(index)) { \
wrapper.get_oid(&var.name); \
}
#define GIT2_ARRAY_LOOKUP_SUBOBJECT(wrapper,obj,name,var) \
if (wrapper.query(#name,sizeof(#name)-1)) { \
obj.set_value(wrapper.get_value()); \
var.name = obj.byval_git2(); \
}
#define GIT2_ARRAY_INDEX_SUBOBJECT(wrapper,index,obj,name,var) \
if (wrapper.query(index)) { \
obj.set_value(wrapper.get_value()); \
var.name = obj.byval_git2(); \
}
#define GIT2_ARRAY_LOOKUP_SUBOBJECT_DEREFERENCE(wrapper,obj,name,var) \
if (wrapper.query(#name,sizeof(#name)-1)) { \
obj.set_value(wrapper.get_value()); \
var.name = *obj.byval_git2(); \
}
#define GIT2_ARRAY_INDEX_SUBOBJECT_DEREFERENCE(wrapper,index,obj,name,var) \
if (wrapper.query(index)) { \
obj.set_value(wrapper.get_value()); \
var.name = *obj.byval_git2(); \
}
#define GIT2_ARRAY_LOOKUP_RESOURCE(wrapper,type,name,var) \
if (wrapper.query(#name,sizeof(#name)-1)) { \
php_resource<type> rr; \
type r; \
zval* zv = wrapper.get_value(); \
if (Z_TYPE_P(zv) != IS_NULL) { \
rr.parse_with_context(zv,#name); \
r = rr.get_object(); \
var.name = r.get_handle(); \
} \
}
#define GIT2_ARRAY_INDEX_RESOURCE(wrapper,index,type,name,var,ctx) \
if (wrapper.query(index)) { \
php_resource<type> rr; \
type r; \
zval* zv = wrapper.get_value(); \
if (Z_TYPE_P(zv) != IS_NULL) { \
rr.parse_with_context(zv,#index); \
r = rr.get_object(); \
var.name = r.get_handle(); \
} \
}
#define GIT2_ARRAY_LOOKUP_CALLBACK(wrapper,callbackWrapper,callbackObject, \
funcName,payloadName,var) \
\
if (wrapper.query(#funcName,sizeof(#funcName)-1)) { \
zval zdataDefault \
zval* zfunc, *zdata; \
zfunc = wrapper.get_value(); \
if (wrapper.query(#payloadName,sizeof(#payloadName)-1)) { \
zdata = wrapper.get_value(); \
} \
else { \
ZVAL_NULL(&zdataDefault); \
zdata = &zdataDefault; \
} \
callbackObject.set_members(zfunc,zdata); \
var.funcName = callbackWrapper::callback; \
var.payloadName = (void*)&callbackObject; \
}
// NOTE: this variant assumes 'var' is already zeroed out or initialized in some
// way.
#define GIT2_ARRAY_LOOKUP_CALLBACK_NULLABLE(wrapper,callbackWrapper, \
callbackObject,funcName,payloadName,var) \
\
if (wrapper.query(#funcName,sizeof(#funcName)-1)) { \
zval zdataDefault; \
zval* zfunc, *zdata; \
zfunc = wrapper.get_value(); \
if (Z_TYPE_P(zfunc) != IS_NULL) { \
if (wrapper.query(#payloadName,sizeof(#payloadName)-1)) { \
zdata = wrapper.get_value(); \
} \
else { \
ZVAL_NULL(&zdataDefault); \
zdata = &zdataDefault; \
} \
callbackObject.set_members(zfunc,zdata); \
var.funcName = callbackWrapper::callback; \
var.payloadName = (void*)&callbackObject; \
} \
}
#endif
/*
* Local Variables:
* mode:c++
* indent-tabs-mode:nil
* tab-width:4
* End:
*/