Blender V5.0
BLI_listbase_wrapper.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
15
16#include "BLI_listbase.h"
17#include "BLI_vector.hh"
18
19#include "DNA_listBase.h"
20
21namespace blender {
22
23template<typename LB, typename T> class ListBaseWrapperTemplate {
24 private:
25 LB *listbase_;
26
27 public:
28 ListBaseWrapperTemplate(LB *listbase) : listbase_(listbase)
29 {
30 BLI_assert(listbase);
31 }
32
34
35 class Iterator {
36 private:
37 LB *listbase_;
38 T *current_;
39
40 public:
41 Iterator(LB *listbase, T *current) : listbase_(listbase), current_(current) {}
42
44 {
45 /* Some types store `next/prev` using `void *`, so cast is necessary. */
46 current_ = static_cast<T *>(current_->next);
47 return *this;
48 }
49
51 {
52 Iterator iterator = *this;
53 ++(*this);
54 return iterator;
55 }
56
57 bool operator!=(const Iterator &iterator) const
58 {
59 return current_ != iterator.current_;
60 }
61
62 T *operator*() const
63 {
64 return current_;
65 }
66 };
67
68 Iterator begin() const
69 {
70 return Iterator(listbase_, static_cast<T *>(listbase_->first));
71 }
72
73 Iterator end() const
74 {
75 return Iterator(listbase_, nullptr);
76 }
77
78 T *get(uint index) const
79 {
80 void *ptr = BLI_findlink(listbase_, index);
82 return static_cast<T *>(ptr);
83 }
84
85 int64_t index_of(const T *value) const
86 {
87 int64_t index = 0;
88 for (T *ptr : *this) {
89 if (ptr == value) {
90 return index;
91 }
92 index++;
93 }
94 BLI_assert(false);
95 return -1;
96 }
97};
98
101
105template<typename T> Vector<T *> listbase_to_vector(ListBase &list)
106{
108
109 for (T *item : ListBaseWrapper<T>(list)) {
110 vector.append(item);
111 }
112 return vector;
113}
114
118template<typename T> Vector<T *> listbase_to_vector(const ListBase &list)
119{
121
122 for (T *item : ConstListBaseWrapper<T>(list)) {
123 vector.append(item);
124 }
125 return vector;
126}
127
128} /* namespace blender */
#define BLI_assert(a)
Definition BLI_assert.h:46
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:534
unsigned int uint
These structs are the foundation for all linked lists in the library system.
long long int int64_t
bool operator!=(const Iterator &iterator) const
#define T
#define LB
ListBaseWrapperTemplate< const ListBase, const T > ConstListBaseWrapper
Vector< T * > listbase_to_vector(ListBase &list)
ListBaseWrapperTemplate< ListBase, T > ListBaseWrapper
PointerRNA * ptr
Definition wm_files.cc:4238