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  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 #include "IO_abstract_hierarchy_iterator.h"
20 
21 #include "BKE_duplilist.h"
22 
23 extern "C" {
24 #include <limits.h> /* For INT_MAX. */
25 }
26 #include <cstring>
27 #include <sstream>
28 
29 namespace blender::io {
30 
ObjectIdentifier(Object * object,Object * duplicated_by,const PersistentID & persistent_id)31 ObjectIdentifier::ObjectIdentifier(Object *object,
32                                    Object *duplicated_by,
33                                    const PersistentID &persistent_id)
34     : object(object), duplicated_by(duplicated_by), persistent_id(persistent_id)
35 {
36 }
37 
ObjectIdentifier(const ObjectIdentifier & other)38 ObjectIdentifier::ObjectIdentifier(const ObjectIdentifier &other)
39     : object(other.object), duplicated_by(other.duplicated_by), persistent_id(other.persistent_id)
40 {
41 }
42 
~ObjectIdentifier()43 ObjectIdentifier::~ObjectIdentifier()
44 {
45 }
46 
for_real_object(Object * object)47 ObjectIdentifier ObjectIdentifier::for_real_object(Object *object)
48 {
49   return ObjectIdentifier(object, nullptr, PersistentID());
50 }
51 
for_hierarchy_context(const HierarchyContext * context)52 ObjectIdentifier ObjectIdentifier::for_hierarchy_context(const HierarchyContext *context)
53 {
54   if (context == nullptr) {
55     return for_graph_root();
56   }
57   if (context->duplicator != nullptr) {
58     return ObjectIdentifier(context->object, context->duplicator, context->persistent_id);
59   }
60   return for_real_object(context->object);
61 }
62 
for_duplicated_object(const DupliObject * dupli_object,Object * duplicated_by)63 ObjectIdentifier ObjectIdentifier::for_duplicated_object(const DupliObject *dupli_object,
64                                                          Object *duplicated_by)
65 {
66   return ObjectIdentifier(dupli_object->ob, duplicated_by, PersistentID(dupli_object));
67 }
68 
for_graph_root()69 ObjectIdentifier ObjectIdentifier::for_graph_root()
70 {
71   return ObjectIdentifier(nullptr, nullptr, PersistentID());
72 }
73 
is_root() const74 bool ObjectIdentifier::is_root() const
75 {
76   return object == nullptr;
77 }
78 
operator <(const ObjectIdentifier & obj_ident_a,const ObjectIdentifier & obj_ident_b)79 bool operator<(const ObjectIdentifier &obj_ident_a, const ObjectIdentifier &obj_ident_b)
80 {
81   if (obj_ident_a.object != obj_ident_b.object) {
82     return obj_ident_a.object < obj_ident_b.object;
83   }
84 
85   if (obj_ident_a.duplicated_by != obj_ident_b.duplicated_by) {
86     return obj_ident_a.duplicated_by < obj_ident_b.duplicated_by;
87   }
88 
89   if (obj_ident_a.duplicated_by == nullptr) {
90     /* Both are real objects, no need to check the persistent ID. */
91     return false;
92   }
93 
94   /* Same object, both are duplicated, use the persistent IDs to determine order. */
95   return obj_ident_a.persistent_id < obj_ident_b.persistent_id;
96 }
97 
operator ==(const ObjectIdentifier & obj_ident_a,const ObjectIdentifier & obj_ident_b)98 bool operator==(const ObjectIdentifier &obj_ident_a, const ObjectIdentifier &obj_ident_b)
99 {
100   if (obj_ident_a.object != obj_ident_b.object) {
101     return false;
102   }
103   if (obj_ident_a.duplicated_by != obj_ident_b.duplicated_by) {
104     return false;
105   }
106   if (obj_ident_a.duplicated_by == nullptr) {
107     return true;
108   }
109 
110   /* Same object, both are duplicated, use the persistent IDs to determine equality. */
111   return obj_ident_a.persistent_id == obj_ident_b.persistent_id;
112 }
113 
114 }  // namespace blender::io
115