Rename a pipe method, add docs
[carla.git] / source / utils / CarlaStringList.hpp
blobfb2e8058b3dab0107e18970453d6348407ca3a0f
1 /*
2 * Carla String List
3 * Copyright (C) 2014-2019 Filipe Coelho <falktx@falktx.com>
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.
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.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #ifndef CARLA_STRING_LIST_HPP_INCLUDED
19 #define CARLA_STRING_LIST_HPP_INCLUDED
21 #include "LinkedList.hpp"
23 // -----------------------------------------------------------------------
24 // Helper class to manage the lifetime of a "char**" object
26 class CharStringListPtr
28 public:
29 CharStringListPtr() noexcept
30 : fCharList(nullptr) {}
32 CharStringListPtr(const char* const* const c) noexcept
33 : fCharList(c) {}
35 CharStringListPtr(const CharStringListPtr& ptr) noexcept
36 : fCharList(nullptr)
38 copy(ptr.fCharList);
41 CharStringListPtr(const LinkedList<const char*>& list) noexcept
42 : fCharList(nullptr)
44 copy(list);
47 ~CharStringListPtr() noexcept
49 clear();
52 // -------------------------------------------------------------------
54 operator const char* const*() const noexcept
56 return fCharList;
59 CharStringListPtr& operator=(const char* const* const c) noexcept
61 clear();
62 fCharList = c;
63 return *this;
66 CharStringListPtr& operator=(const CharStringListPtr& ptr) noexcept
68 clear();
69 copy(ptr.fCharList);
70 return *this;
73 CharStringListPtr& operator=(const LinkedList<const char*>& list) noexcept
75 clear();
76 copy(list);
77 return *this;
80 // -------------------------------------------------------------------
82 protected:
83 void clear() noexcept
85 if (fCharList == nullptr)
86 return;
88 for (int i=0; fCharList[i] != nullptr; ++i)
89 delete[] fCharList[i];
91 delete[] fCharList;
92 fCharList = nullptr;
95 void copy(const char* const* const c) noexcept
97 CARLA_SAFE_ASSERT_RETURN(c != nullptr,);
98 CARLA_SAFE_ASSERT_RETURN(fCharList == nullptr,);
100 std::size_t count = 0;
101 for (; c[count] != nullptr; ++count) {}
102 CARLA_SAFE_ASSERT_RETURN(count > 0,);
104 const char** tmpList;
106 try {
107 tmpList = new const char*[count+1];
108 } CARLA_SAFE_EXCEPTION_RETURN("CharStringListPtr::copy",);
110 tmpList[count] = nullptr;
112 for (std::size_t i=0; i<count; ++i)
114 tmpList[i] = carla_strdup_safe(c[i]);
115 CARLA_SAFE_ASSERT_BREAK(tmpList[i] != nullptr);
118 fCharList = tmpList;
121 void copy(const LinkedList<const char*>& list) noexcept
123 CARLA_SAFE_ASSERT_RETURN(fCharList == nullptr,);
125 const std::size_t count(list.count());
126 CARLA_SAFE_ASSERT_RETURN(count > 0,);
128 const char** tmpList;
130 try {
131 tmpList = new const char*[count+1];
132 } CARLA_SAFE_EXCEPTION_RETURN("CharStringListPtr::copy",);
134 tmpList[count] = nullptr;
136 std::size_t i=0;
137 for (LinkedList<const char*>::Itenerator it = list.begin2(); it.valid(); it.next(), ++i)
139 tmpList[i] = carla_strdup_safe(it.getValue(nullptr));
140 CARLA_SAFE_ASSERT_BREAK(tmpList[i] != nullptr);
142 CARLA_SAFE_ASSERT(i == count);
144 fCharList = tmpList;
147 // -------------------------------------------------------------------
149 private:
150 const char* const* fCharList;
153 // -----------------------------------------------------------------------
154 // CarlaStringList
156 class CarlaStringList : public LinkedList<const char*>
158 public:
159 CarlaStringList(bool allocateElements = true) noexcept
160 : LinkedList<const char*>(),
161 fAllocateElements(allocateElements) {}
163 CarlaStringList(const CarlaStringList& list) noexcept
164 : LinkedList<const char*>(),
165 fAllocateElements(list.fAllocateElements)
167 for (Itenerator it = list.begin2(); it.valid(); it.next())
168 LinkedList<const char*>::append(carla_strdup_safe(it.getValue(nullptr)));
171 ~CarlaStringList() noexcept override
173 clear();
176 // -------------------------------------------------------------------
178 void clear() noexcept
180 if (fAllocateElements)
182 for (Itenerator it = begin2(); it.valid(); it.next())
184 if (const char* const string = it.getValue(nullptr))
185 delete[] string;
189 LinkedList<const char*>::clear();
192 // -------------------------------------------------------------------
194 bool append(const char* const string) noexcept
196 CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
198 if (const char* const stringDup = fAllocateElements ? carla_strdup_safe(string) : string)
200 if (LinkedList<const char*>::append(stringDup))
201 return true;
202 delete[] stringDup;
205 return false;
208 bool appendUnique(const char* const string) noexcept
210 CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
212 if (contains(string))
213 return false;
215 return append(string);
218 bool appendAt(const char* const string, const Itenerator& it) noexcept
220 CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
222 if (const char* const stringDup = fAllocateElements ? carla_strdup_safe(string) : string)
224 if (LinkedList<const char*>::appendAt(stringDup, it))
225 return true;
226 delete[] stringDup;
229 return false;
232 bool insert(const char* const string) noexcept
234 CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
236 if (const char* const stringDup = fAllocateElements ? carla_strdup_safe(string) : string)
238 if (LinkedList<const char*>::insert(stringDup))
239 return true;
240 delete[] stringDup;
243 return false;
246 bool insertAt(const char* const string, const Itenerator& it) noexcept
248 CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
250 if (const char* const stringDup = fAllocateElements ? carla_strdup_safe(string) : string)
252 if (LinkedList<const char*>::insertAt(stringDup, it))
253 return true;
254 delete[] stringDup;
257 return false;
260 // -------------------------------------------------------------------
262 const char* getAt(const std::size_t index) const noexcept
264 CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, nullptr);
266 std::size_t i = 0;
267 ListHead* entry = fQueue.next;
269 for (; i++ != index; entry = entry->next) {}
271 const Data* const data(list_entry_const(entry, Data, siblings));
272 CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
274 return data->value;
277 const char* getFirst() const noexcept
279 CARLA_SAFE_ASSERT_RETURN(fCount > 0, nullptr);
281 const Data* const data(list_entry_const(fQueue.next, Data, siblings));
282 CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
284 return data->value;
287 const char* getLast() const noexcept
289 CARLA_SAFE_ASSERT_RETURN(fCount > 0, nullptr);
291 const Data* const data(list_entry_const(fQueue.prev, Data, siblings));
292 CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
294 return data->value;
297 // -------------------------------------------------------------------
299 const char* getAndRemoveFirst() noexcept
301 CARLA_SAFE_ASSERT_RETURN(fCount > 0, nullptr);
303 const Data* const data = list_entry_const(fQueue.next, Data, siblings);
304 CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
306 const char* const ret = data->value;
308 Itenerator it = begin2();
309 LinkedList<const char*>::remove(it);
311 return ret;
314 // -------------------------------------------------------------------
316 bool contains(const char* const string) noexcept
318 CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
320 if (fCount == 0)
321 return false;
323 for (Itenerator it = begin2(); it.valid(); it.next())
325 const char* const stringComp(it.getValue(nullptr));
326 CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
328 if (std::strcmp(string, stringComp) == 0)
329 return true;
332 return false;
335 const char* containsAndReturnString(const char* const string) noexcept
337 CARLA_SAFE_ASSERT_RETURN(string != nullptr, nullptr);
339 if (fCount == 0)
340 return nullptr;
342 for (Itenerator it = begin2(); it.valid(); it.next())
344 const char* const stringComp(it.getValue(nullptr));
345 CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
347 if (std::strcmp(string, stringComp) == 0)
348 return stringComp;
351 return nullptr;
354 // -------------------------------------------------------------------
356 void remove(Itenerator& it) noexcept
358 if (const char* const string = it.getValue(nullptr))
359 delete[] string;
361 LinkedList<const char*>::remove(it);
364 bool removeOne(const char* const string) noexcept
366 CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
368 for (Itenerator it = begin2(); it.valid(); it.next())
370 const char* const stringComp(it.getValue(nullptr));
371 CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
373 if (std::strcmp(string, stringComp) != 0)
374 continue;
376 delete[] stringComp;
377 LinkedList<const char*>::remove(it);
378 return true;
381 return false;
384 void removeAll(const char* const string) noexcept
386 CARLA_SAFE_ASSERT_RETURN(string != nullptr,);
388 for (Itenerator it = begin2(); it.valid(); it.next())
390 const char* const stringComp(it.getValue(nullptr));
391 CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
393 if (std::strcmp(string, stringComp) != 0)
394 continue;
396 delete[] stringComp;
397 LinkedList<const char*>::remove(it);
401 // -------------------------------------------------------------------
403 CharStringListPtr toCharStringListPtr() const noexcept
405 return CharStringListPtr(*this);
408 CarlaStringList& operator=(const char* const* const charStringList) noexcept
410 CARLA_SAFE_ASSERT_RETURN(! fAllocateElements, *this);
412 clear();
414 CARLA_SAFE_ASSERT_RETURN(charStringList != nullptr, *this);
416 for (int i=0; charStringList[i] != nullptr; ++i)
418 if (const char* const string = carla_strdup_safe(charStringList[i]))
419 LinkedList<const char*>::append(string);
422 return *this;
425 CarlaStringList& operator=(const CarlaStringList& list) noexcept
427 CARLA_SAFE_ASSERT_RETURN(! fAllocateElements, *this);
429 clear();
431 for (Itenerator it = list.begin2(); it.valid(); it.next())
433 if (const char* const string = carla_strdup_safe(it.getValue(nullptr)))
434 LinkedList<const char*>::append(string);
437 return *this;
440 private:
441 const bool fAllocateElements;
443 CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
446 // -----------------------------------------------------------------------
448 #endif // CARLA_STRING_LIST_HPP_INCLUDED