1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2007 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: zend_iterators.c,v 1.12.2.1.2.2 2007/01/01 09:35:46 sebastian Exp $ */
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 :
51 : ZEND_API void zend_register_iterator_wrapper(TSRMLS_D)
52 220 : {
53 220 : INIT_CLASS_ENTRY(zend_iterator_class_entry, "__iterator_wrapper", NULL);
54 220 : free(zend_iterator_class_entry.name);
55 220 : zend_iterator_class_entry.name = "__iterator_wrapper";
56 220 : }
57 :
58 : static void iter_wrapper_dtor(void *object, zend_object_handle handle TSRMLS_DC)
59 4 : {
60 4 : zend_object_iterator *iter = (zend_object_iterator*)object;
61 4 : iter->funcs->dtor(iter TSRMLS_CC);
62 4 : }
63 :
64 : ZEND_API zval *zend_iterator_wrap(zend_object_iterator *iter TSRMLS_DC)
65 4 : {
66 : zval *wrapped;
67 :
68 4 : MAKE_STD_ZVAL(wrapped);
69 4 : Z_TYPE_P(wrapped) = IS_OBJECT;
70 4 : Z_OBJ_HANDLE_P(wrapped) = zend_objects_store_put(iter, iter_wrapper_dtor, NULL, NULL TSRMLS_CC);
71 4 : Z_OBJ_HT_P(wrapped) = &iterator_object_handlers;
72 :
73 4 : return wrapped;
74 : }
75 :
76 : ZEND_API enum zend_object_iterator_kind zend_iterator_unwrap(
77 : zval *array_ptr, zend_object_iterator **iter TSRMLS_DC)
78 5340 : {
79 5340 : switch (Z_TYPE_P(array_ptr)) {
80 : case IS_OBJECT:
81 26 : if (Z_OBJ_HT_P(array_ptr) == &iterator_object_handlers) {
82 22 : *iter = (zend_object_iterator *)zend_object_store_get_object(array_ptr TSRMLS_CC);
83 22 : return ZEND_ITER_OBJECT;
84 : }
85 4 : if (HASH_OF(array_ptr)) {
86 4 : return ZEND_ITER_PLAIN_OBJECT;
87 : }
88 0 : return ZEND_ITER_INVALID;
89 :
90 : case IS_ARRAY:
91 5314 : if (HASH_OF(array_ptr)) {
92 5314 : return ZEND_ITER_PLAIN_ARRAY;
93 : }
94 0 : return ZEND_ITER_INVALID;
95 :
96 : default:
97 0 : 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 : */
|