1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
PsychToolbox2/Source/Common/PsychStructGlue.c
AUTHORS:
Allen.Ingling@nyu.edu awi
mario.kleiner.de@gmail.com mk
PLATFORMS: All
PROJECTS: All
HISTORY:
12/31/02 awi wrote it.
03/28/11 mk Make 64-bit clean.
DESCRIPTION:
PsychStructGlue defines abstracted functions to create structs passed
between the calling environment and the PsychToolbox.
TO DO:
-All "PsychAllocOut*" functions should be modified to accept -1 as the position argument and
in that case allocate an mxArray which may be embedded within a struct. We then have only one
function for settings structure array fields for all types. This requires the addition of another
pointer argument which points to the native struct representation, we could accept that optionally
only when we specify -1 as the argument number.
-PsychSetStructArray* functions should check that the named field is present in the struct
and exit gracefully with an error.
-Consider changing PsychSetStructArrayStructElement() do de-allocate the inner struct which it was
passed. Pass the pointer in indirectly and set it to point to the field embedded within the struct.
*/
#include "Psych.h"
#if PSYCH_LANGUAGE == PSYCH_MATLAB
// functions for outputting structs
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
PsychAllocOutStructArray()
-If argument is optional we allocate the structure even if the argument is not present. If this bothers you,
then check within the subfunction for the presense of a return argument before creating the struct array. We
allocate space regardeless of whether the argument is present because this is consistant with other "PsychAllocOut*"
functions which behave this way because in some situations subfunctions might derive returned results from values
stored in an optional argument.
-If position is -1 then don't attempt to return the created structure to the calling environment. Instead just
allocate the structure and return it in pStruct. This is how to create a structure which is embeded within another
structure using PsychSetStructArrayStructArray(). Note that we use -1 as the flag and not NULL because NULL is 0 and
0 is reserved for future use as a reference to the subfunction name, of if none then the function name.
The special value numElements == -1 means to return a single struct, instead of a struct array.
*/
psych_bool PsychAllocOutStructArray(int position,
PsychArgRequirementType isRequired,
int numElements,
int numFields,
const char **fieldNames,
PsychGenericScriptType **pStruct)
{
mxArray **mxArrayOut;
mwSize structArrayNumDims = 2;
mwSize structArrayDims[2];
PsychError matchError;
psych_bool putOut;
// -1 is special and means "a single struct", which is the same as a 1-element
// struct array in Octave/Matlab. This -1 special case is needed for other scripting
// environments that don't have mex api semantics:
if (numElements == -1)
numElements = 1;
structArrayDims[0] = 1;
structArrayDims[1] = numElements;
if (position != kPsychNoArgReturn) { //Return the result to both the C caller and the scripting environment.
PsychSetReceivedArgDescriptor(position, FALSE, PsychArgOut);
PsychSetSpecifiedArgDescriptor(position, PsychArgOut, PsychArgType_structArray, isRequired, 1,1,numElements,numElements,0,0);
*pStruct = mxCreateStructArray(structArrayNumDims, structArrayDims, numFields, fieldNames);
matchError = PsychMatchDescriptors();
putOut = PsychAcceptOutputArgumentDecider(isRequired, matchError);
if (putOut) {
mxArrayOut = PsychGetOutArgMxPtr(position);
*mxArrayOut = *pStruct;
}
return(putOut);
} else{ //Return the result only to the C caller. Ignore "required".
*pStruct = mxCreateStructArray(structArrayNumDims, structArrayDims, numFields, fieldNames);
return(TRUE);
}
}
/* FONTS on OSX only.
PsychAssignOutStructArray()
Accept a pointer to a struct array and Assign the struct array to be the designated return variable.
*/
psych_bool PsychAssignOutStructArray( int position,
PsychArgRequirementType isRequired,
PsychGenericScriptType *pStruct)
{
mxArray **mxArrayOut;
PsychError matchError;
psych_bool putOut;
PsychSetReceivedArgDescriptor(position, FALSE, PsychArgOut);
PsychSetSpecifiedArgDescriptor(position, PsychArgOut, PsychArgType_structArray, isRequired, 1,1,0,kPsychUnboundedArraySize,0,0);
matchError = PsychMatchDescriptors();
putOut = PsychAcceptOutputArgumentDecider(isRequired, matchError);
if (putOut) {
mxArrayOut = PsychGetOutArgMxPtr(position);
*mxArrayOut = pStruct;
}
return(putOut);
}
// functions for filling in struct elements by type
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
PsychSetStructArrayStringElement()
The variable "index", the index of the element within the struct array, is zero-indexed.
*/
void PsychSetStructArrayStringElement(const char *fieldName,
int index,
char *text,
PsychGenericScriptType *pStruct)
{
int fieldNumber;
size_t numElements;
psych_bool isStruct;
mxArray *mxFieldValue;
char errmsg[256];
//check for bogus arguments
numElements = mxGetM(pStruct) * mxGetN(pStruct);
if ((size_t) index >= numElements)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index");
fieldNumber = mxGetFieldNumber(pStruct, fieldName);
if (fieldNumber == -1) {
sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName);
PsychErrorExitMsg(PsychError_internal, errmsg);
}
isStruct = mxIsStruct(pStruct);
if (!isStruct)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure.");
//do stuff
mxFieldValue = mxCreateString(text);
mxSetField(pStruct, (mwIndex) index, fieldName, mxFieldValue);
if (PSYCH_LANGUAGE == PSYCH_OCTAVE) mxDestroyArray(mxFieldValue);
}
/*
PsychSetStructArrayDoubleElement()
Note: The variable "index" is zero-indexed.
*/
void PsychSetStructArrayDoubleElement(const char *fieldName,
int index,
double value,
PsychGenericScriptType *pStruct)
{
int fieldNumber;
size_t numElements;
psych_bool isStruct;
mxArray *mxFieldValue;
char errmsg[256];
//check for bogus arguments
numElements = mxGetM(pStruct) * mxGetN(pStruct);
if ((size_t) index >= numElements)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index");
fieldNumber = mxGetFieldNumber(pStruct, fieldName);
if (fieldNumber == -1) {
sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName);
PsychErrorExitMsg(PsychError_internal, errmsg);
}
isStruct = mxIsStruct(pStruct);
if (!isStruct)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure.");
//do stuff
mxFieldValue = mxCreateDoubleMatrix(1, 1, mxREAL);
mxGetPr(mxFieldValue)[0] = value;
mxSetField(pStruct, (mwIndex) index, fieldName, mxFieldValue);
if (PSYCH_LANGUAGE == PSYCH_OCTAVE) mxDestroyArray(mxFieldValue);
}
/*
PsychSetStructArrayUnsignedInt64Element()
Note: The variable "index" is zero-indexed.
*/
void PsychSetStructArrayUnsignedInt64Element(const char *fieldName,
int index,
psych_uint64 value,
PsychGenericScriptType *pStruct)
{
int fieldNumber;
size_t numElements;
psych_bool isStruct;
mxArray *mxFieldValue;
char errmsg[256];
mwSize dimArray[2];
int numDims = 2;
dimArray[0] = dimArray[1] = 1;
//check for bogus arguments
numElements = mxGetM(pStruct) * mxGetN(pStruct);
if ((size_t) index >= numElements)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index");
fieldNumber = mxGetFieldNumber(pStruct, fieldName);
if (fieldNumber == -1) {
sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName);
PsychErrorExitMsg(PsychError_internal, errmsg);
}
isStruct = mxIsStruct(pStruct);
if (!isStruct)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure.");
//do stuff
mxFieldValue = mxCreateNumericArray(numDims, (mwSize*) dimArray, mxUINT64_CLASS, mxREAL);
((psych_uint64*) mxGetData(mxFieldValue))[0] = value;
mxSetField(pStruct, (mwIndex) index, fieldName, mxFieldValue);
if (PSYCH_LANGUAGE == PSYCH_OCTAVE) mxDestroyArray(mxFieldValue);
}
/*
PsychSetStructArrayBooleanElement()
Note: The variable "index" is zero-indexed.
*/
void PsychSetStructArrayBooleanElement( const char *fieldName,
int index,
psych_bool state,
PsychGenericScriptType *pStruct)
{
int fieldNumber;
size_t numElements;
psych_bool isStruct;
mxArray *mxFieldValue;
char errmsg[256];
//check for bogus arguments
numElements = mxGetM(pStruct) * mxGetN(pStruct);
if ((size_t) index >= numElements)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index");
fieldNumber = mxGetFieldNumber(pStruct, fieldName);
if (fieldNumber == -1) {
sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName);
PsychErrorExitMsg(PsychError_internal, errmsg);
}
isStruct = mxIsStruct(pStruct);
if (!isStruct)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure.");
//do stuff
mxFieldValue = mxCreateLogicalMatrix(1, 1);
mxGetLogicals(mxFieldValue)[0] = state;
mxSetField(pStruct, (mwIndex) index, fieldName, mxFieldValue);
if (PSYCH_LANGUAGE == PSYCH_OCTAVE) mxDestroyArray(mxFieldValue);
}
/*
PsychSetStructArrayStructElement()
*/
void PsychSetStructArrayStructElement(const char *fieldName,
int index,
PsychGenericScriptType *pStructInner,
PsychGenericScriptType *pStructOuter)
{
int fieldNumber;
size_t numElements;
psych_bool isStruct;
char errmsg[256];
//check for bogus arguments
numElements = mxGetM(pStructOuter) * mxGetN(pStructOuter);
if ((size_t) index >= numElements)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index");
fieldNumber = mxGetFieldNumber(pStructOuter, fieldName);
if (fieldNumber == -1) {
sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName);
PsychErrorExitMsg(PsychError_internal, errmsg);
}
isStruct = mxIsStruct(pStructInner);
if (!isStruct)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a struct field to a non-existent structure.");
isStruct = mxIsStruct(pStructOuter);
if (!isStruct)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure.");
//do stuff
mxSetField(pStructOuter, (mwIndex) index, fieldName, pStructInner);
if (PSYCH_LANGUAGE == PSYCH_OCTAVE) mxDestroyArray(pStructInner);
}
/*
PsychSetStructArrayNativeElement()
*/
void PsychSetStructArrayNativeElement(const char *fieldName,
int index,
PsychGenericScriptType *pNativeElement,
PsychGenericScriptType *pStructArray)
{
int fieldNumber;
size_t numElements;
psych_bool isStruct;
char errmsg[256];
//check for bogus arguments
numElements = mxGetM(pStructArray) * mxGetN(pStructArray);
if ((size_t) index >= numElements)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index");
fieldNumber = mxGetFieldNumber(pStructArray, fieldName);
if (fieldNumber == -1) {
sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName);
PsychErrorExitMsg(PsychError_internal, errmsg);
}
isStruct = mxIsStruct(pStructArray);
if (!isStruct)
PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure.");
//do stuff
mxSetField(pStructArray, (mwIndex) index, fieldName, pNativeElement);
}
#endif
|