qlist.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * The MiniQt Library
  3. *
  4. * Copyright (C) 1999 Sandro Sigala <ssigala@globalnet.it>
  5. */
  6. #ifndef QLIST_H
  7. #define QLIST_H
  8. #include "qminiqt.h"
  9. template <class T>
  10. class QList {
  11. public:
  12. QList();
  13. ~QList();
  14. void append(T *item);
  15. void take(T *item);
  16. void remove(T *item);
  17. T *first();
  18. T *next();
  19. T *current();
  20. void setAutoDelete(bool value);
  21. bool autoDelete();
  22. private:
  23. bool autodelete;
  24. struct ListItem { T *item; ListItem *next; };
  25. ListItem *li_head, *li_current;
  26. };
  27. template <class T>
  28. QList<T>::QList()
  29. {
  30. autodelete = FALSE;
  31. li_head = li_current = 0;
  32. }
  33. template <class T>
  34. QList<T>::~QList()
  35. {
  36. for (ListItem *li = li_head; li != 0; ) {
  37. ListItem *next = li->next;
  38. if (autodelete)
  39. delete li->item;
  40. li = next;
  41. }
  42. }
  43. template <class T>
  44. void QList<T>::append(T *item)
  45. {
  46. ListItem *newli = new ListItem;
  47. newli->item = item;
  48. newli->next = 0;
  49. if (!li_head)
  50. li_head = newli;
  51. else {
  52. ListItem *li;
  53. for (li = li_head; li->next != 0; li = li->next)
  54. ;
  55. li->next = newli;
  56. }
  57. }
  58. template <class T>
  59. void QList<T>::take(T *item)
  60. {
  61. if (!li_head)
  62. return;
  63. // Update the current pointer
  64. if (li_current && li_current->item == item)
  65. li_current = li_current->next;
  66. // Remove the first item
  67. if (li_head->item == item) {
  68. ListItem *li = li_head;
  69. li_head = li_head->next;
  70. delete li;
  71. return;
  72. }
  73. // Remove an interim item
  74. for (ListItem *li = li_head; li->next != 0; li = li->next)
  75. if (li->next->item == item) {
  76. ListItem *del = li->next;
  77. li->next = li->next->next;
  78. delete del;
  79. return;
  80. }
  81. }
  82. template <class T>
  83. void QList<T>::remove(T *item)
  84. {
  85. take(item);
  86. if (autodelete)
  87. delete item;
  88. }
  89. template <class T>
  90. T *QList<T>::first()
  91. {
  92. li_current = li_head;
  93. return li_head ? li_head->item : 0;
  94. }
  95. template <class T>
  96. T *QList<T>::next()
  97. {
  98. if (li_current)
  99. li_current = li_current->next;
  100. return li_current ? li_current->item : 0;
  101. }
  102. template <class T>
  103. T *QList<T>::current()
  104. {
  105. return li_current ? li_current->item : 0;
  106. }
  107. template <class T>
  108. void QList<T>::setAutoDelete(bool value)
  109. {
  110. autodelete = value;
  111. }
  112. template <class T>
  113. bool QList<T>::autoDelete()
  114. {
  115. return autodelete;
  116. }
  117. #endif // !QLIST_H