xref: /php-src/Zend/zend_iterators.h (revision 6335264c)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Author: Wez Furlong <wez@thebrainroom.com>                           |
16    |         Marcus Boerger <helly@php.net>                               |
17    +----------------------------------------------------------------------+
18 */
19 
20 #ifndef ZEND_ITERATORS_H
21 #define ZEND_ITERATORS_H
22 
23 #include "zend_types.h"
24 
25 /* These iterators were designed to operate within the foreach()
26  * structures provided by the engine, but could be extended for use
27  * with other iterative engine opcodes.
28  * These methods have similar semantics to the zend_hash API functions
29  * with similar names.
30  * */
31 
32 typedef struct _zend_object_iterator zend_object_iterator;
33 
34 typedef struct _zend_object_iterator_funcs {
35 	/* release all resources associated with this iterator instance */
36 	void (*dtor)(zend_object_iterator *iter);
37 
38 	/* check for end of iteration (FAILURE or SUCCESS if data is valid) */
39 	zend_result (*valid)(zend_object_iterator *iter);
40 
41 	/* fetch the item data for the current element */
42 	zval *(*get_current_data)(zend_object_iterator *iter);
43 
44 	/* fetch the key for the current element (optional, may be NULL). The key
45 	 * should be written into the provided zval* using the ZVAL_* macros. If
46 	 * this handler is not provided auto-incrementing integer keys will be
47 	 * used. */
48 	void (*get_current_key)(zend_object_iterator *iter, zval *key);
49 
50 	/* step forwards to next element */
51 	void (*move_forward)(zend_object_iterator *iter);
52 
53 	/* rewind to start of data (optional, may be NULL) */
54 	void (*rewind)(zend_object_iterator *iter);
55 
56 	/* invalidate current value/key (optional, may be NULL) */
57 	void (*invalidate_current)(zend_object_iterator *iter);
58 
59 	/* Expose owned values to GC.
60 	 * This has the same semantics as the corresponding object handler. */
61 	HashTable *(*get_gc)(zend_object_iterator *iter, zval **table, int *n);
62 } zend_object_iterator_funcs;
63 
64 struct _zend_object_iterator {
65 	zend_object std;
66 	zval data;
67 	const zend_object_iterator_funcs *funcs;
68 	zend_ulong index; /* private to fe_reset/fe_fetch opcodes */
69 };
70 
71 typedef struct _zend_class_iterator_funcs {
72 	zend_function *zf_new_iterator;
73 	zend_function *zf_valid;
74 	zend_function *zf_current;
75 	zend_function *zf_key;
76 	zend_function *zf_next;
77 	zend_function *zf_rewind;
78 } zend_class_iterator_funcs;
79 
80 typedef struct _zend_class_arrayaccess_funcs {
81 	zend_function *zf_offsetget;
82 	zend_function *zf_offsetexists;
83 	zend_function *zf_offsetset;
84 	zend_function *zf_offsetunset;
85 } zend_class_arrayaccess_funcs;
86 
87 BEGIN_EXTERN_C()
88 /* given a zval, returns stuff that can be used to iterate it. */
89 ZEND_API zend_object_iterator* zend_iterator_unwrap(zval *array_ptr);
90 
91 /* given an iterator, wrap it up as a zval for use by the engine opcodes */
92 ZEND_API void zend_iterator_init(zend_object_iterator *iter);
93 ZEND_API void zend_iterator_dtor(zend_object_iterator *iter);
94 
95 ZEND_API void zend_register_iterator_wrapper(void);
96 END_EXTERN_C()
97 
98 #endif /* ZEND_ITERATORS_H */
99