+int64_t bt_ctf_field_type_enumeration_get_mapping_count(
+ struct bt_ctf_field_type *type)
+{
+ int64_t ret = 0;
+ struct bt_ctf_field_type_enumeration *enumeration;
+
+ if (!type || (type->declaration->id != CTF_TYPE_ENUM)) {
+ ret = -1;
+ goto end;
+ }
+
+ enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
+ parent);
+ ret = enumeration->entries->len;
+end:
+ return ret;
+}
+
+static inline
+struct enumeration_mapping *get_enumeration_mapping(
+ struct bt_ctf_field_type *type, size_t index)
+{
+ struct enumeration_mapping *mapping = NULL;
+ struct bt_ctf_field_type_enumeration *enumeration;
+
+ enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
+ parent);
+ if (index >= enumeration->entries->len) {
+ goto end;
+ }
+
+ mapping = g_ptr_array_index(enumeration->entries, index);
+end:
+ return mapping;
+}
+
+int bt_ctf_field_type_enumeration_get_mapping(
+ struct bt_ctf_field_type *type, size_t index,
+ const char **string, int64_t *range_start, int64_t *range_end)
+{
+ struct enumeration_mapping *mapping;
+ int ret = 0;
+
+ if (!type || !string || !range_start || !range_end ||
+ (type->declaration->id != CTF_TYPE_ENUM)) {
+ ret = -1;
+ goto end;
+ }
+
+ mapping = get_enumeration_mapping(type, index);
+ if (!mapping) {
+ ret = -1;
+ goto end;
+ }
+
+ *string = g_quark_to_string(mapping->string);
+ *range_start = mapping->range_start._signed;
+ *range_end = mapping->range_end._signed;
+end:
+ return ret;
+}
+
+int bt_ctf_field_type_enumeration_get_mapping_unsigned(
+ struct bt_ctf_field_type *type, size_t index,
+ const char **string, uint64_t *range_start, uint64_t *range_end)
+{
+ struct enumeration_mapping *mapping;
+ int ret = 0;
+
+ if (!type || !string || !range_start || !range_end ||
+ (type->declaration->id != CTF_TYPE_ENUM)) {
+ ret = -1;
+ goto end;
+ }
+
+ mapping = get_enumeration_mapping(type, index);
+ if (!mapping) {
+ ret = -1;
+ goto end;
+ }
+
+ *string = g_quark_to_string(mapping->string);
+ *range_start = mapping->range_start._unsigned;
+ *range_end = mapping->range_end._unsigned;
+end:
+ return ret;
+}
+
+int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
+ struct bt_ctf_field_type *type, const char *name,
+ size_t *index)
+{
+ size_t i;
+ GQuark name_quark;
+ struct bt_ctf_field_type_enumeration *enumeration;
+ int ret = 0;
+
+ if (!type || !name || !index ||
+ (type->declaration->id != CTF_TYPE_ENUM)) {
+ ret = -1;
+ goto end;
+ }
+
+ name_quark = g_quark_try_string(name);
+ if (!name_quark) {
+ ret = -1;
+ goto end;
+ }
+
+ enumeration = container_of(type,
+ struct bt_ctf_field_type_enumeration, parent);
+ for (i = 0; i < enumeration->entries->len; i++) {
+ struct enumeration_mapping *mapping =
+ get_enumeration_mapping(type, i);
+
+ if (mapping->string == name_quark) {
+ *index = i;
+ goto end;
+ }
+ }
+
+ ret = -1;
+end:
+ return ret;
+}
+
+int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
+ struct bt_ctf_field_type *type, int64_t value,
+ size_t *index)
+{
+ struct bt_ctf_field_type_enumeration *enumeration;
+ size_t i;
+ int ret = 0;
+
+ if (!type || !index || (type->declaration->id != CTF_TYPE_ENUM)) {
+ ret = -1;
+ goto end;
+ }
+
+ enumeration = container_of(type,
+ struct bt_ctf_field_type_enumeration, parent);
+ for (i = 0; i < enumeration->entries->len; i++) {
+ struct enumeration_mapping *mapping =
+ get_enumeration_mapping(type, i);
+
+ if (value >= mapping->range_start._signed &&
+ value <= mapping->range_end._signed) {
+ *index = i;
+ goto end;
+ }
+ }
+
+ ret = -1;
+end:
+ return ret;
+}
+
+int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
+ struct bt_ctf_field_type *type, uint64_t value,
+ size_t *index)
+{
+ struct bt_ctf_field_type_enumeration *enumeration;
+ size_t i;
+ int ret = 0;
+
+ if (!type || !index || (type->declaration->id != CTF_TYPE_ENUM)) {
+ ret = -1;
+ goto end;
+ }
+
+ enumeration = container_of(type,
+ struct bt_ctf_field_type_enumeration, parent);
+ for (i = 0; i < enumeration->entries->len; i++) {
+ struct enumeration_mapping *mapping =
+ get_enumeration_mapping(type, i);
+
+ if (value >= mapping->range_start._unsigned &&
+ value <= mapping->range_end._unsigned) {
+ *index = i;
+ goto end;
+ }
+ }
+
+ ret = -1;
+end:
+ return ret;
+}
+