@@ -1557,28 +1557,32 @@ void php_phongo_cursor_free(php_phongo_cursor_t *cursor)
1557
1557
}
1558
1558
}
1559
1559
1560
- /* {{{ Iterator */
1561
- static void phongo_cursor_iterator_invalidate_current (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
1560
+ static void php_phongo_cursor_free_current (php_phongo_cursor_t * cursor ) /* {{{ */
1562
1561
{
1563
- php_phongo_cursor_t * cursor = NULL ;
1564
-
1565
- cursor = ((phongo_cursor_it * )iter )-> iterator .data ;
1566
1562
if (cursor -> visitor_data .zchild ) {
1567
1563
zval_ptr_dtor (& cursor -> visitor_data .zchild );
1568
1564
cursor -> visitor_data .zchild = NULL ;
1569
1565
}
1570
1566
} /* }}} */
1571
1567
1572
- static void phongo_cursor_iterator_dtor (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
1568
+ /* {{{ Iterator */
1569
+ static void php_phongo_cursor_iterator_dtor (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
1573
1570
{
1574
- efree (iter );
1571
+ php_phongo_cursor_iterator * cursor_it = (php_phongo_cursor_iterator * )iter ;
1572
+
1573
+ if (cursor_it -> intern .data ) {
1574
+ zval_ptr_dtor ((zval * * )& cursor_it -> intern .data );
1575
+ cursor_it -> intern .data = NULL ;
1576
+ }
1577
+
1578
+ php_phongo_cursor_free_current (cursor_it -> cursor );
1579
+
1580
+ efree (cursor_it );
1575
1581
} /* }}} */
1576
1582
1577
- static int phongo_cursor_iterator_valid (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
1583
+ static int php_phongo_cursor_iterator_valid (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
1578
1584
{
1579
- php_phongo_cursor_t * cursor = NULL ;
1580
-
1581
- cursor = ((phongo_cursor_it * )iter )-> iterator .data ;
1585
+ php_phongo_cursor_t * cursor = ((php_phongo_cursor_iterator * )iter )-> cursor ;
1582
1586
1583
1587
if (cursor -> visitor_data .zchild ) {
1584
1588
return SUCCESS ;
@@ -1588,37 +1592,34 @@ static int phongo_cursor_iterator_valid(zend_object_iterator *iter TSRMLS_DC) /*
1588
1592
} /* }}} */
1589
1593
1590
1594
#if PHP_VERSION_ID < 50500
1591
- static int phongo_cursor_iterator_get_current_key (zend_object_iterator * iter , char * * str_key , uint * str_key_len , ulong * int_key TSRMLS_DC ) /* {{{ */
1595
+ static int php_phongo_cursor_iterator_get_current_key (zend_object_iterator * iter , char * * str_key , uint * str_key_len , ulong * int_key TSRMLS_DC ) /* {{{ */
1592
1596
{
1593
- * int_key = (ulong ) ((phongo_cursor_it * )iter )-> current ;
1597
+ * int_key = (ulong ) ((php_phongo_cursor_iterator * )iter )-> current ;
1594
1598
return HASH_KEY_IS_LONG ;
1595
1599
} /* }}} */
1596
1600
#else
1597
- static void phongo_cursor_iterator_get_current_key (zend_object_iterator * iter , zval * key TSRMLS_DC ) /* {{{ */
1601
+ static void php_phongo_cursor_iterator_get_current_key (zend_object_iterator * iter , zval * key TSRMLS_DC ) /* {{{ */
1598
1602
{
1599
- ZVAL_LONG (key , ((phongo_cursor_it * )iter )-> current );
1603
+ ZVAL_LONG (key , ((php_phongo_cursor_iterator * )iter )-> current );
1600
1604
} /* }}} */
1601
1605
#endif
1602
1606
1603
- static void phongo_cursor_iterator_get_current_data (zend_object_iterator * iter , zval * * * data TSRMLS_DC ) /* {{{ */
1607
+ static void php_phongo_cursor_iterator_get_current_data (zend_object_iterator * iter , zval * * * data TSRMLS_DC ) /* {{{ */
1604
1608
{
1605
- php_phongo_cursor_t * cursor = NULL ;
1606
-
1607
- cursor = ((phongo_cursor_it * )iter )-> iterator .data ;
1609
+ php_phongo_cursor_t * cursor = ((php_phongo_cursor_iterator * )iter )-> cursor ;
1608
1610
1609
1611
* data = & cursor -> visitor_data .zchild ;
1610
1612
} /* }}} */
1611
1613
1612
- static void phongo_cursor_iterator_move_forward (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
1614
+ static void php_phongo_cursor_iterator_move_forward (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
1613
1615
{
1614
- php_phongo_cursor_t * cursor = NULL ;
1615
- phongo_cursor_it * cursor_it = ( phongo_cursor_it * ) iter ;
1616
- const bson_t * doc ;
1616
+ php_phongo_cursor_iterator * cursor_it = ( php_phongo_cursor_iterator * ) iter ;
1617
+ php_phongo_cursor_t * cursor = cursor_it -> cursor ;
1618
+ const bson_t * doc ;
1617
1619
1618
- cursor = (( phongo_cursor_it * ) iter ) -> iterator . data ;
1619
- iter -> funcs -> invalidate_current ( iter TSRMLS_CC ) ;
1620
+ php_phongo_cursor_free_current ( cursor ) ;
1621
+ cursor_it -> current ++ ;
1620
1622
1621
- ((phongo_cursor_it * )iter )-> current ++ ;
1622
1623
if (bson_iter_next (& cursor_it -> first_batch_iter )) {
1623
1624
if (BSON_ITER_HOLDS_DOCUMENT (& cursor_it -> first_batch_iter )) {
1624
1625
const uint8_t * data = NULL ;
@@ -1635,20 +1636,18 @@ static void phongo_cursor_iterator_move_forward(zend_object_iterator *iter TSRML
1635
1636
MAKE_STD_ZVAL (cursor -> visitor_data .zchild );
1636
1637
bson_to_zval (bson_get_data (doc ), doc -> len , & cursor -> visitor_data );
1637
1638
} else {
1638
- iter -> funcs -> invalidate_current (iter TSRMLS_CC );
1639
+ /* TODO: is this really necessary? */
1640
+ php_phongo_cursor_free_current (cursor );
1639
1641
}
1640
-
1641
1642
} /* }}} */
1642
1643
1643
- static void phongo_cursor_iterator_rewind (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
1644
+ static void php_phongo_cursor_iterator_rewind (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
1644
1645
{
1645
- php_phongo_cursor_t * cursor = NULL ;
1646
- phongo_cursor_it * cursor_it = ( phongo_cursor_it * ) iter ;
1646
+ php_phongo_cursor_iterator * cursor_it = ( php_phongo_cursor_iterator * ) iter ;
1647
+ php_phongo_cursor_t * cursor = cursor_it -> cursor ;
1647
1648
1648
- cursor = ((phongo_cursor_it * )iter )-> iterator .data ;
1649
-
1650
- iter -> funcs -> invalidate_current (iter TSRMLS_CC );
1651
- ((phongo_cursor_it * )iter )-> current = 0 ;
1649
+ php_phongo_cursor_free_current (cursor );
1650
+ cursor_it -> current = 0 ;
1652
1651
1653
1652
/* firstBatch is empty when the query simply didn't return any results */
1654
1653
if (cursor -> firstBatch ) {
@@ -1674,34 +1673,33 @@ static void phongo_cursor_iterator_rewind(zend_object_iterator *iter TSRMLS_DC)
1674
1673
} /* }}} */
1675
1674
1676
1675
/* iterator handler table */
1677
- zend_object_iterator_funcs phongo_cursor_iterator_funcs = {
1678
- phongo_cursor_iterator_dtor ,
1679
- phongo_cursor_iterator_valid ,
1680
- phongo_cursor_iterator_get_current_data ,
1681
- phongo_cursor_iterator_get_current_key ,
1682
- phongo_cursor_iterator_move_forward ,
1683
- phongo_cursor_iterator_rewind ,
1684
- phongo_cursor_iterator_invalidate_current
1676
+ zend_object_iterator_funcs php_phongo_cursor_iterator_funcs = {
1677
+ php_phongo_cursor_iterator_dtor ,
1678
+ php_phongo_cursor_iterator_valid ,
1679
+ php_phongo_cursor_iterator_get_current_data ,
1680
+ php_phongo_cursor_iterator_get_current_key ,
1681
+ php_phongo_cursor_iterator_move_forward ,
1682
+ php_phongo_cursor_iterator_rewind ,
1683
+ NULL /* invalidate_current is not used */
1685
1684
};
1686
1685
1687
- zend_object_iterator * phongo_cursor_get_iterator (zend_class_entry * ce , zval * object , int by_ref TSRMLS_DC ) /* {{{ */
1686
+ zend_object_iterator * php_phongo_cursor_get_iterator (zend_class_entry * ce , zval * object , int by_ref TSRMLS_DC ) /* {{{ */
1688
1687
{
1689
- php_phongo_cursor_t * cursor = (php_phongo_cursor_t * )zend_object_store_get_object (object TSRMLS_CC );
1690
- phongo_cursor_it * cursor_it = NULL ;
1688
+ php_phongo_cursor_iterator * cursor_it = NULL ;
1691
1689
1692
1690
if (by_ref ) {
1693
1691
zend_error (E_ERROR , "An iterator cannot be used with foreach by reference" );
1694
1692
}
1695
1693
1696
- cursor_it = ecalloc (1 , sizeof (phongo_cursor_it ));
1694
+ cursor_it = ecalloc (1 , sizeof (php_phongo_cursor_iterator ));
1697
1695
1698
- if (cursor -> visitor_data .zchild ) {
1699
- zval_ptr_dtor (& cursor -> visitor_data .zchild );
1700
- cursor -> visitor_data .zchild = NULL ;
1701
- }
1696
+ Z_ADDREF_P (object );
1697
+ cursor_it -> intern .data = (void * )object ;
1698
+ cursor_it -> intern .funcs = & php_phongo_cursor_iterator_funcs ;
1699
+ cursor_it -> cursor = (php_phongo_cursor_t * )zend_object_store_get_object (object TSRMLS_CC );
1700
+ /* cursor_it->current should already be allocated to zero */
1702
1701
1703
- cursor_it -> iterator .data = cursor ;
1704
- cursor_it -> iterator .funcs = & phongo_cursor_iterator_funcs ;
1702
+ php_phongo_cursor_free_current (cursor_it -> cursor );
1705
1703
1706
1704
return (zend_object_iterator * )cursor_it ;
1707
1705
} /* }}} */
0 commit comments