xref: /PHP-5.6/Zend/zend_iterators.c (revision 3537e95d)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2016 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 /* $Id$ */
21 
22 #include "zend.h"
23 #include "zend_API.h"
24 
25 static zend_class_entry zend_iterator_class_entry;
26 
27 static zend_object_handlers iterator_object_handlers = {
28 	ZEND_OBJECTS_STORE_HANDLERS,
29 	NULL, /* prop read */
30 	NULL, /* prop write */
31 	NULL, /* read dim */
32 	NULL, /* write dim */
33 	NULL,
34 	NULL, /* get */
35 	NULL, /* set */
36 	NULL, /* has prop */
37 	NULL, /* unset prop */
38 	NULL, /* has dim */
39 	NULL, /* unset dim */
40 	NULL, /* props get */
41 	NULL, /* method get */
42 	NULL, /* call */
43 	NULL, /* get ctor */
44 	NULL, /* get_ce */
45 	NULL, /* get class name */
46 	NULL, /* compare */
47 	NULL, /* cast */
48 	NULL  /* count */
49 };
50 
zend_register_iterator_wrapper(TSRMLS_D)51 ZEND_API void zend_register_iterator_wrapper(TSRMLS_D)
52 {
53 	INIT_CLASS_ENTRY(zend_iterator_class_entry, "__iterator_wrapper", NULL);
54 	str_free(zend_iterator_class_entry.name);
55 	zend_iterator_class_entry.name = "__iterator_wrapper";
56 }
57 
iter_wrapper_dtor(void * object,zend_object_handle handle TSRMLS_DC)58 static void iter_wrapper_dtor(void *object, zend_object_handle handle TSRMLS_DC)
59 {
60 	zend_object_iterator *iter = (zend_object_iterator*)object;
61 	iter->funcs->dtor(iter TSRMLS_CC);
62 }
63 
zend_iterator_wrap(zend_object_iterator * iter TSRMLS_DC)64 ZEND_API zval *zend_iterator_wrap(zend_object_iterator *iter TSRMLS_DC)
65 {
66 	zval *wrapped;
67 
68 	MAKE_STD_ZVAL(wrapped);
69 	Z_TYPE_P(wrapped) = IS_OBJECT;
70 	Z_OBJ_HANDLE_P(wrapped) = zend_objects_store_put(iter, iter_wrapper_dtor, NULL, NULL TSRMLS_CC);
71 	Z_OBJ_HT_P(wrapped) = &iterator_object_handlers;
72 
73 	return wrapped;
74 }
75 
zend_iterator_unwrap(zval * array_ptr,zend_object_iterator ** iter TSRMLS_DC)76 ZEND_API enum zend_object_iterator_kind zend_iterator_unwrap(
77 	zval *array_ptr, zend_object_iterator **iter TSRMLS_DC)
78 {
79 	switch (Z_TYPE_P(array_ptr)) {
80 		case IS_OBJECT:
81 			if (Z_OBJ_HT_P(array_ptr) == &iterator_object_handlers) {
82 				*iter = (zend_object_iterator *)zend_object_store_get_object(array_ptr TSRMLS_CC);
83 				return ZEND_ITER_OBJECT;
84 			}
85 			if (Z_OBJPROP_P(array_ptr)) {
86 				return ZEND_ITER_PLAIN_OBJECT;
87 			}
88 			return ZEND_ITER_INVALID;
89 
90 		case IS_ARRAY:
91 			if (Z_ARRVAL_P(array_ptr)) {
92 				return ZEND_ITER_PLAIN_ARRAY;
93 			}
94 			return ZEND_ITER_INVALID;
95 
96 		default:
97 			return ZEND_ITER_INVALID;
98 	}
99 }
100 
101 /*
102  * Local variables:
103  * tab-width: 4
104  * c-basic-offset: 4
105  * indent-tabs-mode: t
106  * End:
107  */
108