Skip to content

Commit c1630c4

Browse files
committed
PHPC-116: Implement invalidate_current handler to reduce code
1 parent 2a78c63 commit c1630c4

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

php_phongo.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,14 +1177,20 @@ typedef struct {
11771177
php_phongo_bson_state visitor_data;
11781178
} phongo_cursor_it;
11791179

1180-
static void phongo_cursor_it_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1180+
static void phongo_cursor_it_invalidate_current(zend_object_iterator *iter TSRMLS_DC)
11811181
{
11821182
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
11831183

11841184
if (cursor_it->visitor_data.zchild) {
11851185
zval_ptr_dtor(&cursor_it->visitor_data.zchild);
11861186
cursor_it->visitor_data.zchild = NULL;
11871187
}
1188+
}
1189+
static void phongo_cursor_it_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1190+
{
1191+
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
1192+
1193+
iter->funcs->invalidate_current(iter TSRMLS_CC);
11881194

11891195
zval_ptr_dtor((zval**)&cursor_it->iterator.data);
11901196
efree(cursor_it);
@@ -1205,11 +1211,9 @@ static void phongo_cursor_it_move_forward(zend_object_iterator *iter TSRMLS_DC)
12051211
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
12061212
const bson_t *doc;
12071213

1208-
if (cursor_it->visitor_data.zchild) {
1209-
zval_ptr_dtor(&cursor_it->visitor_data.zchild);
1210-
cursor_it->visitor_data.zchild = NULL;
1211-
}
1214+
iter->funcs->invalidate_current(iter TSRMLS_CC);
12121215

1216+
iter->index++;
12131217
if (bson_iter_next(&cursor_it->first_batch_iter)) {
12141218
if (BSON_ITER_HOLDS_DOCUMENT (&cursor_it->first_batch_iter)) {
12151219
const uint8_t *data = NULL;
@@ -1226,7 +1230,7 @@ static void phongo_cursor_it_move_forward(zend_object_iterator *iter TSRMLS_DC)
12261230
MAKE_STD_ZVAL(cursor_it->visitor_data.zchild);
12271231
bson_to_zval(bson_get_data(doc), doc->len, &cursor_it->visitor_data);
12281232
} else {
1229-
cursor_it->visitor_data.zchild = NULL;
1233+
iter->funcs->invalidate_current(iter TSRMLS_CC);
12301234
}
12311235

12321236
} /* }}} */
@@ -1235,10 +1239,7 @@ static void phongo_cursor_it_rewind(zend_object_iterator *iter TSRMLS_DC) /* {{{
12351239
{
12361240
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
12371241

1238-
if (cursor_it->visitor_data.zchild) {
1239-
zval_ptr_dtor(&cursor_it->visitor_data.zchild);
1240-
cursor_it->visitor_data.zchild = NULL;
1241-
}
1242+
iter->funcs->invalidate_current(iter TSRMLS_CC);
12421243

12431244
/* firstBatch is empty when the query simply didn't return any results */
12441245
if (cursor_it->firstBatch) {
@@ -1271,7 +1272,7 @@ zend_object_iterator_funcs phongo_cursor_it_funcs = {
12711272
NULL, /* void (*get_current_key)(zend_object_iterator *iter, zval *key TSRMLS_DC); */
12721273
phongo_cursor_it_move_forward,
12731274
phongo_cursor_it_rewind,
1274-
NULL /* void (*invalidate_current)(zend_object_iterator *iter TSRMLS_DC); */
1275+
phongo_cursor_it_invalidate_current
12751276
};
12761277
zend_object_iterator_funcs zend_interface_iterator_funcs_iterator_default = {
12771278
phongo_cursor_it_dtor,

0 commit comments

Comments
 (0)