Blender  V2.93
BLI_listbase_wrapper.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
28 #include "BLI_listbase.h"
29 #include "DNA_listBase.h"
30 
31 namespace blender {
32 
33 template<typename T> class ListBaseWrapper {
34  private:
35  ListBase *listbase_;
36 
37  public:
38  ListBaseWrapper(ListBase *listbase) : listbase_(listbase)
39  {
40  BLI_assert(listbase);
41  }
42 
43  ListBaseWrapper(ListBase &listbase) : ListBaseWrapper(&listbase)
44  {
45  }
46 
47  class Iterator {
48  private:
49  ListBase *listbase_;
50  T *current_;
51 
52  public:
53  Iterator(ListBase *listbase, T *current) : listbase_(listbase), current_(current)
54  {
55  }
56 
58  {
59  /* Some types store next/prev using `void *`, so cast is necessary. */
60  current_ = static_cast<T *>(current_->next);
61  return *this;
62  }
63 
65  {
66  Iterator iterator = *this;
67  ++*this;
68  return iterator;
69  }
70 
71  bool operator!=(const Iterator &iterator) const
72  {
73  return current_ != iterator.current_;
74  }
75 
76  T *operator*() const
77  {
78  return current_;
79  }
80  };
81 
82  Iterator begin() const
83  {
84  return Iterator(listbase_, static_cast<T *>(listbase_->first));
85  }
86 
87  Iterator end() const
88  {
89  return Iterator(listbase_, nullptr);
90  }
91 
92  T get(uint index) const
93  {
94  void *ptr = BLI_findlink(listbase_, index);
95  BLI_assert(ptr);
96  return static_cast<T *>(ptr);
97  }
98 
99  int64_t index_of(const T *value) const
100  {
101  int64_t index = 0;
102  for (T *ptr : *this) {
103  if (ptr == value) {
104  return index;
105  }
106  index++;
107  }
108  BLI_assert(false);
109  return -1;
110  }
111 };
112 
113 } /* namespace blender */
#define BLI_assert(a)
Definition: BLI_assert.h:58
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
unsigned int uint
Definition: BLI_sys_types.h:83
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:92
void * first
Definition: DNA_listBase.h:47
PointerRNA * ptr
Definition: wm_files.c:3157