Skip to content

Commit

Permalink
Add dynType_realType for code deduplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
PengZheng committed Jan 22, 2024
1 parent 74f1243 commit 6223856
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 53 deletions.
5 changes: 5 additions & 0 deletions libs/dfi/include/dyn_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ CELIX_DFI_EXPORT size_t dynType_size(const dyn_type* type);
*/
CELIX_DFI_EXPORT int dynType_type(const dyn_type* type);

/**
* Returns the real (non-reference) type of the dyn type.
*/
CELIX_DFI_EXPORT const dyn_type* dynType_realType(const dyn_type* type);

/**
* Returns the char identifier of the dyn type type.
* E.g. 'D' for a double, or '{' for a complex type.
Expand Down
57 changes: 18 additions & 39 deletions libs/dfi/src/dyn_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,8 @@ static void dynType_clearTypedPointer(dyn_type* type) {
}

int dynType_alloc(const dyn_type* type, void** bufLoc) {
const dyn_type* current = type;
const dyn_type* current = dynType_realType(type);

while (current->type == DYN_TYPE_REF) {
current = current->ref.ref;
}
void* inst = calloc(1, current->ffiType->size);
if (inst == NULL) {
celix_err_pushf("Error allocating memory for type '%c'", current->descriptor);
Expand Down Expand Up @@ -534,11 +531,7 @@ int dynType_complex_indexForName(const dyn_type* type, const char* name) {
const dyn_type* dynType_complex_dynTypeAt(const dyn_type* type, int index) {
assert(type->type == DYN_TYPE_COMPLEX);
assert(index >= 0);
dyn_type* sub = type->complex.types[index];
while (sub->type == DYN_TYPE_REF) {
sub = sub->ref.ref;
}
return sub;
return dynType_realType(type->complex.types[index]);
}

int dynType_complex_setValueAt(const dyn_type* type, int index, void* start, const void* in) {
Expand Down Expand Up @@ -629,9 +622,7 @@ static void dynType_deepFree(const dyn_type* type, void* loc, bool alsoDeleteSel
if (loc != NULL) {
const dyn_type* subType = NULL;
char* text = NULL;
while (type->type == DYN_TYPE_REF) {
type = type->ref.ref;
}
type = dynType_realType(type);
switch (type->type) {
case DYN_TYPE_COMPLEX :
dynType_freeComplexType(type, loc);
Expand Down Expand Up @@ -727,11 +718,7 @@ int dynType_sequence_increaseLengthAndReturnLastLoc(const dyn_type* type, void*

const dyn_type* dynType_sequence_itemType(const dyn_type* type) {
assert(type->type == DYN_TYPE_SEQUENCE);
dyn_type *itemType = type->sequence.itemType;
while (itemType->type == DYN_TYPE_REF) {
itemType = itemType->ref.ref;
}
return itemType;
return dynType_realType(type->sequence.itemType);
}

void dynType_simple_setValue(const dyn_type *type, void *inst, const void *in) {
Expand Down Expand Up @@ -834,24 +821,26 @@ static unsigned short dynType_getOffset(const dyn_type* type, int index) {
}

size_t dynType_size(const dyn_type* type) {
const dyn_type* rType = type;
while (rType->type == DYN_TYPE_REF) {
rType = type->ref.ref;
}
const dyn_type* rType = dynType_realType(type);
return rType->ffiType->size;
}

int dynType_type(const dyn_type* type) {
return type->type;
}

const dyn_type* dynType_realType(const dyn_type* type) {
const dyn_type* real = type;
while (real->type == DYN_TYPE_REF) {
real= real->ref.ref;
}
return real;

}

const dyn_type* dynType_typedPointer_getTypedType(const dyn_type* type) {
assert(type->type == DYN_TYPE_TYPED_POINTER);
dyn_type* typedType = type->typedPointer.typedType;
while (typedType->type == DYN_TYPE_REF) {
typedType = typedType->ref.ref;
}
return typedType;
return dynType_realType(type->typedPointer.typedType);
}


Expand Down Expand Up @@ -886,10 +875,7 @@ static void dynType_printDepth(int depth, FILE *stream) {
}

static void dynType_printAny(const char* name, const dyn_type* type, int depth, FILE *stream) {
const dyn_type* toPrint = type;
while (toPrint->type == DYN_TYPE_REF) {
toPrint = toPrint->ref.ref;
}
const dyn_type* toPrint = dynType_realType(type);
name = (name != NULL) ? name : "(unnamed)";
switch(toPrint->type) {
case DYN_TYPE_COMPLEX :
Expand Down Expand Up @@ -998,10 +984,7 @@ static void dynType_printText(const char* name, const dyn_type* type, int depth,

static void dynType_printTypes(const dyn_type* type, FILE* stream) {
if (type->type == DYN_TYPE_REF) {
const dyn_type* real = type->ref.ref;
while (real->type == DYN_TYPE_REF) {
real = real->ref.ref;
}
const dyn_type* real = dynType_realType(type->ref.ref);
dyn_type* parent = type->parent;
struct type_entry* pentry = NULL;
while (parent != NULL) {
Expand All @@ -1016,11 +999,7 @@ static void dynType_printTypes(const dyn_type* type, FILE* stream) {

struct type_entry* entry = NULL;
TAILQ_FOREACH(entry, &type->nestedTypesHead, entries) {
dyn_type* toPrint = entry->type;
while (toPrint->type == DYN_TYPE_REF) {
toPrint = toPrint->ref.ref;
}

const dyn_type* toPrint = dynType_realType(entry->type);
switch(toPrint->type) {
case DYN_TYPE_COMPLEX :
dynType_printComplexType(toPrint, stream);
Expand Down
5 changes: 1 addition & 4 deletions libs/dfi/src/dyn_type_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ dyn_type* dynType_findType(dyn_type *type, char *name) {
}

ffi_type* dynType_ffiType(const dyn_type * type) {
while (type->type == DYN_TYPE_REF) {
type = type->ref.ref;
}
return type->ffiType;
return dynType_realType(type)->ffiType;
}

12 changes: 2 additions & 10 deletions libs/dfi/src/json_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ int jsonSerializer_deserialize(const dyn_type* type, const char* input, size_t l
}

int jsonSerializer_deserializeJson(const dyn_type* type, json_t* input, void** out) {
const dyn_type* real = type;
while (real->type == DYN_TYPE_REF) {
real = real->ref.ref;
}
return jsonSerializer_createType(real, input, out);
return jsonSerializer_createType(dynType_realType(type), input, out);
}

static int jsonSerializer_createType(const dyn_type* type, json_t* val, void** result) {
Expand Down Expand Up @@ -269,11 +265,7 @@ static int jsonSerializer_parseEnum(const dyn_type* type, const char* enum_name,
}

int jsonSerializer_serializeJson(const dyn_type* type, const void* input, json_t** out) {
const dyn_type* real = type;
while (real->type == DYN_TYPE_REF) {
real = real->ref.ref;
}
return jsonSerializer_writeAny(real, input, out);
return jsonSerializer_writeAny(dynType_realType(type), input, out);
}

static int jsonSerializer_writeAny(const dyn_type* type, const void* input, json_t** out) {
Expand Down

0 comments on commit 6223856

Please sign in to comment.