Blender V4.3
dupli_persistent_id.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include <climits>
8#include <cstring>
9#include <ostream>
10#include <sstream>
11
12namespace blender::io {
13
15{
16 persistent_id_[0] = INT_MAX;
17}
18
20{
21 for (int index = 0; index < array_length_; ++index) {
22 persistent_id_[index] = dupli_ob->persistent_id[index];
23 }
24}
25
26PersistentID::PersistentID(const PIDArray &persistent_id_values)
27{
28 persistent_id_ = persistent_id_values;
29}
30
32{
33 if (persistent_id_[0] == INT_MAX || other.persistent_id_[0] == INT_MAX) {
34 /* Either one or the other is not instanced at all, so definitely not from the same instancer.
35 */
36 return false;
37 }
38
39 /* Start at index 1 to skip the first digit. */
40 for (int index = 1; index < array_length_; ++index) {
41 const int pid_digit_a = persistent_id_[index];
42 const int pid_digit_b = other.persistent_id_[index];
43
44 if (pid_digit_a != pid_digit_b) {
45 return false;
46 }
47
48 if (pid_digit_a == INT_MAX) {
49 /* Both persistent IDs were identical so far, and this marks the end of the useful data. */
50 break;
51 }
52 }
53 return true;
54}
55
57{
58 if (persistent_id_[0] == INT_MAX) {
59 return PersistentID();
60 }
61
62 /* Left-shift the entire PID by 1. */
63 PIDArray new_pid_values;
64 int index;
65 for (index = 0; index < array_length_ - 1; ++index) {
66 new_pid_values[index] = persistent_id_[index + 1];
67 }
68 new_pid_values[index] = INT_MAX;
69
70 return PersistentID(new_pid_values);
71}
72
74{
75 std::stringstream stream;
76
77 /* Find one past the last index. */
78 int index;
79 for (index = 0; index < array_length_ && persistent_id_[index] < INT_MAX; ++index) {
80 ;
81 }
82
83 /* Iterate backward to construct the string. */
84 --index;
85 for (; index >= 0; --index) {
86 stream << persistent_id_[index];
87 if (index > 0) {
88 stream << "-";
89 }
90 }
91
92 return stream.str();
93}
94
95bool operator<(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
96{
97 const PersistentID::PIDArray &pid_a = persistent_id_a.persistent_id_;
98 const PersistentID::PIDArray &pid_b = persistent_id_b.persistent_id_;
99
100 for (int index = 0; index < PersistentID::array_length_; ++index) {
101 const int pid_digit_a = pid_a[index];
102 const int pid_digit_b = pid_b[index];
103
104 if (pid_digit_a != pid_digit_b) {
105 return pid_digit_a < pid_digit_b;
106 }
107
108 if (pid_a[index] == INT_MAX) {
109 break;
110 }
111 }
112 /* Both Persistent IDs were equal, so not less-than. */
113 return false;
114}
115
116bool operator==(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
117{
118 const PersistentID::PIDArray &pid_a = persistent_id_a.persistent_id_;
119 const PersistentID::PIDArray &pid_b = persistent_id_b.persistent_id_;
120
121 for (int index = 0; index < PersistentID::array_length_; ++index) {
122 const int pid_digit_a = pid_a[index];
123 const int pid_digit_b = pid_b[index];
124
125 if (pid_digit_a != pid_digit_b) {
126 return false;
127 }
128
129 if (pid_a[index] == INT_MAX) {
130 break;
131 }
132 }
133 return true;
134}
135
136std::ostream &operator<<(std::ostream &os, const PersistentID &persistent_id)
137{
138 if (persistent_id.persistent_id_[0] == INT_MAX) {
139 return os;
140 }
141
142 const PersistentID::PIDArray &pid_array = persistent_id.persistent_id_;
143 for (int index = 0; index < PersistentID::array_length_ && pid_array[index] < INT_MAX; ++index) {
144 if (index > 0) {
145 os << "-";
146 }
147 os << pid_array[index];
148 }
149 return os;
150}
151
152} // namespace blender::io
std::array< int, array_length_ > PIDArray
PersistentID instancer_pid() const
std::string as_object_name_suffix() const
bool is_from_same_instancer_as(const PersistentID &other) const
std::ostream & operator<<(std::ostream &os, const PersistentID &persistent_id)
bool operator<(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
bool operator==(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
int persistent_id[8]