2022-02-10 23:07:11 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-04-21 17:31:56 +02:00
|
|
|
#pragma once
|
2019-09-13 13:12:26 +02:00
|
|
|
|
2019-09-12 14:23:21 +02:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
|
|
|
*
|
2020-06-25 15:13:02 +02:00
|
|
|
* `blender::ListBaseWrapper` is a typed wrapper for the #ListBase struct. That makes it safer and
|
2020-06-09 10:27:24 +02:00
|
|
|
* more convenient to use in C++ in some cases. However, if you find yourself iterating over a
|
|
|
|
* linked list a lot, consider to convert it into a vector for further processing. This improves
|
2020-06-25 15:13:02 +02:00
|
|
|
* performance and debug-ability.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BLI_listbase.h"
|
|
|
|
#include "DNA_listBase.h"
|
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
namespace blender {
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2023-03-07 15:23:59 +01:00
|
|
|
template<typename LB, typename T> class ListBaseWrapperTemplate {
|
2019-09-12 14:23:21 +02:00
|
|
|
private:
|
2023-03-07 15:23:59 +01:00
|
|
|
LB *listbase_;
|
2019-09-12 14:23:21 +02:00
|
|
|
|
|
|
|
public:
|
2023-03-07 15:23:59 +01:00
|
|
|
ListBaseWrapperTemplate(LB *listbase) : listbase_(listbase)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
|
|
|
BLI_assert(listbase);
|
|
|
|
}
|
|
|
|
|
2023-03-29 16:50:54 +02:00
|
|
|
ListBaseWrapperTemplate(LB &listbase) : ListBaseWrapperTemplate(&listbase) {}
|
2019-09-12 14:23:21 +02:00
|
|
|
|
|
|
|
class Iterator {
|
|
|
|
private:
|
2023-03-07 15:23:59 +01:00
|
|
|
LB *listbase_;
|
2020-07-03 14:15:05 +02:00
|
|
|
T *current_;
|
2019-09-12 14:23:21 +02:00
|
|
|
|
|
|
|
public:
|
2023-03-29 16:50:54 +02:00
|
|
|
Iterator(LB *listbase, T *current) : listbase_(listbase), current_(current) {}
|
2019-09-12 14:23:21 +02:00
|
|
|
|
|
|
|
Iterator &operator++()
|
|
|
|
{
|
2022-09-19 06:47:27 +02:00
|
|
|
/* Some types store `next/prev` using `void *`, so cast is necessary. */
|
2020-11-07 20:13:37 +01:00
|
|
|
current_ = static_cast<T *>(current_->next);
|
2019-09-12 14:23:21 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator operator++(int)
|
|
|
|
{
|
|
|
|
Iterator iterator = *this;
|
2022-07-28 20:43:51 +02:00
|
|
|
++(*this);
|
2019-09-12 14:23:21 +02:00
|
|
|
return iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Iterator &iterator) const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return current_ != iterator.current_;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
T *operator*() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return current_;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterator begin() const
|
|
|
|
{
|
2020-08-07 18:24:59 +02:00
|
|
|
return Iterator(listbase_, static_cast<T *>(listbase_->first));
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator end() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return Iterator(listbase_, nullptr);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2022-11-13 16:48:30 +01:00
|
|
|
T *get(uint index) const
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
void *ptr = BLI_findlink(listbase_, index);
|
2019-09-12 14:23:21 +02:00
|
|
|
BLI_assert(ptr);
|
2020-08-07 18:24:59 +02:00
|
|
|
return static_cast<T *>(ptr);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
2020-02-10 13:54:57 +01:00
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
int64_t index_of(const T *value) const
|
2020-02-10 13:54:57 +01:00
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
int64_t index = 0;
|
2020-02-10 13:54:57 +01:00
|
|
|
for (T *ptr : *this) {
|
|
|
|
if (ptr == value) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
BLI_assert(false);
|
2020-07-20 12:16:20 +02:00
|
|
|
return -1;
|
2020-02-10 13:54:57 +01:00
|
|
|
}
|
2019-09-12 14:23:21 +02:00
|
|
|
};
|
|
|
|
|
2023-03-07 15:23:59 +01:00
|
|
|
template<typename T> using ListBaseWrapper = ListBaseWrapperTemplate<ListBase, T>;
|
|
|
|
template<typename T> using ConstListBaseWrapper = ListBaseWrapperTemplate<const ListBase, const T>;
|
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
} /* namespace blender */
|