Back to index

php5  5.3.10
zend_iterators.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        | 
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Author: Wez Furlong <wez@thebrainroom.com>                           |
00016    |         Marcus Boerger <helly@php.net>                               |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_iterators.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "zend.h"
00023 #include "zend_API.h"
00024 
00025 static zend_class_entry zend_iterator_class_entry;
00026 
00027 static zend_object_handlers iterator_object_handlers = {
00028        ZEND_OBJECTS_STORE_HANDLERS,
00029        NULL, /* prop read */
00030        NULL, /* prop write */
00031        NULL, /* read dim */
00032        NULL, /* write dim */
00033        NULL,
00034        NULL, /* get */
00035        NULL, /* set */
00036        NULL, /* has prop */
00037        NULL, /* unset prop */
00038        NULL, /* has dim */
00039        NULL, /* unset dim */
00040        NULL, /* props get */
00041        NULL, /* method get */
00042        NULL, /* call */
00043        NULL, /* get ctor */
00044        NULL, /* get_ce */
00045        NULL, /* get class name */
00046        NULL, /* compare */
00047        NULL, /* cast */
00048        NULL  /* count */
00049 };
00050 
00051 ZEND_API void zend_register_iterator_wrapper(TSRMLS_D)
00052 {
00053        INIT_CLASS_ENTRY(zend_iterator_class_entry, "__iterator_wrapper", NULL);
00054        free(zend_iterator_class_entry.name);
00055        zend_iterator_class_entry.name = "__iterator_wrapper";
00056 }
00057 
00058 static void iter_wrapper_dtor(void *object, zend_object_handle handle TSRMLS_DC)
00059 {
00060        zend_object_iterator *iter = (zend_object_iterator*)object;
00061        iter->funcs->dtor(iter TSRMLS_CC);
00062 }
00063 
00064 ZEND_API zval *zend_iterator_wrap(zend_object_iterator *iter TSRMLS_DC)
00065 {
00066        zval *wrapped;
00067 
00068        MAKE_STD_ZVAL(wrapped);
00069        Z_TYPE_P(wrapped) = IS_OBJECT;
00070        Z_OBJ_HANDLE_P(wrapped) = zend_objects_store_put(iter, iter_wrapper_dtor, NULL, NULL TSRMLS_CC);
00071        Z_OBJ_HT_P(wrapped) = &iterator_object_handlers;
00072 
00073        return wrapped;
00074 }
00075 
00076 ZEND_API enum zend_object_iterator_kind zend_iterator_unwrap(
00077        zval *array_ptr, zend_object_iterator **iter TSRMLS_DC)
00078 {
00079        switch (Z_TYPE_P(array_ptr)) {
00080               case IS_OBJECT:
00081                      if (Z_OBJ_HT_P(array_ptr) == &iterator_object_handlers) {
00082                             *iter = (zend_object_iterator *)zend_object_store_get_object(array_ptr TSRMLS_CC);
00083                             return ZEND_ITER_OBJECT;
00084                      }
00085                      if (HASH_OF(array_ptr)) {
00086                             return ZEND_ITER_PLAIN_OBJECT;
00087                      }
00088                      return ZEND_ITER_INVALID;
00089 
00090               case IS_ARRAY:
00091                      if (HASH_OF(array_ptr)) {
00092                             return ZEND_ITER_PLAIN_ARRAY;
00093                      }
00094                      return ZEND_ITER_INVALID;
00095 
00096               default:
00097                      return ZEND_ITER_INVALID;
00098        }
00099 }
00100 
00101 /*
00102  * Local variables:
00103  * tab-width: 4
00104  * c-basic-offset: 4
00105  * indent-tabs-mode: t
00106  * End:
00107  */