Skip to content

Commit 3073fe4

Browse files
authored
Add macros to simplify handling of internal structs (#1965)
* Add macro to declare and extract internal struct from $this * Add macros to declare intern from zend_object and zval * Add macro to simplify object allocations * Add macro to simplify calls to object_init_ex
1 parent 637f28d commit 3073fe4

50 files changed

Lines changed: 917 additions & 1477 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/BSON/Binary.c

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ static bool phongo_binary_init_from_hash(phongo_binary_t* intern, HashTable* pro
7676

7777
static HashTable* phongo_binary_get_properties_hash(zend_object* object, bool is_temp, bool is_debug)
7878
{
79-
phongo_binary_t* intern;
80-
HashTable* props;
79+
PHONGO_INTERN_FROM_Z_OBJ(binary, object);
8180

82-
intern = Z_OBJ_BINARY(object);
81+
HashTable* props;
8382

8483
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_temp, intern, props, 2);
8584

@@ -108,12 +107,10 @@ static HashTable* phongo_binary_get_properties_hash(zend_object* object, bool is
108107
/* Construct a new BSON binary type */
109108
static PHP_METHOD(MongoDB_BSON_Binary, __construct)
110109
{
111-
phongo_binary_t* intern;
112-
char* data;
113-
size_t data_len;
114-
zend_long type = BSON_SUBTYPE_BINARY;
115-
116-
intern = Z_BINARY_OBJ_P(getThis());
110+
PHONGO_INTERN_FROM_THIS(binary);
111+
char* data;
112+
size_t data_len;
113+
zend_long type = BSON_SUBTYPE_BINARY;
117114

118115
PHONGO_PARSE_PARAMETERS_START(1, 2)
119116
Z_PARAM_STRING(data, data_len)
@@ -126,39 +123,32 @@ static PHP_METHOD(MongoDB_BSON_Binary, __construct)
126123

127124
static PHP_METHOD(MongoDB_BSON_Binary, __set_state)
128125
{
129-
phongo_binary_t* intern;
130-
HashTable* props;
131-
zval* array;
126+
HashTable* props;
127+
zval* array;
132128

133129
PHONGO_PARSE_PARAMETERS_START(1, 1)
134130
Z_PARAM_ARRAY(array)
135131
PHONGO_PARSE_PARAMETERS_END();
136132

137-
object_init_ex(return_value, phongo_binary_ce);
138-
139-
intern = Z_BINARY_OBJ_P(return_value);
140-
props = Z_ARRVAL_P(array);
133+
PHONGO_INTERN_INIT_EX(binary, return_value);
134+
props = Z_ARRVAL_P(array);
141135

142136
phongo_binary_init_from_hash(intern, props);
143137
}
144138

145139
/* Return the Binary's data string. */
146140
static PHP_METHOD(MongoDB_BSON_Binary, __toString)
147141
{
148-
phongo_binary_t* intern;
142+
PHONGO_INTERN_FROM_THIS(binary);
149143

150144
PHONGO_PARSE_PARAMETERS_NONE();
151145

152-
intern = Z_BINARY_OBJ_P(getThis());
153-
154146
RETURN_STRINGL(intern->data, intern->data_len);
155147
}
156148

157149
static PHP_METHOD(MongoDB_BSON_Binary, getData)
158150
{
159-
phongo_binary_t* intern;
160-
161-
intern = Z_BINARY_OBJ_P(getThis());
151+
PHONGO_INTERN_FROM_THIS(binary);
162152

163153
PHONGO_PARSE_PARAMETERS_NONE();
164154

@@ -167,9 +157,7 @@ static PHP_METHOD(MongoDB_BSON_Binary, getData)
167157

168158
static PHP_METHOD(MongoDB_BSON_Binary, getType)
169159
{
170-
phongo_binary_t* intern;
171-
172-
intern = Z_BINARY_OBJ_P(getThis());
160+
PHONGO_INTERN_FROM_THIS(binary);
173161

174162
PHONGO_PARSE_PARAMETERS_NONE();
175163

@@ -178,14 +166,12 @@ static PHP_METHOD(MongoDB_BSON_Binary, getType)
178166

179167
static PHP_METHOD(MongoDB_BSON_Binary, jsonSerialize)
180168
{
181-
phongo_binary_t* intern;
182-
char type[3];
183-
int type_len;
169+
PHONGO_INTERN_FROM_THIS(binary);
170+
char type[3];
171+
int type_len;
184172

185173
PHONGO_PARSE_PARAMETERS_NONE();
186174

187-
intern = Z_BINARY_OBJ_P(getThis());
188-
189175
array_init_size(return_value, 2);
190176

191177
{
@@ -221,7 +207,7 @@ static zend_object_handlers phongo_handler_binary;
221207

222208
static void phongo_binary_free_object(zend_object* object)
223209
{
224-
phongo_binary_t* intern = Z_OBJ_BINARY(object);
210+
PHONGO_INTERN_FROM_Z_OBJ(binary, object);
225211

226212
zend_object_std_dtor(&intern->std);
227213

@@ -237,10 +223,7 @@ static void phongo_binary_free_object(zend_object* object)
237223

238224
static zend_object* phongo_binary_create_object(zend_class_entry* class_type)
239225
{
240-
phongo_binary_t* intern = zend_object_alloc(sizeof(phongo_binary_t), class_type);
241-
242-
zend_object_std_init(&intern->std, class_type);
243-
object_properties_init(&intern->std, class_type);
226+
PHONGO_INTERN_OBJECT_ALLOC(binary, class_type);
244227

245228
intern->std.handlers = &phongo_handler_binary;
246229

@@ -249,11 +232,11 @@ static zend_object* phongo_binary_create_object(zend_class_entry* class_type)
249232

250233
static zend_object* phongo_binary_clone_object(zend_object* object)
251234
{
252-
phongo_binary_t* intern;
235+
PHONGO_INTERN_FROM_Z_OBJ(binary, object);
236+
253237
phongo_binary_t* new_intern;
254238
zend_object* new_object;
255239

256-
intern = Z_OBJ_BINARY(object);
257240
new_object = phongo_binary_create_object(object->ce);
258241

259242
new_intern = Z_OBJ_BINARY(new_object);
@@ -291,7 +274,7 @@ static HashTable* phongo_binary_get_debug_info(zend_object* object, int* is_temp
291274
*is_temp = 1;
292275
HashTable* props = phongo_binary_get_properties_hash(object, true, true);
293276

294-
phongo_binary_t* intern = Z_OBJ_BINARY(object);
277+
PHONGO_INTERN_FROM_Z_OBJ(binary, object);
295278

296279
if (intern->type == BSON_SUBTYPE_VECTOR) {
297280
zval vector;
@@ -510,7 +493,7 @@ static PHP_METHOD(MongoDB_BSON_Binary, fromVector)
510493
zend_object* type;
511494

512495
object_init_ex(return_value, phongo_binary_ce);
513-
phongo_binary_t* intern = Z_BINARY_OBJ_P(return_value);
496+
PHONGO_INTERN_FROM_ZVAL(binary, return_value);
514497

515498
PHONGO_PARSE_PARAMETERS_START(2, 2)
516499
Z_PARAM_ARRAY_HT(vector)
@@ -557,9 +540,9 @@ static phongo_bson_vector_type_t phongo_binary_get_vector_type(const phongo_bina
557540

558541
static PHP_METHOD(MongoDB_BSON_Binary, getVectorType)
559542
{
560-
PHONGO_PARSE_PARAMETERS_NONE();
543+
PHONGO_INTERN_FROM_THIS(binary);
561544

562-
phongo_binary_t* intern = Z_BINARY_OBJ_P(getThis());
545+
PHONGO_PARSE_PARAMETERS_NONE();
563546

564547
if (intern->type != BSON_SUBTYPE_VECTOR) {
565548
phongo_throw_exception(PHONGO_ERROR_LOGIC, "Expected Binary of type vector (%" PRId8 ") but it is %" PHONGO_LONG_FORMAT, BSON_SUBTYPE_VECTOR, intern->type);
@@ -664,9 +647,9 @@ static void phongo_binary_get_vector_as_array(const phongo_binary_t* intern, zva
664647

665648
static PHP_METHOD(MongoDB_BSON_Binary, toArray)
666649
{
667-
PHONGO_PARSE_PARAMETERS_NONE();
650+
PHONGO_INTERN_FROM_THIS(binary);
668651

669-
phongo_binary_t* intern = Z_BINARY_OBJ_P(getThis());
652+
PHONGO_PARSE_PARAMETERS_NONE();
670653

671654
if (intern->type != BSON_SUBTYPE_VECTOR) {
672655
phongo_throw_exception(PHONGO_ERROR_LOGIC, "Expected Binary of type vector (%" PRId8 ") but it is %" PHONGO_LONG_FORMAT, BSON_SUBTYPE_VECTOR, intern->type);

src/BSON/DBPointer.c

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ static bool phongo_dbpointer_init_from_hash(phongo_dbpointer_t* intern, HashTabl
6666

6767
HashTable* phongo_dbpointer_get_properties_hash(zend_object* object, bool is_temp)
6868
{
69-
phongo_dbpointer_t* intern;
70-
HashTable* props;
69+
PHONGO_INTERN_FROM_Z_OBJ(dbpointer, object);
7170

72-
intern = Z_OBJ_DBPOINTER(object);
71+
HashTable* props;
7372

7473
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_temp, intern, props, 2);
7574

@@ -94,13 +93,12 @@ PHONGO_DISABLED_CONSTRUCTOR(MongoDB_BSON_DBPointer)
9493
/* Return the DBPointer's namespace string and ObjectId. */
9594
static PHP_METHOD(MongoDB_BSON_DBPointer, __toString)
9695
{
97-
phongo_dbpointer_t* intern;
98-
char* retval;
99-
int retval_len;
96+
PHONGO_INTERN_FROM_THIS(dbpointer);
10097

101-
PHONGO_PARSE_PARAMETERS_NONE();
98+
char* retval;
99+
int retval_len;
102100

103-
intern = Z_DBPOINTER_OBJ_P(getThis());
101+
PHONGO_PARSE_PARAMETERS_NONE();
104102

105103
retval_len = spprintf(&retval, 0, "[%s/%s]", intern->ref, intern->id);
106104
RETVAL_STRINGL(retval, retval_len);
@@ -109,31 +107,27 @@ static PHP_METHOD(MongoDB_BSON_DBPointer, __toString)
109107

110108
static PHP_METHOD(MongoDB_BSON_DBPointer, __set_state)
111109
{
112-
phongo_dbpointer_t* intern;
113-
HashTable* props;
114-
zval* array;
110+
HashTable* props;
111+
zval* array;
115112

116113
PHONGO_PARSE_PARAMETERS_START(1, 1)
117114
Z_PARAM_ARRAY(array)
118115
PHONGO_PARSE_PARAMETERS_END();
119116

120-
object_init_ex(return_value, phongo_dbpointer_ce);
121-
122-
intern = Z_DBPOINTER_OBJ_P(return_value);
123-
props = Z_ARRVAL_P(array);
117+
PHONGO_INTERN_INIT_EX(dbpointer, return_value);
118+
props = Z_ARRVAL_P(array);
124119

125120
phongo_dbpointer_init_from_hash(intern, props);
126121
}
127122

128123
static PHP_METHOD(MongoDB_BSON_DBPointer, jsonSerialize)
129124
{
130-
phongo_dbpointer_t* intern;
131-
zval zdb_pointer;
132-
zval zoid;
125+
PHONGO_INTERN_FROM_THIS(dbpointer);
133126

134-
PHONGO_PARSE_PARAMETERS_NONE();
127+
zval zdb_pointer;
128+
zval zoid;
135129

136-
intern = Z_DBPOINTER_OBJ_P(getThis());
130+
PHONGO_PARSE_PARAMETERS_NONE();
137131

138132
array_init_size(&zdb_pointer, 2);
139133
array_init_size(&zoid, 1);
@@ -168,7 +162,7 @@ static zend_object_handlers phongo_handler_dbpointer;
168162

169163
static void phongo_dbpointer_free_object(zend_object* object)
170164
{
171-
phongo_dbpointer_t* intern = Z_OBJ_DBPOINTER(object);
165+
PHONGO_INTERN_FROM_Z_OBJ(dbpointer, object);
172166

173167
zend_object_std_dtor(&intern->std);
174168

@@ -184,10 +178,7 @@ static void phongo_dbpointer_free_object(zend_object* object)
184178

185179
zend_object* phongo_dbpointer_create_object(zend_class_entry* class_type)
186180
{
187-
phongo_dbpointer_t* intern = zend_object_alloc(sizeof(phongo_dbpointer_t), class_type);
188-
189-
zend_object_std_init(&intern->std, class_type);
190-
object_properties_init(&intern->std, class_type);
181+
PHONGO_INTERN_OBJECT_ALLOC(dbpointer, class_type);
191182

192183
intern->std.handlers = &phongo_handler_dbpointer;
193184

@@ -196,11 +187,11 @@ zend_object* phongo_dbpointer_create_object(zend_class_entry* class_type)
196187

197188
static zend_object* phongo_dbpointer_clone_object(zend_object* object)
198189
{
199-
phongo_dbpointer_t* intern;
190+
PHONGO_INTERN_FROM_Z_OBJ(dbpointer, object);
191+
200192
phongo_dbpointer_t* new_intern;
201193
zend_object* new_object;
202194

203-
intern = Z_OBJ_DBPOINTER(object);
204195
new_object = phongo_dbpointer_create_object(object->ce);
205196

206197
new_intern = Z_OBJ_DBPOINTER(new_object);
@@ -257,11 +248,8 @@ void phongo_dbpointer_init_ce(INIT_FUNC_ARGS)
257248

258249
bool phongo_dbpointer_new(zval* object, const char* ref, size_t ref_len, const bson_oid_t* oid)
259250
{
260-
phongo_dbpointer_t* intern;
261-
262-
object_init_ex(object, phongo_dbpointer_ce);
251+
PHONGO_INTERN_INIT_EX(dbpointer, object);
263252

264-
intern = Z_DBPOINTER_OBJ_P(object);
265253
intern->ref = estrndup(ref, ref_len);
266254
intern->ref_len = ref_len;
267255
bson_oid_to_string(oid, intern->id);

0 commit comments

Comments
 (0)