Enumeration.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU Lesser General Public License as        *
00006  *   published by the Free Software Foundation; either version 3 of the    *
00007  *   License, or (at your option) any later version.                       *
00008  *                                                                         *
00009  ***************************************************************************/
00010 /* $Id: Enumeration.c 4687 2007-12-09 18:57:18Z voxus $ */
00011 
00012 #include "onphp.h"
00013 
00014 #include "zend_globals.h"
00015 #include "zend_exceptions.h"
00016 
00017 #include "core/Base/Enumeration.h"
00018 #include "core/Exceptions.h"
00019 
00020 ONPHP_METHOD(Enumeration, __construct)
00021 {
00022     zval *id;
00023     
00024     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &id) == FAILURE) {
00025         WRONG_PARAM_COUNT;
00026     }
00027     
00028     zend_call_method_with_1_params(
00029         &getThis(),
00030         Z_OBJCE_P(getThis()),
00031         NULL,
00032         "setid",
00033         NULL,
00034         id
00035     );
00036 }
00037 
00038 ONPHP_METHOD(Enumeration, __sleep)
00039 {
00040     zval *out;
00041     
00042     array_init(out);
00043     
00044     add_next_index_string(
00045         out,
00046         "id",
00047         1
00048     );
00049     
00050     RETURN_ZVAL(out, 1, 1);
00051 }
00052 
00053 ONPHP_METHOD(Enumeration, __wakeup)
00054 {
00055     zend_call_method_with_1_params(
00056         &getThis(),
00057         Z_OBJCE_P(getThis()),
00058         NULL,
00059         "setid",
00060         NULL,
00061         ONPHP_READ_PROPERTY(getThis(), "id")
00062     );
00063 }
00064 
00065 ONPHP_METHOD(Enumeration, serialize)
00066 {
00067     zval *id;
00068     char *out = NULL;
00069     unsigned int length = 0;
00070     
00071     id = ONPHP_READ_PROPERTY(getThis(), "id");
00072     
00073     switch (Z_TYPE_P(id)) {
00074         case IS_LONG:
00075             
00076             out = emalloc(MAX_LENGTH_OF_LONG + 1 + 1);
00077             length = sprintf(out, "%ld", Z_LVAL_P(id));
00078             
00079             RETURN_STRINGL(out, length, 0);
00080             
00081         case IS_STRING:
00082             
00083             RETURN_ZVAL(id, 1, 0);
00084             
00085         case IS_NULL:
00086         default:
00087             
00088             RETURN_STRING("", 1);
00089     }
00090 }
00091 
00092 ONPHP_METHOD(Enumeration, unserialize)
00093 {
00094     zval *id;
00095     
00096     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &id) == FAILURE) {
00097         WRONG_PARAM_COUNT;
00098     }
00099     
00100     zend_call_method_with_1_params(
00101         &getThis(),
00102         Z_OBJCE_P(getThis()),
00103         NULL,
00104         "setid",
00105         NULL,
00106         id
00107     );
00108 }
00109 
00110 ONPHP_METHOD(Enumeration, getId)
00111 {
00112     zval *id;
00113     
00114     id = ONPHP_READ_PROPERTY(getThis(), "id");
00115     
00116     RETURN_ZVAL(id, 1, 0);
00117 }
00118 
00119 ONPHP_METHOD(Enumeration, setId)
00120 {
00121     zval *id, *names;
00122     zval **found;
00123     int result;
00124     
00125     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &id) == FAILURE) {
00126         WRONG_PARAM_COUNT;
00127     }
00128     
00129     zend_call_method_with_0_params(
00130         &getThis(),
00131         Z_OBJCE_P(getThis()),
00132         NULL,
00133         "getnamelist",
00134         &names
00135     );
00136     
00137     if (EG(exception)) {
00138         return;
00139     }
00140     
00141     if (Z_TYPE_P(names) != IS_ARRAY) {
00142         zend_throw_exception_ex(
00143             onphp_ce_WrongStateException,
00144             0 TSRMLS_CC,
00145             "names array is not an array"
00146         );
00147         ZVAL_FREE(names);
00148         return;
00149     }
00150     
00151     switch (Z_TYPE_P(id)) {
00152         case IS_LONG:
00153             
00154             result =
00155                 zend_hash_index_find(
00156                     Z_ARRVAL_P(names),
00157                     Z_LVAL_P(id),
00158                     (void **) &found
00159                 );
00160             
00161             break;
00162             
00163         case IS_STRING:
00164             
00165             result =
00166                 zend_symtable_find(
00167                     Z_ARRVAL_P(names),
00168                     Z_STRVAL_P(id),
00169                     Z_STRLEN_P(id) + 1,
00170                     (void **) &found
00171                 );
00172             
00173             break;
00174             
00175         case IS_NULL:
00176             
00177             result =
00178                 zend_hash_find(
00179                     Z_ARRVAL_P(names),
00180                     "",
00181                     1,
00182                     (void **) &found
00183                 );
00184             
00185             break;
00186             
00187         default:
00188             
00189             zend_throw_exception_ex(
00190                 onphp_ce_WrongArgumentException,
00191                 0 TSRMLS_CC,
00192                 "string or an integer expected"
00193             );
00194             ZVAL_FREE(names);
00195             return;
00196     }
00197     
00198     if (result == SUCCESS) {
00199         ONPHP_UPDATE_PROPERTY(getThis(), "id", id);
00200         ONPHP_UPDATE_PROPERTY(getThis(), "name", *found);
00201         
00202         ZVAL_FREE(names);
00203     } else {
00204         if (Z_TYPE_P(id) != IS_STRING) {
00205             SEPARATE_ARG_IF_REF(id);
00206             convert_to_string(id);
00207         }
00208         
00209         zend_throw_exception_ex(
00210             onphp_ce_MissingElementException,
00211             0 TSRMLS_CC,
00212             "knows nothing about such id == {%s}",
00213             Z_STRVAL_P(id)
00214         );
00215         ZVAL_FREE(names);
00216         return;
00217     }
00218     
00219     RETURN_ZVAL(getThis(), 1, 0);
00220 }
00221 
00222 ONPHP_METHOD(Enumeration, getList)
00223 {
00224     zval *enm, *out;
00225     
00226     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &enm) == FAILURE) {
00227         WRONG_PARAM_COUNT;
00228     }
00229     
00230     zend_call_method_with_0_params(
00231         &enm,
00232         Z_OBJCE_P(enm),
00233         NULL,
00234         "getobjectlist",
00235         &out
00236     );
00237     
00238     if (EG(exception)) {
00239         return;
00240     } else {
00241         RETURN_ZVAL(out, 1, 1);
00242     }
00243 }
00244 
00245 ONPHP_METHOD(Enumeration, getAnyId)
00246 {
00247     RETURN_LONG(1);
00248 }
00249 
00250 ONPHP_METHOD(Enumeration, getObjectList)
00251 {
00252     zval *names, *list;
00253     zval **element;
00254     HashTable *table;
00255     HashPosition pointer;
00256     
00257     ALLOC_INIT_ZVAL(list);
00258     array_init(list);
00259     
00260     zend_call_method_with_0_params(
00261         &getThis(),
00262         Z_OBJCE_P(getThis()),
00263         NULL,
00264         "getnamelist",
00265         &names
00266     );
00267     
00268     if (
00269         Z_TYPE_P(names) != IS_ARRAY
00270     ) {
00271         RETURN_ZVAL(list, 1, 1);
00272     } else if (EG(exception)) {
00273         return;
00274     }
00275     
00276     table = Z_ARRVAL_P(names);
00277     
00278     for (
00279         zend_hash_internal_pointer_reset_ex(table, &pointer);
00280         zend_hash_get_current_data_ex(table, (void **) &element, &pointer) == SUCCESS; zend_hash_move_forward_ex(table, &pointer)
00281     ) {
00282         char *key;
00283         unsigned long index;
00284         unsigned int length;
00285         int result;
00286         zval *object, *arg, *out;
00287         
00288         result = 
00289             zend_hash_get_current_key_ex(
00290                 table,
00291                 &key,
00292                 &length,
00293                 &index,
00294                 0,
00295                 &pointer
00296             );
00297         
00298         MAKE_STD_ZVAL(arg);
00299         
00300         if (result == HASH_KEY_IS_STRING) {
00301             ZVAL_STRINGL(arg, key, length, 1);
00302         } else if (result == HASH_KEY_IS_LONG) {
00303             ZVAL_LONG(arg, index);
00304         } else {
00305             zend_throw_exception_ex(
00306                 onphp_ce_WrongStateException,
00307                 0 TSRMLS_CC,
00308                 "weird key found"
00309             );
00310             ZVAL_FREE(arg);
00311             ZVAL_FREE(list);
00312             return;
00313         }
00314         
00315         MAKE_STD_ZVAL(object);
00316         object->value.obj = onphp_empty_object_new(Z_OBJCE_P(getThis()) TSRMLS_CC);
00317         Z_TYPE_P(object) = IS_OBJECT;
00318         
00319         zend_call_method_with_1_params(
00320             &object,
00321             Z_OBJCE_P(object),
00322             NULL,
00323             "__construct",
00324             &out,
00325             arg
00326         );
00327         
00328         if (EG(exception)) {
00329             ZVAL_FREE(object);
00330             ZVAL_FREE(list);
00331             ZVAL_FREE(arg);
00332             return;
00333         } else {
00334             zval_dtor(arg);
00335             
00336             add_next_index_zval(list, object);
00337         }
00338     }
00339     
00340     RETURN_ZVAL(list, 1, 1);
00341 }
00342 
00343 ONPHP_METHOD(Enumeration, toString)
00344 {
00345     zval *name = ONPHP_READ_PROPERTY(getThis(), "name");
00346     
00347     RETURN_ZVAL(name, 1, 0);
00348 }
00349 
00350 ONPHP_METHOD(Enumeration, getNameList)
00351 {
00352     zval *names = ONPHP_READ_PROPERTY(getThis(), "names");
00353 
00354     RETURN_ZVAL(names, 1, 0);
00355 }
00356 
00357 static ONPHP_ARGINFO_ONE;
00358 
00359 static
00360 ZEND_BEGIN_ARG_INFO(arginfo_enum, 0) \
00361     ZEND_ARG_OBJ_INFO(0, enumeration, Enumeration, 0) \
00362 ZEND_END_ARG_INFO();
00363 
00364 zend_function_entry onphp_funcs_Enumeration[] = {
00365     ONPHP_ME(Enumeration, __construct,  arginfo_one, ZEND_ACC_PUBLIC |  ZEND_ACC_FINAL | ZEND_ACC_CTOR)
00366     ONPHP_ME(Enumeration, getList,      arginfo_enum, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
00367     ONPHP_ME(Enumeration, getAnyId,     NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
00368     ONPHP_ME(Enumeration, getObjectList,NULL, ZEND_ACC_PUBLIC)
00369     ONPHP_ME(Enumeration, toString,     NULL, ZEND_ACC_PUBLIC)
00370     ONPHP_ME(Enumeration, getNameList,  NULL, ZEND_ACC_PUBLIC)
00371     ONPHP_ME(Enumeration, __sleep,      NULL, ZEND_ACC_PUBLIC)
00372     ONPHP_ME(Enumeration, __wakeup,     NULL, ZEND_ACC_PUBLIC)
00373     ONPHP_ME(Enumeration, serialize,    NULL, ZEND_ACC_PUBLIC)
00374     ONPHP_ME(Enumeration, unserialize,  arginfo_one, ZEND_ACC_PUBLIC)
00375     ONPHP_ME(Enumeration, getId,        NULL, ZEND_ACC_PUBLIC)
00376     ONPHP_ME(Enumeration, setId,        arginfo_one, ZEND_ACC_PUBLIC)
00377     {NULL, NULL, NULL}
00378 };

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4