MaterialX 1.38.2
Element.h
Go to the documentation of this file.
1//
2// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
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
18namespace MaterialX
19{
20
21class Element;
22class TypedElement;
23class ValueElement;
24class Token;
25class CommentElement;
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 GenericElementPtr = shared_ptr<GenericElement>;
58using ConstGenericElementPtr = shared_ptr<const GenericElement>;
59
61using StringResolverPtr = shared_ptr<StringResolver>;
62
64using ElementMap = std::unordered_map<string, ElementPtr>;
65
67using ElementPredicate = std::function<bool(ConstElementPtr)>;
68
74class MX_CORE_API Element : public std::enable_shared_from_this<Element>
75{
76 protected:
77 Element(ElementPtr parent, const string& category, const string& name) :
78 _category(category),
79 _name(name),
80 _parent(parent),
81 _root(parent ? parent->getRoot() : nullptr)
82 {
83 }
84 public:
85 virtual ~Element() { }
86 Element(const Element&) = delete;
87 Element& operator=(const Element&) = delete;
88
89 protected:
90 using DocumentPtr = shared_ptr<Document>;
91 using ConstDocumentPtr = shared_ptr<const Document>;
92
93 template <class T> friend class ElementRegistry;
94
95 public:
98 bool operator==(const Element& rhs) const;
99
102 bool operator!=(const Element& rhs) const;
103
106
108 void setCategory(const string& category)
109 {
110 _category = category;
111 }
112
116 const string& getCategory() const
117 {
118 return _category;
119 }
120
124
129 void setName(const string& name);
130
132 const string& getName() const
133 {
134 return _name;
135 }
136
142 string getNamePath(ConstElementPtr relativeTo = nullptr) const;
143
149 ElementPtr getDescendant(const string& namePath) const;
150
154
156 void setFilePrefix(const string& prefix)
157 {
158 setAttribute(FILE_PREFIX_ATTRIBUTE, prefix);
159 }
160
162 bool hasFilePrefix() const
163 {
164 return hasAttribute(FILE_PREFIX_ATTRIBUTE);
165 }
166
168 const string& getFilePrefix() const
169 {
170 return getAttribute(FILE_PREFIX_ATTRIBUTE);
171 }
172
175 const string& getActiveFilePrefix() const
176 {
177 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
178 {
179 if (elem->hasFilePrefix())
180 {
181 return elem->getFilePrefix();
182 }
183 }
184 return EMPTY_STRING;
185 }
186
190
192 void setGeomPrefix(const string& prefix)
193 {
194 setAttribute(GEOM_PREFIX_ATTRIBUTE, prefix);
195 }
196
198 bool hasGeomPrefix() const
199 {
200 return hasAttribute(GEOM_PREFIX_ATTRIBUTE);
201 }
202
204 const string& getGeomPrefix() const
205 {
206 return getAttribute(GEOM_PREFIX_ATTRIBUTE);
207 }
208
211 const string& getActiveGeomPrefix() const
212 {
213 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
214 {
215 if (elem->hasGeomPrefix())
216 {
217 return elem->getGeomPrefix();
218 }
219 }
220 return EMPTY_STRING;
221 }
222
226
228 void setColorSpace(const string& colorSpace)
229 {
230 setAttribute(COLOR_SPACE_ATTRIBUTE, colorSpace);
231 }
232
234 bool hasColorSpace() const
235 {
236 return hasAttribute(COLOR_SPACE_ATTRIBUTE);
237 }
238
240 const string& getColorSpace() const
241 {
242 return getAttribute(COLOR_SPACE_ATTRIBUTE);
243 }
244
247 const string& getActiveColorSpace() const
248 {
249 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
250 {
251 if (elem->hasColorSpace())
252 {
253 return elem->getColorSpace();
254 }
255 }
256 return EMPTY_STRING;
257 }
258
262
264 void setInheritString(const string& inherit)
265 {
266 setAttribute(INHERIT_ATTRIBUTE, inherit);
267 }
268
270 bool hasInheritString() const
271 {
272 return hasAttribute(INHERIT_ATTRIBUTE);
273 }
274
276 const string& getInheritString() const
277 {
278 return getAttribute(INHERIT_ATTRIBUTE);
279 }
280
283 {
284 if (super)
285 {
286 setInheritString(super->getName());
287 }
288 else
289 {
290 removeAttribute(INHERIT_ATTRIBUTE);
291 }
292 }
293
296 {
297 return resolveRootNameReference<Element>(getInheritString());
298 }
299
302 bool hasInheritedBase(ConstElementPtr base) const;
303
305 bool hasInheritanceCycle() const;
306
310
312 void setNamespace(const string& space)
313 {
314 setAttribute(NAMESPACE_ATTRIBUTE, space);
315 }
316
318 bool hasNamespace() const
319 {
320 return hasAttribute(NAMESPACE_ATTRIBUTE);
321 }
322
324 const string& getNamespace() const
325 {
326 return getAttribute(NAMESPACE_ATTRIBUTE);
327 }
328
331 string getQualifiedName(const string& name) const
332 {
333 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
334 {
335 const string& namespaceStr = elem->getNamespace();
336 if (!namespaceStr.empty())
337 {
338 // Check if the name is qualified already.
339 const size_t i = name.find_first_of(NAME_PREFIX_SEPARATOR);
340 if (i != string::npos && name.substr(0, i) == namespaceStr)
341 {
342 // The name is already qualified with this namespace,
343 // so just return it as is.
344 return name;
345 }
346 return namespaceStr + NAME_PREFIX_SEPARATOR + name;
347 }
348 }
349 return name;
350 }
351
355
357 void setDocString(const string& doc)
358 {
359 setAttribute(DOC_ATTRIBUTE, doc);
360 }
361
363 string getDocString() const
364 {
365 return getAttribute(DOC_ATTRIBUTE);
366 }
367
371
375 template<class T> bool isA(const string& category = EMPTY_STRING) const
376 {
377 if (!asA<T>())
378 return false;
379 if (!category.empty() && getCategory() != category)
380 return false;
381 return true;
382 }
383
385 template<class T> shared_ptr<T> asA();
386
388 template<class T> shared_ptr<const T> asA() const;
389
393
401 template<class T> shared_ptr<T> addChild(const string& name = EMPTY_STRING);
402
413 ElementPtr addChildOfCategory(const string& category, string name = EMPTY_STRING);
414
420 ElementPtr changeChildCategory(ElementPtr child, const string& category);
421
423 ElementPtr getChild(const string& name) const
424 {
425 ElementMap::const_iterator it = _childMap.find(name);
426 return (it != _childMap.end()) ? it->second : ElementPtr();
427 }
428
432 template<class T> shared_ptr<T> getChildOfType(const string& name) const
433 {
434 ElementPtr child = getChild(name);
435 return child ? child->asA<T>() : shared_ptr<T>();
436 }
437
440 const vector<ElementPtr>& getChildren() const
441 {
442 return _childOrder;
443 }
444
448 template<class T> vector< shared_ptr<T> > getChildrenOfType(const string& category = EMPTY_STRING) const
449 {
450 vector< shared_ptr<T> > children;
451 for (ElementPtr child : _childOrder)
452 {
453 shared_ptr<T> instance = child->asA<T>();
454 if (!instance)
455 continue;
456 if (!category.empty() && child->getCategory() != category)
457 continue;
458 children.push_back(instance);
459 }
460 return children;
461 }
462
465 void setChildIndex(const string& name, int index);
466
469 int getChildIndex(const string& name) const;
470
472 void removeChild(const string& name);
473
477 template<class T> void removeChildOfType(const string& name)
478 {
479 if (getChildOfType<T>(name))
480 removeChild(name);
481 }
482
486
488 void setAttribute(const string& attrib, const string& value);
489
491 bool hasAttribute(const string& attrib) const
492 {
493 return _attributeMap.count(attrib) != 0;
494 }
495
498 const string& getAttribute(const string& attrib) const
499 {
500 StringMap::const_iterator it = _attributeMap.find(attrib);
501 return (it != _attributeMap.end()) ? it->second : EMPTY_STRING;
502 }
503
506 {
507 return _attributeOrder;
508 }
509
513 template<class T> void setTypedAttribute(const string& attrib, const T& data)
514 {
515 setAttribute(attrib, toValueString(data));
516 }
517
521 template<class T> T getTypedAttribute(const string& attrib) const
522 {
523 if (hasAttribute(attrib))
524 {
525 try
526 {
527 return fromValueString<T>(getAttribute(attrib));
528 }
529 catch (ExceptionTypeError&)
530 {
531 }
532 }
533 return {};
534 }
535
537 void removeAttribute(const string& attrib);
538
542
545 {
546 return shared_from_this();
547 }
548
551 {
552 return shared_from_this();
553 }
554
557 {
558 return _parent.lock();
559 }
560
563 {
564 return _parent.lock();
565 }
566
568 ElementPtr getRoot();
569
571 ConstElementPtr getRoot() const;
572
574 DocumentPtr getDocument();
575
577 ConstDocumentPtr getDocument() const;
578
581 template<class T> shared_ptr<const T> getAncestorOfType() const
582 {
583 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
584 {
585 shared_ptr<const T> typedElem = elem->asA<T>();
586 if (typedElem)
587 {
588 return typedElem;
589 }
590 }
591 return nullptr;
592 }
593
597
616 TreeIterator traverseTree() const;
617
641 GraphIterator traverseGraph() const;
642
648 virtual Edge getUpstreamEdge(size_t index = 0) const;
649
651 virtual size_t getUpstreamEdgeCount() const
652 {
653 return 0;
654 }
655
661 ElementPtr getUpstreamElement(size_t index = 0) const;
662
677 InheritanceIterator traverseInheritance() const;
678
680 int getTreeIndex() const
681 {
682 ConstElementPtr parent = getParent();
683 if (!parent)
684 {
685 return 0;
686 }
687 return parent->getTreeIndex() * (int) (parent->getChildren().size() + 1) +
688 parent->getChildIndex(getName()) + 1;
689 }
690
694
700 void setSourceUri(const string& sourceUri)
701 {
702 _sourceUri = sourceUri;
703 }
704
706 bool hasSourceUri() const
707 {
708 return !_sourceUri.empty();
709 }
710
712 const string& getSourceUri() const
713 {
714 return _sourceUri;
715 }
716
719 const string& getActiveSourceUri() const
720 {
721 for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
722 {
723 if (elem->hasSourceUri())
724 {
725 return elem->getSourceUri();
726 }
727 }
728 return EMPTY_STRING;
729 }
730
734
737 virtual bool validate(string* message = nullptr) const;
738
742
745 void copyContentFrom(const ConstElementPtr& source);
746
748 void clearContent();
749
752 string createValidChildName(string name) const
753 {
754 name = createValidName(name);
755 while (_childMap.count(name))
756 {
757 name = incrementName(name);
758 }
759 return name;
760 }
761
770 StringResolverPtr createStringResolver(const string& geom = EMPTY_STRING) const;
771
774 string asString() const;
775
778 template<class T> shared_ptr<T> resolveRootNameReference(const string& name) const
779 {
780 ConstElementPtr root = getRoot();
781 shared_ptr<T> child = root->getChildOfType<T>(getQualifiedName(name));
782 return child ? child : root->getChildOfType<T>(name);
783 }
784
786
787 protected:
788 // Enforce a requirement within a validate method, updating the validation
789 // state and optional output text if the requirement is not met.
790 void validateRequire(bool expression, bool& res, string* message, string errorDesc) const;
791
792 public:
793 static const string NAME_ATTRIBUTE;
794 static const string FILE_PREFIX_ATTRIBUTE;
795 static const string GEOM_PREFIX_ATTRIBUTE;
796 static const string COLOR_SPACE_ATTRIBUTE;
797 static const string INHERIT_ATTRIBUTE;
798 static const string NAMESPACE_ATTRIBUTE;
799 static const string DOC_ATTRIBUTE;
800
801 protected:
802 virtual void registerChildElement(ElementPtr child);
803 virtual void unregisterChildElement(ElementPtr child);
804
805 // Return a non-const copy of our self pointer, for use in constructing
806 // graph traversal objects that require non-const storage.
807 ElementPtr getSelfNonConst() const
808 {
809 return std::const_pointer_cast<Element>(shared_from_this());
810 }
811
812 protected:
813 string _category;
814 string _name;
815 string _sourceUri;
816
817 ElementMap _childMap;
818 vector<ElementPtr> _childOrder;
819
820 StringMap _attributeMap;
821 StringVec _attributeOrder;
822
823 weak_ptr<Element> _parent;
824 weak_ptr<Element> _root;
825
826 private:
827 template <class T> static ElementPtr createElement(ElementPtr parent, const string& name)
828 {
829 return std::make_shared<T>(parent, name);
830 }
831
832 private:
833 using CreatorFunction = ElementPtr (*)(ElementPtr, const string&);
834 using CreatorMap = std::unordered_map<string, CreatorFunction>;
835
836 static CreatorMap _creatorMap;
837};
838
841class MX_CORE_API TypedElement : public Element
842{
843 protected:
844 TypedElement(ElementPtr parent, const string& category, const string& name) :
845 Element(parent, category, name)
846 {
847 }
848 public:
849 virtual ~TypedElement() { }
850
851 protected:
852 using TypeDefPtr = shared_ptr<class TypeDef>;
853
854 public:
857
859 void setType(const string& type)
860 {
861 setAttribute(TYPE_ATTRIBUTE, type);
862 }
863
865 bool hasType() const
866 {
867 return hasAttribute(TYPE_ATTRIBUTE);
868 }
869
871 virtual const string& getType() const
872 {
873 return getAttribute(TYPE_ATTRIBUTE);
874 }
875
877 bool isMultiOutputType() const
878 {
879 return getType() == MULTI_OUTPUT_TYPE_STRING;
880 }
881
885
888 TypeDefPtr getTypeDef() const;
889
891
892public:
893 static const string TYPE_ATTRIBUTE;
894};
895
898class MX_CORE_API ValueElement : public TypedElement
899{
900 protected:
901 ValueElement(ElementPtr parent, const string& category, const string& name) :
902 TypedElement(parent, category, name)
903 {
904 }
905 public:
906 virtual ~ValueElement() { }
907
910
912 void setValueString(const string& value)
913 {
914 setAttribute(VALUE_ATTRIBUTE, value);
915 }
916
918 bool hasValueString() const
919 {
920 return hasAttribute(VALUE_ATTRIBUTE);
921 }
922
924 const string& getValueString() const
925 {
926 return getAttribute(VALUE_ATTRIBUTE);
927 }
928
934 string getResolvedValueString(StringResolverPtr resolver = nullptr) const;
935
939
941 void setInterfaceName(const string& name)
942 {
943 setAttribute(INTERFACE_NAME_ATTRIBUTE, name);
944 }
945
947 bool hasInterfaceName() const
948 {
949 return hasAttribute(INTERFACE_NAME_ATTRIBUTE);
950 }
951
953 const string& getInterfaceName() const
954 {
955 return getAttribute(INTERFACE_NAME_ATTRIBUTE);
956 }
957
961
963 void setImplementationName(const string& name)
964 {
965 setAttribute(IMPLEMENTATION_NAME_ATTRIBUTE, name);
966 }
967
970 {
971 return hasAttribute(IMPLEMENTATION_NAME_ATTRIBUTE);
972 }
973
975 const string& getImplementationName() const
976 {
977 return getAttribute(IMPLEMENTATION_NAME_ATTRIBUTE);
978 }
979
983
985 template<class T> void setValue(const T& value, const string& type = EMPTY_STRING)
986 {
987 setType(!type.empty() ? type : getTypeString<T>());
988 setValueString(toValueString(value));
989 }
990
992 void setValue(const char* value, const string& type = EMPTY_STRING)
993 {
994 setValue(value ? string(value) : EMPTY_STRING, type);
995 }
996
998 bool hasValue() const
999 {
1000 return hasAttribute(VALUE_ATTRIBUTE);
1001 }
1002
1009 {
1010 if (!hasValue())
1011 return ValuePtr();
1012 return Value::createValueFromStrings(getValueString(), getType());
1013 }
1014
1024 {
1025 if (!hasValue())
1026 return ValuePtr();
1027 return Value::createValueFromStrings(getResolvedValueString(resolver), getType());
1028 }
1029
1035 ValuePtr getDefaultValue() const;
1036
1040
1042 void setUnit(const string& unit)
1043 {
1044 setAttribute(UNIT_ATTRIBUTE, unit);
1045 }
1046
1048 bool hasUnit() const
1049 {
1050 return hasAttribute(UNIT_ATTRIBUTE);
1051 }
1052
1054 const string& getUnit() const
1055 {
1056 return getAttribute(UNIT_ATTRIBUTE);
1057 }
1058
1061 const string& getActiveUnit() const;
1062
1064 void setUnitType(const string& unit)
1065 {
1066 setAttribute(UNITTYPE_ATTRIBUTE, unit);
1067 }
1068
1070 bool hasUnitType() const
1071 {
1072 return hasAttribute(UNITTYPE_ATTRIBUTE);
1073 }
1074
1076 const string& getUnitType() const
1077 {
1078 return getAttribute(UNITTYPE_ATTRIBUTE);
1079 }
1080
1084
1086 void setIsUniform(bool value)
1087 {
1088 setTypedAttribute<bool>(UNIFORM_ATTRIBUTE, value);
1089 }
1090
1092 bool getIsUniform() const
1093 {
1094 return getTypedAttribute<bool>(UNIFORM_ATTRIBUTE);
1095 }
1096
1100
1103 bool validate(string* message = nullptr) const override;
1104
1106
1107 public:
1108 static const string VALUE_ATTRIBUTE;
1109 static const string INTERFACE_NAME_ATTRIBUTE;
1110 static const string IMPLEMENTATION_NAME_ATTRIBUTE;
1111 static const string IMPLEMENTATION_TYPE_ATTRIBUTE;
1112 static const string ENUM_ATTRIBUTE;
1113 static const string ENUM_VALUES_ATTRIBUTE;
1114 static const string UI_NAME_ATTRIBUTE;
1115 static const string UI_FOLDER_ATTRIBUTE;
1116 static const string UI_MIN_ATTRIBUTE;
1117 static const string UI_MAX_ATTRIBUTE;
1118 static const string UI_SOFT_MIN_ATTRIBUTE;
1119 static const string UI_SOFT_MAX_ATTRIBUTE;
1120 static const string UI_STEP_ATTRIBUTE;
1121 static const string UI_ADVANCED_ATTRIBUTE;
1122 static const string UNIT_ATTRIBUTE;
1123 static const string UNITTYPE_ATTRIBUTE;
1124 static const string UNIFORM_ATTRIBUTE;
1125};
1126
1132class MX_CORE_API Token : public ValueElement
1133{
1134 public:
1135 Token(ElementPtr parent, const string& name) :
1136 ValueElement(parent, CATEGORY, name)
1137 {
1138 }
1139 virtual ~Token() { }
1140
1141 public:
1142 static const string CATEGORY;
1143};
1144
1152class MX_CORE_API CommentElement : public Element
1153{
1154 public:
1155 CommentElement(ElementPtr parent, const string& name) :
1156 Element(parent, CATEGORY, name)
1157 {
1158 }
1159 virtual ~CommentElement() { }
1160
1161 public:
1162 static const string CATEGORY;
1163};
1164
1167class MX_CORE_API GenericElement : public Element
1168{
1169 public:
1170 GenericElement(ElementPtr parent, const string& name) :
1171 Element(parent, CATEGORY, name)
1172 {
1173 }
1174 virtual ~GenericElement() { }
1175
1176 public:
1177 static const string CATEGORY;
1178};
1179
1193class MX_CORE_API StringResolver
1194{
1195 public:
1198 {
1199 return StringResolverPtr(new StringResolver());
1200 }
1201
1202 virtual ~StringResolver() { }
1203
1206
1208 void setFilePrefix(const string& filePrefix)
1209 {
1210 _filePrefix = filePrefix;
1211 }
1212
1214 const string& getFilePrefix() const
1215 {
1216 return _filePrefix;
1217 }
1218
1222
1224 void setGeomPrefix(const string& geomPrefix)
1225 {
1226 _geomPrefix = geomPrefix;
1227 }
1228
1230 const string& getGeomPrefix() const
1231 {
1232 return _geomPrefix;
1233 }
1234
1238
1241 void setUdimString(const string& udim);
1242
1245 void setUvTileString(const string& uvTile);
1246
1248 void setFilenameSubstitution(const string& key, const string& value)
1249 {
1250 _filenameMap[key] = value;
1251 }
1252
1255 {
1256 return _filenameMap;
1257 }
1258
1262
1264 void setGeomNameSubstitution(const string& key, const string& value)
1265 {
1266 _geomNameMap[key] = value;
1267 }
1268
1271 {
1272 return _geomNameMap;
1273 }
1274
1278
1281 virtual string resolve(const string& str, const string& type) const;
1282
1284 static bool isResolvedType(const string& type)
1285 {
1286 return type == FILENAME_TYPE_STRING || type == GEOMNAME_TYPE_STRING;
1287 }
1288
1290
1291 protected:
1292 StringResolver() { }
1293
1294 protected:
1295 string _filePrefix;
1296 string _geomPrefix;
1297 StringMap _filenameMap;
1298 StringMap _geomNameMap;
1299};
1300
1304class MX_CORE_API ExceptionOrphanedElement : public Exception
1305{
1306 public:
1307 using Exception::Exception;
1308};
1309
1310template<class T> shared_ptr<T> Element::addChild(const string& name)
1311{
1312 string childName = name;
1313 if (childName.empty())
1314 {
1315 childName = createValidChildName(T::CATEGORY + "1");
1316 }
1317
1318 if (_childMap.count(childName))
1319 throw Exception("Child name is not unique: " + childName);
1320
1321 shared_ptr<T> child = std::make_shared<T>(getSelf(), childName);
1322 registerChildElement(child);
1323
1324 return child;
1325}
1326
1330MX_CORE_API bool targetStringsMatch(const string& target1, const string& target2);
1331
1334MX_CORE_API string prettyPrint(ConstElementPtr elem);
1335
1336} // namespace MaterialX
1337
1338#endif
shared_ptr< TypeDef > TypeDefPtr
A shared pointer to a TypeDef.
Definition: Definition.h:43
shared_ptr< const Document > ConstDocumentPtr
A shared pointer to a const Document.
Definition: Document.h:25
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:23
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:58
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:61
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
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:64
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:56
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:67
shared_ptr< const Element > ConstElementPtr
A shared pointer to a const Element.
Definition: Element.h:33
Import and export declarations for the Core library.
vector< string > StringVec
A vector of strings.
Definition: Library.h:56
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:58
Utility methods.
Graph traversal classes.
Generic value classes.
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:30
An element representing a block of descriptive text within a document, which will be stored a comment...
Definition: Element.h:1153
An edge between two connected Elements, returned during graph traversal.
Definition: Traversal.h:31
The base class for MaterialX elements.
Definition: Element.h:75
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:719
bool hasNamespace() const
Return true if this element has a namespace string.
Definition: Element.h:318
const string & getAttribute(const string &attrib) const
Return the value string of the given attribute.
Definition: Element.h:498
ConstElementPtr getSelf() const
Return our self pointer.
Definition: Element.h:550
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:211
bool isA(const string &category=EMPTY_STRING) const
Return true if this element belongs to the given subclass.
Definition: Element.h:375
void setDocString(const string &doc)
Set the documentation string of this element.
Definition: Element.h:357
bool hasGeomPrefix() const
Return true if the given element has a geom prefix string.
Definition: Element.h:198
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:581
const string & getInheritString() const
Return the inherit string of this element.
Definition: Element.h:276
bool hasSourceUri() const
Return true if this element has a source URI.
Definition: Element.h:706
const string & getColorSpace() const
Return the element's color space string.
Definition: Element.h:240
shared_ptr< T > addChild(const string &name=EMPTY_STRING)
Add a child element of the given subclass and name.
Definition: Element.h:1310
const string & getName() const
Return the element's name string.
Definition: Element.h:132
void setColorSpace(const string &colorSpace)
Set the element's color space string.
Definition: Element.h:228
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:331
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:752
const string & getGeomPrefix() const
Return the element's geom prefix string.
Definition: Element.h:204
void setFilePrefix(const string &prefix)
Set the element's file prefix string.
Definition: Element.h:156
shared_ptr< T > resolveRootNameReference(const string &name) const
Resolve a reference to a named element at the root scope of this document, taking the namespace at th...
Definition: Element.h:778
const StringVec & getAttributeNames() const
Return a vector of stored attribute names, in the order they were set.
Definition: Element.h:505
ElementPtr getChild(const string &name) const
Return the child element, if any, with the given name.
Definition: Element.h:423
const string & getSourceUri() const
Return the element's source URI.
Definition: Element.h:712
bool hasColorSpace() const
Return true if the given element has a color space string.
Definition: Element.h:234
const string & getFilePrefix() const
Return the element's file prefix string.
Definition: Element.h:168
void setTypedAttribute(const string &attrib, const T &data)
Set the value of an implicitly typed attribute.
Definition: Element.h:513
virtual size_t getUpstreamEdgeCount() const
Return the number of queriable upstream edges for this element.
Definition: Element.h:651
bool hasInheritString() const
Return true if this element has an inherit string.
Definition: Element.h:270
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:247
int getTreeIndex() const
Return the breadth-first index of this element within the document tree.
Definition: Element.h:680
ElementPtr getInheritsFrom() const
Return the element, if any, that this one directly inherits from.
Definition: Element.h:295
T getTypedAttribute(const string &attrib) const
Return the value of an implicitly typed attribute.
Definition: Element.h:521
void setCategory(const string &category)
Set the element's category string.
Definition: Element.h:108
shared_ptr< T > getChildOfType(const string &name) const
Return the child element, if any, with the given name and subclass.
Definition: Element.h:432
void setGeomPrefix(const string &prefix)
Set the element's geom prefix string.
Definition: Element.h:192
void setNamespace(const string &space)
Set the namespace string of this element.
Definition: Element.h:312
ElementPtr getSelf()
Return our self pointer.
Definition: Element.h:544
const vector< ElementPtr > & getChildren() const
Return a constant vector of all child elements.
Definition: Element.h:440
ConstElementPtr getParent() const
Return our parent element.
Definition: Element.h:562
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:175
void setInheritString(const string &inherit)
Set the inherit string of this element.
Definition: Element.h:264
const string & getNamespace() const
Return the namespace string of this element.
Definition: Element.h:324
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
Definition: Element.h:491
void removeChildOfType(const string &name)
Remove the child element, if any, with the given name and subclass.
Definition: Element.h:477
void setSourceUri(const string &sourceUri)
Set the element's source URI.
Definition: Element.h:700
string getDocString() const
Return the documentation string of this element.
Definition: Element.h:363
void setInheritsFrom(ConstElementPtr super)
Set the element that this one directly inherits from.
Definition: Element.h:282
ElementPtr getParent()
Return our parent element.
Definition: Element.h:556
bool hasFilePrefix() const
Return true if the given element has a file prefix string.
Definition: Element.h:162
const string & getCategory() const
Return the element's category string.
Definition: Element.h:116
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...
Definition: Element.h:448
The base class for exceptions that are propagated from the MaterialX library to the client applicatio...
Definition: Exception.h:23
An exception that is thrown when an ElementPtr is used after its owning Document has gone out of scop...
Definition: Element.h:1305
An exception that is thrown when a type mismatch is encountered.
Definition: Value.h:39
A generic element subclass, for instantiating elements with unrecognized categories.
Definition: Element.h:1168
An iterator object representing the state of an upstream graph traversal.
Definition: Traversal.h:193
An iterator object representing the current state of an inheritance traversal.
Definition: Traversal.h:335
A helper object for applying string modifiers to data values in the context of a specific element and...
Definition: Element.h:1194
void setFilenameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for filename data values.
Definition: Element.h:1248
static bool isResolvedType(const string &type)
Return true if the given type may be resolved by this class.
Definition: Element.h:1284
const string & getGeomPrefix() const
Return the geom prefix for this context.
Definition: Element.h:1230
const string & getFilePrefix() const
Return the file prefix for this context.
Definition: Element.h:1214
const StringMap & getGeomNameSubstitutions() const
Return the map of geometry name substring substitutions.
Definition: Element.h:1270
void setFilePrefix(const string &filePrefix)
Set the file prefix for this context.
Definition: Element.h:1208
void setGeomPrefix(const string &geomPrefix)
Set the geom prefix for this context.
Definition: Element.h:1224
const StringMap & getFilenameSubstitutions() const
Return the map of filename substring substitutions.
Definition: Element.h:1254
void setGeomNameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for geometry name data values.
Definition: Element.h:1264
static StringResolverPtr create()
Create a new string resolver.
Definition: Element.h:1197
A token element representing a string value.
Definition: Element.h:1133
An iterator object representing the state of a tree traversal.
Definition: Traversal.h:90
The base class for typed elements.
Definition: Element.h:842
virtual const string & getType() const
Return the element's type string.
Definition: Element.h:871
void setType(const string &type)
Set the element's type string.
Definition: Element.h:859
bool isMultiOutputType() const
Return true if the element is of multi-output type.
Definition: Element.h:877
bool hasType() const
Return true if the given element has a type string.
Definition: Element.h:865
The base class for elements that support typed values.
Definition: Element.h:899
ValuePtr getValue() const
Return the typed value of an element as a generic value object, which may be queried to access its da...
Definition: Element.h:1008
void setUnit(const string &unit)
Set the unit string of an element.
Definition: Element.h:1042
const string & getValueString() const
Get the value string of a element.
Definition: Element.h:924
void setInterfaceName(const string &name)
Set the interface name of an element.
Definition: Element.h:941
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...
Definition: Element.h:1023
const string & getImplementationName() const
Return the implementation name of an element.
Definition: Element.h:975
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:992
bool hasUnitType() const
Return true if the given element has a unit type.
Definition: Element.h:1070
void setIsUniform(bool value)
Set the uniform attribute flag on this element.
Definition: Element.h:1086
const string & getUnit() const
Return the unit string of an element.
Definition: Element.h:1054
bool getIsUniform() const
The the uniform attribute flag for this element.
Definition: Element.h:1092
void setValueString(const string &value)
Set the value string of an element.
Definition: Element.h:912
void setValue(const T &value, const string &type=EMPTY_STRING)
Set the typed value of an element.
Definition: Element.h:985
bool hasUnit() const
Return true if the given element has a unit string.
Definition: Element.h:1048
bool hasInterfaceName() const
Return true if the given element has an interface name.
Definition: Element.h:947
void setUnitType(const string &unit)
Set the unit type of an element.
Definition: Element.h:1064
bool hasImplementationName() const
Return true if the given element has an implementation name.
Definition: Element.h:969
void setImplementationName(const string &name)
Set the implementation name of an element.
Definition: Element.h:963
const string & getInterfaceName() const
Return the interface name of an element.
Definition: Element.h:953
bool hasValueString() const
Return true if the given element has a value string.
Definition: Element.h:918
const string & getUnitType() const
Return the unit type of an element.
Definition: Element.h:1076
bool hasValue() const
Return true if the element possesses a typed value.
Definition: Element.h:998
static ValuePtr createValueFromStrings(const string &value, const string &type)
Create a new value instance from value and type strings.
Definition: Value.cpp:211