Debian Codegraph @

src/App/ObjectIdentifier.h

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Eivind Kvedalen <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #ifndef APP_PATH_H
25 #define APP_PATH_H
26
27 #include <bitset>
28 #include <map>
29 #include <limits>
30 #include <set>
31 #include <string>
32 #include <vector>
33 #include <boost/any.hpp>
34 #include <FCConfig.h>
35
36 #include "ElementNamingUtils.h"
37
38 namespace Py
39 {
40 class Object;
41 }
42 namespace App
43 {
44
45 using any = boost::any;
46
47 /**
48 * @brief Extract a const reference from a boost::any object.
49 *
50 * This function is a wrapper around boost::any_cast that allows
51 * to extract a const reference from a boost::any object.
52 *
53 * @tparam T The type to extract.
54 *
55 * @param[in] value The boost::any object to extract from.
56 *
57 * @return A const reference to the extracted value.
58 * @throws boost::bad_any_cast if the type of the value does not match T.
59 */
60 template<class T>
61 inline const T& any_cast(const boost::any& value)
62 {
63 return boost::any_cast<const T&>(value);
64 }
65
66 /**
67 * @brief Extract a mutable reference from a boost::any object.
68 *
69 * This function is a wrapper around boost::any_cast that allows
70 * to extract a mutable reference from a boost::any object.
71 *
72 * @tparam T The type to extract.
73 *
74 * @param[in] value The boost::any object to extract from.
75 *
76 * @return A const reference to the extracted value.
77 * @throws boost::bad_any_cast if the type of the value does not match T.
78 */
79 template<class T>
80 inline T& any_cast(boost::any& value)
81 {
82 return boost::any_cast<T&>(value);
83 }
84
85 class Property;
86 class Document;
87 class PropertyContainer;
88 class DocumentObject;
89 class ExpressionVisitor;
90
91 /**
92 * @brief Quote a string.
93 *
94 * Quote an input string according to quoting rules for an expression: because
95 * " and ' are used to designate inch and foot units, strings are quoted as
96 * `<<string>>`.
97 *
98 * @param[in] input The string to quote.
99 * @param[in] toPython If true, use Python quoting rules. Otherwise, use
100 * FreeCAD quoting rules.
101 *
102 * @return The string quoted.
103 */
104 AppExport std::string quote(const std::string& input, bool toPython = false);
105
106 // Unfortunately VS2013 does not support default move constructor, so we have
107 // to implement them manually
108 #define FC_DEFAULT_CTORS(_t) \
109 _t(const _t&) = default; \
110 _t& operator=(const _t&) = default; \
111 _t(_t&& other) \
112 { \
113 *this = std::move(other); \
114 } \
115 _t& operator=(_t&& other)
116
117
118 /**
119 * @brief A class that identifies properties in document objects.
120 *
121 * An object identifier is a data structure that identifies a (sub)properties
122 * in a document or document objects, or documents or document objects
123 * themselves.
124 */
125 class AppExport ObjectIdentifier
126 {
127
128 public:
129 /**
130 * @brief A helper class to maintain a mapping between document names.
131 *
132 * This class maps old document names to new document names and is used to
133 * provide a scope in which calls to setDocumentName() make use of the
134 * mapping stored here.
135 */
136 class AppExport DocumentMapper
137 {
138 public:
139 /**
140 * @brief Construct a DocumentMapper object.
141 *
142 * The constructor takes a map of document names and stores a pointer
143 * to it in a variable local to the compilation unit. The idea is to
144 * create a %DocumentMapper on the stack and when it goes out of scope,
145 * all is reset. During that time calls to setDocumentName() can be
146 * done, making use of the mapping provided with this constructor.
147 */
148 explicit DocumentMapper(const std::map<std::string, std::string>&);
149
150 /**
151 * @brief Destroy the DocumentMapper object.
152 *
153 * When the DocumentMapper goes out of scope, the pointer to the
154 * variable local to the compilation unit is set to `nullptr`.
155 */
156 ~DocumentMapper();
157 };
158
159 /**
160 * @brief A class that represents a string in an ObjectIdentifier.
161 */
162 class String
163 {
164 friend class ObjectIdentifier;
165
166 public:
167 /**
168 * @brief Construct a String object for object identifiers.
169 *
170 * @param[in] s The string to be used.
171 * @param[in] _isRealString If true, the string is a real string and should be
172 * quoted. Otherwise, it is a simple identifier.
173 * @param[in] _forceIdentifier If true, the string is a forced identifier.
174 */
175 String(const std::string& s = "", bool _isRealString = false, bool _forceIdentifier = false)
176 : str(s)
177 , isString(_isRealString)
178 , forceIdentifier(_forceIdentifier)
179 {} // explicit bombs
180
181 /**
182 * @brief Explicit move‐construct a String object for object identifiers.
183 *
184 * This constructor takes ownership of the provided string via move semantics,
185 * preventing an implicit conversion from temporary std::string.
186 *
187 * @param[in,out] s The string to be used (will be moved-from).
188 * @param[in] _isRealString If true, the string is a real string and should be
189 * quoted. Otherwise, it is a simple identifier.
190 * @param[in] _forceIdentifier If true, the string is a forced identifier.
191 */
192 explicit String(std::string&& s, bool _isRealString = false, bool _forceIdentifier = false)
193 : str(std::move(s))
194 , isString(_isRealString)
195 , forceIdentifier(_forceIdentifier)
196 {}
197
198 FC_DEFAULT_CTORS(String)
199 {
200 str = std::move(other.str);
201 isString = other.isString;
202 forceIdentifier = other.forceIdentifier;
203 return *this;
204 }
205
206 // Accessors
207
208 /**
209 * @brief Get the string as a `std::string`.
210 *
211 * @return The string as a `std::string`.
212 */
213 const std::string& getString() const
214 {
215 return str;
216 }
217
218 /**
219 * @brief Test whether the string is a real string.
220 *
221 * A real string is a string that should be quoted. A simple identifier
222 * is a string that should not be quoted.
223 *
224 * @return True if the string is a real string, false otherwise.
225 */
226 bool isRealString() const
227 {
228 return isString;
229 }
230
231 /**
232 * @brief Test whether the string is a forced identifier.
233 *
234 * A forced identifier is a string that should be treated as an
235 * identifier.
236 *
237 * @return True if the string is a forced identifier, false otherwise.
238 */
239 bool isForceIdentifier() const
240 {
241 return forceIdentifier;
242 }
243
244 /**
245 * @brief Get a string representation of this object identifier.
246 *
247 * @param[in] toPython If true, use Python quoting rules. Otherwise, use
248 * FreeCAD quoting rules.
249 * @return The string representation.
250 */
251 std::string toString(bool toPython = false) const;
252
253 // Operators
254
255 /**
256 * @brief Explicitly convert to `std::string`.
257 *
258 * Returns a copy of the internal std::string. Because this operator is
259 * marked explicit, it is required to use a cast:
260 * `static_cast<std::string>(myString)`.
261 *
262 * @return A copy of the string as `std::string`.
263 */
264 explicit operator std::string() const
265 {
266 return str;
267 }
268
269 /**
270 * @brief Explicitly convert to C-style string.
271 *
272 * Returns a pointer to the internal null-terminated character array.
273 * The pointer remains valid as long as the String object is alive and
274 * unmodified. This operator is explicit, so it is required to use:
275 * `static_cast<const char*>(myString)`.
276 *
277 * @return A const char* pointing at the internal string buffer.
278 */
279 explicit operator const char*() const
280 {
281 return str.c_str();
282 }
283
284 /**
285 * @brief Test for equality.
286 *
287 * Compares the underlying string values for exact equality.
288 *
289 * @param[in] other The %String to compare against.
290 * @return true if both strings have identical content; false otherwise.
291 */
292 bool operator==(const String& other) const
293 {
294 return str == other.str;
295 }
296
297 /**
298 * @brief Test for inequality.
299 *
300 * Compares the underlying string values for inequality.
301 *
302 * @param[in] other The %String we want to compare to.
303 * @return true if this string does not have identical content to the
304 * other string; false otherwise.
305 */
306 bool operator!=(const String& other) const
307 {
308 return str != other.str;
309 }
310
311 /**
312 * @brief Lexicographical greater-than-or-equal-to comparison.
313 *
314 * Determines if this string is lexicographically greater than or equal
315 * to the other string.
316 *
317 * @param[in] other The %String to compare against.
318 * @return true if this string is lexicographically greater than or
319 * equal to `other`; false otherwise.
320 */
321 bool operator>=(const String& other) const
322 {
323 return str >= other.str;
324 }
325
326 /**
327 * @brief Lexicographical less-than comparison.
328 *
329 * Determines if this string precedes `other` in lexicographical order.
330 *
331 * @param[in] other The %String to compare against.
332 * @return true if this string is lexicographically less than `other`; false otherwise.
333 */
334 bool operator<(const String& other) const
335 {
336 return str < other.str;
337 }
338
339 /**
340 * @brief Lexicographical greater-than comparison.
341 *
342 * Determines if this string follows `other` in lexicographical order.
343 *
344 * @param[in] other The %String to compare against.
345 * @return true if this string is lexicographically greater than `other`; false otherwise.
346 */
347 bool operator>(const String& other) const
348 {
349 return str > other.str;
350 }
351
352 /**
353 * @brief Remaps and resolves identifier strings during import.
354 *
355 * Applies name mapping and, if “@” markers are present, defers label
356 * substitution to import-time via PropertyLinkBase::importSubName() and
357 * PropertyLinkBase::restoreLabelReference().
358 *
359 * @param[in] owner The importing document’s owner object.
360 * @param[in] obj (Optional) Direct pointer to the target object.
361 * @param[in] objName (Optional) Name to look up the object if @p obj is null.
362 *
363 * @sa importSubName(), restoreLabelReference()
364 */
365 void checkImport(const App::DocumentObject* owner,
366 const App::DocumentObject* obj = nullptr,
367 const String* objName = nullptr);
368
369 private:
370 std::string str;
371 bool isString;
372 bool forceIdentifier;
373 };
374
375 /**
376 * @brief A component is a part of an ObjectIdentifier.
377 *
378 * It is used to either name a property or a field within a property. A
379 * component can be either a single entry, and array, or a map to other
380 * sub-fields.
381 */
382 class AppExport Component
383 {
384
385 private:
386 enum typeEnum
387 {
388 SIMPLE,
389 MAP,
390 ARRAY,
391 RANGE,
392 };
393
394 public:
395 // Constructors
396 FC_DEFAULT_CTORS(Component)
397 {
398 name = std::move(other.name);
399 type = other.type;
400 begin = other.begin;
401 end = other.end;
402 step = other.step;
403 return *this;
404 }
405
406 /**
407 * @brief Construct a Component part.
408 *
409 * @param[in] _name The name of the component.
410 * @param[in] _type The type: `SIMPLE`, `ARRAY`, `RANGE` or `MAP`.
411 * @param[in] begin The array index or beginning of a range, or `INT_MAX` for other types.
412 * @param[in] end The ending of a range, or `INT_MAX` for other types.
413 * @param[in] step The step of a range, or `1` for other types.
414 */
415 Component(const String& _name = String(),
416 typeEnum _type = SIMPLE,
417 int begin = std::numeric_limits<int>::max(),
418 int end = std::numeric_limits<int>::max(),
419 int step = 1); // explicit bombs
420
421 /**
422 * @brief Construct a Component with move semantics.
423 *
424 * @param[in,out] _name The name of the component.
425 * @param[in] _type The type: `SIMPLE`, `ARRAY`, `RANGE` or `MAP`.
426 * @param[in] begin The array index or beginning of a range, or `INT_MAX` for other types.
427 * @param[in] end The ending of a range, or `INT_MAX` for other types.
428 * @param[in] step The step of a range, or `1` for other types.
429 */
430 Component(String&& _name,
431 typeEnum _type = SIMPLE,
432 int begin = std::numeric_limits<int>::max(),
433 int end = std::numeric_limits<int>::max(),
434 int step = 1); // explicit bombs
435
436 /**
437 * @brief Create a simple component with the given name.
438 *
439 * @param[in] _component The name of the component.
440 * @return A new Component object.
441 */
442 static Component SimpleComponent(const char* _component);
443
444 /**
445 * @brief Create a simple component with the given name.
446 *
447 * @param[in] _component The name of the component.
448 * @return A new Component object.
449 */
450 static Component SimpleComponent(const String& _component);
451
452 /**
453 * @brief Create a simple component with move semantics.
454 *
455 * @param[in,out] _component The name of the component.
456 * @return A new Component object.
457 */
458 static Component SimpleComponent(String&& _component);
459
460 /**
461 * @brief Create an array component an index.
462 *
463 * @param[in] _index The index of the component.
464 * @return A new Component object.
465 */
466 static Component ArrayComponent(int _index);
467
468 /**
469 * @brief Create a range component with given begin and end.
470 *
471 * @param[in] _begin The begin index of the range.
472 * @param[in] _end The end index of the range.
473 * @param[in] _step The step of the range.
474 * @return A new Component object.
475 */
476 static Component RangeComponent(int _begin,
477 int _end = std::numeric_limits<int>::max(),
478 int _step = 1);
479
480 /**
481 * @brief Create a map component with a given key.
482 *
483 * @param[in] _key The key of the component.
484 * @return A new Component object.
485 */
486 static Component MapComponent(const String& _key);
487
488 /**
489 * @brief Create a map component with move semantics.
490 *
491 * Create a map component with a given key.
492 *
493 * @param[in] _key The key of the component.
494 * @return A new Component object.
495 */
496 static Component MapComponent(String&& _key);
497
498 // Type queries
499
500 /**
501 * @brief Check if the component is a simple component.
502 *
503 * @return true if the component is a simple component, false otherwise.
504 */
505 bool isSimple() const
506 {
507 return type == SIMPLE;
508 }
509
510 /**
511 * @brief Check if the component is a map component.
512 *
513 *@return true if the component is a map component, false otherwise.
514 */
515 bool isMap() const
516 {
517 return type == MAP;
518 }
519
520 /**
521 * @brief Check if the component is an array component.
522 *
523 * @return true if the component is an array component, false otherwise.
524 */
525 bool isArray() const
526 {
527 return type == ARRAY;
528 }
529
530 /**
531 * @brief Check if the component is a range component.
532 *
533 * @return true if the component is a range component, false otherwise.
534 */
535 bool isRange() const
536 {
537 return type == RANGE;
538 }
539
540 // Accessors
541
542 /**
543 * @brief Create a string representation of a component.
544 *
545 * The string is appended to the output stream.
546 *
547 * @param[in,out] ss The output stream to write to.
548 * @param[in] toPython If true, use Python quoting rules. Otherwise, use
549 * FreeCAD quoting rules.
550 */
551 void toString(std::ostream& ss, bool toPython = false) const;
552
553 /**
554 * @brief Get the name of the component.
555 *
556 * @return The name of the component.
557 */
558 const std::string& getName() const
559 {
560 return name.getString();
561 }
562
563 /**
564 * @brief Get the index of the component.
565 *
566 * @return The index of the component.
567 */
568 int getIndex() const
569 {
570 return begin;
571 }
572
573 /**
574 * @brief Get the index given a maximum count.
575 *
576 * This method interprets the member `begin` as a Python-style index
577 * that may be negative. Given the maximum count it returns a valid
578 * index.
579 *
580 * @param[in] count The number of elements in the target collection.
581 * @return The index in the range [0, count).
582 * @throws Base::IndexError If `begin` is out of bounds.
583 */
584 size_t getIndex(size_t count) const;
585
586 /**
587 * @brief Get the begin index of the component.
588 *
589 * @return The begin index of the component.
590 */
591 int getBegin() const
592 {
593 return begin;
594 }
595
596 /**
597 * @brief Get the end index of the component.
598 *
599 * @return The end index of the component.
600 */
601 int getEnd() const
602 {
603 return end;
604 }
605
606 /**
607 * @brief Get the step of the component.
608 *
609 * @return The step of the component.
610 */
611 int getStep() const
612 {
613 return step;
614 }
615
616 // Operators
617
618 /**
619 * @brief Test the component for equality.
620 *
621 * @param[in] other The object we want to compare to.
622 * @return true if the components are equal, false if not.
623 */
624 bool operator==(const Component& other) const;
625
626 /**
627 * @brief Lexicographical less-than comparison.
628 *
629 * @param[in] other The object we want to compare to.
630 * @return true if this component is lexicographically less than the other.
631 */
632 bool operator<(const Component& other) const;
633
634 /**
635 * @brief Get the value of the component given a Python object.
636 *
637 * @param[in] pyobj The Python object to get the value from.
638 * @return The value of the component.
639 */
640 Py::Object get(const Py::Object& pyobj) const;
641
642 /**
643 * @brief Set the value of the component given a Python object.
644 *
645 * @param[in,out] pyobj The Python object to set the value to.
646 * @param[in] value The value to set.
647 */
648 void set(Py::Object& pyobj, const Py::Object& value) const;
649
650 /**
651 * @brief Delete the value of the component given a Python object.
652 *
653 * @param[in,out] pyobj The Python object to delete the value from.
654 */
655 void del(Py::Object& pyobj) const;
656
657 private:
658 String name;
659 typeEnum type;
660 int begin;
661 int end;
662 int step;
663 friend class ObjectIdentifier;
664 };
665
666 /**
667 * @brief Create a simple component with the given name.
668 *
669 * @param[in] _component The name of the component.
670 * @return A new Component object.
671 */
672 static Component SimpleComponent(const char* _component)
673 {
674 return Component::SimpleComponent(_component);
675 }
676
677 /**
678 * @brief Create a simple component with the given name.
679 *
680 * @param[in] _component The name of the component.
681 * @return A new Component object.
682 */
683 static Component SimpleComponent(const String& _component)
684 {
685 return Component::SimpleComponent(_component);
686 }
687
688 /**
689 * @brief Create a simple component with move semantics.
690 *
691 * @param[in,out] _component The name of the component.
692 * @return A new Component object.
693 */
694 static Component SimpleComponent(String&& _component)
695 {
696 return Component::SimpleComponent(std::move(_component));
697 }
698
699 /**
700 * @brief Create a simple component with the given name.
701 *
702 * @param[in] _component The name of the component.
703 * @return A new Component object.
704 */
705 static Component SimpleComponent(const std::string _component)
706 {
707 return Component::SimpleComponent(_component.c_str());
708 }
709
710 /**
711 * @brief Create an array component with the given index.
712 *
713 * @param[in] _index The index of the component.
714 * @return A new Component object.
715 */
716 static Component ArrayComponent(int _index)
717 {
718 return Component::ArrayComponent(_index);
719 }
720
721 /**
722 * @brief Create a range component with given begin and end.
723 *
724 * @param[in] _begin The begin index of the range.
725 * @param[in] _end The end index of the range.
726 * @param[in] _step The step of the range.
727 * @return A new Component object.
728 */
729 static Component RangeComponent(int _begin,
730 int _end = std::numeric_limits<int>::max(),
731 int _step = 1)
732 {
733 return Component::RangeComponent(_begin, _end, _step);
734 }
735
736 /**
737 * @brief Create a map component with a given key.
738 *
739 * @param[in] _key The key of the component.
740 * @return A new Component object.
741 */
742 static Component MapComponent(const String& _key)
743 {
744 return Component::MapComponent(_key);
745 }
746
747 /**
748 * @brief Create a map component with move semantics.
749 *
750 * @param[in,out] _key The key of the component.
751 * @return A new Component object.
752 */
753 static Component MapComponent(String&& _key)
754 {
755 return Component::MapComponent(_key);
756 }
757
758 /**
759 * @brief Construct an ObjectIdentifier object.
760 *
761 * Construct an ObjectIdentifier object given an owner and a single-value
762 * property, possibly with an array index.
763 *
764 * @param[in] _owner The owner of the property.
765 * @param[in] property The name of the property.
766 * @param[in] index The index into the array.
767 * @throw Base::RuntimeError if the owner is not a document object.
768 */
769 explicit ObjectIdentifier(const App::PropertyContainer* _owner = nullptr,
770 const std::string& property = std::string(),
771 int index = std::numeric_limits<int>::max());
772
773 /**
774 *@brief Construct an ObjectIdentifier object.
775 *
776 * @param[in] _owner The owner of the property.
777 * @param[in] localProperty If true, the property is a local property.
778 * @throw Base::RuntimeError if the owner is not a document object.
779 */
780 ObjectIdentifier(const App::PropertyContainer* _owner, bool localProperty);
781
782 /**
783 * @brief Construct an ObjectIdentifier object given a property.
784 *
785 * The property is assumed to be single-valued but may have an array index.
786 *
787 * @param[in] prop The property to construct object identifier for.
788 * @param[in] index The index into the array.
789 *
790 * @throw Base::RuntimeError if the owner is not a document object or if
791 * the property does not have a name.
792 */
793 ObjectIdentifier(const App::Property& prop,
794 int index = std::numeric_limits<int>::max()); // explicit bombs
795
796 FC_DEFAULT_CTORS(ObjectIdentifier)
797 {
798 owner = other.owner;
799 documentName = std::move(other.documentName);
800 documentObjectName = std::move(other.documentObjectName);
801 subObjectName = std::move(other.subObjectName);
802 shadowSub = std::move(other.shadowSub);
803 components = std::move(other.components);
804 documentNameSet = other.documentNameSet;
805 documentObjectNameSet = other.documentObjectNameSet;
806 localProperty = other.localProperty;
807 _cache = std::move(other._cache);
808 _hash = other._hash;
809 return *this;
810 }
811
812 /// Destruct an ObjectIdentifier object.
813 virtual ~ObjectIdentifier() = default;
814
815 /**
816 * @brief Get the owner of this object identifier.
817 *
818 * @return The owner of this object identifier.
819 */
820 App::DocumentObject* getOwner() const
821 {
822 return owner;
823 }
824
825 /**
826 * @brief Add a component to this object identifier.
827 *
828 * @param[in] c The component to add.
829 */
830 void addComponent(const Component& c)
831 {
832 components.push_back(c);
833 _cache.clear();
834 }
835
836 /**
837 * @brief Add a component to this object identifier with move semantics.
838 *
839 * @param[in] c The component to add.
840 */
841 void addComponent(Component&& c)
842 {
843 components.push_back(std::move(c));
844 _cache.clear();
845 }
846
847 /**
848 * @brief Get the name of the property.
849 * @return The name of the property.
850 */
851 std::string getPropertyName() const;
852
853 /**
854 * @brief Add components to this object identifier.
855 *
856 * @tparam C A container type that supports iterators.
857 * @param[in] cs The components to add.
858 */
859 template<typename C>
860 void addComponents(const C& cs)
861 {
862 components.insert(components.end(), cs.begin(), cs.end());
863 }
864
865 /**
866 * @brief Get a component given an index.
867 *
868 * @param[in] i: The index of the component.
869 * @param[out] idx: Optional return of an adjusted component index.
870 * @return A component.
871 */
872 const Component& getPropertyComponent(int i, int* idx = nullptr) const;
873
874 /**
875 * @brief Set a component at an index with move semantics.
876 *
877 * @param[in] idx: The index to store the component.
878 * @param[in,out] comp: The component to set.
879 */
880 void setComponent(int idx, Component&& comp);
881
882 /**
883 * @brief Set a component at an index.
884 *
885 * @param[in] idx: The index to store the component.
886 * @param[in] comp: The component to set.
887 */
888 void setComponent(int idx, const Component& comp);
889
890 /**
891 * @brief Get the property components of this object identifier.
892 *
893 * @return A vector of components of properties.
894 */
895 std::vector<Component> getPropertyComponents() const;
896
897 /**
898 * @brief Get the components of this object identifier.
899 *
900 * @return A vector of components.
901 */
902 const std::vector<Component>& getComponents() const
903 {
904 return components;
905 }
906
907 /**
908 * @brief Get a string representation of the subpath.
909 *
910 * @param[in] toPython If true, use Python quoting rules. Otherwise, use
911 * FreeCAD quoting rules.
912 *
913 * @return The string representation of the subpath.
914 */
915 std::string getSubPathStr(bool toPython = false) const;
916
917 /**
918 * @brief Return the number of components.
919 *
920 * @return The number of components in this identifier.
921 */
922 int numComponents() const;
923
924 /**
925 * @brief Compute the number of sub components.
926 *
927 * Compute the number of sub components, meaning that this excludes the property.
928 *
929 * @return Number of components.
930 */
931 int numSubComponents() const;
932
933 /**
934 * @brief Create a string representation of this object identifier.
935 *
936 * An identifier is written as
937 * `document#documentobject.property.subproperty1...subpropertyN`. The
938 * string `document#` may be dropped; in that case it is assumed to be
939 * within owner's document. If `documentobject` is dropped, the property
940 * is assumed to be owned by the owner specified in the object identifiers
941 * constructor.
942 *
943 * @return A string representation of the object identifier.
944 */
945 const std::string& toString() const;
946
947 /**
948 * @brief Create a persistent string representation of this object identifier.
949 *
950 * The persistent string representation is used where the object identifier
951 * is required to survive import and export.
952 *
953 * @see toString()
954 *
955 * @return A persistent string representation of the object identifier.
956 */
957 std::string toPersistentString() const;
958
959 /**
960 * @brief Create an escapedstring representation of this object identifier.
961 *
962 * The escaped string representation is suitable for being embedded in a
963 * Python command.
964 *
965 * @return The escaped string representation.
966 */
967 std::string toEscapedString() const;
968
969 /**
970 * @brief Whether the property of the object identifier is touched.
971 *
972 * This method is used to determine if the property that this object
973 * identifier represents is touched.
974 *
975 * @return true if the property of the object identifier is touched, false
976 * otherwise.
977 */
978 bool isTouched() const;
979
980 /**
981 * @brief Get the property this object identifier represents.
982 *
983 * @param[out] ptype Optional return of the property type.
984 *
985 * @return A pointer to property if it is uniquely defined, or `nullptr` otherwise.
986 */
987 App::Property* getProperty(int* ptype = nullptr) const;
988
989 /**
990 * @brief Create a canonical representation of the object identifier.
991 *
992 * The main work is actually done by the property's virtual
993 * Property::canonicalPath() method that is invoked by this call.
994 *
995 * @return A new object identifier.
996 */
997 App::ObjectIdentifier canonicalPath() const;
998
999 // Document-centric functions
1000
1001 /**
1002 * @brief Set the document name for this object identifier.
1003 *
1004 * If @p force is true, the document name will always be included in the
1005 * string representation.
1006 *
1007 * @param[in,out] name The name of the document.
1008 * @param[in] force Force the name to be set.
1009 */
1010 void setDocumentName(String&& name, bool force = false);
1011
1012 /**
1013 * @brief Get the document name from this object identifier
1014 *
1015 * @return The document name as a String object.
1016 */
1017 String getDocumentName() const;
1018
1019 /**
1020 * @brief Set the document object name of this object identifier.
1021 *
1022 * If force is true, the document object will not be resolved dynamically
1023 * from the object identifier's components, but used as given by this
1024 * method.
1025 *
1026 * This function uses move semantics on @p name and @p subname.
1027 *
1028 * @param[in,out] name The name of document object.
1029 * @param[in] force Force the name to be set.
1030 * @param[in,out] subname The name of the subobject.
1031 * @param[in] checkImport If true, check for import.
1032 */
1033 void setDocumentObjectName(String&& name,
1034 bool force = false,
1035 String&& subname = String(),
1036 bool checkImport = false);
1037
1038 /**
1039 * @brief Set the document object name of this object identifier.
1040 *
1041 * If force is true, the document object will not be resolved dynamically
1042 * from the object identifier's components, but used as given by this method.
1043 *
1044 * @param[in] obj The document object which name is set in the object identifier.
1045 * @param[in] force Force the name to be set.
1046 * @param[in,out] subname The name of the subobject using move semantics.
1047 * @param[in] checkImport If true, check for import.
1048 */
1049 void setDocumentObjectName(const App::DocumentObject* obj,
1050 bool force = false,
1051 String&& subname = String(),
1052 bool checkImport = false);
1053
1054 /**
1055 * @brief Whether the object identifier has a document object name.
1056 *
1057 * This method checks if the object identifier has a document object name
1058 * and optionally checks if the name was forced.
1059 *
1060 * @param[in] forced If true, check if the name was forced.
1061 * @return true if the object identifier has a document object name, false
1062 * otherwise.
1063 */
1064 bool hasDocumentObjectName(bool forced = false) const;
1065
1066 /**
1067 * @brief Test whether the property is local.
1068 *
1069 * This method checks whether the property this object identifier
1070 * represents is a local property.
1071 *
1072 * @return true if the property is local, false otherwise.
1073 */
1074 bool isLocalProperty() const
1075 {
1076 return localProperty;
1077 }
1078
1079 /**
1080 * @brief Get the document object name.
1081 *
1082 * @return String with name of document object as resolved by the object
1083 * identifier.
1084 */
1085 String getDocumentObjectName() const;
1086
1087 /**
1088 * @brief Get the subobject name.
1089 *
1090 * @param[in] newStyle If true, use the new style of subobject name.
1091 * @return The subobject name.
1092 */
1093 const std::string& getSubObjectName(bool newStyle) const;
1094
1095 /**
1096 * @brief Get the subobject name.
1097 *
1098 * @return The subobject name.
1099 */
1100 const std::string& getSubObjectName() const;
1101
1102 /**
1103 * @brief A type for a map of subobject names.
1104 *
1105 * The map is a map with keys of a pair of document object and a
1106 * subobject name to the subobject name for the imported object.
1107 */
1108 using SubNameMap = std::map<std::pair<App::DocumentObject*, std::string>, std::string>;
1109
1110 /**
1111 * @brief Import subnames from a map of subnames.
1112 *
1113 * Given a map of subnames from the linked subobject to the imported
1114 * subobject, this method will update the subnames in the object
1115 * identifier.
1116 *
1117 * @param[in] subNameMap The map of subnames to import.
1118 */
1119 void importSubNames(const SubNameMap& subNameMap);
1120
1121 /**
1122 * @brief Update the label reference.
1123 *
1124 * This method updates the label reference of the object identifier.
1125 *
1126 * @param[in] obj The document object that owns the label.
1127 * @param[in] ref The old label reference.
1128 * @param[in] newLabel The new label to set.
1129 */
1130 bool updateLabelReference(const App::DocumentObject* obj,
1131 const std::string& ref,
1132 const char* newLabel);
1133
1134 /**
1135 * @brief Relabel the document name.
1136 *
1137 * This method relabels the document name of the object identifier as part
1138 * of a visit of the ExpressionVisitor.
1139 *
1140 * @param[in,out] v The expression visitor.
1141 * @param[in] oldLabel The old label to relabel.
1142 * @param[in] newLabel The new label to set.
1143 */
1144 bool relabeledDocument(ExpressionVisitor& v,
1145 const std::string& oldLabel,
1146 const std::string& newLabel);
1147
1148 /**
1149 * @brief A type for storing dependencies of an ObjectIdentifier.
1150 *
1151 * It is a map from document object to a set of property names. An object
1152 * identifier may references multiple objects using syntax like
1153 * `%Part.%Group[0].Width`.
1154 *
1155 * Additionally, we use a set of strings instead of set of Properties,
1156 * because the property may not exist at the time this ObjectIdentifier is
1157 * constructed.
1158 */
1159 using Dependencies = std::map<App::DocumentObject*, std::set<std::string>>;
1160
1161 /**
1162 * @brief Get the dependencies of this object identifier.
1163 *
1164 * @param[in] needProps: Whether we need property dependencies.
1165 * @param[out] labels: Optional return of any label references.
1166 *
1167 * In case of multi-object references, like `%Part.%Group[0].Width`, if no
1168 * property dependency is required, then this function will only return the
1169 * first referred object dependency. Or else, all object and property
1170 * dependencies will be returned.
1171 */
1172 Dependencies getDep(bool needProps, std::vector<std::string>* labels = nullptr) const;
1173
1174 /**
1175 * @brief Get the dependencies of this object identifier.
1176 *
1177 * @param[in,out] deps: Returns the dependencies.
1178 * @param[in] needProps: Whether need property dependencies.
1179 * @param[out] labels: Optional return of any label references.
1180 *
1181 * @see ObjectIdentifier::getDep(bool,std::vector<std::string>*) const
1182 */
1183 void
1184 getDep(Dependencies& deps, bool needProps, std::vector<std::string>* labels = nullptr) const;
1185
1186 /**
1187 * @brief Returns all label references in the object identifier.
1188 *
1189 * @param[in,out] labels The container in which the labels are returned.
1190 */
1191 void getDepLabels(std::vector<std::string>& labels) const;
1192
1193 /**
1194 * @brief Find a document with the given name.
1195 *
1196 * This method will search for a document with the given name. If @p name
1197 * is not provided, the document of the object identifier is returned. If
1198 * @p ambiguous is not `nullptr`, the method will return whether the found
1199 * document is unique.
1200 *
1201 * @param[in] name The name of the document.
1202 * @param[out] ambiguous If true, the document is not uniquely defined by name.
1203 *
1204 * @return The found document, or `nullptr` if it is not found or not
1205 * uniquely defined by name.
1206 */
1207 App::Document* getDocument(String name = String(), bool* ambiguous = nullptr) const;
1208
1209 /**
1210 * @brief Get the document object for the object identifier.
1211 * @return The document object, or `nullptr` if not found or uniquely defined.
1212 */
1213 App::DocumentObject* getDocumentObject() const;
1214
1215 /**
1216 * @brief Get the parts of the object identifier as a string list.
1217 *
1218 * The parts include the document name, the document object name, the
1219 * subobject name, and the various components.
1220 *
1221 * @return The list of strings representing the various components.
1222 */
1223 std::vector<std::string> getStringList() const;
1224
1225 /**
1226 * @brief Construct the simplest possible object identifier relative to another.
1227 *
1228 * @param[in] other The other object identifier.
1229 * @return A new simplified object identifier.
1230 */
1231 App::ObjectIdentifier relativeTo(const App::ObjectIdentifier& other) const;
1232
1233 /**
1234 * @brief Replace an object in the object identifier.
1235 *
1236 * This method replaces an object in the object identifier with another
1237 * object. It is used to create a new object identifier when the document
1238 * object is replaced.
1239 *
1240 * @param[out] res The resulting object identifier.
1241 * @param[in] parent The parent document object.
1242 * @param[in] oldObj The old document object to replace.
1243 * @param[in] newObj The new document object to replace with.
1244 *
1245 * @return true if the object was replaced, false otherwise.
1246 */
1247 bool replaceObject(ObjectIdentifier& res,
1248 const App::DocumentObject* parent,
1249 App::DocumentObject* oldObj,
1250 App::DocumentObject* newObj) const;
1251
1252 // Operators
1253
1254 /**
1255 * @brief Operator to add a component to the object identifier.
1256 *
1257 * @param[in] value Component object
1258 *
1259 * @return A reference to itself.
1260 */
1261 App::ObjectIdentifier& operator<<(const Component& value);
1262
1263 /**
1264 * @brief Operator to add a component to the object identifier.
1265 *
1266 * This method uses move semantics.
1267 *
1268 * @param[in,out] value Component object
1269 *
1270 * @return A reference to itself.
1271 */
1272 App::ObjectIdentifier& operator<<(Component&& value);
1273
1274 /**
1275 * @brief Compare object identifiers for equality.
1276 *
1277 * @param[in] other The other object identifier.
1278 *
1279 * @return true if they are equal, false otherwise.
1280 */
1281 bool operator==(const ObjectIdentifier& other) const;
1282
1283 /**
1284 * @brief Compare object identifiers for inequality.
1285 *
1286 * @param[in] other The other object identifier.
1287 *
1288 * @return true if they differ from each other, false otherwise.
1289 */
1290 bool operator!=(const ObjectIdentifier& other) const;
1291
1292 /**
1293 * @brief Lexicographical less-than comparison.
1294 *
1295 * @param[in] other The other object identifier.
1296 *
1297 * @return true if this object is less than the other.
1298 */
1299 bool operator<(const ObjectIdentifier& other) const;
1300
1301 // Getter
1302
1303 /**
1304 * @brief Get the value of the property or field pointed to by this object
1305 * identifier.
1306 *
1307 * All type of objects are supported. Some types are casted to FC native
1308 * type, including: Int, Float, %String, Unicode %String, and Quantities. Others
1309 * are just kept as Python object wrapped by App::any.
1310 *
1311 * @param[in] pathValue: if true, calls the property's getPathValue(), which is
1312 * necessary for quantities to work.
1313 * @param[in] isPseudoProperty: if not `nullptr`, set to true if the property is a
1314 * pseudo property.
1315 *
1316 * @return The value of the property or field.
1317 */
1318 App::any getValue(bool pathValue = false, bool* isPseudoProperty = nullptr) const;
1319
1320 /**
1321 * @brief Get the value of the property or field pointed to by this object
1322 * identifier.
1323 *
1324 * @see ObjectIdentifier::getValue(). In contrast, this method
1325 * returns a Python %object.
1326 *
1327 * @param[in] pathValue: if true, calls the property's getPyPathValue(), which is
1328 * necessary for quantities to work.
1329 * @param[in] isPseudoProperty: if not `nullptr`, set to true if the property is a
1330 * pseudo property.
1331 *
1332 * @return The value of the property or field.
1333 */
1334 Py::Object getPyValue(bool pathValue = false, bool* isPseudoProperty = nullptr) const;
1335
1336 // Setter: is const because it does not alter the object state,
1337 // but does have an aiding effect.
1338
1339 /**
1340 * @brief Set the value of a property or field pointed to by this object identifier.
1341 *
1342 * This method uses Python to do the actual work and a limited set of types
1343 * that can be in the `App::any` variable are supported: `Base::Quantity`,
1344 * `double`, `char*`, `const char*`, `int`, `unsigned int`, `short`, `unsigned short`,
1345 * `char`, and `unsigned char`.
1346 *
1347 * @param[in] value The value to set.
1348 */
1349 void setValue(const App::any& value) const;
1350
1351 // Static functions
1352
1353 /**
1354 * @brief Parse a string to create an object identifier.
1355 *
1356 * @param[in] docObj Document object that will own this object identifier.
1357 * @param[in] str String to parse
1358 *
1359 * @return A new object identifier.
1360 * @throw Base::RuntimeError if the string is invalid.
1361 */
1362 static ObjectIdentifier parse(const App::DocumentObject* docObj, const std::string& str);
1363
1364 /**
1365 * @brief Acquire the error string after name resolution of the object identifier.
1366 *
1367 * @return The error string.
1368 */
1369 std::string resolveErrorString() const;
1370
1371 /**
1372 * @brief Adjust the links of the object identifier.
1373 *
1374 * This method will adjust the links of the object identifier and is part
1375 * of a visit from an expression visitor. This is typically necessary when
1376 * a link object is moved. The @p inList is a list of dependencies of the
1377 * object for which the visitor was started.
1378 *
1379 * @param[in,out] v The expression visitor.
1380 * @param[in] inList The list of dependencies.
1381 *
1382 * @return true if the links were adjusted, false otherwise.
1383 */
1384 bool adjustLinks(ExpressionVisitor& v, const std::set<App::DocumentObject*>& inList);
1385
1386 /**
1387 * @brief Update the element reference of the object identifier.
1388 *
1389 * This method is part of the expression visitor and is used to update the
1390 * element reference of object identifiers in case of a geometry element
1391 * reference change due to geometry model changes.
1392 *
1393 * @see PropertyLinkBase::_updateElementReference()
1394 *
1395 * @param[in,out] v The expression visitor.
1396 * @param[in,out] feature If given, then only update element references for this
1397 * feature, otherwise update geometry element references.
1398 * @param[in] reverse If true, use the old style before the new style. If
1399 * false the other way around.
1400 *
1401 * @return true if the element reference was updated, false otherwise.
1402 */
1403 bool updateElementReference(ExpressionVisitor& v,
1404 App::DocumentObject* feature = nullptr,
1405 bool reverse = false);
1406
1407
1408 /// Resolve ambiguity in the object identifier.
1409 void resolveAmbiguity();
1410
1411 /**
1412 * @brief Verify the object identifier.
1413 *
1414 * This method will verify the object identifier against property @p prop.
1415 *
1416 * @param[in] prop The property to verify against.
1417 * @param[in] silent If true, do not throw an exception.
1418 *
1419 * @return true if verification succeeded, false otherwise.
1420 * @throw Base::ValueError if there is an invalid property path.
1421 */
1422 bool verify(const App::Property& prop, bool silent = false) const;
1423
1424 /**
1425 * @brief Compute the hash of the object identifier.
1426 *
1427 * This method computes the hash of the object identifier. The hash is
1428 * computed from the string representation of the object identifier.
1429 *
1430 * @return The hash of the object identifier.
1431 */
1432 std::size_t hash() const;
1433
1434 protected:
1435 /**
1436 * @brief A structure to hold the results of resolving an ObjectIdentifier.
1437 *
1438 * This structure is used to hold the results of resolving an object
1439 * identifier.
1440 */
1441 struct ResolveResults
1442 {
1443
1444 /**
1445 * @brief Construct and initialize a ResolveResults object, given an ObjectIdentifier instance.
1446 *
1447 * The constructor will invoke the ObjectIdentifier's resolve() method
1448 * to initialize the object's data.
1449 */
1450 explicit ResolveResults(const ObjectIdentifier& oi);
1451
1452 int propertyIndex {0};
1453 App::Document* resolvedDocument {nullptr};
1454 String resolvedDocumentName;
1455 App::DocumentObject* resolvedDocumentObject {nullptr};
1456 String resolvedDocumentObjectName;
1457 String subObjectName;
1458 App::DocumentObject* resolvedSubObject {nullptr};
1459 App::Property* resolvedProperty {nullptr};
1460 std::string propertyName;
1461 int propertyType {0};
1462 std::bitset<32> flags;
1463
1464 /**
1465 * @brief Acquire the error string after name resolution.
1466 *
1467 * @return The error string.
1468 */
1469 std::string resolveErrorString() const;
1470
1471 /**
1472 * @brief Obtain the property of the object identifier for this ResolveResults.
1473 *
1474 * @param[in] oi The object identifier.
1475 */
1476 void getProperty(const ObjectIdentifier& oi);
1477 };
1478
1479
1480 /// Provide access to the internals of ObjectIdentifier.
1481 friend struct ResolveResults;
1482
1483 /**
1484 * @brief Resolve the property of the object identifier.
1485 *
1486 * This method will resolve the property of the object identifier and
1487 * return the property type.
1488 *
1489 * @param[in] obj The document object that owns the property.
1490 * @param[in] propertyName The name of the property to resolve.
1491 * @param[out] sobj The sub object.
1492 * @param[out] ptype The type of the property.
1493 *
1494 * @return The property or `nullptr` if not resolved.
1495 */
1496 App::Property* resolveProperty(const App::DocumentObject* obj,
1497 const char* propertyName,
1498 App::DocumentObject*& sobj,
1499 int& ptype) const;
1500
1501 /**
1502 * @brief Get sub field part of a property as a string.
1503 *
1504 * @param[in,out] ss The output stream to write to.
1505 * @param[in] result The resolve results.
1506 * @param[in] toPython If true, use Python quoting rules. Otherwise, use
1507 * FreeCAD quoting rules.
1508 */
1509 void getSubPathStr(std::ostream& ss, const ResolveResults& result, bool toPython = false) const;
1510
1511 /**
1512 * @brief Access the value of the property or field pointed to by this
1513 * object.
1514 *
1515 * This method can either set or get a value of the property or field of
1516 * the object identifier given a ResolveResults. If @p deps is not
1517 * `nullptr`, the dependencies are updated.
1518 *
1519 * @param[in] rs The resolve results.
1520 * @param[in] value The value to set.
1521 * @param[in,out] deps The dependencies to set.
1522 */
1523 Py::Object access(const ResolveResults& rs,
1524 const Py::Object* value = nullptr,
1525 Dependencies* deps = nullptr) const;
1526
1527 /**
1528 * @brief Resolve the object identifier to a concrete document, document
1529 * object, and property.
1530 *
1531 * This method is a helper method that fills out data in the given
1532 * ResolveResults object.
1533 *
1534 * @param[in,out] results The ResolveResults object to fill out.
1535 */
1536 void resolve(ResolveResults& results) const;
1537
1538 /**
1539 * @brief Resolve ambiguity in the object identifier.
1540 *
1541 * Given a ResolveResults, the ambiguities in the object identifier are resolved.
1542 *
1543 * @param[in] results The result of the resolve method.
1544 */
1545 void resolveAmbiguity(const ResolveResults& results);
1546
1547 /**
1548 * @brief Returns all label references in the object identifier.
1549 *
1550 * @param[in] result The result of the resolve method.
1551 * @param[in,out] labels The container in which the labels are returned.
1552 */
1553 void getDepLabels(const ResolveResults& result, std::vector<std::string>& labels) const;
1554
1555 /// The owner of the object identifier.
1556 App::DocumentObject* owner;
1557 /// The document name that this object identifier refers to.
1558 String documentName;
1559 /// The document object name that this object identifier refers to.
1560 String documentObjectName;
1561 /// The sub object name that this object identifier refers to.
1562 String subObjectName;
1563 /**
1564 * @brief The shadow sub-element names that this object identifier refers to.
1565 *
1566 * This contains both the new and old style sub-element names.
1567 */
1568 ElementNamePair shadowSub;
1569 /// The components of the object identifier.
1570 std::vector<Component> components;
1571 /// Whether a document name is forced set.
1572 bool documentNameSet;
1573 /// Whether a document object name is forced set.
1574 bool documentObjectNameSet;
1575 /// Whether the property is local.
1576 bool localProperty;
1577
1578 private:
1579 static App::DocumentObject*
1580 getDocumentObject(const App::Document* doc, const String& name, std::bitset<32>& flags);
1581
1582 private:
1583 std::string _cache; // Cached string represstation of this identifier
1584 std::size_t _hash; // Cached hash of this string
1585 };
1586
1587 /**
1588 * @brief Hash function for ObjectIdentifier.
1589 *
1590 * @param[in] path The object identifier to hash.
1591 *
1592 * @return The hash value of the object identifier.
1593 */
1594 inline std::size_t hash_value(const App::ObjectIdentifier& path)
1595 {
1596 return path.hash();
1597 }
1598
1599 //@{
1600 /**
1601 * @brief Helper function to convert Python object to App::any
1602 *
1603 * @warning Must hold Python global interpreter lock before calling these
1604 * functions
1605 *
1606 * @param[in] pyobj The Python object to convert.
1607 * @param[in] check If true, check if the object is convertible to App::any,
1608 * otherwise just return as is.
1609 *
1610 * @return The converted App::any object.
1611 * @throw Base::ValueError if the object is not convertible to a unicode string.
1612 */
1613 App::any AppExport pyObjectToAny(Py::Object pyobj, bool check = true);
1614
1615 /**
1616 * @brief Helper function to convert Python object from App::any
1617 *
1618 * @warning Must hold Python global interpreter lock before calling these
1619 * functions
1620 *
1621 * @param[in] value The App::any object to convert.
1622 * @return The converted Python object.
1623 * @throw Base::ExpressionError if the value has an unknown type.
1624 */
1625 Py::Object AppExport pyObjectFromAny(const App::any& value);
1626 //@}
1627 } // namespace App
1628
1629 namespace std
1630 {
1631
1632 template<>
1633 struct hash<App::ObjectIdentifier>
1634 {
1635 using argument_type = App::ObjectIdentifier;
1636 using result_type = std::size_t;
1637 inline result_type operator()(argument_type const& s) const
1638 {
1639 return s.hash();
1640 }
1641 };
1642 } // namespace std
1643
1644 #endif