Skip to content

Commit

Permalink
Model cosmetics and fixes (#258)
Browse files Browse the repository at this point in the history
* use const declaration where appropriate
* parse enum fix: indicate token was processed
  • Loading branch information
ekoby authored Mar 18, 2021
1 parent fc642b7 commit 42b6417
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions includes/ziti/model_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ return rc;\
int parse_##type##_array(array(type) *a, const char *json, size_t len) { return model_parse_array((void***)a, json, len, &type##_META); }\
ptr(type) alloc_##type() { return (ptr(type))calloc(1, sizeof(type)); } \
int cmp_##type(const type *lh, const type *rh) { return model_cmp(lh, rh, &type##_META); }\
void free_##type(type *v) { model_free(v, &type##_META); } \
void free_##type(ptr(type) v) { model_free(v, &type##_META); } \
void free_##type##_array(array(type) *ap) { model_free_array((void***)ap, &type##_META); } \
MODEL_API ssize_t type##_to_json_r(const ptr(type) v, int flags, char *outbuf, size_t max) { \
return model_to_json_r(v, &type##_META, flags, outbuf, max); } \
Expand Down Expand Up @@ -202,19 +202,19 @@ typedef struct model_map {
struct model_impl_s *impl;
} model_map;

ZITI_FUNC size_t model_map_size(model_map *map);
ZITI_FUNC size_t model_map_size(const model_map *map);

ZITI_FUNC void *model_map_set(model_map *map, const char *key, void *val);

ZITI_FUNC void *model_map_get(model_map *map, const char *key);
ZITI_FUNC void *model_map_get(const model_map *map, const char *key);

ZITI_FUNC void *model_map_remove(model_map *map, const char *key);

ZITI_FUNC void model_map_clear(model_map *map, _free_f val_free_func);

ZITI_FUNC model_map_iter model_map_iterator(model_map *map);
ZITI_FUNC model_map_iter model_map_iterator(const model_map *map);

ZITI_FUNC const char *model_map_it_key(model_map_iter *it);
ZITI_FUNC const char *model_map_it_key(const model_map_iter *it);

ZITI_FUNC void *model_map_it_value(model_map_iter it);

Expand Down
18 changes: 9 additions & 9 deletions library/model_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ limitations under the License.

static int parse_obj(void *obj, const char *json, jsmntok_t *tok, type_meta *meta);

static int model_map_compare(model_map *lh, model_map *rh, type_meta *m);
static int model_map_compare(const model_map *lh, const model_map *rh, type_meta *m);

jsmntok_t* parse_tokens(jsmn_parser *parser, const char *json, size_t len, size_t *ntok) {
size_t tok_cap = 256;
Expand Down Expand Up @@ -972,7 +972,7 @@ int parse_enum(void *ptr, const char *json, void *tok, const void *enum_type) {
} else {
return -1;
}
return 0;
return 1;
}

int json_enum(const void *ptr, void *bufp, int indent, int flags, const void *enum_type) {
Expand Down Expand Up @@ -1030,7 +1030,7 @@ static void map_resize_table(model_map* m) {
}
}

static struct model_map_entry *find_map_entry(model_map *m, const char *key, uint32_t *hash_out) {
static struct model_map_entry *find_map_entry(const model_map *m, const char *key, uint32_t *hash_out) {
uint32_t kh = key_hash(key);
if (hash_out) {
*hash_out = kh;
Expand All @@ -1046,7 +1046,7 @@ static struct model_map_entry *find_map_entry(model_map *m, const char *key, uin
return NULL;
}

size_t model_map_size(model_map *m) {
size_t model_map_size(const model_map *m) {
return m->impl ? m->impl->size : 0;
}

Expand Down Expand Up @@ -1084,8 +1084,8 @@ void *model_map_set(model_map *m, const char *key, void *val) {
return NULL;
}

void* model_map_get(model_map *m, const char* key) {
if (m->impl == NULL) {
void* model_map_get(const model_map *m, const char* key) {
if (m == NULL || m->impl == NULL) {
return NULL;
}

Expand Down Expand Up @@ -1128,12 +1128,12 @@ void model_map_clear(model_map *map, _free_f free_func) {
FREE(map->impl);
}

model_map_iter model_map_iterator(model_map *m) {
model_map_iter model_map_iterator(const model_map *m) {
if (m->impl == NULL) { return NULL; }
return LIST_FIRST(&m->impl->entries);
}

const char *model_map_it_key(model_map_iter *it) {
const char *model_map_it_key(const model_map_iter *it) {
return it != NULL ? ((struct model_map_entry *) it)->key : NULL;
}

Expand All @@ -1158,7 +1158,7 @@ model_map_iter model_map_it_remove(model_map_iter it) {
return next;
}

static int model_map_compare(model_map *lh, model_map *rh, type_meta *m) {
static int model_map_compare(const model_map *lh, const model_map *rh, type_meta *m) {
null_checks(lh, rh)

int rc = 0;
Expand Down

0 comments on commit 42b6417

Please sign in to comment.