Blender  V3.3
BLI_listbase_wrapper.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
14 #include "BLI_listbase.h"
15 #include "DNA_listBase.h"
16 
17 namespace blender {
18 
19 template<typename T> class ListBaseWrapper {
20  private:
21  ListBase *listbase_;
22 
23  public:
24  ListBaseWrapper(ListBase *listbase) : listbase_(listbase)
25  {
26  BLI_assert(listbase);
27  }
28 
29  ListBaseWrapper(ListBase &listbase) : ListBaseWrapper(&listbase)
30  {
31  }
32 
33  class Iterator {
34  private:
35  ListBase *listbase_;
36  T *current_;
37 
38  public:
39  Iterator(ListBase *listbase, T *current) : listbase_(listbase), current_(current)
40  {
41  }
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);
81  BLI_assert(ptr);
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 
99 } /* namespace blender */
#define BLI_assert(a)
Definition: BLI_assert.h:46
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
unsigned int uint
Definition: BLI_sys_types.h:67
These structs are the foundation for all linked lists in the library system.
Iterator(ListBase *listbase, T *current)
bool operator!=(const Iterator &iterator) const
ListBaseWrapper(ListBase *listbase)
int64_t index_of(const T *value) const
ListBaseWrapper(ListBase &listbase)
#define T
__int64 int64_t
Definition: stdint.h:89
void * first
Definition: DNA_listBase.h:31
PointerRNA * ptr
Definition: wm_files.c:3480