Debian Codegraph @

src/App/ElementMap.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /****************************************************************************
4 * Copyright (c) 2018-2022 Zheng, Lei (realthunder) *
5 * <[email protected]> *
6 * Copyright (c) 2023 FreeCAD Project Association *
7 * *
8 * This file is part of FreeCAD. *
9 * *
10 * FreeCAD is free software: you can redistribute it and/or modify it *
11 * under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 2.1 of the *
13 * License, or (at your option) any later version. *
14 * *
15 * FreeCAD is distributed in the hope that it will be useful, but *
16 * WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18 * Lesser General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU Lesser General Public *
21 * License along with FreeCAD. If not, see *
22 * <https://www.gnu.org/licenses/>. *
23 * *
24 ***************************************************************************/
25
26 #ifndef DATA_ELEMENTMAP_H
27 #define DATA_ELEMENTMAP_H
28
29 #include "FCGlobal.h"
30
31 #include "Application.h"
32 #include "MappedElement.h"
33 #include "StringHasher.h"
34
35 #include <cstring>
36 #include <deque>
37 #include <functional>
38 #include <map>
39 #include <memory>
40
41
42 namespace Data
43 {
44
45 class ElementMap;
46 using ElementMapPtr = std::shared_ptr<ElementMap>;
47
48 /** Element trace callback
49 *
50 * The callback has the following call signature
51 * (const std::string &name, size_t offset, long encodedTag, long tag) -> bool
52 *
53 * @param name: the current element name.
54 * @param offset: the offset skipping the encoded element name for the next iteration.
55 * @param encodedTag: the tag encoded inside the current element, which is usually the tag
56 * of the previous step in the shape history.
57 * @param tag: the tag of the current shape element.
58 *
59 * @sa traceElement()
60 */
61 typedef std::function<bool(const MappedName&, int, long, long)> TraceCallback;
62
63 /* This class provides for ComplexGeoData's ability to provide proper naming.
64 * Specifically, ComplexGeoData uses this class for it's `_id` property.
65 * Most of the operations work with the `indexedNames` and `mappedNames` maps.
66 * `indexedNames` maps a string to both a name queue and children.
67 * each of those children store an IndexedName, offset details, postfix, ids, and
68 * possibly a recursive elementmap
69 * `mappedNames` maps a MappedName to a specific IndexedName.
70 */
71 class AppExport ElementMap
72 : public std::enable_shared_from_this<ElementMap> // TODO can remove shared_from_this?
73 {
74 public:
75 /** Default constructor: hooks internal functions to \c signalSaveDocument and
76 * \c signalStartRestoreDocument. This is related to the save and restore process
77 * of the map.
78 */
79 ElementMap();
80
81 /** Ensures that naming is properly assigned. It then marks as "used" all the StringID
82 * that are used to make up this particular map and are stored in the hasherRef passed
83 * as a parameter. Finally do this recursively for all childEelementMaps as well.
84 *
85 * @param hasherRef where all the StringID needed to build the map are stored.
86 */
87 // FIXME this should be made part of \c save, to achieve symmetry with the restore method
88 void beforeSave(const ::App::StringHasherRef& hasherRef) const;
89
90 /** Serialize this map. Calls \c collectChildMaps to get \c childMapSet and
91 * \c postfixMap, then calls the other (private) save function with those parameters.
92 * @param stream: serialized stream
93 */
94 void save(std::ostream& stream) const;
95
96 /** Deserialize and restore this map. This function restores \c childMaps and
97 * \c postfixes from the stream, then calls the other (private) restore function with those
98 * parameters.
99 * @param hasherRef: where all the StringIDs are stored
100 * @param stream: stream to deserialize
101 */
102 ElementMapPtr restore(::App::StringHasherRef hasherRef, std::istream& stream);
103
104
105 /** Add a sub-element name mapping.
106 *
107 * @param element: the original \c Type + \c Index element name
108 * @param name: the mapped sub-element name. May or may not start with
109 * elementMapPrefix().
110 * @param sid: in case you use a hasher to hash the element name, pass in
111 * the string id reference using this parameter. You can have more than one
112 * string id associated with the same name.
113 * @param overwrite: if true, it will overwrite existing names
114 *
115 * @return Returns the stored mapped element name.
116 *
117 * An element can have multiple mapped names. However, a name can only be
118 * mapped to one element
119 *
120 * Note: the original proc was in the context of ComplexGeoData, which provided `Tag` access,
121 * now you must pass in `long masterTag` explicitly.
122 */
123 MappedName setElementName(const IndexedName& element,
124 const MappedName& name,
125 long masterTag,
126 const ElementIDRefs* sid = nullptr,
127 bool overwrite = false);
128
129 /* Generates a new MappedName from the current details.
130 *
131 * The result is streamed to `ss` and stored in `name`.
132 *
133 * Note: the original proc was in the context of ComplexGeoData, which provided `Tag` access,
134 * now you must pass in `long masterTag` explicitly.
135 */
136 void encodeElementName(char element_type,
137 MappedName& name,
138 std::ostringstream& ss,
139 ElementIDRefs* sids,
140 long masterTag,
141 const char* postfix = nullptr,
142 long tag = 0,
143 bool forceTag = false) const;
144
145 /// Remove \c name from the map
146 void erase(const MappedName& name);
147
148 /// Remove \c idx and all the MappedNames associated with it
149 void erase(const IndexedName& idx);
150
151 unsigned long size() const;
152
153 bool empty() const;
154
155 IndexedName find(const MappedName& name, ElementIDRefs* sids = nullptr) const;
156
157 MappedName find(const IndexedName& idx, ElementIDRefs* sids = nullptr) const;
158
159 std::vector<std::pair<MappedName, ElementIDRefs>> findAll(const IndexedName& idx) const;
160
161 // prefix searching is disabled, as TopoShape::getRelatedElement() is
162 // deprecated in favor of GeoFeature::getRelatedElement(). Besides, there
163 // is efficient way to support child element map if we were to implement
164 // prefix search.
165 #if 0
166 std::vector<MappedElement> findAllStartsWith(const char *prefix) const;
167 #endif
168
169 bool hasChildElementMap() const;
170
171 /* Ensures that for each IndexedName mapped to IndexedElements, that
172 * each child is properly hashed (cached).
173 *
174 * Note: the original proc was in the context of ComplexGeoData, which provided `Tag` access,
175 * now you must pass in `long masterTag` explicitly.
176 */
177 void hashChildMaps(long masterTag);
178
179 struct AppExport MappedChildElements
180 {
181 IndexedName indexedName;
182 int count;
183 int offset;
184 long tag;
185 ElementMapPtr elementMap;
186 QByteArray postfix;
187 ElementIDRefs sids;
188
189 // prefix() has been moved to ElementNamingUtils.h
190 };
191
192 /* Note: the original addChildElements passed `ComplexGeoData& master` for getting the `Tag`,
193 * now it just passes `long masterTag`.*/
194 void addChildElements(long masterTag, const std::vector<MappedChildElements>& children);
195
196 std::vector<MappedChildElements> getChildElements() const;
197
198 std::vector<MappedElement> getAll() const;
199
200 long getElementHistory(const MappedName& name,
201 long masterTag,
202 MappedName* original = nullptr,
203 std::vector<MappedName>* history = nullptr) const;
204
205 /** Iterate through the history of the give element name with a given callback
206 *
207 * @param name: the input element name
208 * @param cb: trace callback with call signature.
209 * @sa TraceCallback
210 */
211 void traceElement(const MappedName& name, long masterTag, TraceCallback cb) const;
212
213
214 private:
215 /** Serialize this map
216 * @param stream: serialized stream
217 * @param childMapSet: where all child element maps are stored
218 * @param postfixMap. where all postfixes are stored
219 */
220 void save(std::ostream& stream,
221 int index,
222 const std::map<const ElementMap*, int>& childMapSet,
223 const std::map<QByteArray, int>& postfixMap) const;
224
225 /** Deserialize and restore this map.
226 * @param hasherRef: where all the StringIDs are stored
227 * @param stream: stream to deserialize
228 * @param childMaps: where all child element maps are stored
229 * @param postfixes. where all postfixes are stored
230 */
231 ElementMapPtr restore(::App::StringHasherRef hasherRef,
232 std::istream& stream,
233 std::vector<ElementMapPtr>& childMaps,
234 const std::vector<std::string>& postfixes);
235
236 /** Associate the MappedName \c name with the IndexedName \c idx.
237 * @param name: the name to add
238 * @param idx: the indexed name that \c name will be bound to
239 * @param sids: where StringIDs that make up the name are stored
240 * @param overwrite: if true, all the names associated with \c idx will be discarded
241 * @param existing: out variable: if not overwriting, and \c name is already
242 * associated with another indexedName, set \c existing to that indexedname
243 * @return the name just added, or an empty name if it wasn't added.
244 */
245 MappedName addName(MappedName& name,
246 const IndexedName& idx,
247 const ElementIDRefs& sids,
248 bool overwrite,
249 IndexedName* existing);
250
251 /** Utility function that adds \c postfix to \c postfixMap, and to \c postfixes
252 * if it was not present in the map.
253 */
254 static void addPostfix(const QByteArray& postfix,
255 std::map<QByteArray, int>& postfixMap,
256 std::vector<QByteArray>& postfixes);
257
258 /* Note: the original proc passed `ComplexGeoData& master` for getting the `Tag`,
259 * now it just passes `long masterTag`.*/
260 MappedName renameDuplicateElement(int index,
261 const IndexedName& element,
262 const IndexedName& element2,
263 const MappedName& name,
264 ElementIDRefs& sids,
265 long masterTag) const;
266
267 /** Convenience method to hash the main element name
268 *
269 * @param name: main element name
270 * @param sid: store any output string ID references
271 * @return the hashed element name;
272 */
273 MappedName hashElementName(const MappedName& name, ElementIDRefs& sids) const;
274
275 /// Reverse hashElementName()
276 MappedName dehashElementName(const MappedName& name) const;
277
278 // FIXME duplicate code? as in copy/paste
279 const MappedNameRef* findMappedRef(const IndexedName& idx) const;
280 MappedNameRef* findMappedRef(const IndexedName& idx);
281
282 MappedNameRef& mappedRef(const IndexedName& idx);
283
284 void collectChildMaps(std::map<const ElementMap*, int>& childMapSet,
285 std::vector<const ElementMap*>& childMaps,
286 std::map<QByteArray, int>& postfixMap,
287 std::vector<QByteArray>& postfixes) const;
288
289 struct CStringComp
290 {
291 public:
292 bool operator()(const char* str1, const char* str2) const
293 {
294 return std::strcmp(str1, str2) < 0;
295 }
296 };
297
298 struct IndexedElements
299 {
300 std::deque<MappedNameRef> names;
301 std::map<int, MappedChildElements> children;
302 };
303
304 std::map<const char*, IndexedElements, CStringComp> indexedNames;
305
306 std::map<MappedName, IndexedName, std::less<>> mappedNames;
307
308 struct ChildMapInfo
309 {
310 int index = 0;
311 MappedChildElements* childMap = nullptr;
312 std::map<ElementMap*, int> mapIndices;
313 };
314
315 QHash<QByteArray, ChildMapInfo> childElements;
316 std::size_t childElementSize = 0;
317
318 mutable unsigned _id = 0;
319
320 void init();
321
322 public:
323 /// String hasher for element name shortening
324 App::StringHasherRef hasher;
325 };
326
327
328 } // namespace Data
329
330 #endif // DATA_ELEMENTMAP_H