Skip to content

Commit

Permalink
Apply const qualifier to dyn_type and improve ergonomics for function…
Browse files Browse the repository at this point in the history
…s that cannot fail.
  • Loading branch information
PengZheng committed Dec 21, 2023
1 parent 9e9b14f commit b6d5e39
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 359 deletions.
6 changes: 3 additions & 3 deletions libs/dfi/gtest/src/dyn_function_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ static bool func_acc() {
int nrOfArgs = 0;
bool isStruct = false;
bool isVoid = false;
dyn_type *nonExist = nullptr;
const dyn_type *nonExist = nullptr;
if (rc == 0) {
nrOfArgs = dynFunction_nrOfArguments(dynFunc);
dyn_type *arg1 = dynFunction_argumentTypeForIndex(dynFunc, 1);
const dyn_type *arg1 = dynFunction_argumentTypeForIndex(dynFunc, 1);
if (arg1 != nullptr) {
isStruct = '{' == dynType_descriptorType(arg1);
}
nonExist = dynFunction_argumentTypeForIndex(dynFunc, 10);
dyn_type *returnType = dynFunction_returnType(dynFunc);
const dyn_type* returnType = dynFunction_returnType(dynFunc);
if (returnType != nullptr) {
isVoid = 'V' == dynType_descriptorType(returnType);
}
Expand Down
6 changes: 2 additions & 4 deletions libs/dfi/gtest/src/dyn_type_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,8 @@ TEST_F(DynTypeTests, AssignTest2) {
dynType_complex_setValueAt(type, 0, &inst, &a);
ASSERT_EQ(2, inst.a);

void *loc = NULL;
dyn_type *subType = NULL;
dynType_complex_valLocAt(type, 1, (void *)&inst, &loc);
dynType_complex_dynTypeAt(type, 1, &subType);
(void)dynType_complex_valLocAt(type, 1, (void *)&inst);
const dyn_type* subType = dynType_complex_dynTypeAt(type, 1);

dynType_complex_setValueAt(subType, 0, &inst.b, &b_a);
ASSERT_EQ(1.1, inst.b.a);
Expand Down
4 changes: 2 additions & 2 deletions libs/dfi/include/dyn_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ CELIX_DFI_EXPORT int dynFunction_nrOfArguments(dyn_function_type *dynFunc);
* @param[in] argumentNr The argument index.
* @return The argument type.
*/
CELIX_DFI_EXPORT dyn_type *dynFunction_argumentTypeForIndex(dyn_function_type *dynFunc, int argumentNr);
CELIX_DFI_EXPORT const dyn_type* dynFunction_argumentTypeForIndex(dyn_function_type* dynFunc, int argumentNr);

/**
* @brief Returns the argument meta for the given argument index.
Expand All @@ -112,7 +112,7 @@ CELIX_DFI_EXPORT enum dyn_function_argument_meta dynFunction_argumentMetaForInde
* @param[in] dynFunc The dynamic type instance for function.
* @return The return value type.
*/
CELIX_DFI_EXPORT dyn_type * dynFunction_returnType(dyn_function_type *dynFunction);
CELIX_DFI_EXPORT const dyn_type* dynFunction_returnType(dyn_function_type *dynFunction);

/**
* @brief Destroys the given dynamic function type instance.
Expand Down
95 changes: 44 additions & 51 deletions libs/dfi/include/dyn_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ extern "C" {
* //Extended
* b unsigned char
* i uint32_t
* j uint62_t
* s uint64_t
* j uint64_t
* s uint16_t
* P untyped pointer (void *)
* t char* string
* N native int
Expand Down Expand Up @@ -112,72 +112,72 @@ typedef struct _dyn_type dyn_type;

TAILQ_HEAD(types_head, type_entry);
struct type_entry {
dyn_type *type;
dyn_type* type;
TAILQ_ENTRY(type_entry) entries;
};

TAILQ_HEAD(complex_type_entries_head, complex_type_entry);
struct complex_type_entry {
dyn_type *type;
char *name;
dyn_type* type;
char* name;
TAILQ_ENTRY(complex_type_entry) entries;
};

TAILQ_HEAD(meta_properties_head, meta_entry);
struct meta_entry {
char *name;
char *value;
char* name;
char* value;
TAILQ_ENTRY(meta_entry) entries;
};

/**
* Parses a descriptor stream and creates a dyn_type (dynamic type).
* If successful the type output argument points to the newly created dyn type.
* The caller is the owner of the dyn type and use dynType_destroy deallocate the memory.
* The caller is the owner of the dyn type and use dynType_destroy to deallocate the memory.
*
* In case of an error, an error message is added to celix_err.
*
* @param descriptorStream Stream to the descriptor.
* @param name name for the dyn_type. This can be used in references to dyn types.
* @param refTypes A list if reference-able dyn types.
* @param refTypes A list of referable dyn types.
* @param type Output argument for the parsed dyn type.
* @return 0 if successful.
*/
CELIX_DFI_EXPORT int dynType_parse(FILE *descriptorStream, const char *name, struct types_head *refTypes, dyn_type **type);
CELIX_DFI_EXPORT int dynType_parse(FILE* descriptorStream, const char* name, const struct types_head* refTypes, dyn_type** type);

/**
* Parses a descriptor string and creates a dyn_type (dynamic type).
* If successful the type output argument points to the newly created dyn type.
* The caller is the owner of the dyn type and use dynType_destroy deallocate the memory.
* The caller is the owner of the dyn type and use dynType_destroy to deallocate the memory.
*
* In case of an error, an error message is added to celix_err.
*
* @param descriptor The descriptor.
* @param name name for the dyn_type. This can be used in references to dyn types.
* @param refTypes A list if reference-able dyn types.
* @param refTypes A list if referable dyn types.
* @param type Output argument for the parsed dyn type.
* @return 0 if successful.
*/
CELIX_DFI_EXPORT int dynType_parseWithStr(const char *descriptor, const char *name, struct types_head *refTypes, dyn_type **type);
CELIX_DFI_EXPORT int dynType_parseWithStr(const char* descriptor, const char* name, const struct types_head* refTypes, dyn_type** type);

/**
* Destroy a dyn type and de-allocates the memory.
* @param type The dyn type to destroy.
*/
CELIX_DFI_EXPORT void dynType_destroy(dyn_type *type);
CELIX_DFI_EXPORT void dynType_destroy(dyn_type* type);

CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(dyn_type, dynType_destroy);

/**
* Allocates memory for a type instance described by a dyn type. The memory will be 0 allocated (calloc).
* Allocates memory for a type instance described by a dyn type. The memory will be set to zero (calloc).
*
* In case of an error, an error message is added to celix_err.
*
* @param type The dyn type for which structure to allocate.
* @param instance The output argument for the allocated memory.
* @return 0 on success.
*/
CELIX_DFI_EXPORT int dynType_alloc(dyn_type *type, void **instance);
CELIX_DFI_EXPORT int dynType_alloc(const dyn_type* type, void** instance);

/**
* free the memory for a type instance described by a dyn type.
Expand All @@ -186,61 +186,60 @@ CELIX_DFI_EXPORT int dynType_alloc(dyn_type *type, void **instance);
* @param type The dyn type for which structure to allocate.
* @param instance The memory location of the type instance.
*/
CELIX_DFI_EXPORT void dynType_free(dyn_type *type, void *instance);
CELIX_DFI_EXPORT void dynType_free(const dyn_type* type, void* instance);

/**
* Prints the dyn type information to the provided output stream.
* @param type The dyn type to print.
* @param stream The output stream (e.g. stdout).
*/
CELIX_DFI_EXPORT void dynType_print(dyn_type *type, FILE *stream);
CELIX_DFI_EXPORT void dynType_print(const dyn_type* type, FILE* stream);

/**
* Return the size of the structure descripbed by the dyn type.
* This this _not_ includes sizes of data where is only pointed to (i.e. content of sequences, strings, etc).
* Return the size of the structure described by the dyn type.
* This does _not_ includes sizes of data which is only pointed to (i.e. content of sequences, strings, etc).
*
* @param type The dyn type.
* @return The size of the type instance described by dyn type.
*/
CELIX_DFI_EXPORT size_t dynType_size(dyn_type *type);
CELIX_DFI_EXPORT size_t dynType_size(const dyn_type* type);

/**
* The type of the dyn type
* E.g. DYN_TYPE_SIMPLE, DYN_TYPE_COMPLEX, etc
* @param type The dyn type
* @return The type of the dyn type.
*/
CELIX_DFI_EXPORT int dynType_type(dyn_type *type);
CELIX_DFI_EXPORT int dynType_type(const dyn_type* type);

/**
* Returns the char identifier of the dyn type type.
* E.g. 'D' for a double, or '{' for a complex type.
* @param type The dyn type
* @return The descriptor of the dyn type.
*/
CELIX_DFI_EXPORT char dynType_descriptorType(dyn_type *type);
CELIX_DFI_EXPORT char dynType_descriptorType(const dyn_type* type);

/**
* Get the dyn type meta information for the provided name.
* @param type The dyn type.
* @param name The name of the requested meta information.
* @return The meta information or NULL if the meta information is not present.
*/
CELIX_DFI_EXPORT const char * dynType_getMetaInfo(dyn_type *type, const char *name);
CELIX_DFI_EXPORT const char* dynType_getMetaInfo(const dyn_type* type, const char* name);

/**
* Returns a list of meta entries.
* Returns the list of meta entries.
* Note that dyn type is owner of the entries. Traversing the entries is not thread safe.
* @param type The dyn type.
* @param entries The output arguments for the meta entries.
* @return 0 on success.
* @note It always returns valid list.
*/
CELIX_DFI_EXPORT int dynType_metaEntries(dyn_type *type, struct meta_properties_head **entries);
CELIX_DFI_EXPORT const struct meta_properties_head* dynType_metaEntries(const dyn_type* type);

/**
* Returns the name of the dyn type. Name can be NULL.
*/
CELIX_DFI_EXPORT const char * dynType_getName(dyn_type *type);
CELIX_DFI_EXPORT const char* dynType_getName(const dyn_type* type);


/**
Expand All @@ -251,30 +250,27 @@ CELIX_DFI_EXPORT const char * dynType_getName(dyn_type *type);
* @param name The field name.
* @return The field index or -1 if no field with the name was found.
*/
CELIX_DFI_EXPORT int dynType_complex_indexForName(dyn_type *type, const char *name);
CELIX_DFI_EXPORT int dynType_complex_indexForName(const dyn_type* type, const char* name);

/**
* Returns the field dyn type for a given complex type and a field index.
*
* @param type The dyn type. Must be a complex type.
* @param index The field index for which field the dyn type should be returned. The field index must be correct.
* @param subType The field dyn type as output.
* exists.
* @return 0 if successful.
*/
CELIX_DFI_EXPORT int dynType_complex_dynTypeAt(dyn_type *type, int index, dyn_type **subType);
CELIX_DFI_EXPORT const dyn_type* dynType_complex_dynTypeAt(const dyn_type* type, int index);

CELIX_DFI_EXPORT int dynType_complex_setValueAt(dyn_type *type, int index, void *inst, void *in);
CELIX_DFI_EXPORT int dynType_complex_valLocAt(dyn_type *type, int index, void *inst, void **valLoc);
CELIX_DFI_EXPORT int dynType_complex_entries(dyn_type *type, struct complex_type_entries_head **entries);
CELIX_DFI_EXPORT size_t dynType_complex_nrOfEntries(dyn_type *type);
CELIX_DFI_EXPORT int dynType_complex_setValueAt(const dyn_type* type, int index, void* inst, const void* in);
CELIX_DFI_EXPORT void* dynType_complex_valLocAt(const dyn_type* type, int index, void* inst);
CELIX_DFI_EXPORT const struct complex_type_entries_head* dynType_complex_entries(const dyn_type* type);
CELIX_DFI_EXPORT size_t dynType_complex_nrOfEntries(const dyn_type* type);

//sequence

/**
* Initialize a sequence struct with a cap & len of 0 and the buf to NULL.
*/
CELIX_DFI_EXPORT void dynType_sequence_init(dyn_type *type, void *inst);
CELIX_DFI_EXPORT void dynType_sequence_init(const dyn_type* type, void* inst);

/**
* Allocates memory for a sequence with capacity cap.
Expand All @@ -284,7 +280,7 @@ CELIX_DFI_EXPORT void dynType_sequence_init(dyn_type *type, void *inst);
* In case of an error, an error message is added to celix_err.
*
*/
CELIX_DFI_EXPORT int dynType_sequence_alloc(dyn_type *type, void *inst, uint32_t cap);
CELIX_DFI_EXPORT int dynType_sequence_alloc(const dyn_type* type, void* inst, uint32_t cap);

/**
* Reserve a sequence capacity of cap
Expand All @@ -295,7 +291,7 @@ CELIX_DFI_EXPORT int dynType_sequence_alloc(dyn_type *type, void *inst, uint32_t
* In case of an error, an error message is added to celix_err.
*
*/
CELIX_DFI_EXPORT int dynType_sequence_reserve(dyn_type *type, void *inst, uint32_t cap);
CELIX_DFI_EXPORT int dynType_sequence_reserve(const dyn_type* type, void* inst, uint32_t cap);

/**
* @brief Gets the value location for a specific sequence index from a sequence instance.
Expand All @@ -309,7 +305,7 @@ CELIX_DFI_EXPORT int dynType_sequence_reserve(dyn_type *type, void *inst, uint32
* @return 0 if successful.
* @retval 1 if the index is out of bounds.
*/
CELIX_DFI_EXPORT int dynType_sequence_locForIndex(dyn_type *type, void *seqLoc, int index, void **valLoc);
CELIX_DFI_EXPORT int dynType_sequence_locForIndex(const dyn_type* type, void* seqLoc, int index, void** valLoc);

/**
* @brief Increase the length of the sequence by one and return the value location for the last element.
Expand All @@ -322,29 +318,26 @@ CELIX_DFI_EXPORT int dynType_sequence_locForIndex(dyn_type *type, void *seqLoc,
* @return 0 if successful.
* @retval 1 if the sequence length is already at the max.
*/
CELIX_DFI_EXPORT int dynType_sequence_increaseLengthAndReturnLastLoc(dyn_type *type, void *seqLoc, void **valLoc);
CELIX_DFI_EXPORT int dynType_sequence_increaseLengthAndReturnLastLoc(const dyn_type* type, void* seqLoc, void** valLoc);

/**
* @brief Get the item type of a sequence.
* @param[in] type The dyn type. Must be a sequence type.
* @return The item type of the sequence.
*/
CELIX_DFI_EXPORT dyn_type * dynType_sequence_itemType(dyn_type *type);
CELIX_DFI_EXPORT const dyn_type* dynType_sequence_itemType(const dyn_type* type);

/**
* @brief Get the length of a sequence.
* @param[in] seqLoc The sequence instance.
* @return The length of the sequence.
*/
CELIX_DFI_EXPORT uint32_t dynType_sequence_length(void *seqLoc);
CELIX_DFI_EXPORT uint32_t dynType_sequence_length(const void* seqLoc);

/**
* @brief Gets the typedType of a typedPointer type.
* @param[in] type The dyn type. Must be a typedPointer type.
* @param[out] typedType The typedType of the typedPointer type.
* @return 0
*/
CELIX_DFI_EXPORT int dynType_typedPointer_getTypedType(dyn_type *type, dyn_type **typedType);
CELIX_DFI_EXPORT const dyn_type* dynType_typedPointer_getTypedType(const dyn_type* type);

/**
* @brief Allocates and initializes a string type.
Expand All @@ -357,15 +350,15 @@ CELIX_DFI_EXPORT int dynType_typedPointer_getTypedType(dyn_type *type, dyn_type
* @return 0 if successful.
* @retval 1 if Cannot allocate memory.
*/
CELIX_DFI_EXPORT int dynType_text_allocAndInit(dyn_type *type, void *textLoc, const char *value);
CELIX_DFI_EXPORT int dynType_text_allocAndInit(const dyn_type* type, void* textLoc, const char* value);

/**
* @brief Sets the value of a simple type.
* @param[in] type The dyn type. Must be a simple type.
* @param[out] inst The instance of the simple type.
* @param[in] in The value to set.
*/
CELIX_DFI_EXPORT void dynType_simple_setValue(dyn_type *type, void *inst, void *in);
CELIX_DFI_EXPORT void dynType_simple_setValue(const dyn_type* type, void* inst, const void* in);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions libs/dfi/include/json_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ CELIX_DFI_EXPORT int jsonSerializer_deserialize(dyn_type *type, const char *inpu
* @return 0 if successful, otherwise 1.
*
*/
CELIX_DFI_EXPORT int jsonSerializer_deserializeJson(dyn_type *type, json_t *input, void **result);
CELIX_DFI_EXPORT int jsonSerializer_deserializeJson(const dyn_type *type, json_t *input, void **result);

/**
* @brief Serialize a given type to a JSON string.
Expand Down Expand Up @@ -88,7 +88,7 @@ CELIX_DFI_EXPORT int jsonSerializer_serialize(dyn_type *type, const void* input,
* @return 0 if successful, otherwise 1.
*
*/
CELIX_DFI_EXPORT int jsonSerializer_serializeJson(dyn_type *type, const void* input, json_t **out);
CELIX_DFI_EXPORT int jsonSerializer_serializeJson(const dyn_type *type, const void* input, json_t **out);

#ifdef __cplusplus
}
Expand Down
12 changes: 6 additions & 6 deletions libs/dfi/src/dyn_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ int dynFunction_nrOfArguments(dyn_function_type *dynFunc) {
return count;
}

dyn_type *dynFunction_argumentTypeForIndex(dyn_function_type *dynFunc, int argumentNr) {
dyn_type *result = NULL;
const dyn_type* dynFunction_argumentTypeForIndex(dyn_function_type* dynFunc, int argumentNr) {
dyn_type* result = NULL;
int index = 0;
dyn_function_argument_type *entry = NULL;
dyn_function_argument_type* entry = NULL;
TAILQ_FOREACH(entry, &dynFunc->arguments, entries) {
if (index == argumentNr) {
result = entry->type;
Expand All @@ -300,12 +300,12 @@ dyn_type *dynFunction_argumentTypeForIndex(dyn_function_type *dynFunc, int argum
return result;
}

dyn_type * dynFunction_returnType(dyn_function_type *dynFunction) {
const dyn_type* dynFunction_returnType(dyn_function_type *dynFunction) {
return dynFunction->funcReturn;
}

bool dynFunction_hasReturn(dyn_function_type *dynFunction) {
dyn_type *t = dynFunction_returnType(dynFunction);
bool dynFunction_hasReturn(dyn_function_type* dynFunction) {
const dyn_type* t = dynFunction_returnType(dynFunction);
return t->descriptor != 'V';
}

Expand Down
2 changes: 1 addition & 1 deletion libs/dfi/src/dyn_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static int dynInterface_checkInterface(dyn_interface_type *intf) {

struct method_entry *mEntry = NULL;
TAILQ_FOREACH(mEntry, &intf->methods, entries) {
dyn_type *type = dynFunction_returnType(mEntry->dynFunc);
const dyn_type* type = dynFunction_returnType(mEntry->dynFunc);
int descriptor = dynType_descriptorType(type);
if (descriptor != 'N') {
celix_err_pushf("Parse Error. Only method with a return type 'N' (native int) are supported. Got return type '%c'\n", descriptor);
Expand Down
Loading

0 comments on commit b6d5e39

Please sign in to comment.