Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LinkedList.hpp 11KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * High-level, templated, C++ doubly-linked list
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef LINKED_LIST_HPP_INCLUDED
  18. #define LINKED_LIST_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #include <new>
  21. extern "C" {
  22. #include "rtmempool/list.h"
  23. }
  24. // Declare non copyable and prevent heap allocation
  25. #ifdef CARLA_PROPER_CPP11_SUPPORT
  26. # define LINKED_LIST_DECLARATIONS(ClassName) \
  27. ClassName(ClassName&) = delete; \
  28. ClassName(const ClassName&) = delete; \
  29. ClassName& operator=(const ClassName&) = delete; \
  30. static void* operator new(size_t) = delete;
  31. #else
  32. # define LINKED_LIST_DECLARATIONS(ClassName) \
  33. ClassName(ClassName&); \
  34. ClassName(const ClassName&); \
  35. ClassName& operator=(const ClassName&);
  36. #endif
  37. typedef struct list_head k_list_head;
  38. // -----------------------------------------------------------------------
  39. // Abstract Linked List class
  40. // _allocate() and _deallocate are virtual calls provided by subclasses
  41. // NOTE: data-type classes are allowed to throw on creation, but NOT on deletion!
  42. template<typename T>
  43. class AbstractLinkedList
  44. {
  45. protected:
  46. struct Data {
  47. T value;
  48. k_list_head siblings;
  49. };
  50. AbstractLinkedList(const bool needsCopyCtr) noexcept
  51. : fDataSize(sizeof(Data)),
  52. fCount(0),
  53. fNeedsCopyCtr(needsCopyCtr)
  54. {
  55. _init();
  56. }
  57. public:
  58. virtual ~AbstractLinkedList() noexcept
  59. {
  60. CARLA_SAFE_ASSERT(fCount == 0);
  61. }
  62. class Itenerator {
  63. public:
  64. Itenerator(const k_list_head* queue) noexcept
  65. : fData(nullptr),
  66. fEntry(queue->next),
  67. fEntry2(fEntry->next),
  68. fQueue(queue)
  69. {
  70. CARLA_SAFE_ASSERT(fEntry != nullptr);
  71. CARLA_SAFE_ASSERT(fEntry2 != nullptr);
  72. CARLA_SAFE_ASSERT(fQueue != nullptr);
  73. }
  74. bool valid() const noexcept
  75. {
  76. return (fEntry != fQueue);
  77. }
  78. void next() noexcept
  79. {
  80. fEntry = fEntry2;
  81. fEntry2 = fEntry->next;
  82. }
  83. T& getValue() noexcept
  84. {
  85. fData = list_entry(fEntry, Data, siblings);
  86. return fData->value;
  87. }
  88. private:
  89. Data* fData;
  90. k_list_head* fEntry;
  91. k_list_head* fEntry2;
  92. const k_list_head* const fQueue;
  93. friend class AbstractLinkedList;
  94. };
  95. Itenerator begin() const noexcept
  96. {
  97. return Itenerator(&fQueue);
  98. }
  99. void clear() noexcept
  100. {
  101. if (fCount != 0)
  102. {
  103. k_list_head* entry;
  104. k_list_head* entry2;
  105. list_for_each_safe(entry, entry2, &fQueue)
  106. {
  107. if (Data* data = list_entry(entry, Data, siblings))
  108. {
  109. if (fNeedsCopyCtr)
  110. data->~Data();
  111. _deallocate(data);
  112. }
  113. }
  114. }
  115. _init();
  116. }
  117. size_t count() const noexcept
  118. {
  119. return fCount;
  120. }
  121. bool isEmpty() const noexcept
  122. {
  123. return (fCount == 0);
  124. }
  125. bool append(const T& value) noexcept
  126. {
  127. return _add(value, true, &fQueue);
  128. }
  129. bool appendAt(const T& value, const Itenerator& it) noexcept
  130. {
  131. return _add(value, true, it.fEntry->next);
  132. }
  133. bool insert(const T& value) noexcept
  134. {
  135. return _add(value, false, &fQueue);
  136. }
  137. bool insertAt(const T& value, const Itenerator& it) noexcept
  138. {
  139. return _add(value, false, it.fEntry->prev);
  140. }
  141. T& getAt(const size_t index) const noexcept
  142. {
  143. if (fCount == 0 || index >= fCount)
  144. return fRetValue;
  145. size_t i = 0;
  146. Data* data = nullptr;
  147. k_list_head* entry;
  148. k_list_head* entry2;
  149. list_for_each_safe(entry, entry2, &fQueue)
  150. {
  151. if (index != i++)
  152. continue;
  153. data = list_entry(entry, Data, siblings);
  154. if (data != nullptr)
  155. fRetValue = data->value;
  156. break;
  157. }
  158. return fRetValue;
  159. }
  160. T& getAt(const size_t index, const bool removeObj) noexcept
  161. {
  162. if (fCount == 0 || index >= fCount)
  163. return fRetValue;
  164. size_t i = 0;
  165. Data* data = nullptr;
  166. k_list_head* entry;
  167. k_list_head* entry2;
  168. list_for_each_safe(entry, entry2, &fQueue)
  169. {
  170. if (index != i++)
  171. continue;
  172. data = list_entry(entry, Data, siblings);
  173. if (data != nullptr)
  174. fRetValue = data->value;
  175. if (removeObj)
  176. {
  177. --fCount;
  178. list_del(entry);
  179. if (data != nullptr)
  180. {
  181. if (fNeedsCopyCtr)
  182. data->~Data();
  183. _deallocate(data);
  184. }
  185. }
  186. break;
  187. }
  188. return fRetValue;
  189. }
  190. T& getFirst(const bool removeObj = false) noexcept
  191. {
  192. return _getFirstOrLast(true, removeObj);
  193. }
  194. T& getLast(const bool removeObj = false) noexcept
  195. {
  196. return _getFirstOrLast(false, removeObj);
  197. }
  198. void remove(Itenerator& it) noexcept
  199. {
  200. CARLA_SAFE_ASSERT_RETURN(it.fEntry != nullptr,);
  201. --fCount;
  202. list_del(it.fEntry);
  203. CARLA_SAFE_ASSERT_RETURN(it.fData != nullptr,);
  204. if (fNeedsCopyCtr)
  205. it.fData->~Data();
  206. _deallocate(it.fData);
  207. }
  208. bool removeOne(const T& value) noexcept
  209. {
  210. Data* data = nullptr;
  211. k_list_head* entry;
  212. k_list_head* entry2;
  213. list_for_each_safe(entry, entry2, &fQueue)
  214. {
  215. data = list_entry(entry, Data, siblings);
  216. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  217. if (data->value == value)
  218. {
  219. --fCount;
  220. list_del(entry);
  221. if (fNeedsCopyCtr)
  222. data->~Data();
  223. _deallocate(data);
  224. break;
  225. }
  226. }
  227. return (data != nullptr);
  228. }
  229. void removeAll(const T& value) noexcept
  230. {
  231. Data* data;
  232. k_list_head* entry;
  233. k_list_head* entry2;
  234. list_for_each_safe(entry, entry2, &fQueue)
  235. {
  236. data = list_entry(entry, Data, siblings);
  237. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  238. if (data->value == value)
  239. {
  240. --fCount;
  241. list_del(entry);
  242. if (fNeedsCopyCtr)
  243. data->~Data();
  244. _deallocate(data);
  245. }
  246. }
  247. }
  248. void spliceAppend(AbstractLinkedList& list, const bool init = true) noexcept
  249. {
  250. if (init)
  251. {
  252. list_splice_tail_init(&fQueue, &list.fQueue);
  253. list.fCount += fCount;
  254. fCount = 0;
  255. }
  256. else
  257. {
  258. list_splice_tail(&fQueue, &list.fQueue);
  259. list.fCount += fCount;
  260. }
  261. }
  262. void spliceInsert(AbstractLinkedList& list, const bool init = true) noexcept
  263. {
  264. if (init)
  265. {
  266. list_splice_init(&fQueue, &list.fQueue);
  267. list.fCount += fCount;
  268. fCount = 0;
  269. }
  270. else
  271. {
  272. list_splice(&fQueue, &list.fQueue);
  273. list.fCount += fCount;
  274. }
  275. }
  276. protected:
  277. const size_t fDataSize;
  278. size_t fCount;
  279. k_list_head fQueue;
  280. const bool fNeedsCopyCtr;
  281. virtual Data* _allocate() noexcept = 0;
  282. virtual void _deallocate(Data* const dataPtr) noexcept = 0;
  283. private:
  284. mutable T fRetValue;
  285. void _init() noexcept
  286. {
  287. fCount = 0;
  288. INIT_LIST_HEAD(&fQueue);
  289. }
  290. bool _add(const T& value, const bool inTail, k_list_head* const queue) noexcept
  291. {
  292. if (Data* const data = _allocate())
  293. {
  294. if (fNeedsCopyCtr)
  295. {
  296. try {
  297. new(data)Data();
  298. }
  299. catch(...) {
  300. _deallocate(data);
  301. return false;
  302. }
  303. try {
  304. data->value = value;
  305. }
  306. catch(...) {
  307. data->~Data();
  308. _deallocate(data);
  309. return false;
  310. }
  311. }
  312. else
  313. {
  314. std::memcpy(&data->value, &value, fDataSize);
  315. }
  316. if (inTail)
  317. list_add_tail(&data->siblings, queue);
  318. else
  319. list_add(&data->siblings, queue);
  320. ++fCount;
  321. return true;
  322. }
  323. return false;
  324. }
  325. T& _getFirstOrLast(const bool first, const bool removeObj) noexcept
  326. {
  327. if (fCount == 0)
  328. return fRetValue;
  329. k_list_head* const entry = first ? fQueue.next : fQueue.prev;
  330. Data* data = list_entry(entry, Data, siblings);
  331. if (data != nullptr)
  332. fRetValue = data->value;
  333. if (removeObj)
  334. {
  335. --fCount;
  336. list_del(entry);
  337. if (data != nullptr)
  338. {
  339. if (fNeedsCopyCtr)
  340. data->~Data();
  341. _deallocate(data);
  342. }
  343. }
  344. return fRetValue;
  345. }
  346. LINKED_LIST_DECLARATIONS(AbstractLinkedList)
  347. };
  348. // -----------------------------------------------------------------------
  349. // LinkedList
  350. template<typename T>
  351. class LinkedList : public AbstractLinkedList<T>
  352. {
  353. public:
  354. LinkedList(const bool needsCopyCtr = false) noexcept
  355. : AbstractLinkedList<T>(needsCopyCtr) {}
  356. protected:
  357. typename AbstractLinkedList<T>::Data* _allocate() noexcept override
  358. {
  359. return (typename AbstractLinkedList<T>::Data*)std::malloc(this->fDataSize);
  360. }
  361. void _deallocate(typename AbstractLinkedList<T>::Data* const dataPtr) noexcept override
  362. {
  363. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr,);
  364. std::free(dataPtr);
  365. }
  366. LINKED_LIST_DECLARATIONS(LinkedList)
  367. };
  368. // -----------------------------------------------------------------------
  369. #endif // LINKED_LIST_HPP_INCLUDED