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.

421 lines
9.2KB

  1. /*
  2. * High-level, templated, C++ doubly-linked list
  3. * Copyright (C) 2013 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 LIST_HPP_INCLUDED
  18. #define 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 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 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 List class
  40. // _allocate() and _deallocate are virtual calls provided by subclasses
  41. template<typename T>
  42. class AbstractList
  43. {
  44. protected:
  45. struct Data {
  46. T value;
  47. k_list_head siblings;
  48. };
  49. AbstractList()
  50. : fDataSize(sizeof(Data)),
  51. fCount(0)
  52. {
  53. _init();
  54. }
  55. virtual ~AbstractList()
  56. {
  57. CARLA_ASSERT(fCount == 0);
  58. }
  59. public:
  60. class Itenerator {
  61. public:
  62. Itenerator(const k_list_head* queue)
  63. : fData(nullptr),
  64. fEntry(queue->next),
  65. fEntry2(fEntry->next),
  66. fQueue(queue)
  67. {
  68. CARLA_ASSERT(fEntry != nullptr);
  69. CARLA_ASSERT(fEntry2 != nullptr);
  70. CARLA_ASSERT(fQueue != nullptr);
  71. }
  72. bool valid() const noexcept
  73. {
  74. return (fEntry != fQueue);
  75. }
  76. void next() noexcept
  77. {
  78. fEntry = fEntry2;
  79. fEntry2 = fEntry->next;
  80. }
  81. T& operator*()
  82. {
  83. fData = list_entry(fEntry, Data, siblings);
  84. CARLA_ASSERT(fData != nullptr);
  85. return fData->value;
  86. }
  87. private:
  88. Data* fData;
  89. k_list_head* fEntry;
  90. k_list_head* fEntry2;
  91. const k_list_head* const fQueue;
  92. friend class AbstractList;
  93. };
  94. Itenerator begin() const
  95. {
  96. return Itenerator(&fQueue);
  97. }
  98. void clear()
  99. {
  100. if (fCount != 0)
  101. {
  102. k_list_head* entry;
  103. k_list_head* entry2;
  104. list_for_each_safe(entry, entry2, &fQueue)
  105. {
  106. if (Data* data = list_entry(entry, Data, siblings))
  107. {
  108. data->~Data();
  109. _deallocate(data);
  110. }
  111. }
  112. }
  113. _init();
  114. }
  115. size_t count() const noexcept
  116. {
  117. return fCount;
  118. }
  119. bool isEmpty() const noexcept
  120. {
  121. return (fCount == 0);
  122. }
  123. bool append(const T& value)
  124. {
  125. if (Data* const data = _allocate())
  126. {
  127. new(data)Data();
  128. data->value = value;
  129. list_add_tail(&data->siblings, &fQueue);
  130. ++fCount;
  131. return true;
  132. }
  133. return false;
  134. }
  135. bool appendAt(const T& value, const Itenerator& it)
  136. {
  137. if (Data* const data = _allocate())
  138. {
  139. new(data)Data();
  140. data->value = value;
  141. list_add_tail(&data->siblings, it.fEntry->next);
  142. ++fCount;
  143. return true;
  144. }
  145. return false;
  146. }
  147. bool insert(const T& value)
  148. {
  149. if (Data* const data = _allocate())
  150. {
  151. new(data)Data();
  152. data->value = value;
  153. list_add(&data->siblings, &fQueue);
  154. ++fCount;
  155. return true;
  156. }
  157. return false;
  158. }
  159. bool insertAt(const T& value, const Itenerator& it)
  160. {
  161. if (Data* const data = _allocate())
  162. {
  163. new(data)Data();
  164. data->value = value;
  165. list_add(&data->siblings, it.fEntry->prev);
  166. ++fCount;
  167. return true;
  168. }
  169. return false;
  170. }
  171. T& getAt(const size_t index, const bool remove = false)
  172. {
  173. if (fCount == 0 || index >= fCount)
  174. return fRetValue;
  175. size_t i = 0;
  176. Data* data = nullptr;
  177. k_list_head* entry;
  178. k_list_head* entry2;
  179. list_for_each_safe(entry, entry2, &fQueue)
  180. {
  181. if (index != i++)
  182. continue;
  183. data = list_entry(entry, Data, siblings);
  184. if (data != nullptr)
  185. fRetValue = data->value;
  186. if (remove)
  187. {
  188. --fCount;
  189. list_del(entry);
  190. if (data != nullptr)
  191. {
  192. data->~Data();
  193. _deallocate(data);
  194. }
  195. }
  196. break;
  197. }
  198. return fRetValue;
  199. }
  200. T& getFirst(const bool remove = false)
  201. {
  202. return _getFirstOrLast(true, remove);
  203. }
  204. T& getLast(const bool remove = false)
  205. {
  206. return _getFirstOrLast(false, remove);
  207. }
  208. void remove(Itenerator& it)
  209. {
  210. CARLA_ASSERT(it.fEntry != nullptr);
  211. CARLA_ASSERT(it.fData != nullptr);
  212. if (it.fEntry != nullptr && it.fData != nullptr)
  213. {
  214. --fCount;
  215. list_del(it.fEntry);
  216. it.fData->~Data();
  217. _deallocate(it.fData);
  218. }
  219. }
  220. bool removeOne(const T& value)
  221. {
  222. Data* data = nullptr;
  223. k_list_head* entry;
  224. k_list_head* entry2;
  225. list_for_each_safe(entry, entry2, &fQueue)
  226. {
  227. data = list_entry(entry, Data, siblings);
  228. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  229. if (data->value == value)
  230. {
  231. --fCount;
  232. list_del(entry);
  233. data->~Data();
  234. _deallocate(data);
  235. break;
  236. }
  237. }
  238. return (data != nullptr);
  239. }
  240. void removeAll(const T& value)
  241. {
  242. Data* data;
  243. k_list_head* entry;
  244. k_list_head* entry2;
  245. list_for_each_safe(entry, entry2, &fQueue)
  246. {
  247. data = list_entry(entry, Data, siblings);
  248. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  249. if (data->value == value)
  250. {
  251. --fCount;
  252. list_del(entry);
  253. data->~Data();
  254. _deallocate(data);
  255. }
  256. }
  257. }
  258. void spliceAppend(AbstractList& list, const bool init = true)
  259. {
  260. if (init)
  261. {
  262. list_splice_tail_init(&fQueue, &list.fQueue);
  263. list.fCount += fCount;
  264. fCount = 0;
  265. }
  266. else
  267. {
  268. list_splice_tail(&fQueue, &list.fQueue);
  269. list.fCount += fCount;
  270. }
  271. }
  272. void spliceInsert(AbstractList& list, const bool init = true)
  273. {
  274. if (init)
  275. {
  276. list_splice_init(&fQueue, &list.fQueue);
  277. list.fCount += fCount;
  278. fCount = 0;
  279. }
  280. else
  281. {
  282. list_splice(&fQueue, &list.fQueue);
  283. list.fCount += fCount;
  284. }
  285. }
  286. protected:
  287. const size_t fDataSize;
  288. size_t fCount;
  289. k_list_head fQueue;
  290. virtual Data* _allocate() = 0;
  291. virtual void _deallocate(Data*& dataPtr) = 0;
  292. private:
  293. T fRetValue;
  294. void _init() noexcept
  295. {
  296. fCount = 0;
  297. INIT_LIST_HEAD(&fQueue);
  298. }
  299. T& _getFirstOrLast(const bool first, const bool remove)
  300. {
  301. if (fCount == 0)
  302. return fRetValue;
  303. k_list_head* const entry = first ? fQueue.next : fQueue.prev;
  304. Data* data = list_entry(entry, Data, siblings);
  305. if (data != nullptr)
  306. fRetValue = data->value;
  307. if (remove)
  308. {
  309. --fCount;
  310. list_del(entry);
  311. if (data != nullptr)
  312. {
  313. data->~Data();
  314. _deallocate(data);
  315. }
  316. }
  317. return fRetValue;
  318. }
  319. LIST_DECLARATIONS(AbstractList)
  320. };
  321. // -----------------------------------------------------------------------
  322. // List
  323. template<typename T>
  324. class List : public AbstractList<T>
  325. {
  326. public:
  327. List()
  328. {
  329. }
  330. private:
  331. typename AbstractList<T>::Data* _allocate() override
  332. {
  333. return (typename AbstractList<T>::Data*)std::malloc(this->fDataSize);
  334. }
  335. void _deallocate(typename AbstractList<T>::Data*& dataPtr) override
  336. {
  337. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr,);
  338. std::free(dataPtr);
  339. dataPtr = nullptr;
  340. }
  341. LIST_DECLARATIONS(List)
  342. };
  343. // -----------------------------------------------------------------------
  344. #endif // LIST_HPP_INCLUDED