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

BKE_geometry.hh « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e2532d639c4f5b91ba28e5be66501af7b736425a (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * 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.
 */

#pragma once

/** \file
 * \ingroup bke
 */

#include <atomic>
#include <iostream>

#include "BLI_hash.hh"

struct Mesh;

namespace blender::bke {

/**
 * Geometry can contain geometry of different types, such as meshes and curves (although currently
 * only meshes are supported).
 *
 * Geometries are reference counted. This allows them to be shared without making unnecessary
 * copies. A geometry that is shared is immutable. If some code wants to change it,
 * #make_geometry_mutable should be called first.
 */
class Geometry {
 private:
  /* Number of users of this geometry. If this number goes to zero, the geometry is freed. If it is
   * above 1, the geometry is immutable. */
  std::atomic<int> users_ = 1;

  /* Only contains a mesh for now. */
  Mesh *mesh_ = nullptr;
  /* Determines if the mesh is freed when the geometry does not want to reference it anymore. */
  bool mesh_owned_ = false;

 public:
  Geometry() = default;
  Geometry(const Geometry &other);
  Geometry(Geometry &&other) = delete;
  ~Geometry();

  /* Disable copy and move assignment operators. */
  Geometry &operator=(const Geometry &other) = delete;
  Geometry &operator=(Geometry &&other) = delete;

  void user_add();
  void user_remove();
  bool is_mutable() const;

  void mesh_set_and_keep_ownership(Mesh *mesh);
  void mesh_set_and_transfer_ownership(Mesh *mesh);
  void mesh_reset();
  Mesh *mesh_get_for_read();
  Mesh *mesh_get_for_write();
  Mesh *mesh_release();
};

/**
 * A simple automatic reference counter. This should probably be moved to another file eventually.
 * It is similar to std::shared_ptr, but expects that the reference count is inside the object.
 */
template<typename T> class UserCounter {
 private:
  T *data_ = nullptr;

 public:
  UserCounter() = default;

  UserCounter(T *data) : data_(data)
  {
  }

  UserCounter(const UserCounter &other) : data_(other.data_)
  {
    this->user_add(data_);
  }

  UserCounter(UserCounter &&other) : data_(other.data_)
  {
    other.data_ = nullptr;
  }

  ~UserCounter()
  {
    this->user_remove(data_);
  }

  UserCounter &operator=(const UserCounter &other)
  {
    if (this == &other) {
      return *this;
    }

    this->user_remove(data_);
    data_ = other.data_;
    this->user_add(data_);
    return *this;
  }

  UserCounter &operator=(UserCounter &&other)
  {
    if (this == &other) {
      return *this;
    }

    this->user_remove(data_);
    data_ = other.data_;
    other.data_ = nullptr;
    return *this;
  }

  T *operator->()
  {
    BLI_assert(data_ != nullptr);
    return data_;
  }

  T &operator*()
  {
    BLI_assert(data_ != nullptr);
    return *data_;
  }

  operator bool() const
  {
    return data_ != nullptr;
  }

  T *get()
  {
    return data_;
  }

  T *release()
  {
    T *data = data_;
    data_ = nullptr;
    return data;
  }

  void reset()
  {
    this->user_remove(data_);
    data_ = nullptr;
  }

  bool has_value() const
  {
    return data_ != nullptr;
  }

  uint64_t hash() const
  {
    return DefaultHash<T *>{}(data_);
  }

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

  friend std::ostream &operator<<(std::ostream &stream, const UserCounter &value)
  {
    stream << value.data_;
    return stream;
  }

 private:
  static void user_add(T *data)
  {
    if (data != nullptr) {
      data->user_add();
    }
  }

  static void user_remove(T *data)
  {
    if (data != nullptr) {
      data->user_remove();
    }
  }
};

/* An automatically reference counted geometry. */
using GeometryPtr = UserCounter<class Geometry>;

void make_geometry_mutable(GeometryPtr &geometry);

}  // namespace blender::bke