Ernesto Fernández e154903743
Fix a few issues with the C generator (part 8) (#20378)
* [C] Deprecate *_create() to avoid *_free() confusion

The behaviour of *_free() doesn't match *_create(), so the user should
avoid using them together. But they still need *_free() to clean up
library-allocated objects, so add a _library_owned flag to each struct
as an attempt to tell them apart. This isn't perfect though, because the
user may neglect to zero the field, but they would still see a warning
once in a while so it serves its purpose.

To prevent the new deprecation warnings (intended for the user) from
showing up during the library build itself, define a new family of
*_create_internal() functions, and turn *_create() into simple wrappers.

* Update samples

* add eafer to c technical committee

---------

Co-authored-by: William Cheng <wing328hk@gmail.com>
2025-01-06 10:53:36 +08:00

114 lines
2.9 KiB
C

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "mapped_model.h"
static MappedModel_t *MappedModel_create_internal(
int another_property,
char *uuid_property
) {
MappedModel_t *MappedModel_local_var = malloc(sizeof(MappedModel_t));
if (!MappedModel_local_var) {
return NULL;
}
MappedModel_local_var->another_property = another_property;
MappedModel_local_var->uuid_property = uuid_property;
MappedModel_local_var->_library_owned = 1;
return MappedModel_local_var;
}
__attribute__((deprecated)) MappedModel_t *MappedModel_create(
int another_property,
char *uuid_property
) {
return MappedModel_create_internal (
another_property,
uuid_property
);
}
void MappedModel_free(MappedModel_t *MappedModel) {
if(NULL == MappedModel){
return ;
}
if(MappedModel->_library_owned != 1){
fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "MappedModel_free");
return ;
}
listEntry_t *listEntry;
if (MappedModel->uuid_property) {
free(MappedModel->uuid_property);
MappedModel->uuid_property = NULL;
}
free(MappedModel);
}
cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel) {
cJSON *item = cJSON_CreateObject();
// MappedModel->another_property
if(MappedModel->another_property) {
if(cJSON_AddNumberToObject(item, "another_property", MappedModel->another_property) == NULL) {
goto fail; //Numeric
}
}
// MappedModel->uuid_property
if(MappedModel->uuid_property) {
if(cJSON_AddStringToObject(item, "uuid_property", MappedModel->uuid_property) == NULL) {
goto fail; //String
}
}
return item;
fail:
if (item) {
cJSON_Delete(item);
}
return NULL;
}
MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){
MappedModel_t *MappedModel_local_var = NULL;
// MappedModel->another_property
cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property");
if (cJSON_IsNull(another_property)) {
another_property = NULL;
}
if (another_property) {
if(!cJSON_IsNumber(another_property))
{
goto end; //Numeric
}
}
// MappedModel->uuid_property
cJSON *uuid_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "uuid_property");
if (cJSON_IsNull(uuid_property)) {
uuid_property = NULL;
}
if (uuid_property) {
if(!cJSON_IsString(uuid_property) && !cJSON_IsNull(uuid_property))
{
goto end; //String
}
}
MappedModel_local_var = MappedModel_create_internal (
another_property ? another_property->valuedouble : 0,
uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL
);
return MappedModel_local_var;
end:
return NULL;
}