MaterialX 1.39.5
Loading...
Searching...
No Matches
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
11
13
15#include <MaterialXCore/Util.h>
16#include <MaterialXCore/Value.h>
17
18MATERIALX_NAMESPACE_BEGIN
19
20class Element;
21class TypedElement;
22class ValueElement;
23class Token;
24class CommentElement;
25class NewlineElement;
26class GenericElement;
27class StringResolver;
28class Document;
29
31using ElementPtr = shared_ptr<Element>;
33using ConstElementPtr = shared_ptr<const Element>;
34
36using TypedElementPtr = shared_ptr<TypedElement>;
38using ConstTypedElementPtr = shared_ptr<const TypedElement>;
39
41using ValueElementPtr = shared_ptr<ValueElement>;
43using ConstValueElementPtr = shared_ptr<const ValueElement>;
44
46using TokenPtr = shared_ptr<Token>;
48using ConstTokenPtr = shared_ptr<const Token>;
49
51using CommentElementPtr = shared_ptr<CommentElement>;
53using ConstCommentElementPtr = shared_ptr<const CommentElement>;
54
56using NewlineElementPtr = shared_ptr<NewlineElement>;
58using ConstNewlineElementPtr = shared_ptr<const NewlineElement>;
59
61using GenericElementPtr = shared_ptr<GenericElement>;
63using ConstGenericElementPtr = shared_ptr<const GenericElement>;
64
66using StringResolverPtr = shared_ptr<StringResolver>;
67
69using ElementVec = vector<ElementPtr>;
70
72using ElementMap = std::unordered_map<string, ElementPtr>;
73
75using ElementPredicate = std::function<bool(ConstElementPtr)>;
76
78
84class 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:
109 bool operator==(const Element& rhs) const;
110
113 bool operator!=(const Element& rhs) const;
114
117
119 void setCategory(const string& category)
120 {
121 _category = category;
122 }
123
127 const string& getCategory() const
128 {
129 return _category;
130 }
131
135
140 void setName(const string& name);
141
143 const string& getName() const
144 {
145 return _name;
146 }
147
153 string getNamePath(ConstElementPtr relativeTo = nullptr) const;
154
160 ElementPtr getDescendant(const string& namePath) const;
161
165
167 void setFilePrefix(const string& prefix)
168 {
169 setAttribute(FILE_PREFIX_ATTRIBUTE, prefix);
170 }
171
173 bool hasFilePrefix() const
174 {
175 return hasAttribute(FILE_PREFIX_ATTRIBUTE);
176 }
177
179 const string& getFilePrefix() const
180 {
181 return getAttribute(FILE_PREFIX_ATTRIBUTE);
182 }
183
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
201
203 void setGeomPrefix(const string& prefix)
204 {
205 setAttribute(GEOM_PREFIX_ATTRIBUTE, prefix);
206 }
207
209 bool hasGeomPrefix() const
210 {
211 return hasAttribute(GEOM_PREFIX_ATTRIBUTE);
212 }
213
215 const string& getGeomPrefix() const
216 {
217 return getAttribute(GEOM_PREFIX_ATTRIBUTE);
218 }
219
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
237
239 void setColorSpace(const string& colorSpace)
240 {
241 setAttribute(COLOR_SPACE_ATTRIBUTE, colorSpace);
242 }
243
245 bool hasColorSpace() const
246 {
247 return hasAttribute(COLOR_SPACE_ATTRIBUTE);
248 }
249
251 const string& getColorSpace() const
252 {
253 return getAttribute(COLOR_SPACE_ATTRIBUTE);
254 }
255
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
273
275 void setInheritString(const string& inherit)
276 {
277 setAttribute(INHERIT_ATTRIBUTE, inherit);
278 }
279
281 bool hasInheritString() const
282 {
283 return hasAttribute(INHERIT_ATTRIBUTE);
284 }
285
287 const string& getInheritString() const
288 {
289 return getAttribute(INHERIT_ATTRIBUTE);
290 }
291
294 {
295 if (super)
296 {
297 setInheritString(super->getName());
298 }
299 else
300 {
301 removeAttribute(INHERIT_ATTRIBUTE);
302 }
303 }
304
307 {
308 return hasInheritString() ? resolveNameReference<Element>(getInheritString()) : nullptr;
309 }
310
314
317
321
323 void setNamespace(const string& space)
324 {
325 setAttribute(NAMESPACE_ATTRIBUTE, space);
326 }
327
329 bool hasNamespace() const
330 {
331 return hasAttribute(NAMESPACE_ATTRIBUTE);
332 }
333
335 const string& getNamespace() const
336 {
337 return getAttribute(NAMESPACE_ATTRIBUTE);
338 }
339
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
366
368 void setDocString(const string& doc)
369 {
370 setAttribute(DOC_ATTRIBUTE, doc);
371 }
372
374 string getDocString() const
375 {
376 return getAttribute(DOC_ATTRIBUTE);
377 }
378
382
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
396 template <class T> shared_ptr<T> asA();
397
399 template <class T> shared_ptr<const T> asA() const;
400
404
412 template <class T> shared_ptr<T> addChild(const string& name = EMPTY_STRING);
413
424 ElementPtr addChildOfCategory(const string& category, string name = EMPTY_STRING);
425
431 ElementPtr changeChildCategory(ElementPtr child, const string& category);
432
437 ElementPtr getChild(const string& name) const;
438
442 template <class T> shared_ptr<T> getChildOfType(const string& name) const;
443
446 const ElementVec& getChildren() const
447 {
448 return _childOrder;
449 }
450
454 template <class T> vector<shared_ptr<T>> getChildrenOfType(const string& category = EMPTY_STRING) const;
455
458 void setChildIndex(const string& name, int index);
459
462 int getChildIndex(const string& name) const;
463
465 void removeChild(const string& name);
466
470 template <class T> void removeChildOfType(const string& name)
471 {
472 if (getChildOfType<T>(name))
473 removeChild(name);
474 }
475
479
481 void setAttribute(const string& attrib, const string& value);
482
484 bool hasAttribute(const string& attrib) const
485 {
486 return _attributeMap.count(attrib) != 0;
487 }
488
491 const string& getAttribute(const string& attrib) const
492 {
493 StringMap::const_iterator it = _attributeMap.find(attrib);
494 return (it != _attributeMap.end()) ? it->second : EMPTY_STRING;
495 }
496
499 {
500 return _attributeOrder;
501 }
502
506 template <class T> void setTypedAttribute(const string& attrib, const T& data)
507 {
508 setAttribute(attrib, toValueString(data));
509 }
510
514 template <class T> T getTypedAttribute(const string& attrib) const
515 {
516 if (hasAttribute(attrib))
517 {
518 try
519 {
520 return fromValueString<T>(getAttribute(attrib));
521 }
522 catch (ExceptionTypeError&)
523 {
524 }
525 }
526 return {};
527 }
528
530 void removeAttribute(const string& attrib);
531
535
538 {
539 return shared_from_this();
540 }
541
544 {
545 return shared_from_this();
546 }
547
550 {
551 return _parent.lock();
552 }
553
556 {
557 return _parent.lock();
558 }
559
562
565
567 DocumentPtr getDocument();
568
570 ConstDocumentPtr getDocument() const;
571
574 template <class T> shared_ptr<T> getAncestorOfType()
575 {
576 for (ElementPtr elem = getSelf(); elem; elem = elem->getParent())
577 {
578 shared_ptr<T> typedElem = elem->asA<T>();
579 if (typedElem)
580 {
581 return typedElem;
582 }
583 }
584 return nullptr;
585 }
586
589 template <class T> shared_ptr<const T> getAncestorOfType() const
590 {
591 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
592 {
593 shared_ptr<const T> typedElem = elem->asA<T>();
594 if (typedElem)
595 {
596 return typedElem;
597 }
598 }
599 return nullptr;
600 }
601
605
614 string* message = nullptr) const;
615
623 virtual bool isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
624 const ElementEquivalenceOptions& options,
625 string* message = nullptr) const;
626
630
650
675
681 virtual Edge getUpstreamEdge(size_t index = 0) const;
682
684 virtual size_t getUpstreamEdgeCount() const
685 {
686 return 0;
687 }
688
694 ElementPtr getUpstreamElement(size_t index = 0) const;
695
711
715
721 void setSourceUri(const string& sourceUri)
722 {
723 _sourceUri = sourceUri;
724 }
725
727 bool hasSourceUri() const
728 {
729 return !_sourceUri.empty();
730 }
731
733 const string& getSourceUri() const
734 {
735 return _sourceUri;
736 }
737
740 const string& getActiveSourceUri() const
741 {
742 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
743 {
744 if (elem->hasSourceUri())
745 {
746 return elem->getSourceUri();
747 }
748 }
749 return EMPTY_STRING;
750 }
751
755
758 virtual bool validate(string* message = nullptr) const;
759
763
766 void copyContentFrom(const ConstElementPtr& source);
767
769 virtual void clearContent();
770
773 string createValidChildName(string name) const
774 {
775 name = name.empty() ? "_" : createValidName(name);
776 while (getChild(name))
777 {
778 name = incrementName(name);
779 }
780 return name;
781 }
782
791 StringResolverPtr createStringResolver(const string& geom = EMPTY_STRING) const;
792
795 string asString() const;
796
798
799 protected:
800 // Resolve a reference to a named element at the scope of the given parent,
801 // taking the namespace at the scope of this element into account. If no parent
802 // is provided, then the root scope of the document is used.
803 template <class T> shared_ptr<T> resolveNameReference(const string& name, ConstElementPtr parent = nullptr) const
804 {
805 ConstElementPtr scope = parent ? parent : getRoot();
806 shared_ptr<T> child = scope->getChildOfType<T>(getQualifiedName(name));
807 return child ? child : scope->getChildOfType<T>(name);
808 }
809
810 // Enforce a requirement within a validate method, updating the validation
811 // state and optional output text if the requirement is not met.
812 void validateRequire(bool expression, bool& res, string* message, const string& errorDesc) const;
813
814 public:
815 static const string NAME_ATTRIBUTE;
816 static const string FILE_PREFIX_ATTRIBUTE;
817 static const string GEOM_PREFIX_ATTRIBUTE;
818 static const string COLOR_SPACE_ATTRIBUTE;
819 static const string INHERIT_ATTRIBUTE;
820 static const string NAMESPACE_ATTRIBUTE;
821 static const string DOC_ATTRIBUTE;
822 static const string XPOS_ATTRIBUTE;
823 static const string YPOS_ATTRIBUTE;
824
825 protected:
826 virtual void registerChildElement(ElementPtr child);
827 virtual void unregisterChildElement(ElementPtr child);
828
829 // Return a non-const copy of our self pointer, for use in constructing
830 // graph traversal objects that require non-const storage.
831 ElementPtr getSelfNonConst() const
832 {
833 return std::const_pointer_cast<Element>(shared_from_this());
834 }
835
836 protected:
837 string _category;
838 string _name;
839 string _sourceUri;
840
841 ElementMap _childMap;
842 ElementVec _childOrder;
843
844 StringMap _attributeMap;
845 StringVec _attributeOrder;
846
847 weak_ptr<Element> _parent;
848 weak_ptr<Element> _root;
849
850 private:
851 template <class T> static ElementPtr createElement(ElementPtr parent, const string& name)
852 {
853 return std::make_shared<T>(parent, name);
854 }
855
856 private:
857 using CreatorFunction = ElementPtr (*)(ElementPtr, const string&);
858 using CreatorMap = std::unordered_map<string, CreatorFunction>;
859
860 static CreatorMap _creatorMap;
861};
862
865class MX_CORE_API TypedElement : public Element
866{
867 protected:
868 TypedElement(ElementPtr parent, const string& category, const string& name) :
869 Element(parent, category, name)
870 {
871 }
872
873 public:
874 virtual ~TypedElement() { }
875
876 protected:
877 using TypeDefPtr = shared_ptr<class TypeDef>;
878
879 public:
882
884 void setType(const string& type)
885 {
886 setAttribute(TYPE_ATTRIBUTE, type);
887 }
888
890 bool hasType() const
891 {
892 return hasAttribute(TYPE_ATTRIBUTE);
893 }
894
896 virtual const string& getType() const
897 {
898 return getAttribute(TYPE_ATTRIBUTE);
899 }
900
902 bool isColorType() const
903 {
904 return getType() == "color3" || getType() == "color4";
905 }
906
908 bool isMultiOutputType() const
909 {
910 return getType() == MULTI_OUTPUT_TYPE_STRING;
911 }
912
916
919 TypeDefPtr getTypeDef() const;
920
922
923 public:
924 static const string TYPE_ATTRIBUTE;
925};
926
929class MX_CORE_API ValueElement : public TypedElement
930{
931 protected:
932 ValueElement(ElementPtr parent, const string& category, const string& name) :
933 TypedElement(parent, category, name)
934 {
935 }
936
937 public:
938 virtual ~ValueElement() { }
939
942
944 void setValueString(const string& value)
945 {
946 setAttribute(VALUE_ATTRIBUTE, value);
947 }
948
950 bool hasValueString() const
951 {
952 return hasAttribute(VALUE_ATTRIBUTE);
953 }
954
956 const string& getValueString() const
957 {
958 return getAttribute(VALUE_ATTRIBUTE);
959 }
960
966 string getResolvedValueString(StringResolverPtr resolver = nullptr) const;
967
971
973 void setInterfaceName(const string& name)
974 {
975 setAttribute(INTERFACE_NAME_ATTRIBUTE, name);
976 }
977
979 bool hasInterfaceName() const
980 {
981 return hasAttribute(INTERFACE_NAME_ATTRIBUTE);
982 }
983
985 const string& getInterfaceName() const
986 {
987 return getAttribute(INTERFACE_NAME_ATTRIBUTE);
988 }
989
993
995 void setImplementationName(const string& name)
996 {
997 setAttribute(IMPLEMENTATION_NAME_ATTRIBUTE, name);
998 }
999
1002 {
1003 return hasAttribute(IMPLEMENTATION_NAME_ATTRIBUTE);
1004 }
1005
1007 const string& getImplementationName() const
1008 {
1009 return getAttribute(IMPLEMENTATION_NAME_ATTRIBUTE);
1010 }
1011
1015
1017 template <class T> void setValue(const T& value, const string& type = EMPTY_STRING)
1018 {
1019 setType(!type.empty() ? type : getTypeString<T>());
1021 }
1022
1024 void setValue(const char* value, const string& type = EMPTY_STRING)
1025 {
1026 setValue(value ? string(value) : EMPTY_STRING, type);
1027 }
1028
1030 bool hasValue() const
1031 {
1032 return hasAttribute(VALUE_ATTRIBUTE);
1033 }
1034
1041
1051
1058
1062
1064 void setUnit(const string& unit)
1065 {
1066 setAttribute(UNIT_ATTRIBUTE, unit);
1067 }
1068
1070 bool hasUnit() const
1071 {
1072 return hasAttribute(UNIT_ATTRIBUTE);
1073 }
1074
1076 const string& getUnit() const
1077 {
1078 return getAttribute(UNIT_ATTRIBUTE);
1079 }
1080
1083 const string& getActiveUnit() const;
1084
1086 void setUnitType(const string& unit)
1087 {
1088 setAttribute(UNITTYPE_ATTRIBUTE, unit);
1089 }
1090
1092 bool hasUnitType() const
1093 {
1094 return hasAttribute(UNITTYPE_ATTRIBUTE);
1095 }
1096
1098 const string& getUnitType() const
1099 {
1100 return getAttribute(UNITTYPE_ATTRIBUTE);
1101 }
1102
1106
1108 void setIsUniform(bool value)
1109 {
1110 setTypedAttribute<bool>(UNIFORM_ATTRIBUTE, value);
1111 }
1112
1114 bool getIsUniform() const
1115 {
1116 return getTypedAttribute<bool>(UNIFORM_ATTRIBUTE);
1117 }
1118
1122
1130 bool isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
1131 const ElementEquivalenceOptions& options,
1132 string* message = nullptr) const override;
1133
1137
1140 bool validate(string* message = nullptr) const override;
1141
1143
1144 public:
1145 static const string VALUE_ATTRIBUTE;
1146 static const string INTERFACE_NAME_ATTRIBUTE;
1147 static const string IMPLEMENTATION_NAME_ATTRIBUTE;
1148 static const string IMPLEMENTATION_TYPE_ATTRIBUTE;
1149 static const string ENUM_ATTRIBUTE;
1150 static const string ENUM_VALUES_ATTRIBUTE;
1151 static const string UI_NAME_ATTRIBUTE;
1152 static const string UI_FOLDER_ATTRIBUTE;
1153 static const string UI_MIN_ATTRIBUTE;
1154 static const string UI_MAX_ATTRIBUTE;
1155 static const string UI_SOFT_MIN_ATTRIBUTE;
1156 static const string UI_SOFT_MAX_ATTRIBUTE;
1157 static const string UI_STEP_ATTRIBUTE;
1158 static const string UI_ADVANCED_ATTRIBUTE;
1159 static const string UNIT_ATTRIBUTE;
1160 static const string UNITTYPE_ATTRIBUTE;
1161 static const string UNIFORM_ATTRIBUTE;
1162};
1163
1169class MX_CORE_API Token : public ValueElement
1170{
1171 public:
1172 Token(ElementPtr parent, const string& name) :
1173 ValueElement(parent, CATEGORY, name)
1174 {
1175 }
1176 virtual ~Token() { }
1177
1178 public:
1179 static const string CATEGORY;
1180};
1181
1189class MX_CORE_API CommentElement : public Element
1190{
1191 public:
1192 CommentElement(ElementPtr parent, const string& name) :
1193 Element(parent, CATEGORY, name)
1194 {
1195 }
1196 virtual ~CommentElement() { }
1197
1198 public:
1199 static const string CATEGORY;
1200};
1201
1204class MX_CORE_API NewlineElement : public Element
1205{
1206 public:
1207 NewlineElement(ElementPtr parent, const string& name) :
1208 Element(parent, CATEGORY, name)
1209 {
1210 }
1211 virtual ~NewlineElement() { }
1212
1213 public:
1214 static const string CATEGORY;
1215};
1216
1219class MX_CORE_API GenericElement : public Element
1220{
1221 public:
1222 GenericElement(ElementPtr parent, const string& name) :
1223 Element(parent, CATEGORY, name)
1224 {
1225 }
1226 virtual ~GenericElement() { }
1227
1228 public:
1229 static const string CATEGORY;
1230};
1231
1245class MX_CORE_API StringResolver
1246{
1247 public:
1250 {
1251 return StringResolverPtr(new StringResolver());
1252 }
1253
1254 virtual ~StringResolver() { }
1255
1258
1260 void setFilePrefix(const string& filePrefix)
1261 {
1262 _filePrefix = filePrefix;
1263 }
1264
1266 const string& getFilePrefix() const
1267 {
1268 return _filePrefix;
1269 }
1270
1274
1276 void setGeomPrefix(const string& geomPrefix)
1277 {
1278 _geomPrefix = geomPrefix;
1279 }
1280
1282 const string& getGeomPrefix() const
1283 {
1284 return _geomPrefix;
1285 }
1286
1290
1293 void setUdimString(const string& udim);
1294
1297 void setUvTileString(const string& uvTile);
1298
1300 void setFilenameSubstitution(const string& key, const string& value)
1301 {
1302 _filenameMap[key] = value;
1303 }
1304
1307
1310 {
1311 return _filenameMap;
1312 }
1313
1317
1319 void setGeomNameSubstitution(const string& key, const string& value)
1320 {
1321 _geomNameMap[key] = value;
1322 }
1323
1326 {
1327 return _geomNameMap;
1328 }
1329
1333
1336 virtual string resolve(const string& str, const string& type) const;
1337
1339 static bool isResolvedType(const string& type)
1340 {
1341 return type == FILENAME_TYPE_STRING || type == GEOMNAME_TYPE_STRING;
1342 }
1343
1345
1346 protected:
1347 StringResolver() { }
1348
1349 protected:
1350 string _filePrefix;
1351 string _geomPrefix;
1352 StringMap _filenameMap;
1353 StringMap _geomNameMap;
1354};
1355
1358class MX_CORE_API ElementEquivalenceOptions
1359{
1360 public:
1361 ElementEquivalenceOptions()
1362 {
1367 };
1368 ~ElementEquivalenceOptions() = default;
1369
1373
1376
1379
1390};
1391
1395class MX_CORE_API ExceptionOrphanedElement : public Exception
1396{
1397 public:
1398 using Exception::Exception;
1399};
1400
1401template <class T> shared_ptr<T> Element::addChild(const string& name)
1402{
1403 string childName = name;
1404 if (childName.empty())
1405 {
1406 childName = createValidChildName(T::CATEGORY + "1");
1407 }
1408
1409 if (_childMap.count(childName))
1410 throw Exception("Child name is not unique: " + childName);
1411
1412 shared_ptr<T> child = std::make_shared<T>(getSelf(), childName);
1413 registerChildElement(child);
1414
1415 return child;
1416}
1417
1421MX_CORE_API bool targetStringsMatch(const string& target1, const string& target2);
1422
1425MX_CORE_API string prettyPrint(ConstElementPtr elem);
1426
1427MATERIALX_NAMESPACE_END
1428
1429#endif
shared_ptr< Token > TokenPtr
A shared pointer to a Token.
Definition Element.h:46
shared_ptr< const GenericElement > ConstGenericElementPtr
A shared pointer to a const GenericElement.
Definition Element.h:63
shared_ptr< TypedElement > TypedElementPtr
A shared pointer to a TypedElement.
Definition Element.h:36
shared_ptr< StringResolver > StringResolverPtr
A shared pointer to a StringResolver.
Definition Element.h:66
shared_ptr< const CommentElement > ConstCommentElementPtr
A shared pointer to a const CommentElement.
Definition Element.h:53
std::unordered_map< string, ElementPtr > ElementMap
A hash map from strings to elements.
Definition Element.h:72
shared_ptr< NewlineElement > NewlineElementPtr
A shared pointer to a NewlineElement.
Definition Element.h:56
shared_ptr< const Token > ConstTokenPtr
A shared pointer to a const Token.
Definition Element.h:48
shared_ptr< GenericElement > GenericElementPtr
A shared pointer to a GenericElement.
Definition Element.h:61
shared_ptr< const Element > ConstElementPtr
A shared pointer to a const Element.
Definition Element.h:33
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition Element.h:31
shared_ptr< ValueElement > ValueElementPtr
A shared pointer to a ValueElement.
Definition Element.h:41
shared_ptr< const ValueElement > ConstValueElementPtr
A shared pointer to a const ValueElement.
Definition Element.h:43
shared_ptr< CommentElement > CommentElementPtr
A shared pointer to a CommentElement.
Definition Element.h:51
shared_ptr< const TypedElement > ConstTypedElementPtr
A shared pointer to a const TypedElement.
Definition Element.h:38
std::function< bool(ConstElementPtr)> ElementPredicate
A standard function taking an ElementPtr and returning a boolean.
Definition Element.h:75
shared_ptr< const NewlineElement > ConstNewlineElementPtr
A shared pointer to a const NewlineElement.
Definition Element.h:58
MX_CORE_API string prettyPrint(ConstElementPtr elem)
Pretty print the given element tree, calling asString recursively on each element in depth-first orde...
vector< ElementPtr > ElementVec
A vector of elements.
Definition Element.h:69
MX_CORE_API bool targetStringsMatch(const string &target1, const string &target2)
Given two target strings, each containing a string array of target names, return true if they have an...
Import and export declarations for the Core library.
std::set< string > StringSet
A set of strings.
Definition Library.h:65
vector< string > StringVec
A vector of strings.
Definition Library.h:61
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition Library.h:63
Utility methods.
MX_CORE_API string incrementName(const string &name)
Increment the numeric suffix of a name.
MX_CORE_API string createValidName(string name, char replaceChar='_')
Create a valid MaterialX name from the given string.
Graph traversal classes.
Generic value classes.
MX_CORE_API T fromValueString(const string &value)
Convert the given value string to a data value of the given type.
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition Value.h:30
MX_CORE_API string toValueString(const T &data)
Convert the given data value to a value string.
MX_CORE_API const string & getTypeString()
Return the type string associated with the given data type.
An element representing a block of descriptive text within a document, which will be stored a comment...
Definition Element.h:1190
A MaterialX document, which represents the top-level element in the MaterialX ownership hierarchy.
Definition Document.h:32
An edge between two connected Elements, returned during graph traversal.
Definition Traversal.h:30
A set of options for comparing the functional equivalence of elements.
Definition Element.h:1359
StringSet attributeExclusionList
Specifies the set of attributes that should be excluded when performing a comparison.
Definition Element.h:1389
bool performValueComparisons
Perform value comparisons as opposed to literal string comparisons.
Definition Element.h:1372
int floatPrecision
Floating point precision to use for floating point value comparisons.
Definition Element.h:1378
Value::FloatFormat floatFormat
Floating point format to use for floating point value comparisons.
Definition Element.h:1375
The base class for MaterialX elements.
Definition Element.h:85
const string & getActiveSourceUri() const
Return the source URI that is active at the scope of this element, taking all ancestor elements into ...
Definition Element.h:740
bool hasNamespace() const
Return true if this element has a namespace string.
Definition Element.h:329
const string & getAttribute(const string &attrib) const
Return the value string of the given attribute.
Definition Element.h:491
ConstElementPtr getSelf() const
Return our self pointer.
Definition Element.h:543
const string & getActiveGeomPrefix() const
Return the geom prefix string that is active at the scope of this element, taking all ancestor elemen...
Definition Element.h:222
string asString() const
Return a single-line description of this element, including its category, name, and attributes.
bool isA(const string &category=EMPTY_STRING) const
Return true if this element belongs to the given subclass.
Definition Element.h:386
void setDocString(const string &doc)
Set the documentation string of this element.
Definition Element.h:368
shared_ptr< T > getAncestorOfType()
Return the first ancestor of the given subclass, or an empty shared pointer if no ancestor of this su...
Definition Element.h:574
bool hasGeomPrefix() const
Return true if the given element has a geom prefix string.
Definition Element.h:209
shared_ptr< const T > getAncestorOfType() const
Return the first ancestor of the given subclass, or an empty shared pointer if no ancestor of this su...
Definition Element.h:589
void setChildIndex(const string &name, int index)
Set the index of the child, if any, with the given name.
const string & getInheritString() const
Return the inherit string of this element.
Definition Element.h:287
TreeIterator traverseTree() const
Traverse the tree from the given element to each of its descendants in depth-first order,...
GraphIterator traverseGraph() const
Traverse the dataflow graph from the given element to each of its upstream sources in depth-first ord...
bool hasSourceUri() const
Return true if this element has a source URI.
Definition Element.h:727
void removeChild(const string &name)
Remove the child element, if any, with the given name.
ElementPtr changeChildCategory(ElementPtr child, const string &category)
Change the category of the given child element.
const string & getColorSpace() const
Return the element's color space string.
Definition Element.h:251
shared_ptr< T > addChild(const string &name=EMPTY_STRING)
Add a child element of the given subclass and name.
Definition Element.h:1401
const string & getName() const
Return the element's name string.
Definition Element.h:143
void setColorSpace(const string &colorSpace)
Set the element's color space string.
Definition Element.h:239
string getQualifiedName(const string &name) const
Return a qualified version of the given name, taking the namespace at the scope of this element into ...
Definition Element.h:342
string createValidChildName(string name) const
Using the input name as a starting point, modify it to create a valid, unique name for a child elemen...
Definition Element.h:773
shared_ptr< const T > asA() const
Dynamic cast to a const instance of the given subclass.
const string & getGeomPrefix() const
Return the element's geom prefix string.
Definition Element.h:215
void setFilePrefix(const string &prefix)
Set the element's file prefix string.
Definition Element.h:167
bool hasInheritedBase(ConstElementPtr base) const
Return true if this element has the given element as an inherited base, taking the full inheritance c...
bool hasInheritanceCycle() const
Return true if the inheritance chain for this element contains a cycle.
void setAttribute(const string &attrib, const string &value)
Set the value string of the given attribute.
const StringVec & getAttributeNames() const
Return a vector of stored attribute names, in the order they were set.
Definition Element.h:498
void setName(const string &name)
Set the element's name string.
virtual void clearContent()
Clear all attributes and descendants from this element.
int getChildIndex(const string &name) const
Return the index of the child, if any, with the given name.
ElementPtr getChild(const string &name) const
Return the child element, if any, with the given name.
const string & getSourceUri() const
Return the element's source URI.
Definition Element.h:733
virtual bool validate(string *message=nullptr) const
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
bool hasColorSpace() const
Return true if the given element has a color space string.
Definition Element.h:245
InheritanceIterator traverseInheritance() const
Traverse the inheritance chain from the given element to each element from which it inherits.
ElementPtr getRoot()
Return the root element of our tree.
const string & getFilePrefix() const
Return the element's file prefix string.
Definition Element.h:179
void copyContentFrom(const ConstElementPtr &source)
Copy all attributes and descendants from the given element to this one.
void setTypedAttribute(const string &attrib, const T &data)
Set the value of an implicitly typed attribute.
Definition Element.h:506
bool operator!=(const Element &rhs) const
Return true if the given element tree, including all descendants, differs from this one.
DocumentPtr getDocument()
Return the root document of our tree.
virtual size_t getUpstreamEdgeCount() const
Return the number of queryable upstream edges for this element.
Definition Element.h:684
StringResolverPtr createStringResolver(const string &geom=EMPTY_STRING) const
Construct a StringResolver at the scope of this element.
bool hasInheritString() const
Return true if this element has an inherit string.
Definition Element.h:281
const string & getActiveColorSpace() const
Return the color space string that is active at the scope of this element, taking all ancestor elemen...
Definition Element.h:258
shared_ptr< T > asA()
Dynamic cast to an instance of the given subclass.
ElementPtr getInheritsFrom() const
Return the element, if any, that this one directly inherits from.
Definition Element.h:306
ElementPtr getUpstreamElement(size_t index=0) const
Return the Element with the given index that lies directly upstream from this one in the dataflow gra...
T getTypedAttribute(const string &attrib) const
Return the value of an implicitly typed attribute.
Definition Element.h:514
void setCategory(const string &category)
Set the element's category string.
Definition Element.h:119
void removeAttribute(const string &attrib)
Remove the given attribute, if present.
bool isEquivalent(ConstElementPtr rhs, const ElementEquivalenceOptions &options, string *message=nullptr) const
Return true if the given element tree, including all descendents, is considered to be equivalent to t...
bool operator==(const Element &rhs) const
Return true if the given element tree, including all descendants, is identical to this one.
shared_ptr< T > getChildOfType(const string &name) const
Return the child element, if any, with the given name and subclass.
ElementPtr getDescendant(const string &namePath) const
Return the element specified by the given hierarchical name path, relative to the current element.
string getNamePath(ConstElementPtr relativeTo=nullptr) const
Return the element's hierarchical name path, relative to the root document.
ConstElementPtr getRoot() const
Return the root element of our tree.
void setGeomPrefix(const string &prefix)
Set the element's geom prefix string.
Definition Element.h:203
void setNamespace(const string &space)
Set the namespace string of this element.
Definition Element.h:323
ElementPtr getSelf()
Return our self pointer.
Definition Element.h:537
ConstElementPtr getParent() const
Return our parent element.
Definition Element.h:555
const ElementVec & getChildren() const
Return a constant vector of all child elements.
Definition Element.h:446
const string & getActiveFilePrefix() const
Return the file prefix string that is active at the scope of this element, taking all ancestor elemen...
Definition Element.h:186
void setInheritString(const string &inherit)
Set the inherit string of this element.
Definition Element.h:275
const string & getNamespace() const
Return the namespace string of this element.
Definition Element.h:335
ConstDocumentPtr getDocument() const
Return the root document of our tree.
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
Definition Element.h:484
void removeChildOfType(const string &name)
Remove the child element, if any, with the given name and subclass.
Definition Element.h:470
void setSourceUri(const string &sourceUri)
Set the element's source URI.
Definition Element.h:721
virtual bool isAttributeEquivalent(ConstElementPtr rhs, const string &attributeName, const ElementEquivalenceOptions &options, string *message=nullptr) const
Return true if the attribute on a given element is equivalent based on the equivalence criteria provi...
string getDocString() const
Return the documentation string of this element.
Definition Element.h:374
void setInheritsFrom(ConstElementPtr super)
Set the element that this one directly inherits from.
Definition Element.h:293
ElementPtr getParent()
Return our parent element.
Definition Element.h:549
bool hasFilePrefix() const
Return true if the given element has a file prefix string.
Definition Element.h:173
ElementPtr addChildOfCategory(const string &category, string name=EMPTY_STRING)
Add a child element of the given category and name.
const string & getCategory() const
Return the element's category string.
Definition Element.h:127
vector< shared_ptr< T > > getChildrenOfType(const string &category=EMPTY_STRING) const
Return a vector of all child elements that are instances of the given subclass, optionally filtered b...
virtual Edge getUpstreamEdge(size_t index=0) const
Return the Edge with the given index that lies directly upstream from this element in the dataflow gr...
The base class for exceptions that are propagated from the MaterialX library to the client applicatio...
Definition Exception.h:22
An exception that is thrown when an ElementPtr is used after its owning Document has gone out of scop...
Definition Element.h:1396
An exception that is thrown when a type mismatch is encountered.
Definition Exception.h:56
A generic element subclass, for instantiating elements with unrecognized categories.
Definition Element.h:1220
An iterator object representing the state of an upstream graph traversal.
Definition Traversal.h:192
An iterator object representing the current state of an inheritance traversal.
Definition Traversal.h:336
An element representing a newline within a document.
Definition Element.h:1205
A helper object for applying string modifiers to data values in the context of a specific element and...
Definition Element.h:1246
void setFilenameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for filename data values.
Definition Element.h:1300
static bool isResolvedType(const string &type)
Return true if the given type may be resolved by this class.
Definition Element.h:1339
const string & getGeomPrefix() const
Return the geom prefix for this context.
Definition Element.h:1282
void setUvTileString(const string &uvTile)
Set the UV-tile substring substitution for filename data values.
const string & getFilePrefix() const
Return the file prefix for this context.
Definition Element.h:1266
const StringMap & getGeomNameSubstitutions() const
Return the map of geometry name substring substitutions.
Definition Element.h:1325
void addTokenSubstitutions(ConstElementPtr element)
Add filename token substitutions for a given element.
void setFilePrefix(const string &filePrefix)
Set the file prefix for this context.
Definition Element.h:1260
virtual string resolve(const string &str, const string &type) const
Given an input string and type, apply all appropriate modifiers and return the resulting string.
void setUdimString(const string &udim)
Set the UDIM substring substitution for filename data values.
void setGeomPrefix(const string &geomPrefix)
Set the geom prefix for this context.
Definition Element.h:1276
const StringMap & getFilenameSubstitutions() const
Return the map of filename substring substitutions.
Definition Element.h:1309
void setGeomNameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for geometry name data values.
Definition Element.h:1319
static StringResolverPtr create()
Create a new string resolver.
Definition Element.h:1249
A token element representing a string value.
Definition Element.h:1170
An iterator object representing the state of a tree traversal.
Definition Traversal.h:89
The base class for typed elements.
Definition Element.h:866
virtual const string & getType() const
Return the element's type string.
Definition Element.h:896
bool isColorType() const
Return true if the element is of color type.
Definition Element.h:902
void setType(const string &type)
Set the element's type string.
Definition Element.h:884
bool isMultiOutputType() const
Return true if the element is of multi-output type.
Definition Element.h:908
TypeDefPtr getTypeDef() const
Return the TypeDef declaring the type string of this element.
bool hasType() const
Return true if the given element has a type string.
Definition Element.h:890
The base class for elements that support typed values.
Definition Element.h:930
ValuePtr getValue() const
Return the typed value of an element as a generic value object, which may be queried to access its da...
bool isAttributeEquivalent(ConstElementPtr rhs, const string &attributeName, const ElementEquivalenceOptions &options, string *message=nullptr) const override
Return true if the attribute on a given element is equivalent based on the equivalence criteria provi...
void setUnit(const string &unit)
Set the unit string of an element.
Definition Element.h:1064
const string & getValueString() const
Get the value string of a element.
Definition Element.h:956
void setInterfaceName(const string &name)
Set the interface name of an element.
Definition Element.h:973
ValuePtr getDefaultValue() const
Return the default value for this element as a generic value object, which may be queried to access i...
string getResolvedValueString(StringResolverPtr resolver=nullptr) const
Return the resolved value string of an element, applying any string substitutions that are defined at...
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
ValuePtr getResolvedValue(StringResolverPtr resolver=nullptr) const
Return the resolved value of an element as a generic value object, which may be queried to access its...
const string & getImplementationName() const
Return the implementation name of an element.
Definition Element.h:1007
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:1024
bool hasUnitType() const
Return true if the given element has a unit type.
Definition Element.h:1092
void setIsUniform(bool value)
Set the uniform attribute flag on this element.
Definition Element.h:1108
const string & getUnit() const
Return the unit string of an element.
Definition Element.h:1076
bool getIsUniform() const
The the uniform attribute flag for this element.
Definition Element.h:1114
void setValueString(const string &value)
Set the value string of an element.
Definition Element.h:944
void setValue(const T &value, const string &type=EMPTY_STRING)
Set the typed value of an element.
Definition Element.h:1017
bool hasUnit() const
Return true if the given element has a unit string.
Definition Element.h:1070
bool hasInterfaceName() const
Return true if the given element has an interface name.
Definition Element.h:979
void setUnitType(const string &unit)
Set the unit type of an element.
Definition Element.h:1086
bool hasImplementationName() const
Return true if the given element has an implementation name.
Definition Element.h:1001
void setImplementationName(const string &name)
Set the implementation name of an element.
Definition Element.h:995
const string & getActiveUnit() const
Return the unit defined by the associated NodeDef if this element is a child of a Node.
const string & getInterfaceName() const
Return the interface name of an element.
Definition Element.h:985
bool hasValueString() const
Return true if the given element has a value string.
Definition Element.h:950
const string & getUnitType() const
Return the unit type of an element.
Definition Element.h:1098
bool hasValue() const
Return true if the element possesses a typed value.
Definition Element.h:1030
static FloatFormat getFloatFormat()
Return the current float format.
FloatFormat
Float formats to use when converting values to strings.
Definition Value.h:50
static int getFloatPrecision()
Return the current float precision.