swima_inventory.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2017 Andreas Steffen
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "swima_inventory.h"
  16. #include "swima_record.h"
  17. #include <collections/linked_list.h>
  18. #include <utils/debug.h>
  19. typedef struct private_swima_inventory_t private_swima_inventory_t;
  20. /**
  21. * Private data of a swima_inventory_t object.
  22. *
  23. */
  24. struct private_swima_inventory_t {
  25. /**
  26. * Public swima_inventory_t interface.
  27. */
  28. swima_inventory_t public;
  29. /**
  30. * Earliest or last event ID of the inventory
  31. */
  32. uint32_t eid;
  33. /**
  34. * Epoch of event IDs
  35. */
  36. uint32_t epoch;
  37. /**
  38. * List of SW records
  39. */
  40. linked_list_t *list;
  41. /**
  42. * Reference count
  43. */
  44. refcount_t ref;
  45. };
  46. METHOD(swima_inventory_t, add, void,
  47. private_swima_inventory_t *this, swima_record_t *record)
  48. {
  49. this->list->insert_last(this->list, record);
  50. }
  51. METHOD(swima_inventory_t, get_count, int,
  52. private_swima_inventory_t *this)
  53. {
  54. return this->list->get_count(this->list);
  55. }
  56. METHOD(swima_inventory_t, set_eid, void,
  57. private_swima_inventory_t *this, uint32_t eid, uint32_t epoch)
  58. {
  59. this->eid = eid;
  60. this->epoch = epoch;
  61. }
  62. METHOD(swima_inventory_t, get_eid, uint32_t,
  63. private_swima_inventory_t *this, uint32_t *epoch)
  64. {
  65. if (epoch)
  66. {
  67. *epoch = this->epoch;
  68. }
  69. return this->eid;
  70. }
  71. METHOD(swima_inventory_t, create_enumerator, enumerator_t*,
  72. private_swima_inventory_t *this)
  73. {
  74. return this->list->create_enumerator(this->list);
  75. }
  76. METHOD(swima_inventory_t, get_ref, swima_inventory_t*,
  77. private_swima_inventory_t *this)
  78. {
  79. ref_get(&this->ref);
  80. return &this->public;
  81. }
  82. METHOD(swima_inventory_t, clear, void,
  83. private_swima_inventory_t *this)
  84. {
  85. this->list->destroy_offset(this->list, offsetof(swima_record_t, destroy));
  86. this->list = linked_list_create();
  87. }
  88. METHOD(swima_inventory_t, destroy, void,
  89. private_swima_inventory_t *this)
  90. {
  91. if (ref_put(&this->ref))
  92. {
  93. this->list->destroy_offset(this->list, offsetof(swima_record_t, destroy));
  94. free(this);
  95. }
  96. }
  97. /**
  98. * See header
  99. */
  100. swima_inventory_t *swima_inventory_create(void)
  101. {
  102. private_swima_inventory_t *this;
  103. INIT(this,
  104. .public = {
  105. .add = _add,
  106. .get_count = _get_count,
  107. .set_eid = _set_eid,
  108. .get_eid = _get_eid,
  109. .create_enumerator = _create_enumerator,
  110. .get_ref = _get_ref,
  111. .clear = _clear,
  112. .destroy = _destroy,
  113. },
  114. .list = linked_list_create(),
  115. .ref = 1,
  116. );
  117. return &this->public;
  118. }