/* {{{ proto array MongoDB\Driver\WriteConcern::bsonSerialize() */ static PHP_METHOD(WriteConcern, bsonSerialize) { const mongoc_write_concern_t *write_concern = phongo_write_concern_from_zval(getThis() TSRMLS_CC); if (zend_parse_parameters_none() == FAILURE) { return; } php_phongo_write_concern_to_zval(return_value, write_concern); convert_to_object(return_value); } /* }}} */
/* {{{ MongoCursor->sort */ PHP_METHOD(MongoCursor, sort) { zval *zfields, *query; mongo_cursor *cursor = (mongo_cursor*)zend_object_store_get_object(getThis() TSRMLS_CC); MONGO_CHECK_INITIALIZED(cursor->link, MongoCursor); if (cursor->started_iterating) { zend_throw_exception(mongo_ce_CursorException, "cannot modify cursor after beginning iteration.", 0 TSRMLS_CC); return; } if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfields) == FAILURE || IS_SCALAR_P(zfields)) { return; } query = cursor->query; zval_add_ref(&zfields); convert_to_object(zfields); add_assoc_zval(query, "orderby", zfields); RETURN_ZVAL(getThis(), 1, 0); }
static PyObject *read_global_var(GlobSupportObject *gs) { return convert_to_object(gs->gs_data, gs->gs_type); }
/** Get a value (object, clock, or global) \verbatim gl('get',name) \endverbatim **/ void cmex_get(int nlhs, mxArray *plhs[], /**< {data} */ int nrhs, const mxArray *prhs[] ) /**< (name) */ { if (nrhs>0) { char name[1024]; OBJECT *obj=NULL; if (!mxIsChar(prhs[0])) output_error("entity name (arg 1) is not a string"); else if (nlhs>1) output_error("only one return value is possible"); else if (mxGetString(prhs[0],name,sizeof(name))!=0) output_error("object name too long"); else if (strcmp(name,"clock")==0) { char *fnames[] = {"timestamp","timestring","timezone"}; char buffer[256]; mxArray *pTimestamp = mxCreateDoubleMatrix(1,1,mxREAL); mxArray *pTimestring = mxCreateString(convert_from_timestamp(global_clock,buffer,sizeof(buffer))?buffer:"(error)"); mxArray *pTimezone = mxCreateString(timestamp_current_timezone()); *(double*)mxGetPr(pTimestamp) = ((double)global_clock)/TS_SECOND; plhs[0] = mxCreateStructMatrix(1,1,sizeof(fnames)/sizeof(fnames[0]),fnames); mxSetFieldByNumber(plhs[0],0,0,pTimestamp); mxSetFieldByNumber(plhs[0],0,1,pTimestring); mxSetFieldByNumber(plhs[0],0,2,pTimezone); } else if (strcmp(name,"property")==0 && nrhs>1) { if (mxGetString(prhs[1],name,sizeof(name))!=0) output_error("missing property name"); else { char classname[256]; char propname[256]; if (sscanf(name,"%[^.].%s",classname,propname)==2) { CLASS *pClass = class_get_class_from_classname(classname); if (pClass) { PROPERTY *pProp = class_find_property(pClass,propname); if (pProp) { char *fields[] = {"class","name","type","size","access","unit","delegation","keywords"}; int fn = 0; mxArray *oclass = mxCreateString(classname); mxArray *prop = mxCreateString(pProp->name); mxArray *type = mxCreateString(class_get_property_typename(pProp->ptype)); mxArray *size = mxCreateDoubleMatrix(1,1,mxREAL); mxArray *access = mxCreateString("(na)"); /** @todo implement get_property access info (ticket #187) */ mxArray *unit = mxCreateString(pProp->unit->name); mxArray *delegation = mxCreateString(pProp->delegation?pProp->delegation->oclass->name:"(none)"); mxArray *keywords = mxCreateString("(na)"); /** @todo implement get_property keywords (ticket #188) */ *(mxGetPr(size)) = pProp->size==0?1:pProp->size; plhs[0] = mxCreateStructMatrix(1,1,sizeof(fields)/sizeof(fields[0]),fields); mxSetFieldByNumber(plhs[0],0,fn++,oclass); mxSetFieldByNumber(plhs[0],0,fn++,prop); mxSetFieldByNumber(plhs[0],0,fn++,type); mxSetFieldByNumber(plhs[0],0,fn++,size); mxSetFieldByNumber(plhs[0],0,fn++,access); mxSetFieldByNumber(plhs[0],0,fn++,unit); mxSetFieldByNumber(plhs[0],0,fn++,delegation); mxSetFieldByNumber(plhs[0],0,fn++,keywords); } else output_error("property %s is not found in class %s", propname,classname); } else output_error("class %s is not found"); } else output_error("property name not in class.name format"); } } else if ((convert_to_object(name,&obj,NULL))==0) { GLOBALVAR *var = global_find(name); if (var==NULL) output_error("entity '%s' not found", name); else if (var->prop->ptype==PT_double) { size_t size = var->prop->size?var->prop->size:1; plhs[0] = mxCreateDoubleMatrix(size,1,mxREAL); memcpy(mxGetPr(plhs[0]),(void*)var->prop->addr,sizeof(double)*size); } else if (var->prop->ptype==PT_int32) { size_t size = var->prop->size?var->prop->size:1; plhs[0] = mxCreateDoubleMatrix(size,1,mxREAL); memcpy(mxGetPr(plhs[0]),(void*)var->prop->addr,sizeof(double)*size); } else if (var->prop->ptype!=PT_double) output_error("cannot retrieve globals that are of type %s",class_get_property_typename(var->prop->ptype)); } else if ((plhs[0]=get_object_data(obj))==NULL) output_error("unable to extract %s data", name); } else output_error("object not specified"); return; }
/** Set a value \verbatim gl('set',id,property,value) \endverbatim **/ void cmex_set(int nlhs, mxArray *plhs[], /**< () */ int nrhs, const mxArray *prhs[]) /**< (object_id,property_name,value) */ { if (nlhs>0) output_error("set does not return a value"); else if (nrhs==1 && mxIsStruct(prhs[0])) set_object_data(prhs[0]); else if (nrhs!=3) output_error("set requires either a structure, or object id, property name and value"); else if (!mxIsChar(prhs[0])) output_error("object id (arg 0) must be a string"); else if (mxIsChar(prhs[1])) { char value[1024]; PROPERTY* prop; OBJECT *obj=NULL; GLOBALVAR *var=NULL; if (mxGetString(prhs[0],value,sizeof(value))!=0) output_error("object name (arg 0) too long"); else if (strcmp(value,"global")==0) { if (!mxIsChar(prhs[1])) output_error("global variable name is not a string"); else if (!mxIsChar(prhs[2])) output_error("global value is not is not a string"); else { char name[32], value[1024]; mxGetString(prhs[1],name,sizeof(name)); mxGetString(prhs[2],value,sizeof(value)); if (global_setvar(name,value)!=SUCCESS) output_error("unable to set global '%s' to '%s'", name,value); } } else if (convert_to_object(value,&obj,NULL)==0) output_error("object (arg 0) %s not found",value); else if (mxGetString(prhs[1],value,sizeof(value))!=0) output_error("property name (arg 1) too long"); else if ((prop=object_get_property(obj,value))==NULL) output_error("property name (arg 1) %s not found in object %s:%d", value,obj->oclass->name, obj->id); else if (mxIsChar(prhs[2])) { if (mxGetString(prhs[2],value,sizeof(value))!=0) output_error("value (arg 2) is too long"); else if (object_set_value_by_name(obj,prop->name,value)==0) output_error("unable to set %s:%d/%s to %s", obj->oclass->name, obj->id, prop->name, value); else return; /* ok */ } else if (mxIsDouble(prhs[2]) && prop->ptype==PT_double) { double v = *mxGetPr(prhs[2]); if (prop->ptype==PT_double && object_set_double_by_name(obj,prop->name,v)==0) output_error("unable to set %s:%d/%s to %lg", obj->oclass->name, obj->id, prop->name, v); } else if (mxIsComplex(prhs[2]) || mxIsDouble(prhs[2])) { sprintf(value,"%lg%+lgi",*mxGetPr(prhs[2]),mxIsComplex(prhs[2])?*mxGetPi(prhs[2]):0); if (object_set_value_by_name(obj,prop->name,value)==0) output_error("unable to set %s:%d/%s to %s", obj->oclass->name, obj->id, prop->name, value); } else output_error("value (arg 2) has an unsupported data type"); } else output_error("property or data (arg 1) type is not valid"); }