Skip to content

Commit

Permalink
Refactor to use Parameter by value instead of pointer
Browse files Browse the repository at this point in the history
Updated the `Parameter` type to `typedef void* Parameter` and refactored function signatures across multiple files to use `Parameter` by value instead of by pointer. This change affects functions in `app_api.h`, `app_api_implementation.c`, `octavelibczi.c`, `mex_function.cpp`, `function.h`, `function.cpp`, `func_getversion.cpp`, `CziReader` class, `CArgsUtils` class, `argsutils.h`, `utils.cpp`, `utils.h`, `mexFunctions.h`, and `exportedfunctions.cpp`.

Key changes include:
- Updated function signatures to use `Parameter` directly.
- Modified method signatures in `CziReader.h` and `CziReader` methods.
- Adjusted `CArgsUtils` methods to handle `Parameter` by value.
- Updated `mexFunction` to reinterpret `plhs` and `prhs` as `Parameter` arrays.
- Simplified memory management and improved code safety by avoiding raw pointers.
  • Loading branch information
ptahmose committed Oct 14, 2024
1 parent fc9a06a commit 569cf74
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 185 deletions.
56 changes: 29 additions & 27 deletions AppModel/include/app_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern "C" {
#include <stdbool.h>
#include <stddef.h>

/// This type is representing a "parameter", which is the abstraction of "some object" in Matlab/Octave.
/// With Matlab, it maps to mxArray*. It is to be regarded as an opaque type.
typedef void* Parameter;

enum AppExtensionClassId
Expand Down Expand Up @@ -36,65 +38,65 @@ extern "C" {

double (*pfn_GetNaNDouble)(void);

void* (*pfn_GetData)(const Parameter* parameter);
void* (*pfn_GetData)(const Parameter parameter);

uint8_t* (*pfn_GetUint8s)(const Parameter* parameter);
uint8_t* (*pfn_GetUint8s)(const Parameter parameter);

int8_t* (*pfn_GetInt8s)(const Parameter* parameter);
int8_t* (*pfn_GetInt8s)(const Parameter parameter);

uint16_t* (*pfn_GetUint16s)(const Parameter* parameter);
uint16_t* (*pfn_GetUint16s)(const Parameter parameter);

int16_t* (*pfn_GetInt16s)(const Parameter* parameter);
int16_t* (*pfn_GetInt16s)(const Parameter parameter);

uint32_t* (*pfn_GetUint32s)(const Parameter* parameter);
uint32_t* (*pfn_GetUint32s)(const Parameter parameter);

int32_t* (*pfn_GetInt32s)(const Parameter* parameter);
int32_t* (*pfn_GetInt32s)(const Parameter parameter);

uint64_t* (*pfn_GetUint64s)(const Parameter* parameter);
uint64_t* (*pfn_GetUint64s)(const Parameter parameter);

int64_t* (*pfn_GetInt64s)(const Parameter* parameter);
int64_t* (*pfn_GetInt64s)(const Parameter parameter);

double* (*pfn_GetDoubles)(const Parameter* parameter);
double* (*pfn_GetDoubles)(const Parameter parameter);

float* (*pfn_GetSingles)(const Parameter* parameter);
float* (*pfn_GetSingles)(const Parameter parameter);

bool* (*pfn_GetLogicals)(const Parameter* parameter);
bool* (*pfn_GetLogicals)(const Parameter parameter);

bool (*pfn_IsNumeric)(const Parameter* parameter);
bool (*pfn_IsNumeric)(const Parameter parameter);

bool (*pfn_IsChar)(const Parameter* parameter);
bool (*pfn_IsChar)(const Parameter parameter);

bool (*pfn_IsSparse)(const Parameter* parameter);
bool (*pfn_IsSparse)(const Parameter parameter);

bool (*pfn_IsStruct)(const Parameter* parameter);
bool (*pfn_IsStruct)(const Parameter parameter);

Parameter* (*pfn_CreateString)(const char* string);
Parameter(*pfn_CreateString)(const char* string);

void (*pfn_ReportErrorAndRaiseSignal)(const char* identifier, const char* message);

char* (*pfn_StrDupHostAllocated)(const char* string);

Parameter* (*pfn_CreateStructArray)(size_t ndim, const size_t* dims, int nfields, const char** field_names);
Parameter(*pfn_CreateStructArray)(size_t ndim, const size_t* dims, int nfields, const char** field_names);

void (*pfn_SetFieldByNumber)(Parameter* pa, size_t i, int fieldnum, Parameter* value);
void (*pfn_SetFieldByNumber)(Parameter pa, size_t i, int fieldnum, Parameter value);

char* (*pfn_ConvertToUTF8String)(const Parameter* parameter);
char* (*pfn_ConvertToUTF8String)(const Parameter parameter);

void (*pfn_Free)(void* ptr);

Parameter* (*pfn_CreateNumericMatrixReal)(size_t m, size_t n, enum AppExtensionClassId class_id);
Parameter(*pfn_CreateNumericMatrixReal)(size_t m, size_t n, enum AppExtensionClassId class_id);

enum AppExtensionClassId(*pfn_GetClassId)(const Parameter* parameter);
enum AppExtensionClassId(*pfn_GetClassId)(const Parameter parameter);

size_t (*pfn_GetNumberOfElements)(const Parameter* parameter);
size_t(*pfn_GetNumberOfElements)(const Parameter parameter);

size_t (*pfn_GetNumberOfDimensions)(const Parameter* parameter);
size_t(*pfn_GetNumberOfDimensions)(const Parameter parameter);

void (*pfn_GetSizeOfDimensions)(const Parameter* parameter, size_t number_of_dimension, size_t* sizes);
void (*pfn_GetSizeOfDimensions)(const Parameter parameter, size_t number_of_dimension, size_t* sizes);

Parameter* (*pfn_GetField)(const Parameter* parameter, const char* field_name);
Parameter(*pfn_GetField)(const Parameter parameter, const char* field_name);

Parameter* (*pfn_CreateNumericArrayReal)(size_t ndim, const size_t* dims, enum AppExtensionClassId class_id);
Parameter(*pfn_CreateNumericArrayReal)(size_t ndim, const size_t* dims, enum AppExtensionClassId class_id);
};

#ifdef __cplusplus
Expand Down
64 changes: 32 additions & 32 deletions MatlabMex/app_api_implementation.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,89 +22,89 @@ double matlabMexGetNaNDouble(void)
return mxGetNaN();
}

void* matlabMexGetData(const Parameter* parameter)
void* matlabMexGetData(const Parameter parameter)
{
return mxGetData((const mxArray*)parameter);
}

uint8_t* matlabMexGetUint8s(const Parameter* parameter)
uint8_t* matlabMexGetUint8s(const Parameter parameter)
{
return mxGetUint8s((const mxArray*)parameter);
}

int8_t* matlabMexGetInt8s(const Parameter* parameter)
int8_t* matlabMexGetInt8s(const Parameter parameter)
{
return mxGetInt8s((const mxArray*)parameter);
}

uint16_t* matlabMexGetUint16s(const Parameter* parameter)
uint16_t* matlabMexGetUint16s(const Parameter parameter)
{
return mxGetUint16s((const mxArray*)parameter);
}

int16_t* matlabMexGetInt16s(const Parameter* parameter)
int16_t* matlabMexGetInt16s(const Parameter parameter)
{
return mxGetInt16s((const mxArray*)parameter);
}

uint32_t* matlabMexGetUint32s(const Parameter* parameter)
uint32_t* matlabMexGetUint32s(const Parameter parameter)
{
return mxGetUint32s((const mxArray*)parameter);
}

int32_t* matlabMexGetInt32s(const Parameter* parameter)
int32_t* matlabMexGetInt32s(const Parameter parameter)
{
return mxGetInt32s((const mxArray*)parameter);
}

uint64_t* matlabMexGetUint64s(const Parameter* parameter)
uint64_t* matlabMexGetUint64s(const Parameter parameter)
{
return mxGetUint64s((const mxArray*)parameter);
}

int64_t* matlabMexGetInt64s(const Parameter* parameter)
int64_t* matlabMexGetInt64s(const Parameter parameter)
{
return mxGetInt64s((const mxArray*)parameter);
}

double* matlabMexGetDoubles(const Parameter* parameter)
double* matlabMexGetDoubles(const Parameter parameter)
{
return mxGetDoubles((const mxArray*)parameter);
}

float* matlabMexGetSingles(const Parameter* parameter)
float* matlabMexGetSingles(const Parameter parameter)
{
return mxGetSingles((const mxArray*)parameter);
}

bool* matlabMexGetLogicals(const Parameter* parameter)
bool* matlabMexGetLogicals(const Parameter parameter)
{
return mxGetLogicals((const mxArray*)parameter);
}

bool matlabMexIsNumeric(const Parameter* parameter)
bool matlabMexIsNumeric(const Parameter parameter)
{
return mxIsNumeric((const mxArray*)parameter);
}

bool matlabMexIsChar(const Parameter* parameter)
bool matlabMexIsChar(const Parameter parameter)
{
return mxIsChar((const mxArray*)parameter);
}

bool matlabMexIsSparse(const Parameter* parameter)
bool matlabMexIsSparse(const Parameter parameter)
{
return mxIsSparse((const mxArray*)parameter);
}

bool matlabMexIsStruct(const Parameter* parameter)
bool matlabMexIsStruct(const Parameter parameter)
{
return mxIsStruct((const mxArray*)parameter);
}

Parameter* matlabMexCreateString(const char* string)
Parameter matlabMexCreateString(const char* string)
{
return (Parameter*)mxCreateString(string);
return (Parameter)mxCreateString(string);
}

void matlabReportErrorAndRaiseSignal(const char* identifier, const char* message)
Expand All @@ -121,17 +121,17 @@ char* matlabStrDupHostAllocated(const char* string)
return msz;
}

Parameter* matlabCreateStructArray(size_t ndim, const size_t* dims, int nfields, const char** field_names)
Parameter matlabCreateStructArray(size_t ndim, const size_t* dims, int nfields, const char** field_names)
{
return (Parameter*)mxCreateStructArray(ndim, dims, nfields, field_names);
return (Parameter)mxCreateStructArray(ndim, dims, nfields, field_names);
}

void matlabSetFieldByNumber(Parameter* pa, size_t i, int fieldnum, Parameter* value)
void matlabSetFieldByNumber(Parameter pa, size_t i, int fieldnum, Parameter value)
{
mxSetFieldByNumber((mxArray*)pa, i, fieldnum, (mxArray*)value);
}

char* matlabConvertToUTF8String(const Parameter* parameter)
char* matlabConvertToUTF8String(const Parameter parameter)
{
return mxArrayToUTF8String((const mxArray*)parameter);
}
Expand Down Expand Up @@ -172,13 +172,13 @@ mxClassID AppExtensionClassIdToMxClassId(enum AppExtensionClassId class_id)
}
}

Parameter* matlabCreateNumericMatrixReal(size_t m, size_t n, enum AppExtensionClassId class_id)
Parameter matlabCreateNumericMatrixReal(size_t m, size_t n, enum AppExtensionClassId class_id)
{
mxClassID mx_class_id = AppExtensionClassIdToMxClassId(class_id);
return (Parameter*)mxCreateNumericMatrix(m, n, mx_class_id, mxREAL);
return (Parameter)mxCreateNumericMatrix(m, n, mx_class_id, mxREAL);
}

enum AppExtensionClassId matlabGetClassId(const Parameter* parameter)
enum AppExtensionClassId matlabGetClassId(const Parameter parameter)
{
mxClassID mx_class_id = mxGetClassID((const mxArray*)parameter);
switch (mx_class_id)
Expand Down Expand Up @@ -210,17 +210,17 @@ enum AppExtensionClassId matlabGetClassId(const Parameter* parameter)
}
}

size_t matlabGetNumberOfElements(const Parameter* parameter)
size_t matlabGetNumberOfElements(const Parameter parameter)
{
return mxGetNumberOfElements((const mxArray*)parameter);
}

size_t matlabGetNumberOfDimensions(const Parameter* parameter)
size_t matlabGetNumberOfDimensions(const Parameter parameter)
{
return mxGetNumberOfDimensions((const mxArray*)parameter);
}

void matlabGetSizeOfDimensions(const Parameter* parameter, size_t number_of_dimension, size_t* sizes)
void matlabGetSizeOfDimensions(const Parameter parameter, size_t number_of_dimension, size_t* sizes)
{
const size_t* ptr_sizes = mxGetDimensions((const mxArray*)parameter);
for (size_t i = 0; i < number_of_dimension; ++i)
Expand All @@ -229,20 +229,20 @@ void matlabGetSizeOfDimensions(const Parameter* parameter, size_t number_of_dime
}
}

Parameter* matlabGetField(const Parameter* parameter, const char* field_name)
Parameter matlabGetField(const Parameter parameter, const char* field_name)
{
if (!matlabMexIsStruct(parameter))
{
return NULL;
}

return (Parameter*)mxGetField((const mxArray*)parameter, 0, field_name);
return (Parameter)mxGetField((const mxArray*)parameter, 0, field_name);
}

Parameter* matlabCreateNumericArrayReal(size_t ndim, const size_t* dims, enum AppExtensionClassId class_id)
Parameter matlabCreateNumericArrayReal(size_t ndim, const size_t* dims, enum AppExtensionClassId class_id)
{
mxClassID mx_class_id = AppExtensionClassIdToMxClassId(class_id);
return (Parameter*)mxCreateNumericArray(ndim, dims, mx_class_id, mxREAL);
return (Parameter)mxCreateNumericArray(ndim, dims, mx_class_id, mxREAL);
}

struct IAppExtensionFunctions g_appExtensionFunctions =
Expand Down
2 changes: 1 addition & 1 deletion MatlabMex/mex_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
gIsInitialized = true;
}

mexlibCZI::mexFunction(nlhs, reinterpret_cast<Parameter**>(plhs), nrhs, reinterpret_cast<const Parameter**>(prhs), &g_appExtensionFunctions);
mexlibCZI::mexFunction(nlhs, reinterpret_cast<Parameter*>(plhs), nrhs, (const Parameter*)prhs, &g_appExtensionFunctions);
}
Loading

0 comments on commit 569cf74

Please sign in to comment.