HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Element.h
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 #ifndef MATERIALX_ELEMENT_H
7 #define MATERIALX_ELEMENT_H
8 
9 /// @file
10 /// Base and generic element classes
11 
12 #include <MaterialXCore/Export.h>
13 
15 #include <MaterialXCore/Util.h>
16 #include <MaterialXCore/Value.h>
17 
19 
20 class Element;
21 class TypedElement;
22 class ValueElement;
23 class Token;
24 class CommentElement;
25 class NewlineElement;
26 class GenericElement;
27 class StringResolver;
28 class Document;
29 
30 /// A shared pointer to an Element
31 using ElementPtr = shared_ptr<Element>;
32 /// A shared pointer to a const Element
33 using ConstElementPtr = shared_ptr<const Element>;
34 
35 /// A shared pointer to a TypedElement
36 using TypedElementPtr = shared_ptr<TypedElement>;
37 /// A shared pointer to a const TypedElement
38 using ConstTypedElementPtr = shared_ptr<const TypedElement>;
39 
40 /// A shared pointer to a ValueElement
41 using ValueElementPtr = shared_ptr<ValueElement>;
42 /// A shared pointer to a const ValueElement
43 using ConstValueElementPtr = shared_ptr<const ValueElement>;
44 
45 /// A shared pointer to a Token
46 using TokenPtr = shared_ptr<Token>;
47 /// A shared pointer to a const Token
48 using ConstTokenPtr = shared_ptr<const Token>;
49 
50 /// A shared pointer to a CommentElement
51 using CommentElementPtr = shared_ptr<CommentElement>;
52 /// A shared pointer to a const CommentElement
53 using ConstCommentElementPtr = shared_ptr<const CommentElement>;
54 
55 /// A shared pointer to a NewlineElement
56 using NewlineElementPtr = shared_ptr<NewlineElement>;
57 /// A shared pointer to a const NewlineElement
58 using ConstNewlineElementPtr = shared_ptr<const NewlineElement>;
59 
60 /// A shared pointer to a GenericElement
61 using GenericElementPtr = shared_ptr<GenericElement>;
62 /// A shared pointer to a const GenericElement
63 using ConstGenericElementPtr = shared_ptr<const GenericElement>;
64 
65 /// A shared pointer to a StringResolver
66 using StringResolverPtr = shared_ptr<StringResolver>;
67 
68 /// A vector of elements.
69 using ElementVec = vector<ElementPtr>;
70 
71 /// A hash map from strings to elements
72 using ElementMap = std::unordered_map<string, ElementPtr>;
73 
74 /// A standard function taking an ElementPtr and returning a boolean.
75 using ElementPredicate = std::function<bool(ConstElementPtr)>;
76 
78 
79 /// @class Element
80 /// The base class for MaterialX elements.
81 ///
82 /// An Element is a named object within a Document, which may possess any
83 /// number of child elements and attributes.
84 class MX_CORE_API Element : public std::enable_shared_from_this<Element>
85 {
86  protected:
87  Element(ElementPtr parent, const string& category, const string& name) :
88  _category(category),
89  _name(name),
90  _parent(parent),
91  _root(parent ? parent->getRoot() : nullptr)
92  {
93  }
94 
95  public:
96  virtual ~Element() { }
97  Element(const Element&) = delete;
98  Element& operator=(const Element&) = delete;
99 
100  protected:
101  using DocumentPtr = shared_ptr<Document>;
102  using ConstDocumentPtr = shared_ptr<const Document>;
103 
104  template <class T> friend class ElementRegistry;
105 
106  public:
107  /// Return true if the given element tree, including all descendants,
108  /// is identical to this one.
109  bool operator==(const Element& rhs) const;
110 
111  /// Return true if the given element tree, including all descendants,
112  /// differs from this one.
113  bool operator!=(const Element& rhs) const;
114 
115  /// @name Category
116  /// @{
117 
118  /// Set the element's category string.
119  void setCategory(const string& category)
120  {
121  _category = category;
122  }
123 
124  /// Return the element's category string. The category of a MaterialX
125  /// element represents its role within the document, with common examples
126  /// being "material", "nodegraph", and "image".
127  const string& getCategory() const
128  {
129  return _category;
130  }
131 
132  /// @}
133  /// @name Name
134  /// @{
135 
136  /// Set the element's name string. The name of a MaterialX element must be
137  /// unique among all elements at the same scope.
138  /// @throws Exception if an element at the same scope already possesses the
139  /// given name.
140  void setName(const string& name);
141 
142  /// Return the element's name string.
143  const string& getName() const
144  {
145  return _name;
146  }
147 
148  /// Return the element's hierarchical name path, relative to the root
149  /// document. The name of each ancestor will be prepended in turn,
150  /// separated by forward slashes.
151  /// @param relativeTo If a valid ancestor element is specified, then
152  /// the returned path will be relative to this ancestor.
153  string getNamePath(ConstElementPtr relativeTo = nullptr) const;
154 
155  /// Return the element specified by the given hierarchical name path,
156  /// relative to the current element. If the name path is empty then the
157  /// current element is returned. If no element is found at the given path,
158  /// then an empty shared pointer is returned.
159  /// @param namePath The relative name path of the specified element.
160  ElementPtr getDescendant(const string& namePath) const;
161 
162  /// @}
163  /// @name File Prefix
164  /// @{
165 
166  /// Set the element's file prefix string.
167  void setFilePrefix(const string& prefix)
168  {
169  setAttribute(FILE_PREFIX_ATTRIBUTE, prefix);
170  }
171 
172  /// Return true if the given element has a file prefix string.
173  bool hasFilePrefix() const
174  {
175  return hasAttribute(FILE_PREFIX_ATTRIBUTE);
176  }
177 
178  /// Return the element's file prefix string.
179  const string& getFilePrefix() const
180  {
181  return getAttribute(FILE_PREFIX_ATTRIBUTE);
182  }
183 
184  /// Return the file prefix string that is active at the scope of this
185  /// element, taking all ancestor elements into account.
186  const string& getActiveFilePrefix() const
187  {
188  for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
189  {
190  if (elem->hasFilePrefix())
191  {
192  return elem->getFilePrefix();
193  }
194  }
195  return EMPTY_STRING;
196  }
197 
198  /// @}
199  /// @name Geom Prefix
200  /// @{
201 
202  /// Set the element's geom prefix string.
203  void setGeomPrefix(const string& prefix)
204  {
205  setAttribute(GEOM_PREFIX_ATTRIBUTE, prefix);
206  }
207 
208  /// Return true if the given element has a geom prefix string.
209  bool hasGeomPrefix() const
210  {
211  return hasAttribute(GEOM_PREFIX_ATTRIBUTE);
212  }
213 
214  /// Return the element's geom prefix string.
215  const string& getGeomPrefix() const
216  {
217  return getAttribute(GEOM_PREFIX_ATTRIBUTE);
218  }
219 
220  /// Return the geom prefix string that is active at the scope of this
221  /// element, taking all ancestor elements into account.
222  const string& getActiveGeomPrefix() const
223  {
224  for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
225  {
226  if (elem->hasGeomPrefix())
227  {
228  return elem->getGeomPrefix();
229  }
230  }
231  return EMPTY_STRING;
232  }
233 
234  /// @}
235  /// @name Color Space
236  /// @{
237 
238  /// Set the element's color space string.
239  void setColorSpace(const string& colorSpace)
240  {
241  setAttribute(COLOR_SPACE_ATTRIBUTE, colorSpace);
242  }
243 
244  /// Return true if the given element has a color space string.
245  bool hasColorSpace() const
246  {
247  return hasAttribute(COLOR_SPACE_ATTRIBUTE);
248  }
249 
250  /// Return the element's color space string.
251  const string& getColorSpace() const
252  {
253  return getAttribute(COLOR_SPACE_ATTRIBUTE);
254  }
255 
256  /// Return the color space string that is active at the scope of this
257  /// element, taking all ancestor elements into account.
258  const string& getActiveColorSpace() const
259  {
260  for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
261  {
262  if (elem->hasColorSpace())
263  {
264  return elem->getColorSpace();
265  }
266  }
267  return EMPTY_STRING;
268  }
269 
270  /// @}
271  /// @name Inheritance
272  /// @{
273 
274  /// Set the inherit string of this element.
275  void setInheritString(const string& inherit)
276  {
277  setAttribute(INHERIT_ATTRIBUTE, inherit);
278  }
279 
280  /// Return true if this element has an inherit string.
281  bool hasInheritString() const
282  {
283  return hasAttribute(INHERIT_ATTRIBUTE);
284  }
285 
286  /// Return the inherit string of this element.
287  const string& getInheritString() const
288  {
289  return getAttribute(INHERIT_ATTRIBUTE);
290  }
291 
292  /// Set the element that this one directly inherits from.
294  {
295  if (super)
296  {
297  setInheritString(super->getName());
298  }
299  else
300  {
301  removeAttribute(INHERIT_ATTRIBUTE);
302  }
303  }
304 
305  /// Return the element, if any, that this one directly inherits from.
307  {
308  return hasInheritString() ? resolveNameReference<Element>(getInheritString()) : nullptr;
309  }
310 
311  /// Return true if this element has the given element as an inherited base,
312  /// taking the full inheritance chain into account.
313  bool hasInheritedBase(ConstElementPtr base) const;
314 
315  /// Return true if the inheritance chain for this element contains a cycle.
316  bool hasInheritanceCycle() const;
317 
318  /// @}
319  /// @name Namespace
320  /// @{
321 
322  /// Set the namespace string of this element.
323  void setNamespace(const string& space)
324  {
325  setAttribute(NAMESPACE_ATTRIBUTE, space);
326  }
327 
328  /// Return true if this element has a namespace string.
329  bool hasNamespace() const
330  {
331  return hasAttribute(NAMESPACE_ATTRIBUTE);
332  }
333 
334  /// Return the namespace string of this element.
335  const string& getNamespace() const
336  {
337  return getAttribute(NAMESPACE_ATTRIBUTE);
338  }
339 
340  /// Return a qualified version of the given name, taking the namespace at the
341  /// scope of this element into account.
342  string getQualifiedName(const string& name) const
343  {
344  for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
345  {
346  const string& namespaceStr = elem->getNamespace();
347  if (!namespaceStr.empty())
348  {
349  // Check if the name is qualified already.
350  const size_t i = name.find_first_of(NAME_PREFIX_SEPARATOR);
351  if (i != string::npos && name.substr(0, i) == namespaceStr)
352  {
353  // The name is already qualified with this namespace,
354  // so just return it as is.
355  return name;
356  }
357  return namespaceStr + NAME_PREFIX_SEPARATOR + name;
358  }
359  }
360  return name;
361  }
362 
363  /// @}
364  /// @name Documentation String
365  /// @{
366 
367  /// Set the documentation string of this element.
368  void setDocString(const string& doc)
369  {
370  setAttribute(DOC_ATTRIBUTE, doc);
371  }
372 
373  /// Return the documentation string of this element
374  string getDocString() const
375  {
376  return getAttribute(DOC_ATTRIBUTE);
377  }
378 
379  /// @}
380  /// @name Subclass
381  /// @{
382 
383  /// Return true if this element belongs to the given subclass.
384  /// If a category string is specified, then both subclass and category
385  /// matches are required.
386  template <class T> bool isA(const string& category = EMPTY_STRING) const
387  {
388  if (!asA<T>())
389  return false;
390  if (!category.empty() && getCategory() != category)
391  return false;
392  return true;
393  }
394 
395  /// Dynamic cast to an instance of the given subclass.
396  template <class T> shared_ptr<T> asA();
397 
398  /// Dynamic cast to a const instance of the given subclass.
399  template <class T> shared_ptr<const T> asA() const;
400 
401  /// @}
402  /// @name Child Elements
403  /// @{
404 
405  /// Add a child element of the given subclass and name.
406  /// @param name The name of the new child element.
407  /// If no name is specified, then a unique name will automatically be
408  /// generated.
409  /// @throws Exception if a child of this element already possesses the
410  /// given name.
411  /// @return A shared pointer to the new child element.
412  template <class T> shared_ptr<T> addChild(const string& name = EMPTY_STRING);
413 
414  /// Add a child element of the given category and name.
415  /// @param category The category string of the new child element.
416  /// If the category string is recognized, then the corresponding Element
417  /// subclass is generated; otherwise, a GenericElement is generated.
418  /// @param name The name of the new child element.
419  /// If no name is specified, then a unique name will automatically be
420  /// generated.
421  /// @throws Exception if a child of this element already possesses the
422  /// given name.
423  /// @return A shared pointer to the new child element.
424  ElementPtr addChildOfCategory(const string& category, string name = EMPTY_STRING);
425 
426  /// Change the category of the given child element.
427  /// @param child The child element that will be modified.
428  /// @param category The new category string for the child element.
429  /// @return A shared pointer to a new child element, containing the contents
430  /// of the original child but with a new category and subclass.
431  ElementPtr changeChildCategory(ElementPtr child, const string& category);
432 
433  /// Return the child element, if any, with the given name.
434  ElementPtr getChild(const string& name) const
435  {
436  ElementMap::const_iterator it = _childMap.find(name);
437  return (it != _childMap.end()) ? it->second : ElementPtr();
438  }
439 
440  /// Return the child element, if any, with the given name and subclass.
441  /// If a child with the given name exists, but belongs to a different
442  /// subclass, then an empty shared pointer is returned.
443  template <class T> shared_ptr<T> getChildOfType(const string& name) const;
444 
445  /// Return a constant vector of all child elements.
446  /// The returned vector maintains the order in which children were added.
447  const ElementVec& getChildren() const
448  {
449  return _childOrder;
450  }
451 
452  /// Return a vector of all child elements that are instances of the given
453  /// subclass, optionally filtered by the given category string. The returned
454  /// vector maintains the order in which children were added.
455  template <class T> vector<shared_ptr<T>> getChildrenOfType(const string& category = EMPTY_STRING) const;
456 
457  /// Set the index of the child, if any, with the given name.
458  /// If the given index is out of bounds, then an exception is thrown.
459  void setChildIndex(const string& name, int index);
460 
461  /// Return the index of the child, if any, with the given name.
462  /// If no child with the given name is found, then -1 is returned.
463  int getChildIndex(const string& name) const;
464 
465  /// Remove the child element, if any, with the given name.
466  void removeChild(const string& name);
467 
468  /// Remove the child element, if any, with the given name and subclass.
469  /// If a child with the given name exists, but belongs to a different
470  /// subclass, then this method has no effect.
471  template <class T> void removeChildOfType(const string& name)
472  {
473  if (getChildOfType<T>(name))
474  removeChild(name);
475  }
476 
477  /// @}
478  /// @name Attributes
479  /// @{
480 
481  /// Set the value string of the given attribute.
482  void setAttribute(const string& attrib, const string& value);
483 
484  /// Return true if the given attribute is present.
485  bool hasAttribute(const string& attrib) const
486  {
487  return _attributeMap.count(attrib) != 0;
488  }
489 
490  /// Return the value string of the given attribute. If the given attribute
491  /// is not present, then an empty string is returned.
492  const string& getAttribute(const string& attrib) const
493  {
494  StringMap::const_iterator it = _attributeMap.find(attrib);
495  return (it != _attributeMap.end()) ? it->second : EMPTY_STRING;
496  }
497 
498  /// Return a vector of stored attribute names, in the order they were set.
500  {
501  return _attributeOrder;
502  }
503 
504  /// Set the value of an implicitly typed attribute. Since an attribute
505  /// stores no explicit type, the same type argument must be used in
506  /// corresponding calls to getTypedAttribute.
507  template <class T> void setTypedAttribute(const string& attrib, const T& data)
508  {
509  setAttribute(attrib, toValueString(data));
510  }
511 
512  /// Return the value of an implicitly typed attribute. If the given
513  /// attribute is not present, or cannot be converted to the given data
514  /// type, then the zero value for the data type is returned.
515  template <class T> T getTypedAttribute(const string& attrib) const
516  {
517  if (hasAttribute(attrib))
518  {
519  try
520  {
521  return fromValueString<T>(getAttribute(attrib));
522  }
523  catch (ExceptionTypeError&)
524  {
525  }
526  }
527  return {};
528  }
529 
530  /// Remove the given attribute, if present.
531  void removeAttribute(const string& attrib);
532 
533  /// @}
534  /// @name Self And Ancestor Elements
535  /// @{
536 
537  /// Return our self pointer.
539  {
540  return shared_from_this();
541  }
542 
543  /// Return our self pointer.
545  {
546  return shared_from_this();
547  }
548 
549  /// Return our parent element.
551  {
552  return _parent.lock();
553  }
554 
555  /// Return our parent element.
557  {
558  return _parent.lock();
559  }
560 
561  /// Return the root element of our tree.
562  ElementPtr getRoot();
563 
564  /// Return the root element of our tree.
565  ConstElementPtr getRoot() const;
566 
567  /// Return the root document of our tree.
568  DocumentPtr getDocument();
569 
570  /// Return the root document of our tree.
571  ConstDocumentPtr getDocument() const;
572 
573  /// Return the first ancestor of the given subclass, or an empty shared
574  /// pointer if no ancestor of this subclass is found.
575  template <class T> shared_ptr<T> getAncestorOfType()
576  {
577  for (ElementPtr elem = getSelf(); elem; elem = elem->getParent())
578  {
579  shared_ptr<T> typedElem = elem->asA<T>();
580  if (typedElem)
581  {
582  return typedElem;
583  }
584  }
585  return nullptr;
586  }
587 
588  /// Return the first ancestor of the given subclass, or an empty shared
589  /// pointer if no ancestor of this subclass is found.
590  template <class T> shared_ptr<const T> getAncestorOfType() const
591  {
592  for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
593  {
594  shared_ptr<const T> typedElem = elem->asA<T>();
595  if (typedElem)
596  {
597  return typedElem;
598  }
599  }
600  return nullptr;
601  }
602 
603  /// @}
604  /// @name Functional Equivalence
605  /// @{
606 
607  /// Return true if the given element tree, including all descendents,
608  /// is considered to be equivalent to this one based on the equivalence
609  /// criteria provided.
610  /// @param rhs Element to compare against
611  /// @param options Equivalence criteria
612  /// @param message Optional text description of differences
613  /// @return True if the elements are equivalent. False otherwise.
614  bool isEquivalent(ConstElementPtr rhs, const ElementEquivalenceOptions& options,
615  string* message = nullptr) const;
616 
617  /// Return true if the attribute on a given element is equivalent
618  /// based on the equivalence criteria provided.
619  /// @param rhs Element to compare against
620  /// @param attributeName Name of attribute to compare
621  /// @param options Equivalence criteria
622  /// @param message Optional text description of differences
623  /// @return True if the attribute on the elements are equivalent. False otherwise.
624  virtual bool isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
625  const ElementEquivalenceOptions& options,
626  string* message = nullptr) const;
627 
628  /// @}
629  /// @name Traversal
630  /// @{
631 
632  /// Traverse the tree from the given element to each of its descendants in
633  /// depth-first order, using pre-order visitation.
634  /// @return A TreeIterator object.
635  /// @details Example usage with an implicit iterator:
636  /// @code
637  /// for (ElementPtr elem : inputElem->traverseTree())
638  /// {
639  /// cout << elem->asString() << endl;
640  /// }
641  /// @endcode
642  /// Example usage with an explicit iterator:
643  /// @code
644  /// for (mx::TreeIterator it = inputElem->traverseTree().begin(); it != mx::TreeIterator::end(); ++it)
645  /// {
646  /// mx::ElementPtr elem = it.getElement();
647  /// cout << elem->asString() << " at depth " << it.getElementDepth() << endl;
648  /// }
649  /// @endcode
650  TreeIterator traverseTree() const;
651 
652  /// Traverse the dataflow graph from the given element to each of its
653  /// upstream sources in depth-first order, using pre-order visitation.
654  /// @throws ExceptionFoundCycle if a cycle is encountered.
655  /// @return A GraphIterator object.
656  /// @details Example usage with an implicit iterator:
657  /// @code
658  /// for (Edge edge : inputElem->traverseGraph())
659  /// {
660  /// ElementPtr upElem = edge.getUpstreamElement();
661  /// ElementPtr downElem = edge.getDownstreamElement();
662  /// cout << upElem->asString() << " lies upstream from " << downElem->asString() << endl;
663  /// }
664  /// @endcode
665  /// Example usage with an explicit iterator:
666  /// @code
667  /// for (mx::GraphIterator it = inputElem->traverseGraph().begin(); it != mx::GraphIterator::end(); ++it)
668  /// {
669  /// mx::ElementPtr elem = it.getUpstreamElement();
670  /// cout << elem->asString() << " at depth " << it.getElementDepth() << endl;
671  /// }
672  /// @endcode
673  /// @sa getUpstreamEdge
674  /// @sa getUpstreamElement
675  GraphIterator traverseGraph() const;
676 
677  /// Return the Edge with the given index that lies directly upstream from
678  /// this element in the dataflow graph.
679  /// @param index An optional index of the edge to be returned, where the
680  /// valid index range may be determined with getUpstreamEdgeCount.
681  /// @return The upstream Edge, if valid, or an empty Edge object.
682  virtual Edge getUpstreamEdge(size_t index = 0) const;
683 
684  /// Return the number of queryable upstream edges for this element.
685  virtual size_t getUpstreamEdgeCount() const
686  {
687  return 0;
688  }
689 
690  /// Return the Element with the given index that lies directly upstream
691  /// from this one in the dataflow graph.
692  /// @param index An optional index of the element to be returned, where the
693  /// valid index range may be determined with getUpstreamEdgeCount.
694  /// @return The upstream Element, if valid, or an empty ElementPtr.
695  ElementPtr getUpstreamElement(size_t index = 0) const;
696 
697  /// Traverse the inheritance chain from the given element to each element
698  /// from which it inherits.
699  /// @throws ExceptionFoundCycle if a cycle is encountered.
700  /// @return An InheritanceIterator object.
701  /// @details Example usage:
702  /// @code
703  /// ConstElementPtr derivedElem;
704  /// for (ConstElementPtr elem : inputElem->traverseInheritance())
705  /// {
706  /// if (derivedElem)
707  /// cout << derivedElem->asString() << " inherits from " << elem->asString() << endl;
708  /// derivedElem = elem;
709  /// }
710  /// @endcode
711  InheritanceIterator traverseInheritance() const;
712 
713  /// @}
714  /// @name Source URI
715  /// @{
716 
717  /// Set the element's source URI.
718  /// @param sourceUri A URI string representing the resource from which
719  /// this element originates. This string may be used by serialization
720  /// and deserialization routines to maintain hierarchies of include
721  /// references.
722  void setSourceUri(const string& sourceUri)
723  {
724  _sourceUri = sourceUri;
725  }
726 
727  /// Return true if this element has a source URI.
728  bool hasSourceUri() const
729  {
730  return !_sourceUri.empty();
731  }
732 
733  /// Return the element's source URI.
734  const string& getSourceUri() const
735  {
736  return _sourceUri;
737  }
738 
739  /// Return the source URI that is active at the scope of this
740  /// element, taking all ancestor elements into account.
741  const string& getActiveSourceUri() const
742  {
743  for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
744  {
745  if (elem->hasSourceUri())
746  {
747  return elem->getSourceUri();
748  }
749  }
750  return EMPTY_STRING;
751  }
752 
753  /// @}
754  /// @name Validation
755  /// @{
756 
757  /// Validate that the given element tree, including all descendants, is
758  /// consistent with the MaterialX specification.
759  virtual bool validate(string* message = nullptr) const;
760 
761  /// @}
762  /// @name Utility
763  /// @{
764 
765  /// Copy all attributes and descendants from the given element to this one.
766  /// @param source The element from which content is copied.
767  void copyContentFrom(const ConstElementPtr& source);
768 
769  /// Clear all attributes and descendants from this element.
770  virtual void clearContent();
771 
772  /// Using the input name as a starting point, modify it to create a valid,
773  /// unique name for a child element.
774  string createValidChildName(string name) const
775  {
776  name = name.empty() ? "_" : createValidName(name);
777  while (_childMap.count(name))
778  {
779  name = incrementName(name);
780  }
781  return name;
782  }
783 
784  /// Construct a StringResolver at the scope of this element. The returned
785  /// object may be used to apply substring modifiers to data values in the
786  /// context of a specific element, geometry, and material.
787  /// @param geom An optional geometry name, which will be used to select the
788  /// applicable set of geometry token substitutions. By default, no
789  /// geometry token substitutions are applied. If the universal geometry
790  /// name "/" is given, then all geometry token substitutions are applied,
791  /// @return A shared pointer to a StringResolver.
792  StringResolverPtr createStringResolver(const string& geom = EMPTY_STRING) const;
793 
794  /// Return a single-line description of this element, including its category,
795  /// name, and attributes.
796  string asString() const;
797 
798  /// @}
799 
800  protected:
801  // Resolve a reference to a named element at the scope of the given parent,
802  // taking the namespace at the scope of this element into account. If no parent
803  // is provided, then the root scope of the document is used.
804  template <class T> shared_ptr<T> resolveNameReference(const string& name, ConstElementPtr parent = nullptr) const
805  {
806  ConstElementPtr scope = parent ? parent : getRoot();
807  shared_ptr<T> child = scope->getChildOfType<T>(getQualifiedName(name));
808  return child ? child : scope->getChildOfType<T>(name);
809  }
810 
811  // Enforce a requirement within a validate method, updating the validation
812  // state and optional output text if the requirement is not met.
813  void validateRequire(bool expression, bool& res, string* message, const string& errorDesc) const;
814 
815  public:
816  static const string NAME_ATTRIBUTE;
817  static const string FILE_PREFIX_ATTRIBUTE;
818  static const string GEOM_PREFIX_ATTRIBUTE;
819  static const string COLOR_SPACE_ATTRIBUTE;
820  static const string INHERIT_ATTRIBUTE;
821  static const string NAMESPACE_ATTRIBUTE;
822  static const string DOC_ATTRIBUTE;
823  static const string XPOS_ATTRIBUTE;
824  static const string YPOS_ATTRIBUTE;
825 
826  protected:
827  virtual void registerChildElement(ElementPtr child);
828  virtual void unregisterChildElement(ElementPtr child);
829 
830  // Return a non-const copy of our self pointer, for use in constructing
831  // graph traversal objects that require non-const storage.
833  {
834  return std::const_pointer_cast<Element>(shared_from_this());
835  }
836 
837  protected:
838  string _category;
839  string _name;
840  string _sourceUri;
841 
844 
847 
848  weak_ptr<Element> _parent;
849  weak_ptr<Element> _root;
850 
851  private:
852  template <class T> static ElementPtr createElement(ElementPtr parent, const string& name)
853  {
854  return std::make_shared<T>(parent, name);
855  }
856 
857  private:
858  using CreatorFunction = ElementPtr (*)(ElementPtr, const string&);
859  using CreatorMap = std::unordered_map<string, CreatorFunction>;
860 
861  static CreatorMap _creatorMap;
862 };
863 
864 /// @class TypedElement
865 /// The base class for typed elements.
867 {
868  protected:
869  TypedElement(ElementPtr parent, const string& category, const string& name) :
870  Element(parent, category, name)
871  {
872  }
873 
874  public:
875  virtual ~TypedElement() { }
876 
877  protected:
878  using TypeDefPtr = shared_ptr<class TypeDef>;
879 
880  public:
881  /// @name Type String
882  /// @{
883 
884  /// Set the element's type string.
885  void setType(const string& type)
886  {
887  setAttribute(TYPE_ATTRIBUTE, type);
888  }
889 
890  /// Return true if the given element has a type string.
891  bool hasType() const
892  {
893  return hasAttribute(TYPE_ATTRIBUTE);
894  }
895 
896  /// Return the element's type string.
897  virtual const string& getType() const
898  {
899  return getAttribute(TYPE_ATTRIBUTE);
900  }
901 
902  /// Return true if the element is of color type.
903  bool isColorType() const
904  {
905  return getType() == "color3" || getType() == "color4";
906  }
907 
908  /// Return true if the element is of multi-output type.
909  bool isMultiOutputType() const
910  {
911  return getType() == MULTI_OUTPUT_TYPE_STRING;
912  }
913 
914  /// @}
915  /// @name TypeDef References
916  /// @{
917 
918  /// Return the TypeDef declaring the type string of this element. If no
919  /// matching TypeDef is found, then an empty shared pointer is returned.
920  TypeDefPtr getTypeDef() const;
921 
922  /// @}
923 
924  public:
925  static const string TYPE_ATTRIBUTE;
926 };
927 
928 /// @class ValueElement
929 /// The base class for elements that support typed values.
931 {
932  protected:
933  ValueElement(ElementPtr parent, const string& category, const string& name) :
934  TypedElement(parent, category, name)
935  {
936  }
937 
938  public:
939  virtual ~ValueElement() { }
940 
941  /// @name Value String
942  /// @{
943 
944  /// Set the value string of an element.
945  void setValueString(const string& value)
946  {
947  setAttribute(VALUE_ATTRIBUTE, value);
948  }
949 
950  /// Return true if the given element has a value string.
951  bool hasValueString() const
952  {
953  return hasAttribute(VALUE_ATTRIBUTE);
954  }
955 
956  /// Get the value string of a element.
957  const string& getValueString() const
958  {
959  return getAttribute(VALUE_ATTRIBUTE);
960  }
961 
962  /// Return the resolved value string of an element, applying any string
963  /// substitutions that are defined at the element's scope.
964  /// @param resolver An optional string resolver, which will be used to
965  /// apply string substitutions. By default, a new string resolver
966  /// will be created at this scope and applied to the return value.
967  string getResolvedValueString(StringResolverPtr resolver = nullptr) const;
968 
969  /// @}
970  /// @name Interface Names
971  /// @{
972 
973  /// Set the interface name of an element.
974  void setInterfaceName(const string& name)
975  {
976  setAttribute(INTERFACE_NAME_ATTRIBUTE, name);
977  }
978 
979  /// Return true if the given element has an interface name.
980  bool hasInterfaceName() const
981  {
982  return hasAttribute(INTERFACE_NAME_ATTRIBUTE);
983  }
984 
985  /// Return the interface name of an element.
986  const string& getInterfaceName() const
987  {
988  return getAttribute(INTERFACE_NAME_ATTRIBUTE);
989  }
990 
991  /// @}
992  /// @name Implementation Names
993  /// @{
994 
995  /// Set the implementation name of an element.
996  void setImplementationName(const string& name)
997  {
998  setAttribute(IMPLEMENTATION_NAME_ATTRIBUTE, name);
999  }
1000 
1001  /// Return true if the given element has an implementation name.
1003  {
1004  return hasAttribute(IMPLEMENTATION_NAME_ATTRIBUTE);
1005  }
1006 
1007  /// Return the implementation name of an element.
1008  const string& getImplementationName() const
1009  {
1010  return getAttribute(IMPLEMENTATION_NAME_ATTRIBUTE);
1011  }
1012 
1013  /// @}
1014  /// @name Typed Value
1015  /// @{
1016 
1017  /// Set the typed value of an element.
1018  template <class T> void setValue(const T& value, const string& type = EMPTY_STRING)
1019  {
1020  setType(!type.empty() ? type : getTypeString<T>());
1021  setValueString(toValueString(value));
1022  }
1023 
1024  /// Set the typed value of an element from a C-style string.
1025  void setValue(const char* value, const string& type = EMPTY_STRING)
1026  {
1027  setValue(value ? string(value) : EMPTY_STRING, type);
1028  }
1029 
1030  /// Return true if the element possesses a typed value.
1031  bool hasValue() const
1032  {
1033  return hasAttribute(VALUE_ATTRIBUTE);
1034  }
1035 
1036  /// Return the typed value of an element as a generic value object, which
1037  /// may be queried to access its data.
1038  ///
1039  /// @return A shared pointer to the typed value of this element, or an
1040  /// empty shared pointer if no value is present.
1041  ValuePtr getValue() const;
1042 
1043  /// Return the resolved value of an element as a generic value object, which
1044  /// may be queried to access its data.
1045  ///
1046  /// @param resolver An optional string resolver, which will be used to
1047  /// apply string substitutions. By default, a new string resolver
1048  /// will be created at this scope and applied to the return value.
1049  /// @return A shared pointer to the typed value of this element, or an
1050  /// empty shared pointer if no value is present.
1051  ValuePtr getResolvedValue(StringResolverPtr resolver = nullptr) const;
1052 
1053  /// Return the default value for this element as a generic value object, which
1054  /// may be queried to access its data.
1055  ///
1056  /// @return A shared pointer to a typed value, or an empty shared pointer if
1057  /// no default value was found.
1058  ValuePtr getDefaultValue() const;
1059 
1060  /// @}
1061  /// @name Units
1062  /// @{
1063 
1064  /// Set the unit string of an element.
1065  void setUnit(const string& unit)
1066  {
1067  setAttribute(UNIT_ATTRIBUTE, unit);
1068  }
1069 
1070  /// Return true if the given element has a unit string.
1071  bool hasUnit() const
1072  {
1073  return hasAttribute(UNIT_ATTRIBUTE);
1074  }
1075 
1076  /// Return the unit string of an element.
1077  const string& getUnit() const
1078  {
1079  return getAttribute(UNIT_ATTRIBUTE);
1080  }
1081 
1082  /// Return the unit defined by the associated NodeDef if this element
1083  /// is a child of a Node.
1084  const string& getActiveUnit() const;
1085 
1086  /// Set the unit type of an element.
1087  void setUnitType(const string& unit)
1088  {
1089  setAttribute(UNITTYPE_ATTRIBUTE, unit);
1090  }
1091 
1092  /// Return true if the given element has a unit type.
1093  bool hasUnitType() const
1094  {
1095  return hasAttribute(UNITTYPE_ATTRIBUTE);
1096  }
1097 
1098  /// Return the unit type of an element.
1099  const string& getUnitType() const
1100  {
1101  return getAttribute(UNITTYPE_ATTRIBUTE);
1102  }
1103 
1104  /// @}
1105  /// @name Uniform attribute
1106  /// @{
1107 
1108  /// Set the uniform attribute flag on this element.
1109  void setIsUniform(bool value)
1110  {
1111  setTypedAttribute<bool>(UNIFORM_ATTRIBUTE, value);
1112  }
1113 
1114  /// The the uniform attribute flag for this element.
1115  bool getIsUniform() const
1116  {
1117  return getTypedAttribute<bool>(UNIFORM_ATTRIBUTE);
1118  }
1119 
1120  /// @}
1121  /// @name Functional Equivalence
1122  /// @{
1123 
1124  /// Return true if the attribute on a given element is equivalent
1125  /// based on the equivalence criteria provided.
1126  /// @param rhs Element to compare against
1127  /// @param attributeName Name of attribute to compare
1128  /// @param options Equivalence criteria
1129  /// @param message Optional text description of differences
1130  /// @return True if the attribute on the elements are equivalent. False otherwise.
1131  bool isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
1132  const ElementEquivalenceOptions& options,
1133  string* message = nullptr) const override;
1134 
1135  /// @}
1136  /// @name Validation
1137  /// @{
1138 
1139  /// Validate that the given element tree, including all descendants, is
1140  /// consistent with the MaterialX specification.
1141  bool validate(string* message = nullptr) const override;
1142 
1143  /// @}
1144 
1145  public:
1146  static const string VALUE_ATTRIBUTE;
1147  static const string INTERFACE_NAME_ATTRIBUTE;
1148  static const string IMPLEMENTATION_NAME_ATTRIBUTE;
1149  static const string IMPLEMENTATION_TYPE_ATTRIBUTE;
1150  static const string ENUM_ATTRIBUTE;
1151  static const string ENUM_VALUES_ATTRIBUTE;
1152  static const string UI_NAME_ATTRIBUTE;
1153  static const string UI_FOLDER_ATTRIBUTE;
1154  static const string UI_MIN_ATTRIBUTE;
1155  static const string UI_MAX_ATTRIBUTE;
1156  static const string UI_SOFT_MIN_ATTRIBUTE;
1157  static const string UI_SOFT_MAX_ATTRIBUTE;
1158  static const string UI_STEP_ATTRIBUTE;
1159  static const string UI_ADVANCED_ATTRIBUTE;
1160  static const string UNIT_ATTRIBUTE;
1161  static const string UNITTYPE_ATTRIBUTE;
1162  static const string UNIFORM_ATTRIBUTE;
1163 };
1164 
1165 /// @class Token
1166 /// A token element representing a string value.
1167 ///
1168 /// Token elements are used to define input and output values for string
1169 /// substitutions in image filenames.
1171 {
1172  public:
1173  Token(ElementPtr parent, const string& name) :
1174  ValueElement(parent, CATEGORY, name)
1175  {
1176  }
1177  virtual ~Token() { }
1178 
1179  public:
1180  static const string CATEGORY;
1181 };
1182 
1183 /// @class CommentElement
1184 /// An element representing a block of descriptive text within a document, which will
1185 /// be stored a comment when the document is written out.
1186 ///
1187 /// The comment text may be accessed with the methods Element::setDocString and
1188 /// Element::getDocString.
1189 ///
1191 {
1192  public:
1193  CommentElement(ElementPtr parent, const string& name) :
1194  Element(parent, CATEGORY, name)
1195  {
1196  }
1197  virtual ~CommentElement() { }
1198 
1199  public:
1200  static const string CATEGORY;
1201 };
1202 
1203 /// @class NewlineElement
1204 /// An element representing a newline within a document.
1206 {
1207  public:
1208  NewlineElement(ElementPtr parent, const string& name) :
1209  Element(parent, CATEGORY, name)
1210  {
1211  }
1212  virtual ~NewlineElement() { }
1213 
1214  public:
1215  static const string CATEGORY;
1216 };
1217 
1218 /// @class GenericElement
1219 /// A generic element subclass, for instantiating elements with unrecognized categories.
1221 {
1222  public:
1223  GenericElement(ElementPtr parent, const string& name) :
1224  Element(parent, CATEGORY, name)
1225  {
1226  }
1227  virtual ~GenericElement() { }
1228 
1229  public:
1230  static const string CATEGORY;
1231 };
1232 
1233 /// @class StringResolver
1234 /// A helper object for applying string modifiers to data values in the context
1235 /// of a specific element and geometry.
1236 ///
1237 /// A StringResolver may be constructed through the Element::createStringResolver
1238 /// method, which initializes it in the context of a specific element, geometry,
1239 /// and material.
1240 ///
1241 /// Calling the StringResolver::resolve method applies all modifiers to a
1242 /// particular string value.
1243 ///
1244 /// Methods such as StringResolver::setFilePrefix may be used to edit the
1245 /// stored string modifiers before calling StringResolver::resolve.
1247 {
1248  public:
1249  /// Create a new string resolver.
1251  {
1252  return StringResolverPtr(new StringResolver());
1253  }
1254 
1255  virtual ~StringResolver() { }
1256 
1257  /// @name File Prefix
1258  /// @{
1259 
1260  /// Set the file prefix for this context.
1261  void setFilePrefix(const string& filePrefix)
1262  {
1263  _filePrefix = filePrefix;
1264  }
1265 
1266  /// Return the file prefix for this context.
1267  const string& getFilePrefix() const
1268  {
1269  return _filePrefix;
1270  }
1271 
1272  /// @}
1273  /// @name Geom Prefix
1274  /// @{
1275 
1276  /// Set the geom prefix for this context.
1277  void setGeomPrefix(const string& geomPrefix)
1278  {
1279  _geomPrefix = geomPrefix;
1280  }
1281 
1282  /// Return the geom prefix for this context.
1283  const string& getGeomPrefix() const
1284  {
1285  return _geomPrefix;
1286  }
1287 
1288  /// @}
1289  /// @name Filename Substitutions
1290  /// @{
1291 
1292  /// Set the UDIM substring substitution for filename data values.
1293  /// This string will be used to replace the standard <UDIM> token.
1294  void setUdimString(const string& udim);
1295 
1296  /// Set the UV-tile substring substitution for filename data values.
1297  /// This string will be used to replace the standard <UVTILE> token.
1298  void setUvTileString(const string& uvTile);
1299 
1300  /// Set an arbitrary substring substitution for filename data values.
1301  void setFilenameSubstitution(const string& key, const string& value)
1302  {
1303  _filenameMap[key] = value;
1304  }
1305 
1306  /// Add filename token substitutions for a given element
1307  void addTokenSubstitutions(ConstElementPtr element);
1308 
1309  /// Return the map of filename substring substitutions.
1311  {
1312  return _filenameMap;
1313  }
1314 
1315  /// @}
1316  /// @name Geometry Name Substitutions
1317  /// @{
1318 
1319  /// Set an arbitrary substring substitution for geometry name data values.
1320  void setGeomNameSubstitution(const string& key, const string& value)
1321  {
1322  _geomNameMap[key] = value;
1323  }
1324 
1325  /// Return the map of geometry name substring substitutions.
1327  {
1328  return _geomNameMap;
1329  }
1330 
1331  /// @}
1332  /// @name Resolution
1333  /// @{
1334 
1335  /// Given an input string and type, apply all appropriate modifiers and
1336  /// return the resulting string.
1337  virtual string resolve(const string& str, const string& type) const;
1338 
1339  /// Return true if the given type may be resolved by this class.
1340  static bool isResolvedType(const string& type)
1341  {
1342  return type == FILENAME_TYPE_STRING || type == GEOMNAME_TYPE_STRING;
1343  }
1344 
1345  /// @}
1346 
1347  protected:
1349 
1350  protected:
1351  string _filePrefix;
1352  string _geomPrefix;
1355 };
1356 
1357 /// @class ElementEquivalenceOptions
1358 /// A set of options for comparing the functional equivalence of elements.
1360 {
1361  public:
1363  {
1364  performValueComparisons = true;
1365  floatFormat = Value::getFloatFormat();
1366  floatPrecision = Value::getFloatPrecision();
1367  attributeExclusionList = {};
1368  };
1370 
1371  /// Perform value comparisons as opposed to literal string comparisons.
1372  /// Default is true.
1374 
1375  /// Floating point format to use for floating point value comparisons
1377 
1378  /// Floating point precision to use for floating point value comparisons
1380 
1381  /// Specifies the set of attributes that should be excluded when performing a comparison.
1382  /// By default all attributes are considered. Name and category attributes cannot be excluded.
1383  ///
1384  /// For example, to exclude UI and documentation attributes from consideration the follow may be set:
1385  /// attributeExclusionList = {
1386  /// ValueElement::UI_MIN_ATTRIBUTE, ValueElement::UI_MAX_ATTRIBUTE,
1387  /// ValueElement::UI_SOFT_MIN_ATTRIBUTE, ValueElement::UI_SOFT_MAX_ATTRIBUTE,
1388  /// ValueElement::UI_STEP_ATTRIBUTE, Element::XPOS_ATTRIBUTE,
1389  /// Element::YPOS_ATTRIBUTE, Element::DOC_ATTRIBUTE };
1391 };
1392 
1393 /// @class ExceptionOrphanedElement
1394 /// An exception that is thrown when an ElementPtr is used after its owning
1395 /// Document has gone out of scope.
1397 {
1398  public:
1399  using Exception::Exception;
1400 };
1401 
1402 template <class T> shared_ptr<T> Element::addChild(const string& name)
1403 {
1404  string childName = name;
1405  if (childName.empty())
1406  {
1407  childName = createValidChildName(T::CATEGORY + "1");
1408  }
1409 
1410  if (_childMap.count(childName))
1411  throw Exception("Child name is not unique: " + childName);
1412 
1413  shared_ptr<T> child = std::make_shared<T>(getSelf(), childName);
1414  registerChildElement(child);
1415 
1416  return child;
1417 }
1418 
1419 /// Given two target strings, each containing a string array of target names,
1420 /// return true if they have any targets in common. An empty target string
1421 /// matches all targets.
1422 MX_CORE_API bool targetStringsMatch(const string& target1, const string& target2);
1423 
1424 /// Pretty print the given element tree, calling asString recursively on each
1425 /// element in depth-first order.
1427 
1429 
1430 #endif
static const string UI_ADVANCED_ATTRIBUTE
Definition: Element.h:1159
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
ValueElement(ElementPtr parent, const string &category, const string &name)
Definition: Element.h:933
const string & getFilePrefix() const
Return the element's file prefix string.
Definition: Element.h:179
const string & getNamespace() const
Return the namespace string of this element.
Definition: Element.h:335
string _geomPrefix
Definition: Element.h:1352
virtual const string & getType() const
Return the element's type string.
Definition: Element.h:897
shared_ptr< const Element > ConstElementPtr
A shared pointer to a const Element.
Definition: Element.h:33
void setType(const string &type)
Set the element's type string.
Definition: Element.h:885
void setGeomPrefix(const string &prefix)
Set the element's geom prefix string.
Definition: Element.h:203
string _sourceUri
Definition: Element.h:840
weak_ptr< Element > _parent
Definition: Element.h:848
bool hasValue() const
Return true if the element possesses a typed value.
Definition: Element.h:1031
bool hasInterfaceName() const
Return true if the given element has an interface name.
Definition: Element.h:980
TypedElement(ElementPtr parent, const string &category, const string &name)
Definition: Element.h:869
shared_ptr< TypeDef > TypeDefPtr
A shared pointer to a TypeDef.
Definition: Definition.h:42
shared_ptr< const T > getAncestorOfType() const
Definition: Element.h:590
string _filePrefix
Definition: Element.h:1351
static const string IMPLEMENTATION_NAME_ATTRIBUTE
Definition: Element.h:1148
shared_ptr< T > addChild(const string &name=EMPTY_STRING)
Definition: Element.h:1402
static const string ENUM_ATTRIBUTE
Definition: Element.h:1150
const string & getFilePrefix() const
Return the file prefix for this context.
Definition: Element.h:1267
void setDocString(const string &doc)
Set the documentation string of this element.
Definition: Element.h:368
ElementPtr getSelf()
Return our self pointer.
Definition: Element.h:538
static const string UI_NAME_ATTRIBUTE
Definition: Element.h:1152
std::function< bool(ConstElementPtr)> ElementPredicate
A standard function taking an ElementPtr and returning a boolean.
Definition: Element.h:75
bool isColorType() const
Return true if the element is of color type.
Definition: Element.h:903
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
StringMap _attributeMap
Definition: Element.h:845
virtual ~GenericElement()
Definition: Element.h:1227
vector< string > StringVec
A vector of strings.
Definition: Library.h:60
shared_ptr< NewlineElement > NewlineElementPtr
A shared pointer to a NewlineElement.
Definition: Element.h:56
bool hasImplementationName() const
Return true if the given element has an implementation name.
Definition: Element.h:1002
std::unordered_map< string, ElementPtr > ElementMap
A hash map from strings to elements.
Definition: Element.h:72
GLsizei const GLfloat * value
Definition: glcorearb.h:824
static FloatFormat getFloatFormat()
Return the current float format.
Definition: Value.h:114
MX_CORE_API const string FILENAME_TYPE_STRING
static const string CATEGORY
Definition: Element.h:1200
void setIsUniform(bool value)
Set the uniform attribute flag on this element.
Definition: Element.h:1109
shared_ptr< TypedElement > TypedElementPtr
A shared pointer to a TypedElement.
Definition: Element.h:36
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
static const string CATEGORY
Definition: Element.h:1215
static StringResolverPtr create()
Create a new string resolver.
Definition: Element.h:1250
static const string UNIFORM_ATTRIBUTE
Definition: Element.h:1162
const string & getAttribute(const string &attrib) const
Definition: Element.h:492
static const string UI_MIN_ATTRIBUTE
Definition: Element.h:1154
void setValue(const T &value, const string &type=EMPTY_STRING)
Set the typed value of an element.
Definition: Element.h:1018
StringMap _geomNameMap
Definition: Element.h:1354
static const string IMPLEMENTATION_TYPE_ATTRIBUTE
Definition: Element.h:1149
__hostdev__ void setValue(uint32_t offset, bool v)
Definition: NanoVDB.h:5750
virtual ~CommentElement()
Definition: Element.h:1197
void setValueString(const string &value)
Set the value string of an element.
Definition: Element.h:945
void setFilenameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for filename data values.
Definition: Element.h:1301
string createValidChildName(string name) const
Definition: Element.h:774
bool hasUnit() const
Return true if the given element has a unit string.
Definition: Element.h:1071
shared_ptr< Token > TokenPtr
A shared pointer to a Token.
Definition: Element.h:46
class OCIOEXPORT Exception
static const string GEOM_PREFIX_ATTRIBUTE
Definition: Element.h:818
const string & getImplementationName() const
Return the implementation name of an element.
Definition: Element.h:1008
#define MX_CORE_API
Definition: Export.h:18
StringSet attributeExclusionList
Definition: Element.h:1390
MX_CORE_API string prettyPrint(ConstElementPtr elem)
bool hasValueString() const
Return true if the given element has a value string.
Definition: Element.h:951
virtual ~StringResolver()
Definition: Element.h:1255
void setImplementationName(const string &name)
Set the implementation name of an element.
Definition: Element.h:996
ElementPtr getChild(const string &name) const
Return the child element, if any, with the given name.
Definition: Element.h:434
const string & getInterfaceName() const
Return the interface name of an element.
Definition: Element.h:986
shared_ptr< GenericElement > GenericElementPtr
A shared pointer to a GenericElement.
Definition: Element.h:61
bool hasColorSpace() const
Return true if the given element has a color space string.
Definition: Element.h:245
shared_ptr< const GenericElement > ConstGenericElementPtr
A shared pointer to a const GenericElement.
Definition: Element.h:63
const string & getActiveColorSpace() const
Definition: Element.h:258
__hostdev__ float getValue(uint32_t i) const
Definition: NanoVDB.h:5578
MX_CORE_API string incrementName(const string &name)
Increment the numeric suffix of a name.
Definition: Traversal.h:29
static const string INTERFACE_NAME_ATTRIBUTE
Definition: Element.h:1147
NewlineElement(ElementPtr parent, const string &name)
Definition: Element.h:1208
void setInheritsFrom(ConstElementPtr super)
Set the element that this one directly inherits from.
Definition: Element.h:293
string getDocString() const
Return the documentation string of this element.
Definition: Element.h:374
weak_ptr< Element > _root
Definition: Element.h:849
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
static const string NAME_ATTRIBUTE
Definition: Element.h:816
ElementPtr getParent()
Return our parent element.
Definition: Element.h:550
const string & getActiveFilePrefix() const
Definition: Element.h:186
vector< ElementPtr > ElementVec
A vector of elements.
Definition: Element.h:69
const string & getName() const
Return the element's name string.
Definition: Element.h:143
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
shared_ptr< const TypedElement > ConstTypedElementPtr
A shared pointer to a const TypedElement.
Definition: Element.h:38
shared_ptr< const Document > ConstDocumentPtr
Definition: Element.h:102
virtual ~Element()
Definition: Element.h:96
void setInterfaceName(const string &name)
Set the interface name of an element.
Definition: Element.h:974
void setValue(const char *value, const string &type=EMPTY_STRING)
Set the typed value of an element from a C-style string.
Definition: Element.h:1025
virtual bool validate(string *message=nullptr) const
shared_ptr< T > resolveNameReference(const string &name, ConstElementPtr parent=nullptr) const
Definition: Element.h:804
static const string CATEGORY
Definition: Element.h:1180
bool hasType() const
Return true if the given element has a type string.
Definition: Element.h:891
const string & getUnitType() const
Return the unit type of an element.
Definition: Element.h:1099
bool hasNamespace() const
Return true if this element has a namespace string.
Definition: Element.h:329
MX_CORE_API const string GEOMNAME_TYPE_STRING
static bool isResolvedType(const string &type)
Return true if the given type may be resolved by this class.
Definition: Element.h:1340
void setSourceUri(const string &sourceUri)
Definition: Element.h:722
GenericElement(ElementPtr parent, const string &name)
Definition: Element.h:1223
void setUnit(const string &unit)
Set the unit string of an element.
Definition: Element.h:1065
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
void setUnitType(const string &unit)
Set the unit type of an element.
Definition: Element.h:1087
MX_CORE_API const string MULTI_OUTPUT_TYPE_STRING
shared_ptr< const ValueElement > ConstValueElementPtr
A shared pointer to a const ValueElement.
Definition: Element.h:43
virtual ~ValueElement()
Definition: Element.h:939
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
T getTypedAttribute(const string &attrib) const
Definition: Element.h:515
void setColorSpace(const string &colorSpace)
Set the element's color space string.
Definition: Element.h:239
static const string UI_MAX_ATTRIBUTE
Definition: Element.h:1155
const string & getSourceUri() const
Return the element's source URI.
Definition: Element.h:734
MX_CORE_API const string NAME_PREFIX_SEPARATOR
MX_CORE_API bool targetStringsMatch(const string &target1, const string &target2)
static const string ENUM_VALUES_ATTRIBUTE
Definition: Element.h:1151
png_const_structrp png_const_inforp int * unit
Definition: png.h:2161
void setTypedAttribute(const string &attrib, const T &data)
Definition: Element.h:507
ElementPtr getInheritsFrom() const
Return the element, if any, that this one directly inherits from.
Definition: Element.h:306
GLuint const GLchar * name
Definition: glcorearb.h:786
static const string VALUE_ATTRIBUTE
Definition: Element.h:1146
ElementPtr getSelfNonConst() const
Definition: Element.h:832
virtual ~NewlineElement()
Definition: Element.h:1212
const string & getCategory() const
Definition: Element.h:127
static const string UI_SOFT_MAX_ATTRIBUTE
Definition: Element.h:1157
bool isMultiOutputType() const
Return true if the element is of multi-output type.
Definition: Element.h:909
void setFilePrefix(const string &filePrefix)
Set the file prefix for this context.
Definition: Element.h:1261
Element(ElementPtr parent, const string &category, const string &name)
Definition: Element.h:87
ConstElementPtr getParent() const
Return our parent element.
Definition: Element.h:556
bool hasUnitType() const
Return true if the given element has a unit type.
Definition: Element.h:1093
const StringMap & getGeomNameSubstitutions() const
Return the map of geometry name substring substitutions.
Definition: Element.h:1326
const string & getValueString() const
Get the value string of a element.
Definition: Element.h:957
const string & getActiveSourceUri() const
Definition: Element.h:741
void setNamespace(const string &space)
Set the namespace string of this element.
Definition: Element.h:323
shared_ptr< const NewlineElement > ConstNewlineElementPtr
A shared pointer to a const NewlineElement.
Definition: Element.h:58
shared_ptr< const Token > ConstTokenPtr
A shared pointer to a const Token.
Definition: Element.h:48
static int getFloatPrecision()
Return the current float precision.
Definition: Value.h:120
virtual ~Token()
Definition: Element.h:1177
static const string UNITTYPE_ATTRIBUTE
Definition: Element.h:1161
shared_ptr< StringResolver > StringResolverPtr
A shared pointer to a StringResolver.
Definition: Element.h:66
string _category
Definition: Element.h:838
void setAttribute(const string &attrib, const string &value)
Set the value string of the given attribute.
shared_ptr< Document > DocumentPtr
Definition: Element.h:101
static const string YPOS_ATTRIBUTE
Definition: Element.h:824
bool hasInheritString() const
Return true if this element has an inherit string.
Definition: Element.h:281
const StringVec & getAttributeNames() const
Return a vector of stored attribute names, in the order they were set.
Definition: Element.h:499
void setFilePrefix(const string &prefix)
Set the element's file prefix string.
Definition: Element.h:167
Exception(const string &msg)
Definition: Exception.h:24
shared_ptr< const CommentElement > ConstCommentElementPtr
A shared pointer to a const CommentElement.
Definition: Element.h:53
const string & getActiveGeomPrefix() const
Definition: Element.h:222
bool hasFilePrefix() const
Return true if the given element has a file prefix string.
Definition: Element.h:173
string getQualifiedName(const string &name) const
Definition: Element.h:342
void setInheritString(const string &inherit)
Set the inherit string of this element.
Definition: Element.h:275
static const string COLOR_SPACE_ATTRIBUTE
Definition: Element.h:819
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
Definition: Element.h:485
MX_CORE_API string createValidName(string name, char replaceChar= '_')
Create a valid MaterialX name from the given string.
shared_ptr< CommentElement > CommentElementPtr
A shared pointer to a CommentElement.
Definition: Element.h:51
const StringMap & getFilenameSubstitutions() const
Return the map of filename substring substitutions.
Definition: Element.h:1310
LeafData & operator=(const LeafData &)=delete
GLuint index
Definition: glcorearb.h:786
static const string UI_STEP_ATTRIBUTE
Definition: Element.h:1158
shared_ptr< T > getAncestorOfType()
Definition: Element.h:575
bool isA(const string &category=EMPTY_STRING) const
Definition: Element.h:386
const string & getUnit() const
Return the unit string of an element.
Definition: Element.h:1077
static const string TYPE_ATTRIBUTE
Definition: Element.h:925
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:62
static const string CATEGORY
Definition: Element.h:1230
void setGeomPrefix(const string &geomPrefix)
Set the geom prefix for this context.
Definition: Element.h:1277
static const string UI_FOLDER_ATTRIBUTE
Definition: Element.h:1153
bool hasGeomPrefix() const
Return true if the given element has a geom prefix string.
Definition: Element.h:209
static const string INHERIT_ATTRIBUTE
Definition: Element.h:820
virtual void registerChildElement(ElementPtr child)
bool getIsUniform() const
The the uniform attribute flag for this element.
Definition: Element.h:1115
std::set< string > StringSet
A set of strings.
Definition: Library.h:64
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
const string & getGeomPrefix() const
Return the element's geom prefix string.
Definition: Element.h:215
ConstElementPtr getSelf() const
Return our self pointer.
Definition: Element.h:544
shared_ptr< ValueElement > ValueElementPtr
A shared pointer to a ValueElement.
Definition: Element.h:41
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
static const string FILE_PREFIX_ATTRIBUTE
Definition: Element.h:817
static const string DOC_ATTRIBUTE
Definition: Element.h:822
const string & getGeomPrefix() const
Return the geom prefix for this context.
Definition: Element.h:1283
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
virtual size_t getUpstreamEdgeCount() const
Return the number of queryable upstream edges for this element.
Definition: Element.h:685
const string & getColorSpace() const
Return the element's color space string.
Definition: Element.h:251
bool hasSourceUri() const
Return true if this element has a source URI.
Definition: Element.h:728
Token(ElementPtr parent, const string &name)
Definition: Element.h:1173
Value::FloatFormat floatFormat
Floating point format to use for floating point value comparisons.
Definition: Element.h:1376
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:30
static const string UI_SOFT_MIN_ATTRIBUTE
Definition: Element.h:1156
virtual ~TypedElement()
Definition: Element.h:875
static const string NAMESPACE_ATTRIBUTE
Definition: Element.h:821
shared_ptr< T >(*)( CreatorFunction)
Definition: Library.h:43
int floatPrecision
Floating point precision to use for floating point value comparisons.
Definition: Element.h:1379
string _name
Definition: Element.h:839
shared_ptr< const Document > ConstDocumentPtr
A shared pointer to a const Document.
Definition: Document.h:24
void setGeomNameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for geometry name data values.
Definition: Element.h:1320
ElementMap _childMap
Definition: Element.h:842
shared_ptr< class TypeDef > TypeDefPtr
Definition: Element.h:878
FloatFormat
Float formats to use when converting values to strings.
Definition: Value.h:49
void setCategory(const string &category)
Set the element's category string.
Definition: Element.h:119
static const string UNIT_ATTRIBUTE
Definition: Element.h:1160
StringVec _attributeOrder
Definition: Element.h:846
MX_CORE_API string toValueString(const T &data)
Convert the given data value to a value string.
void removeChildOfType(const string &name)
Definition: Element.h:471
const string & getInheritString() const
Return the inherit string of this element.
Definition: Element.h:287
StringMap _filenameMap
Definition: Element.h:1353
const ElementVec & getChildren() const
Definition: Element.h:447
Definition: format.h:1821
CommentElement(ElementPtr parent, const string &name)
Definition: Element.h:1193
static const string XPOS_ATTRIBUTE
Definition: Element.h:823
virtual bool isAttributeEquivalent(ConstElementPtr rhs, const string &attributeName, const ElementEquivalenceOptions &options, string *message=nullptr) const
ElementVec _childOrder
Definition: Element.h:843