linked_list.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * linked_list.h
  3. *
  4. * Copyright 2013-2021 Michael Zillgith
  5. *
  6. * This file is part of libIEC61850.
  7. *
  8. * libIEC61850 is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * libIEC61850 is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with libIEC61850. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * See COPYING file for the complete license text.
  22. */
  23. #ifndef LINKED_LIST_H_
  24. #define LINKED_LIST_H_
  25. #include "libiec61850_common_api.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. * \addtogroup common_api_group
  31. */
  32. /**@{*/
  33. /**
  34. * \defgroup LINKED_LIST LinkedList data type definition and handling functions
  35. */
  36. /**@{*/
  37. struct sLinkedList {
  38. void* data;
  39. struct sLinkedList* next;
  40. };
  41. /**
  42. * \brief Reference to a linked list or to a linked list element.
  43. */
  44. typedef struct sLinkedList* LinkedList;
  45. /**
  46. * \brief Create a new LinkedList object
  47. *
  48. * \return the newly created LinkedList instance
  49. */
  50. LIB61850_API LinkedList
  51. LinkedList_create(void);
  52. /**
  53. * \brief Delete a LinkedList object
  54. *
  55. * This function destroy the LinkedList object. It will free all data structures used by the LinkedList
  56. * instance. It will call free for all elements of the linked list. This function should only be used if
  57. * simple objects (like dynamically allocated strings) are stored in the linked list.
  58. *
  59. * \param self the LinkedList instance
  60. */
  61. LIB61850_API void
  62. LinkedList_destroy(LinkedList self);
  63. typedef void (*LinkedListValueDeleteFunction) (void*);
  64. /**
  65. * \brief Delete a LinkedList object
  66. *
  67. * This function destroy the LinkedList object. It will free all data structures used by the LinkedList
  68. * instance. It will call a user provided function for each data element. This user provided function is
  69. * responsible to properly free the data element.
  70. *
  71. * \param self the LinkedList instance
  72. * \param valueDeleteFunction a function that is called for each data element of the LinkedList with the pointer
  73. * to the linked list data element.
  74. */
  75. LIB61850_API void
  76. LinkedList_destroyDeep(LinkedList self, LinkedListValueDeleteFunction valueDeleteFunction);
  77. /**
  78. * \brief Delete a LinkedList object without freeing the element data
  79. *
  80. * This function should be used statically allocated data objects are stored in the LinkedList instance.
  81. * Other use cases would be if the data elements in the list should not be deleted.
  82. *
  83. * \param self the LinkedList instance
  84. */
  85. LIB61850_API void
  86. LinkedList_destroyStatic(LinkedList self);
  87. /**
  88. * \brief Add a new element to the list
  89. *
  90. * This function will add a new data element to the list. The new element will the last element in the
  91. * list.
  92. *
  93. * \param self the LinkedList instance
  94. * \param data data to append to the LinkedList instance
  95. */
  96. LIB61850_API void
  97. LinkedList_add(LinkedList self, void* data);
  98. /**
  99. * \brief Check if the specified data is contained in the list
  100. *
  101. * \param self the LinkedList instance
  102. * \param data data to remove from the LinkedList instance
  103. *
  104. * \return true if data is part of the list, false otherwise
  105. */
  106. LIB61850_API bool
  107. LinkedList_contains(LinkedList self, void* data);
  108. /**
  109. * \brief Removed the specified element from the list
  110. *
  111. * \param self the LinkedList instance
  112. * \param data data to remove from the LinkedList instance
  113. *
  114. * \return true if data has been removed from the list, false otherwise
  115. */
  116. LIB61850_API bool
  117. LinkedList_remove(LinkedList self, void* data);
  118. /**
  119. * \brief Get the list element specified by index (starting with 0).
  120. *
  121. * \param self the LinkedList instance
  122. * \param index index of the requested element.
  123. */
  124. LIB61850_API LinkedList
  125. LinkedList_get(LinkedList self, int index);
  126. /**
  127. * \brief Get the next element in the list (iterator).
  128. *
  129. * \param self the LinkedList instance
  130. */
  131. LIB61850_API LinkedList
  132. LinkedList_getNext(LinkedList self);
  133. /**
  134. * \brief Get the last element in the list.
  135. *
  136. * \param self the LinkedList instance
  137. */
  138. LIB61850_API LinkedList
  139. LinkedList_getLastElement(LinkedList self);
  140. /**
  141. * \brief Insert a new element int the list
  142. *
  143. * \param listElement the LinkedList instance
  144. */
  145. LIB61850_API LinkedList
  146. LinkedList_insertAfter(LinkedList listElement, void* data);
  147. /**
  148. * \brief Get the size of the list
  149. *
  150. * \param self the LinkedList instance
  151. *
  152. * \return number of data elements stored in the list
  153. */
  154. LIB61850_API int
  155. LinkedList_size(LinkedList self);
  156. LIB61850_API void*
  157. LinkedList_getData(LinkedList self);
  158. LIB61850_API void
  159. LinkedList_printStringList(LinkedList self);
  160. /**@}*/
  161. /**@}*/
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. #endif /* LINKED_LIST_H_ */