zori_registry.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "zori.h"
  2. #include "zori_registry.h"
  3. /* registry functionality */
  4. /** Compare registry entries. */
  5. int zori_registry_entry_compare(const void * v1, const void * v2) {
  6. const struct zori_registry_entry * entry1 = v1;
  7. const struct zori_registry_entry * entry2 = v2;
  8. return (entry2->id - entry1->id);
  9. }
  10. /** Initialize the registry. */
  11. zori_id zori_registry_init(struct zori_registry * registry) {
  12. miao_init(registry);
  13. return ZORI_ID_OK;
  14. }
  15. /** Destroy the registry */
  16. void zori_registry_destroy(struct zori_registry * registry) {
  17. if(registry) {
  18. miao_done(registry);
  19. }
  20. }
  21. /** Add an entry to the registry. */
  22. zori_id
  23. zori_registry_add(struct zori_registry * registry, zori_id id,
  24. struct zori_widget * widget) {
  25. struct zori_registry_entry entry = { id, widget };
  26. if (miao_push(registry, entry)) {
  27. miao_qsort(registry, zori_registry_entry_compare);
  28. return ZORI_ID_OK;
  29. }
  30. return ZORI_ID_ENOMEM;
  31. }
  32. /** Look up an entry in the registry. */
  33. struct zori_registry_entry *
  34. zori_registry_lookup_entry(struct zori_registry * registry, zori_id id) {
  35. struct zori_registry_entry key = { id, NULL };
  36. return miao_bsearch(registry, zori_registry_entry_compare, &key);
  37. }
  38. /** Look up a widget in the registry. */
  39. struct zori_widget *
  40. zori_registry_lookup(struct zori_registry * registry, zori_id id) {
  41. struct zori_widget * result = NULL;
  42. struct zori_registry_entry * entry = NULL;
  43. entry = zori_registry_lookup_entry(registry, id);
  44. if (entry) {
  45. result = entry->widget;
  46. }
  47. return result;
  48. }
  49. /** Remove an entry from the registry. */
  50. zori_id
  51. zori_registry_remove(struct zori_registry * registry, zori_id id) {
  52. struct zori_registry_entry * entry = NULL;
  53. entry = zori_registry_lookup_entry(registry, id);
  54. if (entry) {
  55. miao_delete_entry(registry, entry);
  56. }
  57. return ZORI_ID_OK;
  58. }
  59. /** The global registry for Zori. */
  60. static struct zori_registry * the_zori_registry = NULL;
  61. /** Intializes the global registry for zori. */
  62. zori_id zori_initialize_registry() {
  63. the_zori_registry = calloc(1, sizeof(*the_zori_registry));
  64. if (!the_zori_registry) return ZORI_ID_ENOMEM;
  65. return zori_registry_init(the_zori_registry);
  66. }
  67. /** Destroys the global registry for zori. */
  68. zori_id zori_destroy_registry() {
  69. zori_registry_destroy(the_zori_registry);
  70. free(the_zori_registry);
  71. the_zori_registry = NULL;
  72. return ZORI_ID_OK;
  73. }
  74. /** Returns the global registry for Zori. */
  75. struct zori_registry * zori_get_registry() {
  76. return the_zori_registry;
  77. }