Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BKE_persistent_data_handle.hh « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 884e4c0076681f55ccc758bac0f5964157fc0234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef __BKE_PERSISTENT_DATA_HANDLE_H__
#define __BKE_PERSISTENT_DATA_HANDLE_H__

/** \file
 * \ingroup bke
 *
 * A PersistentDataHandle is a weak reference to some data in a Blender file. The handle itself is
 * just a number. A PersistentDataHandleMap is used to convert between handles and the actual data.
 */

#include "BLI_map.hh"

#include "DNA_ID.h"

struct Object;

namespace blender::bke {

class PersistentDataHandleMap;

class PersistentDataHandle {
 private:
  /* Negative values indicate that the handle is "empty". */
  int32_t handle_;

  friend PersistentDataHandleMap;

 protected:
  PersistentDataHandle(int handle) : handle_(handle)
  {
  }

 public:
  PersistentDataHandle() : handle_(-1)
  {
  }

  friend bool operator==(const PersistentDataHandle &a, const PersistentDataHandle &b)
  {
    return a.handle_ == b.handle_;
  }

  friend bool operator!=(const PersistentDataHandle &a, const PersistentDataHandle &b)
  {
    return !(a == b);
  }

  friend std::ostream &operator<<(std::ostream &stream, const PersistentDataHandle &a)
  {
    stream << a.handle_;
    return stream;
  }

  uint64_t hash() const
  {
    return (uint64_t)handle_;
  }
};

class PersistentIDHandle : public PersistentDataHandle {
  friend PersistentDataHandleMap;
  using PersistentDataHandle::PersistentDataHandle;
};

class PersistentObjectHandle : public PersistentIDHandle {
  friend PersistentDataHandleMap;
  using PersistentIDHandle::PersistentIDHandle;
};

class PersistentDataHandleMap {
 private:
  Map<int32_t, ID *> id_by_handle_;
  Map<ID *, int32_t> handle_by_id_;

 public:
  void add(int32_t handle, ID &id)
  {
    BLI_assert(handle >= 0);
    handle_by_id_.add(&id, handle);
    id_by_handle_.add(handle, &id);
  }

  PersistentIDHandle lookup(ID *id) const
  {
    const int handle = handle_by_id_.lookup_default(id, -1);
    return PersistentIDHandle(handle);
  }

  PersistentObjectHandle lookup(Object *object) const
  {
    const int handle = handle_by_id_.lookup_default((ID *)object, -1);
    return PersistentObjectHandle(handle);
  }

  ID *lookup(const PersistentIDHandle &handle) const
  {
    ID *id = id_by_handle_.lookup_default(handle.handle_, nullptr);
    return id;
  }

  Object *lookup(const PersistentObjectHandle &handle) const
  {
    ID *id = this->lookup((const PersistentIDHandle &)handle);
    if (id == nullptr) {
      return nullptr;
    }
    if (GS(id->name) != ID_OB) {
      return nullptr;
    }
    return (Object *)id;
  }
};

}  // namespace blender::bke

#endif /*  __BKE_PERSISTENT_DATA_HANDLE_H__ */