Blender  V2.93
BKE_persistent_data_handle.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 
26 #include "BLI_map.hh"
27 
28 #include "DNA_ID.h"
29 
30 struct Collection;
31 struct Object;
32 
33 namespace blender::bke {
34 
35 class PersistentDataHandleMap;
36 
38  private:
39  /* Negative values indicate that the handle is "empty". */
40  int32_t handle_;
41 
43 
44  protected:
45  PersistentDataHandle(int handle) : handle_(handle)
46  {
47  }
48 
49  public:
50  PersistentDataHandle() : handle_(-1)
51  {
52  }
53 
54  friend bool operator==(const PersistentDataHandle &a, const PersistentDataHandle &b)
55  {
56  return a.handle_ == b.handle_;
57  }
58 
59  friend bool operator!=(const PersistentDataHandle &a, const PersistentDataHandle &b)
60  {
61  return !(a == b);
62  }
63 
64  friend std::ostream &operator<<(std::ostream &stream, const PersistentDataHandle &a)
65  {
66  stream << a.handle_;
67  return stream;
68  }
69 
70  uint64_t hash() const
71  {
72  return static_cast<uint64_t>(handle_);
73  }
74 };
75 
79 };
80 
83  using PersistentIDHandle::PersistentIDHandle;
84 };
85 
88  using PersistentIDHandle::PersistentIDHandle;
89 };
90 
92  private:
93  Map<int32_t, ID *> id_by_handle_;
94  Map<ID *, int32_t> handle_by_id_;
95 
96  public:
97  void add(int32_t handle, ID &id)
98  {
99  BLI_assert(handle >= 0);
100  handle_by_id_.add(&id, handle);
101  id_by_handle_.add(handle, &id);
102  }
103 
105  {
106  const int handle = handle_by_id_.lookup_default(id, -1);
107  return PersistentIDHandle(handle);
108  }
109 
111  {
112  const int handle = handle_by_id_.lookup_default((ID *)object, -1);
113  return PersistentObjectHandle(handle);
114  }
115 
117  {
118  const int handle = handle_by_id_.lookup_default((ID *)collection, -1);
119  return PersistentCollectionHandle(handle);
120  }
121 
122  ID *lookup(const PersistentIDHandle &handle) const
123  {
124  ID *id = id_by_handle_.lookup_default(handle.handle_, nullptr);
125  return id;
126  }
127 
128  Object *lookup(const PersistentObjectHandle &handle) const
129  {
130  ID *id = this->lookup((const PersistentIDHandle &)handle);
131  if (id == nullptr) {
132  return nullptr;
133  }
134  if (GS(id->name) != ID_OB) {
135  return nullptr;
136  }
137  return (Object *)id;
138  }
139 
141  {
142  ID *id = this->lookup((const PersistentIDHandle &)handle);
143  if (id == nullptr) {
144  return nullptr;
145  }
146  if (GS(id->name) != ID_GR) {
147  return nullptr;
148  }
149  return (Collection *)id;
150  }
151 };
152 
153 } // namespace blender::bke
#define BLI_assert(a)
Definition: BLI_assert.h:58
ID and Library types, which are fundamental for sdna.
@ ID_GR
Definition: DNA_ID_enums.h:77
@ ID_OB
Definition: DNA_ID_enums.h:59
bool add(const Key &key, const Value &value)
Definition: BLI_map.hh:264
Value lookup_default(const Key &key, const Value &default_value) const
Definition: BLI_map.hh:524
PersistentObjectHandle lookup(Object *object) const
Collection * lookup(const PersistentCollectionHandle &handle) const
PersistentCollectionHandle lookup(Collection *collection) const
ID * lookup(const PersistentIDHandle &handle) const
Object * lookup(const PersistentObjectHandle &handle) const
friend bool operator!=(const PersistentDataHandle &a, const PersistentDataHandle &b)
friend bool operator==(const PersistentDataHandle &a, const PersistentDataHandle &b)
friend std::ostream & operator<<(std::ostream &stream, const PersistentDataHandle &a)
#define GS(x)
Definition: iris.c:241
static unsigned a[3]
Definition: RandGen.cpp:92
signed int int32_t
Definition: stdint.h:80
unsigned __int64 uint64_t
Definition: stdint.h:93
Definition: DNA_ID.h:273
char name[66]
Definition: DNA_ID.h:283