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

BKE_id_handle.h « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce45ccb1eb3e29341f17e9babacb7aa70511b2f1 (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
#ifndef __BKE_ID_HANDLE_H__
#define __BKE_ID_HANDLE_H__

#include "BLI_utildefines.h"

#include "BLI_map.h"

extern "C" {
struct ID;
struct Object;
struct Image;
}

namespace BKE {

using BLI::Map;

/**
 * This is a weak reference to an ID data-block. It does not contain a pointer to the actual data.
 * It can happen that the IDHandle references data, that does not exist anymore. The handle does
 * not know that.
 */
class IDHandle {
 private:
  uint32_t m_identifier;

 public:
  IDHandle() : m_identifier((uint32_t)-1)
  {
  }

  IDHandle(struct ID *id);

  friend bool operator==(IDHandle a, IDHandle b)
  {
    return a.m_identifier == b.m_identifier;
  }

  friend bool operator!=(IDHandle a, IDHandle b)
  {
    return !(a == b);
  }

  uint32_t internal_identifier() const
  {
    return m_identifier;
  }
};

class ObjectIDHandle : public IDHandle {
 public:
  ObjectIDHandle() : IDHandle()
  {
  }

  ObjectIDHandle(struct Object *object);
};

class ImageIDHandle : public IDHandle {
 public:
  ImageIDHandle() : IDHandle()
  {
  }

  ImageIDHandle(struct Image *image);
};

class IDHandleLookup {
 private:
  Map<IDHandle, ID *> m_handle_to_id_map;

 public:
  void add(ID &id)
  {
    IDHandle handle(&id);
    m_handle_to_id_map.add(handle, &id);
  }

  ID *lookup(IDHandle handle) const
  {
    return m_handle_to_id_map.lookup_default(handle, nullptr);
  }

  struct Object *lookup(ObjectIDHandle handle) const
  {
    return reinterpret_cast<struct Object *>(this->lookup((IDHandle)handle));
  }

  struct Image *lookup(ImageIDHandle handle) const
  {
    return reinterpret_cast<struct Image *>(this->lookup((IDHandle)handle));
  }

  static const IDHandleLookup &Empty();
};

}  // namespace BKE

namespace BLI {
template<> struct DefaultHash<BKE::IDHandle> {
  uint32_t operator()(const BKE::IDHandle &value) const
  {
    return value.internal_identifier();
  }
};
}  // namespace BLI

#endif /* __BKE_ID_HANDLE_H__ */