6 #ifndef MATERIALX_ELEMENT_H
7 #define MATERIALX_ELEMENT_H
69 using ElementMap = std::unordered_map<string, ElementPtr>;
86 _root(parent ? parent->getRoot() : nullptr)
99 template <
class T>
friend class ElementRegistry;
116 _category = category;
135 void setName(
const string&
name);
155 ElementPtr getDescendant(
const string& namePath)
const;
164 setAttribute(FILE_PREFIX_ATTRIBUTE, prefix);
170 return hasAttribute(FILE_PREFIX_ATTRIBUTE);
176 return getAttribute(FILE_PREFIX_ATTRIBUTE);
185 if (elem->hasFilePrefix())
187 return elem->getFilePrefix();
200 setAttribute(GEOM_PREFIX_ATTRIBUTE, prefix);
206 return hasAttribute(GEOM_PREFIX_ATTRIBUTE);
212 return getAttribute(GEOM_PREFIX_ATTRIBUTE);
221 if (elem->hasGeomPrefix())
223 return elem->getGeomPrefix();
236 setAttribute(COLOR_SPACE_ATTRIBUTE, colorSpace);
242 return hasAttribute(COLOR_SPACE_ATTRIBUTE);
248 return getAttribute(COLOR_SPACE_ATTRIBUTE);
257 if (elem->hasColorSpace())
259 return elem->getColorSpace();
272 setAttribute(INHERIT_ATTRIBUTE, inherit);
278 return hasAttribute(INHERIT_ATTRIBUTE);
284 return getAttribute(INHERIT_ATTRIBUTE);
292 setInheritString(super->getName());
296 removeAttribute(INHERIT_ATTRIBUTE);
303 return hasInheritString() ? resolveNameReference<Element>(getInheritString()) :
nullptr;
311 bool hasInheritanceCycle()
const;
320 setAttribute(NAMESPACE_ATTRIBUTE, space);
326 return hasAttribute(NAMESPACE_ATTRIBUTE);
332 return getAttribute(NAMESPACE_ATTRIBUTE);
341 const string& namespaceStr = elem->getNamespace();
342 if (!namespaceStr.empty())
346 if (i != string::npos && name.substr(0, i) == namespaceStr)
365 setAttribute(DOC_ATTRIBUTE, doc);
371 return getAttribute(DOC_ATTRIBUTE);
385 if (!category.empty() && getCategory() != category)
391 template <
class T> shared_ptr<T> asA();
394 template <
class T> shared_ptr<const T> asA()
const;
407 template <
class T> shared_ptr<T> addChild(
const string&
name =
EMPTY_STRING);
431 ElementMap::const_iterator it = _childMap.find(name);
432 return (it != _childMap.end()) ? it->second :
ElementPtr();
441 return child ? child->asA<
T>() : shared_ptr<T>();
456 vector<shared_ptr<T>> children;
459 shared_ptr<T> instance = child->asA<
T>();
462 if (!category.empty() && child->getCategory() != category)
464 children.push_back(instance);
471 void setChildIndex(
const string&
name,
int index);
475 int getChildIndex(
const string&
name)
const;
478 void removeChild(
const string&
name);
485 if (getChildOfType<T>(name))
494 void setAttribute(
const string& attrib,
const string&
value);
499 return _attributeMap.count(attrib) != 0;
506 StringMap::const_iterator it = _attributeMap.find(attrib);
507 return (it != _attributeMap.end()) ? it->second :
EMPTY_STRING;
513 return _attributeOrder;
529 if (hasAttribute(attrib))
533 return fromValueString<T>(getAttribute(attrib));
543 void removeAttribute(
const string& attrib);
552 return shared_from_this();
558 return shared_from_this();
564 return _parent.lock();
570 return _parent.lock();
591 shared_ptr<const T> typedElem = elem->asA<
T>();
654 virtual Edge getUpstreamEdge(
size_t index = 0)
const;
696 _sourceUri = sourceUri;
702 return !_sourceUri.empty();
717 if (elem->hasSourceUri())
719 return elem->getSourceUri();
731 virtual bool validate(
string*
message =
nullptr)
const;
742 virtual void clearContent();
749 while (_childMap.count(name))
768 string asString()
const;
779 shared_ptr<T> child = scope->getChildOfType<
T>(getQualifiedName(name));
780 return child ? child : scope->getChildOfType<
T>(
name);
785 void validateRequire(
bool expression,
bool& res,
string*
message,
const string& errorDesc)
const;
797 virtual void registerChildElement(
ElementPtr child);
798 virtual void unregisterChildElement(
ElementPtr child);
804 return std::const_pointer_cast<
Element>(shared_from_this());
824 return std::make_shared<T>(parent,
name);
829 using CreatorMap = std::unordered_map<string, CreatorFunction>;
831 static CreatorMap _creatorMap;
840 Element(parent, category, name)
875 return getType() ==
"color3" || getType() ==
"color4";
1064 const string& getActiveUnit()
const;
1091 setTypedAttribute<bool>(UNIFORM_ATTRIBUTE,
value);
1097 return getTypedAttribute<bool>(UNIFORM_ATTRIBUTE);
1159 Element(parent, CATEGORY, name)
1174 Element(parent, CATEGORY, name)
1189 Element(parent, CATEGORY, name)
1228 _filePrefix = filePrefix;
1244 _geomPrefix = geomPrefix;
1259 void setUdimString(
const string& udim);
1263 void setUvTileString(
const string& uvTile);
1268 _filenameMap[key] =
value;
1277 return _filenameMap;
1287 _geomNameMap[key] =
value;
1293 return _geomNameMap;
1302 virtual string resolve(
const string& str,
const string&
type)
const;
1333 string childName =
name;
1334 if (childName.empty())
1340 throw Exception(
"Child name is not unique: " + childName);
1342 shared_ptr<T> child = std::make_shared<T>(
getSelf(), childName);
static const string UI_ADVANCED_ATTRIBUTE
GLuint GLsizei const GLchar * message
ValueElement(ElementPtr parent, const string &category, const string &name)
const string & getFilePrefix() const
Return the element's file prefix string.
const string & getNamespace() const
Return the namespace string of this element.
virtual const string & getType() const
Return the element's type string.
shared_ptr< const Element > ConstElementPtr
A shared pointer to a const Element.
void setType(const string &type)
Set the element's type string.
void setGeomPrefix(const string &prefix)
Set the element's geom prefix string.
weak_ptr< Element > _parent
ValuePtr getResolvedValue(StringResolverPtr resolver=nullptr) const
bool hasValue() const
Return true if the element possesses a typed value.
bool hasInterfaceName() const
Return true if the given element has an interface name.
TypedElement(ElementPtr parent, const string &category, const string &name)
shared_ptr< TypeDef > TypeDefPtr
A shared pointer to a TypeDef.
shared_ptr< const T > getAncestorOfType() const
static const string IMPLEMENTATION_NAME_ATTRIBUTE
shared_ptr< T > addChild(const string &name=EMPTY_STRING)
static const string ENUM_ATTRIBUTE
const string & getFilePrefix() const
Return the file prefix for this context.
void setDocString(const string &doc)
Set the documentation string of this element.
ElementPtr getSelf()
Return our self pointer.
static const string UI_NAME_ATTRIBUTE
std::function< bool(ConstElementPtr)> ElementPredicate
A standard function taking an ElementPtr and returning a boolean.
bool isColorType() const
Return true if the element is of color type.
#define MATERIALX_NAMESPACE_BEGIN
virtual ~GenericElement()
vector< string > StringVec
A vector of strings.
shared_ptr< NewlineElement > NewlineElementPtr
A shared pointer to a NewlineElement.
bool hasImplementationName() const
Return true if the given element has an implementation name.
std::unordered_map< string, ElementPtr > ElementMap
A hash map from strings to elements.
GLsizei const GLfloat * value
MX_CORE_API const string FILENAME_TYPE_STRING
void setIsUniform(bool value)
Set the uniform attribute flag on this element.
shared_ptr< TypedElement > TypedElementPtr
A shared pointer to a TypedElement.
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
static const string CATEGORY
static StringResolverPtr create()
Create a new string resolver.
static const string UNIFORM_ATTRIBUTE
const string & getAttribute(const string &attrib) const
ValuePtr getValue() const
static const string UI_MIN_ATTRIBUTE
void setValue(const T &value, const string &type=EMPTY_STRING)
Set the typed value of an element.
static const string IMPLEMENTATION_TYPE_ATTRIBUTE
__hostdev__ void setValue(uint32_t offset, bool v)
void setValueString(const string &value)
Set the value string of an element.
void setFilenameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for filename data values.
string createValidChildName(string name) const
shared_ptr< T > getChildOfType(const string &name) const
bool hasUnit() const
Return true if the given element has a unit string.
shared_ptr< Token > TokenPtr
A shared pointer to a Token.
class OCIOEXPORT Exception
static const string GEOM_PREFIX_ATTRIBUTE
const string & getImplementationName() const
Return the implementation name of an element.
vector< ElementPtr > _childOrder
MX_CORE_API string prettyPrint(ConstElementPtr elem)
bool hasValueString() const
Return true if the given element has a value string.
virtual ~StringResolver()
void setImplementationName(const string &name)
Set the implementation name of an element.
ElementPtr getChild(const string &name) const
Return the child element, if any, with the given name.
const string & getInterfaceName() const
Return the interface name of an element.
shared_ptr< GenericElement > GenericElementPtr
A shared pointer to a GenericElement.
bool hasColorSpace() const
Return true if the given element has a color space string.
shared_ptr< const GenericElement > ConstGenericElementPtr
A shared pointer to a const GenericElement.
const string & getActiveColorSpace() const
MX_CORE_API string incrementName(const string &name)
Increment the numeric suffix of a name.
const vector< ElementPtr > & getChildren() const
static const string INTERFACE_NAME_ATTRIBUTE
NewlineElement(ElementPtr parent, const string &name)
void setInheritsFrom(ConstElementPtr super)
Set the element that this one directly inherits from.
string getDocString() const
Return the documentation string of this element.
weak_ptr< Element > _root
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
static const string NAME_ATTRIBUTE
ElementPtr getParent()
Return our parent element.
const string & getActiveFilePrefix() const
const string & getName() const
Return the element's name string.
shared_ptr< const TypedElement > ConstTypedElementPtr
A shared pointer to a const TypedElement.
shared_ptr< const Document > ConstDocumentPtr
void setInterfaceName(const string &name)
Set the interface name of an element.
void setValue(const char *value, const string &type=EMPTY_STRING)
Set the typed value of an element from a C-style string.
virtual bool validate(string *message=nullptr) const
shared_ptr< T > resolveNameReference(const string &name, ConstElementPtr parent=nullptr) const
static const string CATEGORY
bool hasType() const
Return true if the given element has a type string.
const string & getUnitType() const
Return the unit type of an element.
bool hasNamespace() const
Return true if this element has a namespace string.
MX_CORE_API const string GEOMNAME_TYPE_STRING
static bool isResolvedType(const string &type)
Return true if the given type may be resolved by this class.
void setSourceUri(const string &sourceUri)
GenericElement(ElementPtr parent, const string &name)
void setUnit(const string &unit)
Set the unit string of an element.
GLsizei GLsizei GLchar * source
void setUnitType(const string &unit)
Set the unit type of an element.
MX_CORE_API const string MULTI_OUTPUT_TYPE_STRING
shared_ptr< const ValueElement > ConstValueElementPtr
A shared pointer to a const ValueElement.
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
T getTypedAttribute(const string &attrib) const
void setColorSpace(const string &colorSpace)
Set the element's color space string.
static const string UI_MAX_ATTRIBUTE
const string & getSourceUri() const
Return the element's source URI.
MX_CORE_API const string NAME_PREFIX_SEPARATOR
MX_CORE_API bool targetStringsMatch(const string &target1, const string &target2)
static const string ENUM_VALUES_ATTRIBUTE
png_const_structrp png_const_inforp int * unit
void setTypedAttribute(const string &attrib, const T &data)
ElementPtr getInheritsFrom() const
Return the element, if any, that this one directly inherits from.
GLuint const GLchar * name
static const string VALUE_ATTRIBUTE
ElementPtr getSelfNonConst() const
vector< shared_ptr< T > > getChildrenOfType(const string &category=EMPTY_STRING) const
virtual ~NewlineElement()
const string & getCategory() const
static const string UI_SOFT_MAX_ATTRIBUTE
bool isMultiOutputType() const
Return true if the element is of multi-output type.
void setFilePrefix(const string &filePrefix)
Set the file prefix for this context.
Element(ElementPtr parent, const string &category, const string &name)
ConstElementPtr getParent() const
Return our parent element.
bool hasUnitType() const
Return true if the given element has a unit type.
const StringMap & getGeomNameSubstitutions() const
Return the map of geometry name substring substitutions.
const string & getValueString() const
Get the value string of a element.
const string & getActiveSourceUri() const
void setNamespace(const string &space)
Set the namespace string of this element.
static ValuePtr createValueFromStrings(const string &value, const string &type)
shared_ptr< const NewlineElement > ConstNewlineElementPtr
A shared pointer to a const NewlineElement.
shared_ptr< const Token > ConstTokenPtr
A shared pointer to a const Token.
static const string UNITTYPE_ATTRIBUTE
shared_ptr< StringResolver > StringResolverPtr
A shared pointer to a StringResolver.
void setAttribute(const string &attrib, const string &value)
Set the value string of the given attribute.
shared_ptr< Document > DocumentPtr
bool hasInheritString() const
Return true if this element has an inherit string.
const StringVec & getAttributeNames() const
Return a vector of stored attribute names, in the order they were set.
void setFilePrefix(const string &prefix)
Set the element's file prefix string.
Exception(const string &msg)
shared_ptr< const CommentElement > ConstCommentElementPtr
A shared pointer to a const CommentElement.
const string & getActiveGeomPrefix() const
bool hasFilePrefix() const
Return true if the given element has a file prefix string.
string getQualifiedName(const string &name) const
void setInheritString(const string &inherit)
Set the inherit string of this element.
static const string COLOR_SPACE_ATTRIBUTE
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
MX_CORE_API string createValidName(string name, char replaceChar= '_')
Create a valid MaterialX name from the given string.
shared_ptr< CommentElement > CommentElementPtr
A shared pointer to a CommentElement.
const StringMap & getFilenameSubstitutions() const
Return the map of filename substring substitutions.
LeafData & operator=(const LeafData &)=delete
static const string UI_STEP_ATTRIBUTE
bool isA(const string &category=EMPTY_STRING) const
const string & getUnit() const
Return the unit string of an element.
static const string TYPE_ATTRIBUTE
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
static const string CATEGORY
void setGeomPrefix(const string &geomPrefix)
Set the geom prefix for this context.
static const string UI_FOLDER_ATTRIBUTE
bool hasGeomPrefix() const
Return true if the given element has a geom prefix string.
static const string INHERIT_ATTRIBUTE
virtual void registerChildElement(ElementPtr child)
bool getIsUniform() const
The the uniform attribute flag for this element.
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
const string & getGeomPrefix() const
Return the element's geom prefix string.
ConstElementPtr getSelf() const
Return our self pointer.
shared_ptr< ValueElement > ValueElementPtr
A shared pointer to a ValueElement.
#define MATERIALX_NAMESPACE_END
static const string FILE_PREFIX_ATTRIBUTE
static const string DOC_ATTRIBUTE
const string & getGeomPrefix() const
Return the geom prefix for this context.
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
virtual size_t getUpstreamEdgeCount() const
Return the number of queriable upstream edges for this element.
const string & getColorSpace() const
Return the element's color space string.
bool hasSourceUri() const
Return true if this element has a source URI.
Token(ElementPtr parent, const string &name)
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
static const string UI_SOFT_MIN_ATTRIBUTE
static const string NAMESPACE_ATTRIBUTE
shared_ptr< T >(*)( CreatorFunction)
shared_ptr< const Document > ConstDocumentPtr
A shared pointer to a const Document.
void setGeomNameSubstitution(const string &key, const string &value)
Set an arbitrary substring substitution for geometry name data values.
shared_ptr< class TypeDef > TypeDefPtr
void setCategory(const string &category)
Set the element's category string.
static const string UNIT_ATTRIBUTE
StringVec _attributeOrder
MX_CORE_API string toValueString(const T &data)
Convert the given data value to a value string.
void removeChildOfType(const string &name)
const string & getInheritString() const
Return the inherit string of this element.