Repository: qpid-proton Updated Branches: refs/heads/master 36f81e094 -> 1781b4e99
added pn_record_t Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/901e1473 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/901e1473 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/901e1473 Branch: refs/heads/master Commit: 901e147339d75ade9121c7f9a93df093f9d27576 Parents: 36f81e0 Author: Rafael Schloming <[email protected]> Authored: Sat Nov 22 09:27:33 2014 -0500 Committer: Rafael Schloming <[email protected]> Committed: Tue Nov 25 10:59:20 2014 -0500 ---------------------------------------------------------------------- proton-c/CMakeLists.txt | 1 + proton-c/include/proton/cid.h | 1 + proton-c/include/proton/object.h | 9 +++ proton-c/src/object/record.c | 142 ++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/901e1473/proton-c/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/proton-c/CMakeLists.txt b/proton-c/CMakeLists.txt index b09e1c4..96a0540 100644 --- a/proton-c/CMakeLists.txt +++ b/proton-c/CMakeLists.txt @@ -279,6 +279,7 @@ set (qpid-proton-core src/object/map.c src/object/string.c src/object/iterator.c + src/object/record.c src/util.c src/url.c http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/901e1473/proton-c/include/proton/cid.h ---------------------------------------------------------------------- diff --git a/proton-c/include/proton/cid.h b/proton-c/include/proton/cid.h index 0017020..5a06308 100644 --- a/proton-c/include/proton/cid.h +++ b/proton-c/include/proton/cid.h @@ -30,6 +30,7 @@ typedef enum { CID_pn_list, CID_pn_map, CID_pn_hash, + CID_pn_record, CID_pn_collector, CID_pn_event, http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/901e1473/proton-c/include/proton/object.h ---------------------------------------------------------------------- diff --git a/proton-c/include/proton/object.h b/proton-c/include/proton/object.h index b8ee7d6..2515f39 100644 --- a/proton-c/include/proton/object.h +++ b/proton-c/include/proton/object.h @@ -43,6 +43,7 @@ typedef struct pn_map_t pn_map_t; typedef struct pn_hash_t pn_hash_t; typedef void *(*pn_iterator_next_t)(void *state); typedef struct pn_iterator_t pn_iterator_t; +typedef struct pn_record_t pn_record_t; struct pn_class_t { const char *name; @@ -198,6 +199,14 @@ PN_EXTERN void *pn_iterator_start(pn_iterator_t *iterator, pn_iterator_next_t next, size_t size); PN_EXTERN void *pn_iterator_next(pn_iterator_t *iterator); +#define PN_LEGCTX ((pn_handle_t) 0) + +PN_EXTERN pn_record_t *pn_record(void); +PN_EXTERN void pn_record_def(pn_record_t *record, pn_handle_t key, const pn_class_t *clazz); +PN_EXTERN void *pn_record_get(pn_record_t *record, pn_handle_t key); +PN_EXTERN void pn_record_set(pn_record_t *record, pn_handle_t key, void *value); +PN_EXTERN void pn_record_clear(pn_record_t *record); + #ifdef __cplusplus } #endif http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/901e1473/proton-c/src/object/record.c ---------------------------------------------------------------------- diff --git a/proton-c/src/object/record.c b/proton-c/src/object/record.c new file mode 100644 index 0000000..60d5fae --- /dev/null +++ b/proton-c/src/object/record.c @@ -0,0 +1,142 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include <proton/object.h> +#include <stdlib.h> +#include <assert.h> + +typedef struct { + pn_handle_t key; + const pn_class_t *clazz; + void *value; +} pni_field_t; + +struct pn_record_t { + size_t size; + size_t capacity; + pni_field_t *fields; +}; + +static void pn_record_initialize(void *object) +{ + pn_record_t *record = (pn_record_t *) object; + record->size = 0; + record->capacity = 0; + record->fields = NULL; +} + +static void pn_record_finalize(void *object) +{ + pn_record_t *record = (pn_record_t *) object; + for (size_t i = 0; i < record->size; i++) { + pni_field_t *v = &record->fields[i]; + pn_class_decref(v->clazz, v->value); + } + free(record->fields); +} + +#define pn_record_hashcode NULL +#define pn_record_compare NULL +#define pn_record_inspect NULL + +pn_record_t *pn_record(void) +{ + static const pn_class_t clazz = PN_CLASS(pn_record); + pn_record_t *record = (pn_record_t *) pn_class_new(&clazz, sizeof(pn_record_t)); + pn_record_def(record, PN_LEGCTX, PN_VOID); + return record; +} + +static pni_field_t *pni_record_find(pn_record_t *record, pn_handle_t key) { + for (size_t i = 0; i < record->size; i++) { + pni_field_t *field = &record->fields[i]; + if (field->key == key) { + return field; + } + } + return NULL; +} + +static pni_field_t *pni_record_create(pn_record_t *record) { + record->size++; + if (record->size > record->capacity) { + record->fields = (pni_field_t *) realloc(record->fields, record->size * sizeof(pni_field_t)); + record->capacity = record->size; + } + pni_field_t *field = &record->fields[record->size - 1]; + field->key = 0; + field->clazz = NULL; + field->value = NULL; + return field; +} + +void pn_record_def(pn_record_t *record, pn_handle_t key, const pn_class_t *clazz) +{ + assert(record); + assert(clazz); + + pni_field_t *field = pni_record_find(record, key); + if (field) { + assert(field->clazz == clazz); + } else { + field = pni_record_create(record); + field->key = key; + field->clazz = clazz; + } +} + +void *pn_record_get(pn_record_t *record, pn_handle_t key) +{ + assert(record); + pni_field_t *field = pni_record_find(record, key); + if (field) { + return field->value; + } else { + return NULL; + } +} + +void pn_record_set(pn_record_t *record, pn_handle_t key, void *value) +{ + assert(record); + + pni_field_t *field = pni_record_find(record, key); + if (field) { + void *old = field->value; + field->value = value; + pn_class_incref(field->clazz, value); + pn_class_decref(field->clazz, old); + } +} + +void pn_record_clear(pn_record_t *record) +{ + assert(record); + for (size_t i = 0; i < record->size; i++) { + pni_field_t *field = &record->fields[i]; + pn_class_decref(field->clazz, field->value); + field->key = 0; + field->clazz = NULL; + field->value = NULL; + } + record->size = 0; + pn_record_def(record, PN_LEGCTX, PN_VOID); +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
