MaterialX 1.39.2
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 ElementMap = std::unordered_map<string, ElementPtr>;
70
72using ElementPredicate = std::function<bool(ConstElementPtr)>;
73
75
81class MX_CORE_API Element : public std::enable_shared_from_this<Element>
82{
83 protected:
84 Element(ElementPtr parent, const string& category, const string& name) :
85 _category(category),
86 _name(name),
87 _parent(parent),
88 _root(parent ? parent->getRoot() : nullptr)
89 {
90 }
91
92 public:
93 virtual ~Element() { }
94 Element(const Element&) = delete;
95 Element& operator=(const Element&) = delete;
96
97 protected:
98 using DocumentPtr = shared_ptr<Document>;
99 using ConstDocumentPtr = shared_ptr<const Document>;
100
101 template <class T> friend class ElementRegistry;
102
103 public:
106 bool operator==(const Element& rhs) const;
107
110 bool operator!=(const Element& rhs) const;
111
114
116 void setCategory(const string& category)
117 {
118 _category = category;
119 }
120
124 const string& getCategory() const
125 {
126 return _category;
127 }
128
132
137 void setName(const string& name);
138
140 const string& getName() const
141 {
142 return _name;
143 }
144
150 string getNamePath(ConstElementPtr relativeTo = nullptr) const;
151
157 ElementPtr getDescendant(const string& namePath) const;
158
162
164 void setFilePrefix(const string& prefix)
165 {
166 setAttribute(FILE_PREFIX_ATTRIBUTE, prefix);
167 }
168
170 bool hasFilePrefix() const
171 {
172 return hasAttribute(FILE_PREFIX_ATTRIBUTE);
173 }
174
176 const string& getFilePrefix() const
177 {
178 return getAttribute(FILE_PREFIX_ATTRIBUTE);
179 }
180
183 const string& getActiveFilePrefix() const
184 {
185 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
186 {
187 if (elem->hasFilePrefix())
188 {
189 return elem->getFilePrefix();
190 }
191 }
192 return EMPTY_STRING;
193 }
194
198
200 void setGeomPrefix(const string& prefix)
201 {
202 setAttribute(GEOM_PREFIX_ATTRIBUTE, prefix);
203 }
204
206 bool hasGeomPrefix() const
207 {
208 return hasAttribute(GEOM_PREFIX_ATTRIBUTE);
209 }
210
212 const string& getGeomPrefix() const
213 {
214 return getAttribute(GEOM_PREFIX_ATTRIBUTE);
215 }
216
219 const string& getActiveGeomPrefix() const
220 {
221 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
222 {
223 if (elem->hasGeomPrefix())
224 {
225 return elem->getGeomPrefix();
226 }
227 }
228 return EMPTY_STRING;
229 }
230
234
236 void setColorSpace(const string& colorSpace)
237 {
238 setAttribute(COLOR_SPACE_ATTRIBUTE, colorSpace);
239 }
240
242 bool hasColorSpace() const
243 {
244 return hasAttribute(COLOR_SPACE_ATTRIBUTE);
245 }
246
248 const string& getColorSpace() const
249 {
250 return getAttribute(COLOR_SPACE_ATTRIBUTE);
251 }
252
255 const string& getActiveColorSpace() const
256 {
257 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
258 {
259 if (elem->hasColorSpace())
260 {
261 return elem->getColorSpace();
262 }
263 }
264 return EMPTY_STRING;
265 }
266
270
272 void setInheritString(const string& inherit)
273 {
274 setAttribute(INHERIT_ATTRIBUTE, inherit);
275 }
276
278 bool hasInheritString() const
279 {
280 return hasAttribute(INHERIT_ATTRIBUTE);
281 }
282
284 const string& getInheritString() const
285 {
286 return getAttribute(INHERIT_ATTRIBUTE);
287 }
288
291 {
292 if (super)
293 {
294 setInheritString(super->getName());
295 }
296 else
297 {
298 removeAttribute(INHERIT_ATTRIBUTE);
299 }
300 }
301
304 {
305 return hasInheritString() ? resolveNameReference<Element>(getInheritString()) : nullptr;
306 }
307
311
314
318
320 void setNamespace(const string& space)
321 {
322 setAttribute(NAMESPACE_ATTRIBUTE, space);
323 }
324
326 bool hasNamespace() const
327 {
328 return hasAttribute(NAMESPACE_ATTRIBUTE);
329 }
330
332 const string& getNamespace() const
333 {
334 return getAttribute(NAMESPACE_ATTRIBUTE);
335 }
336
339 string getQualifiedName(const string& name) const
340 {
341 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
342 {
343 const string& namespaceStr = elem->getNamespace();
344 if (!namespaceStr.empty())
345 {
346 // Check if the name is qualified already.
347 const size_t i = name.find_first_of(NAME_PREFIX_SEPARATOR);
348 if (i != string::npos && name.substr(0, i) == namespaceStr)
349 {
350 // The name is already qualified with this namespace,
351 // so just return it as is.
352 return name;
353 }
354 return namespaceStr + NAME_PREFIX_SEPARATOR + name;
355 }
356 }
357 return name;
358 }
359
363
365 void setDocString(const string& doc)
366 {
367 setAttribute(DOC_ATTRIBUTE, doc);
368 }
369
371 string getDocString() const
372 {
373 return getAttribute(DOC_ATTRIBUTE);
374 }
375
379
383 template <class T> bool isA(const string& category = EMPTY_STRING) const
384 {
385 if (!asA<T>())
386 return false;
387 if (!category.empty() && getCategory() != category)
388 return false;
389 return true;
390 }
391
393 template <class T> shared_ptr<T> asA();
394
396 template <class T> shared_ptr<const T> asA() const;
397
401
409 template <class T> shared_ptr<T> addChild(const string& name = EMPTY_STRING);
410
421 ElementPtr addChildOfCategory(const string& category, string name = EMPTY_STRING);
422
428 ElementPtr changeChildCategory(ElementPtr child, const string& category);
429
431 ElementPtr getChild(const string& name) const
432 {
433 ElementMap::const_iterator it = _childMap.find(name);
434 return (it != _childMap.end()) ? it->second : ElementPtr();
435 }
436
440 template <class T> shared_ptr<T> getChildOfType(const string& name) const;
441
444 const vector<ElementPtr>& getChildren() const
445 {
446 return _childOrder;
447 }
448
452 template <class T> vector<shared_ptr<T>> getChildrenOfType(const string& category = EMPTY_STRING) const;
453
456 void setChildIndex(const string& name, int index);
457
460 int getChildIndex(const string& name) const;
461
463 void removeChild(const string& name);
464
468 template <class T> void removeChildOfType(const string& name)
469 {
470 if (getChildOfType<T>(name))
471 removeChild(name);
472 }
473
477
479 void setAttribute(const string& attrib, const string& value);
480
482 bool hasAttribute(const string& attrib) const
483 {
484 return _attributeMap.count(attrib) != 0;
485 }
486
489 const string& getAttribute(const string& attrib) const
490 {
491 StringMap::const_iterator it = _attributeMap.find(attrib);
492 return (it != _attributeMap.end()) ? it->second : EMPTY_STRING;
493 }
494
497 {
498 return _attributeOrder;
499 }
500
504 template <class T> void setTypedAttribute(const string& attrib, const T& data)
505 {
506 setAttribute(attrib, toValueString(data));
507 }
508
512 template <class T> T getTypedAttribute(const string& attrib) const
513 {
514 if (hasAttribute(attrib))
515 {
516 try
517 {
518 return fromValueString<T>(getAttribute(attrib));
519 }
520 catch (ExceptionTypeError&)
521 {
522 }
523 }
524 return {};
525 }
526
528 void removeAttribute(const string& attrib);
529
533
536 {
537 return shared_from_this();
538 }
539
542 {
543 return shared_from_this();
544 }
545
548 {
549 return _parent.lock();
550 }
551
554 {
555 return _parent.lock();
556 }
557
560
563
565 DocumentPtr getDocument();
566
568 ConstDocumentPtr getDocument() const;
569
572 template <class T> shared_ptr<T> getAncestorOfType()
573 {
574 for (ElementPtr elem = getSelf(); elem; elem = elem->getParent())
575 {
576 shared_ptr<T> typedElem = elem->asA<T>();
577 if (typedElem)
578 {
579 return typedElem;
580 }
581 }
582 return nullptr;
583 }
584
587 template <class T> shared_ptr<const T> getAncestorOfType() const
588 {
589 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
590 {
591 shared_ptr<const T> typedElem = elem->asA<T>();
592 if (typedElem)
593 {
594 return typedElem;
595 }
596 }
597 return nullptr;
598 }
599
603
612 string* message = nullptr) const;
613
621 virtual bool isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
622 const ElementEquivalenceOptions& options,
623 string* message = nullptr) const;
624
628
648
673
679 virtual Edge getUpstreamEdge(size_t index = 0) const;
680
682 virtual size_t getUpstreamEdgeCount() const
683 {
684 return 0;
685 }
686
692 ElementPtr getUpstreamElement(size_t index = 0) const;
693
709
713
719 void setSourceUri(const string& sourceUri)
720 {
721 _sourceUri = sourceUri;
722 }
723
725 bool hasSourceUri() const
726 {
727 return !_sourceUri.empty();
728 }
729
731 const string& getSourceUri() const
732 {
733 return _sourceUri;
734 }
735
738 const string& getActiveSourceUri() const
739 {
740 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
741 {
742 if (elem->hasSourceUri())
743 {
744 return elem->getSourceUri();
745 }
746 }
747 return EMPTY_STRING;
748 }
749
753
756 virtual bool validate(string* message = nullptr) const;
757
761
764 void copyContentFrom(const ConstElementPtr& source);
765
767 virtual void clearContent();
768
771 string createValidChildName(string name) const
772 {
773 name = name.empty() ? "_" : createValidName(name);
774 while (_childMap.count(name))
775 {
776 name = incrementName(name);
777 }
778 return name;
779 }
780
789 StringResolverPtr createStringResolver(const string& geom = EMPTY_STRING) const;
790
793 string asString() const;
794
796
797 protected:
798 // Resolve a reference to a named element at the scope of the given parent,
799 // taking the namespace at the scope of this element into account. If no parent
800 // is provided, then the root scope of the document is used.
801 template <class T> shared_ptr<T> resolveNameReference(const string& name, ConstElementPtr parent = nullptr) const
802 {
803 ConstElementPtr scope = parent ? parent : getRoot();
804 shared_ptr<T> child = scope->getChildOfType<T>(getQualifiedName(name));
805 return child ? child : scope->getChildOfType<T>(name);
806 }
807
808 // Enforce a requirement within a validate method, updating the validation
809 // state and optional output text if the requirement is not met.
810 void validateRequire(bool expression, bool& res, string* message, const string& errorDesc) const;
811
812 public:
813 static const string NAME_ATTRIBUTE;
814 static const string FILE_PREFIX_ATTRIBUTE;
815 static const string GEOM_PREFIX_ATTRIBUTE;
816 static const string COLOR_SPACE_ATTRIBUTE;
817 static const string INHERIT_ATTRIBUTE;
818 static const string NAMESPACE_ATTRIBUTE;
819 static const string DOC_ATTRIBUTE;
820 static const string XPOS_ATTRIBUTE;
821 static const string YPOS_ATTRIBUTE;
822
823 protected:
824 virtual void registerChildElement(ElementPtr child);
825 virtual void unregisterChildElement(ElementPtr child);
826
827 // Return a non-const copy of our self pointer, for use in constructing
828 // graph traversal objects that require non-const storage.
829 ElementPtr getSelfNonConst() const
830 {
831 return std::const_pointer_cast<Element>(shared_from_this());
832 }
833
834 protected:
835 string _category;
836 string _name;
837 string _sourceUri;
838
839 ElementMap _childMap;
840 vector<ElementPtr> _childOrder;
841
842 StringMap _attributeMap;
843 StringVec _attributeOrder;
844
845 weak_ptr<Element> _parent;
846 weak_ptr<Element> _root;
847
848 private:
849 template <class T> static ElementPtr createElement(ElementPtr parent, const string& name)
850 {
851 return std::make_shared<T>(parent, name);
852 }
853
854 private:
855 using CreatorFunction = ElementPtr (*)(ElementPtr, const string&);
856 using CreatorMap = std::unordered_map<string, CreatorFunction>;
857
858 static CreatorMap _creatorMap;
859};
860
863class MX_CORE_API TypedElement : public Element
864{
865 protected:
866 TypedElement(ElementPtr parent, const string& category, const string& name) :
867 Element(parent, category, name)
868 {
869 }
870
871 public:
872 virtual ~TypedElement() { }
873
874 protected:
875 using TypeDefPtr = shared_ptr<class TypeDef>;
876
877 public:
880
882 void setType(const string& type)
883 {
884 setAttribute(TYPE_ATTRIBUTE, type);
885 }
886
888 bool hasType() const
889 {
890 return hasAttribute(TYPE_ATTRIBUTE);
891 }
892
894 virtual const string& getType() const
895 {
896 return getAttribute(TYPE_ATTRIBUTE);
897 }
898
900 bool isColorType() const
901 {
902 return getType() == "color3" || getType() == "color4";
903 }
904
906 bool isMultiOutputType() const
907 {
908 return getType() == MULTI_OUTPUT_TYPE_STRING;
909 }
910
914
917 TypeDefPtr getTypeDef() const;
918
920
921 public:
922 static const string TYPE_ATTRIBUTE;
923};
924
927class MX_CORE_API ValueElement : public TypedElement
928{
929 protected:
930 ValueElement(ElementPtr parent, const string& category, const string& name) :
931 TypedElement(parent, category, name)
932 {
933 }
934
935 public:
936 virtual ~ValueElement() { }
937
940
942 void setValueString(const string& value)
943 {
944 setAttribute(VALUE_ATTRIBUTE, value);
945 }
946
948 bool hasValueString() const
949 {
950 return hasAttribute(VALUE_ATTRIBUTE);
951 }
952
954 const string& getValueString() const
955 {
956 return getAttribute(VALUE_ATTRIBUTE);
957 }
958
964 string getResolvedValueString(StringResolverPtr resolver = nullptr) const;
965
969
971 void setInterfaceName(const string& name)
972 {
973 setAttribute(INTERFACE_NAME_ATTRIBUTE, name);
974 }
975
977 bool hasInterfaceName() const
978 {
979 return hasAttribute(INTERFACE_NAME_ATTRIBUTE);
980 }
981
983 const string& getInterfaceName() const
984 {
985 return getAttribute(INTERFACE_NAME_ATTRIBUTE);
986 }
987
991
993 void setImplementationName(const string& name)
994 {
995 setAttribute(IMPLEMENTATION_NAME_ATTRIBUTE, name);
996 }
997
1000 {
1001 return hasAttribute(IMPLEMENTATION_NAME_ATTRIBUTE);
1002 }
1003
1005 const string& getImplementationName() const
1006 {
1007 return getAttribute(IMPLEMENTATION_NAME_ATTRIBUTE);
1008 }
1009
1013
1015 template <class T> void setValue(const T& value, const string& type = EMPTY_STRING)
1016 {
1017 setType(!type.empty() ? type : getTypeString<T>());
1019 }
1020
1022 void setValue(const char* value, const string& type = EMPTY_STRING)
1023 {
1024 setValue(value ? string(value) : EMPTY_STRING, type);
1025 }
1026
1028 bool hasValue() const
1029 {
1030 return hasAttribute(VALUE_ATTRIBUTE);
1031 }
1032
1039
1049
1056
1060
1062 void setUnit(const string& unit)
1063 {
1064 setAttribute(UNIT_ATTRIBUTE, unit);
1065 }
1066
1068 bool hasUnit() const
1069 {
1070 return hasAttribute(UNIT_ATTRIBUTE);
1071 }
1072
1074 const string& getUnit() const
1075 {
1076 return getAttribute(UNIT_ATTRIBUTE);
1077 }
1078
1081 const string& getActiveUnit() const;
1082
1084 void setUnitType(const string& unit)
1085 {
1086 setAttribute(UNITTYPE_ATTRIBUTE, unit);
1087 }
1088
1090 bool hasUnitType() const
1091 {
1092 return hasAttribute(UNITTYPE_ATTRIBUTE);
1093 }
1094
1096 const string& getUnitType() const
1097 {
1098 return getAttribute(UNITTYPE_ATTRIBUTE);
1099 }
1100
1104
1106 void setIsUniform(bool value)
1107 {
1108 setTypedAttribute<bool>(UNIFORM_ATTRIBUTE, value);
1109 }
1110
1112 bool getIsUniform() const
1113 {
1114 return getTypedAttribute<bool>(UNIFORM_ATTRIBUTE);
1115 }
1116
1120
1128 bool isAttributeEquivalent(ConstElementPtr rhs, const string& attributeName,
1129 const ElementEquivalenceOptions& options,
1130 string* message = nullptr) const override;
1131
1135
1138 bool validate(string* message = nullptr) const override;
1139
1141
1142 public:
1143 static const string VALUE_ATTRIBUTE;
1144 static const string INTERFACE_NAME_ATTRIBUTE;
1145 static const string IMPLEMENTATION_NAME_ATTRIBUTE;
1146 static const string IMPLEMENTATION_TYPE_ATTRIBUTE;
1147 static const string ENUM_ATTRIBUTE;
1148 static const string ENUM_VALUES_ATTRIBUTE;
1149 static const string UI_NAME_ATTRIBUTE;
1150 static const string UI_FOLDER_ATTRIBUTE;
1151 static const string UI_MIN_ATTRIBUTE;
1152 static const string UI_MAX_ATTRIBUTE;
1153 static const string UI_SOFT_MIN_ATTRIBUTE;
1154 static const string UI_SOFT_MAX_ATTRIBUTE;
1155 static const string UI_STEP_ATTRIBUTE;
1156 static const string UI_ADVANCED_ATTRIBUTE;
1157 static const string UNIT_ATTRIBUTE;
1158 static const string UNITTYPE_ATTRIBUTE;
1159 static const string UNIFORM_ATTRIBUTE;
1160};
1161
1167class MX_CORE_API Token : public ValueElement
1168{
1169 public:
1170 Token(ElementPtr parent, const string& name) :
1171 ValueElement(parent, CATEGORY, name)
1172 {
1173 }
1174 virtual ~Token() { }
1175
1176 public:
1177 static const string CATEGORY;
1178};
1179
1187class MX_CORE_API CommentElement : public Element
1188{
1189 public:
1190 CommentElement(ElementPtr parent, const string& name) :
1191 Element(parent, CATEGORY, name)
1192 {
1193 }
1194 virtual ~CommentElement() { }
1195
1196 public:
1197 static const string CATEGORY;
1198};
1199
1202class MX_CORE_API NewlineElement : public Element
1203{
1204 public:
1205 NewlineElement(ElementPtr parent, const string& name) :
1206 Element(parent, CATEGORY, name)
1207 {
1208 }
1209 virtual ~NewlineElement() { }
1210
1211 public:
1212 static const string CATEGORY;
1213};
1214
1217class MX_CORE_API GenericElement : public Element
1218{
1219 public:
1220 GenericElement(ElementPtr parent, const string& name) :
1221 Element(parent, CATEGORY, name)
1222 {
1223 }
1224 virtual ~GenericElement() { }
1225
1226 public:
1227 static const string CATEGORY;
1228};
1229
1243class MX_CORE_API StringResolver
1244{
1245 public:
1248 {
1249 return StringResolverPtr(new StringResolver());
1250 }
1251
1252 virtual ~StringResolver() { }
1253
1256
1258 void setFilePrefix(const string& filePrefix)
1259 {
1260 _filePrefix = filePrefix;
1261 }
1262
1264 const string& getFilePrefix() const
1265 {
1266 return _filePrefix;
1267 }
1268
1272
1274 void setGeomPrefix(const string& geomPrefix)
1275 {
1276 _geomPrefix = geomPrefix;
1277 }
1278
1280 const string& getGeomPrefix() const
1281 {
1282 return _geomPrefix;
1283 }
1284
1288
1291 void setUdimString(const string& udim);
1292
1295 void setUvTileString(const string& uvTile);
1296
1298 void setFilenameSubstitution(const string& key, const string& value)
1299 {
1300 _filenameMap[key] = value;
1301 }
1302
1305
1308 {
1309 return _filenameMap;
1310 }
1311
1315
1317 void setGeomNameSubstitution(const string& key, const string& value)
1318 {
1319 _geomNameMap[key] = value;
1320 }
1321
1324 {
1325 return _geomNameMap;
1326 }
1327
1331
1334 virtual string resolve(const string& str, const string& type) const;
1335
1337 static bool isResolvedType(const string& type)
1338 {
1339 return type == FILENAME_TYPE_STRING || type == GEOMNAME_TYPE_STRING;
1340 }
1341
1343
1344 protected:
1345 StringResolver() { }
1346
1347 protected:
1348 string _filePrefix;
1349 string _geomPrefix;
1350 StringMap _filenameMap;
1351 StringMap _geomNameMap;
1352};
1353
1356class MX_CORE_API ElementEquivalenceOptions
1357{
1358 public:
1359 ElementEquivalenceOptions()
1360 {
1365 };
1366 ~ElementEquivalenceOptions() { }
1367
1371
1374
1377
1388};
1389
1393class MX_CORE_API ExceptionOrphanedElement : public Exception
1394{
1395 public:
1396 using Exception::Exception;
1397};
1398
1399template <class T> shared_ptr<T> Element::addChild(const string& name)
1400{
1401 string childName = name;
1402 if (childName.empty())
1403 {
1404 childName = createValidChildName(T::CATEGORY + "1");
1405 }
1406
1407 if (_childMap.count(childName))
1408 throw Exception("Child name is not unique: " + childName);
1409
1410 shared_ptr<T> child = std::make_shared<T>(getSelf(), childName);
1411 registerChildElement(child);
1412
1413 return child;
1414}
1415
1419MX_CORE_API bool targetStringsMatch(const string& target1, const string& target2);
1420
1423MX_CORE_API string prettyPrint(ConstElementPtr elem);
1424
1425MATERIALX_NAMESPACE_END
1426
1427#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:69
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:72
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...
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:64
vector< string > StringVec
A vector of strings.
Definition Library.h:60
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition Library.h:62
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:1188
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:1357
StringSet attributeExclusionList
Specifies the set of attributes that should be excluded when performing a comparison.
Definition Element.h:1387
bool performValueComparisons
Perform value comparisons as opposed to literal string comparisons.
Definition Element.h:1370
int floatPrecision
Floating point precision to use for floating point value comparisons.
Definition Element.h:1376
Value::FloatFormat floatFormat
Floating point format to use for floating point value comparisons.
Definition Element.h:1373
The base class for MaterialX elements.
Definition Element.h:82
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:738
bool hasNamespace() const
Return true if this element has a namespace string.
Definition Element.h:326
const string & getAttribute(const string &attrib) const
Return the value string of the given attribute.
Definition Element.h:489
ConstElementPtr getSelf() const
Return our self pointer.
Definition Element.h:541
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:219
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:383
void setDocString(const string &doc)
Set the documentation string of this element.
Definition Element.h:365
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:572
bool hasGeomPrefix() const
Return true if the given element has a geom prefix string.
Definition Element.h:206
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:587
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:284
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:725
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:248
shared_ptr< T > addChild(const string &name=EMPTY_STRING)
Add a child element of the given subclass and name.
Definition Element.h:1399
const string & getName() const
Return the element's name string.
Definition Element.h:140
void setColorSpace(const string &colorSpace)
Set the element's color space string.
Definition Element.h:236
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:339
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:771
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:212
void setFilePrefix(const string &prefix)
Set the element's file prefix string.
Definition Element.h:164
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:496
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.
Definition Element.h:431
const string & getSourceUri() const
Return the element's source URI.
Definition Element.h:731
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:242
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:176
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:504
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:682
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:278
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:255
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:303
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:512
void setCategory(const string &category)
Set the element's category string.
Definition Element.h:116
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:200
void setNamespace(const string &space)
Set the namespace string of this element.
Definition Element.h:320
ElementPtr getSelf()
Return our self pointer.
Definition Element.h:535
const vector< ElementPtr > & getChildren() const
Return a constant vector of all child elements.
Definition Element.h:444
ConstElementPtr getParent() const
Return our parent element.
Definition Element.h:553
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:183
void setInheritString(const string &inherit)
Set the inherit string of this element.
Definition Element.h:272
const string & getNamespace() const
Return the namespace string of this element.
Definition Element.h:332
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:482
void removeChildOfType(const string &name)
Remove the child element, if any, with the given name and subclass.
Definition Element.h:468
void setSourceUri(const string &sourceUri)
Set the element's source URI.
Definition Element.h:719
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:371
void setInheritsFrom(ConstElementPtr super)
Set the element that this one directly inherits from.
Definition Element.h:290
ElementPtr getParent()
Return our parent element.
Definition Element.h:547
bool hasFilePrefix() const
Return true if the given element has a file prefix string.
Definition Element.h:170
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:124
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:1394
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:1218
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:1203
A helper object for applying string modifiers to data values in the context of a specific element and...
Definition Element.h:1244
void setFilenameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for filename data values.
Definition Element.h:1298
static bool isResolvedType(const string &type)
Return true if the given type may be resolved by this class.
Definition Element.h:1337
const string & getGeomPrefix() const
Return the geom prefix for this context.
Definition Element.h:1280
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:1264
const StringMap & getGeomNameSubstitutions() const
Return the map of geometry name substring substitutions.
Definition Element.h:1323
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:1258
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:1274
const StringMap & getFilenameSubstitutions() const
Return the map of filename substring substitutions.
Definition Element.h:1307
void setGeomNameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for geometry name data values.
Definition Element.h:1317
static StringResolverPtr create()
Create a new string resolver.
Definition Element.h:1247
A token element representing a string value.
Definition Element.h:1168
An iterator object representing the state of a tree traversal.
Definition Traversal.h:89
The base class for typed elements.
Definition Element.h:864
virtual const string & getType() const
Return the element's type string.
Definition Element.h:894
bool isColorType() const
Return true if the element is of color type.
Definition Element.h:900
void setType(const string &type)
Set the element's type string.
Definition Element.h:882
bool isMultiOutputType() const
Return true if the element is of multi-output type.
Definition Element.h:906
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:888
The base class for elements that support typed values.
Definition Element.h:928
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:1062
const string & getValueString() const
Get the value string of a element.
Definition Element.h:954
void setInterfaceName(const string &name)
Set the interface name of an element.
Definition Element.h:971
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:1005
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:1022
bool hasUnitType() const
Return true if the given element has a unit type.
Definition Element.h:1090
void setIsUniform(bool value)
Set the uniform attribute flag on this element.
Definition Element.h:1106
const string & getUnit() const
Return the unit string of an element.
Definition Element.h:1074
bool getIsUniform() const
The the uniform attribute flag for this element.
Definition Element.h:1112
void setValueString(const string &value)
Set the value string of an element.
Definition Element.h:942
void setValue(const T &value, const string &type=EMPTY_STRING)
Set the typed value of an element.
Definition Element.h:1015
bool hasUnit() const
Return true if the given element has a unit string.
Definition Element.h:1068
bool hasInterfaceName() const
Return true if the given element has an interface name.
Definition Element.h:977
void setUnitType(const string &unit)
Set the unit type of an element.
Definition Element.h:1084
bool hasImplementationName() const
Return true if the given element has an implementation name.
Definition Element.h:999
void setImplementationName(const string &name)
Set the implementation name of an element.
Definition Element.h:993
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:983
bool hasValueString() const
Return true if the given element has a value string.
Definition Element.h:948
const string & getUnitType() const
Return the unit type of an element.
Definition Element.h:1096
bool hasValue() const
Return true if the element possesses a typed value.
Definition Element.h:1028
static FloatFormat getFloatFormat()
Return the current float format.
Definition Value.h:114
FloatFormat
Float formats to use when converting values to strings.
Definition Value.h:50
static int getFloatPrecision()
Return the current float precision.
Definition Value.h:120