MaterialX 1.39.0
Loading...
Searching...
No Matches
Definition.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_DEFINITION_H
7#define MATERIALX_DEFINITION_H
8
11
13
15
16MATERIALX_NAMESPACE_BEGIN
17
18extern MX_CORE_API const string COLOR_SEMANTIC;
19extern MX_CORE_API const string SHADER_SEMANTIC;
20
21class NodeDef;
22class Implementation;
23class TypeDef;
24class TargetDef;
25class Member;
26class Unit;
27class UnitDef;
28class UnitTypeDef;
29class AttributeDef;
30
32using NodeDefPtr = shared_ptr<NodeDef>;
34using ConstNodeDefPtr = shared_ptr<const NodeDef>;
35
37using ImplementationPtr = shared_ptr<Implementation>;
39using ConstImplementationPtr = shared_ptr<const Implementation>;
40
42using TypeDefPtr = shared_ptr<TypeDef>;
44using ConstTypeDefPtr = shared_ptr<const TypeDef>;
45
47using TargetDefPtr = shared_ptr<TargetDef>;
49using ConstTargetDefPtr = shared_ptr<const TargetDef>;
50
52using MemberPtr = shared_ptr<Member>;
54using ConstMemberPtr = shared_ptr<const Member>;
55
57using UnitPtr = shared_ptr<Unit>;
59using ConstUnitPtr = shared_ptr<const Unit>;
60
62using UnitDefPtr = shared_ptr<UnitDef>;
64using ConstUnitDefPtr = shared_ptr<const UnitDef>;
65
67using UnitTypeDefPtr = shared_ptr<UnitTypeDef>;
69using ConstUnitTypeDefPtr = shared_ptr<const UnitTypeDef>;
70
72using AttributeDefPtr = shared_ptr<AttributeDef>;
74using AttributeDefDefPtr = shared_ptr<const AttributeDef>;
75
81class MX_CORE_API NodeDef : public InterfaceElement
82{
83 public:
84 NodeDef(ElementPtr parent, const string& name) :
85 InterfaceElement(parent, CATEGORY, name)
86 {
87 }
88 virtual ~NodeDef() { }
89
92
94 void setNodeString(const string& node)
95 {
96 setAttribute(NODE_ATTRIBUTE, node);
97 }
98
100 bool hasNodeString() const
101 {
102 return hasAttribute(NODE_ATTRIBUTE);
103 }
104
106 const string& getNodeString() const
107 {
108 return getAttribute(NODE_ATTRIBUTE);
109 }
110
112 const string& getType() const override;
113
117
119 void setNodeGroup(const string& category)
120 {
121 setAttribute(NODE_GROUP_ATTRIBUTE, category);
122 }
123
125 bool hasNodeGroup() const
126 {
127 return hasAttribute(NODE_GROUP_ATTRIBUTE);
128 }
129
131 const string& getNodeGroup() const
132 {
133 return getAttribute(NODE_GROUP_ATTRIBUTE);
134 }
135
139
147 InterfaceElementPtr getImplementation(const string& target = EMPTY_STRING) const;
148
152
155 bool validate(string* message = nullptr) const override;
156
160
164 bool isVersionCompatible(const string& version) const;
165
168 ConstInterfaceElementPtr getDeclaration(const string& target = EMPTY_STRING) const override;
169
171
172 public:
173 static const string CATEGORY;
174 static const string NODE_ATTRIBUTE;
175 static const string NODE_GROUP_ATTRIBUTE;
176
177 static const string TEXTURE_NODE_GROUP;
178 static const string PROCEDURAL_NODE_GROUP;
179 static const string GEOMETRIC_NODE_GROUP;
180 static const string ADJUSTMENT_NODE_GROUP;
181 static const string CONDITIONAL_NODE_GROUP;
182 static const string CHANNEL_NODE_GROUP;
183 static const string ORGANIZATION_NODE_GROUP;
184 static const string TRANSLATION_NODE_GROUP;
185};
186
193class MX_CORE_API Implementation : public InterfaceElement
194{
195 public:
196 Implementation(ElementPtr parent, const string& name) :
197 InterfaceElement(parent, CATEGORY, name)
198 {
199 }
200 virtual ~Implementation() { }
201
204
206 void setFile(const string& file)
207 {
208 setAttribute(FILE_ATTRIBUTE, file);
209 }
210
212 bool hasFile() const
213 {
214 return hasAttribute(FILE_ATTRIBUTE);
215 }
216
218 const string& getFile() const
219 {
220 return getAttribute(FILE_ATTRIBUTE);
221 }
222
226
228 void setFunction(const string& function)
229 {
230 setAttribute(FUNCTION_ATTRIBUTE, function);
231 }
232
234 bool hasFunction() const
235 {
236 return hasAttribute(FUNCTION_ATTRIBUTE);
237 }
238
240 const string& getFunction() const
241 {
242 return getAttribute(FUNCTION_ATTRIBUTE);
243 }
244
248
250 void setNodeGraph(const string& nodegraph)
251 {
252 setAttribute(NODE_GRAPH_ATTRIBUTE, nodegraph);
253 }
254
256 bool hasNodeGraph() const
257 {
258 return hasAttribute(NODE_GRAPH_ATTRIBUTE);
259 }
260
262 const string& getNodeGraph() const
263 {
264 return getAttribute(PortElement::NODE_GRAPH_ATTRIBUTE);
265 }
266
270
272 void setNodeDef(ConstNodeDefPtr nodeDef);
273
275 NodeDefPtr getNodeDef() const;
276
280
283 bool validate(string* message = nullptr) const override;
284
288
291 ConstInterfaceElementPtr getDeclaration(const string& target = EMPTY_STRING) const override;
292
294
295 public:
296 static const string CATEGORY;
297 static const string FILE_ATTRIBUTE;
298 static const string FUNCTION_ATTRIBUTE;
299 static const string NODE_GRAPH_ATTRIBUTE;
300};
301
304class MX_CORE_API TypeDef : public Element
305{
306 public:
307 TypeDef(ElementPtr parent, const string& name) :
308 Element(parent, CATEGORY, name)
309 {
310 }
311 virtual ~TypeDef() { }
312
315
317 void setSemantic(const string& semantic)
318 {
319 setAttribute(SEMANTIC_ATTRIBUTE, semantic);
320 }
321
323 bool hasSemantic() const
324 {
325 return hasAttribute(SEMANTIC_ATTRIBUTE);
326 }
327
329 const string& getSemantic() const
330 {
331 return getAttribute(SEMANTIC_ATTRIBUTE);
332 }
333
337
339 void setContext(const string& context)
340 {
341 setAttribute(CONTEXT_ATTRIBUTE, context);
342 }
343
345 bool hasContext() const
346 {
347 return hasAttribute(CONTEXT_ATTRIBUTE);
348 }
349
351 const string& getContext() const
352 {
353 return getAttribute(CONTEXT_ATTRIBUTE);
354 }
355
359
365 MemberPtr addMember(const string& name = EMPTY_STRING)
366 {
367 return addChild<Member>(name);
368 }
369
371 MemberPtr getMember(const string& name) const
372 {
373 return getChildOfType<Member>(name);
374 }
375
377 vector<MemberPtr> getMembers() const
378 {
379 return getChildrenOfType<Member>();
380 }
381
383 void removeMember(const string& name)
384 {
385 removeChildOfType<Member>(name);
386 }
387
389
390 public:
391 static const string CATEGORY;
392 static const string SEMANTIC_ATTRIBUTE;
393 static const string CONTEXT_ATTRIBUTE;
394};
395
398class MX_CORE_API TargetDef : public TypedElement
399{
400 public:
401 TargetDef(ElementPtr parent, const string& name) :
402 TypedElement(parent, CATEGORY, name)
403 {
404 }
405 virtual ~TargetDef() { }
406
412
413 public:
414 static const string CATEGORY;
415};
416
419class MX_CORE_API Member : public TypedElement
420{
421 public:
422 Member(ElementPtr parent, const string& name) :
423 TypedElement(parent, CATEGORY, name)
424 {
425 }
426 virtual ~Member() { }
427
428 public:
429 static const string CATEGORY;
430};
431
434class MX_CORE_API Unit : public Element
435{
436 public:
437 Unit(ElementPtr parent, const string& name) :
438 Element(parent, CATEGORY, name)
439 {
440 }
441 virtual ~Unit() { }
442
443 public:
444 static const string CATEGORY;
445};
446
449class MX_CORE_API UnitDef : public Element
450{
451 public:
452 UnitDef(ElementPtr parent, const string& name) :
453 Element(parent, CATEGORY, name)
454 {
455 }
456 virtual ~UnitDef() { }
457
460
462 void setUnitType(const string& type)
463 {
464 setAttribute(UNITTYPE_ATTRIBUTE, type);
465 }
466
468 bool hasUnitType() const
469 {
470 return hasAttribute(UNITTYPE_ATTRIBUTE);
471 }
472
474 const string& getUnitType() const
475 {
476 return getAttribute(UNITTYPE_ATTRIBUTE);
477 }
478
482
487 UnitPtr addUnit(const string& name)
488 {
489 if (name.empty())
490 {
491 throw Exception("A unit definition name cannot be empty");
492 }
493 return addChild<Unit>(name);
494 }
495
497 UnitPtr getUnit(const string& name) const
498 {
499 return getChildOfType<Unit>(name);
500 }
501
503 vector<UnitPtr> getUnits() const
504 {
505 return getChildrenOfType<Unit>();
506 }
507
509 void removeUnit(const string& name)
510 {
511 removeChildOfType<Unit>(name);
512 }
513
515
516 public:
517 static const string CATEGORY;
518 static const string UNITTYPE_ATTRIBUTE;
519};
520
523class MX_CORE_API UnitTypeDef : public Element
524{
525 public:
526 UnitTypeDef(ElementPtr parent, const string& name) :
527 Element(parent, CATEGORY, name)
528 {
529 }
530 virtual ~UnitTypeDef() { }
531
533 vector<UnitDefPtr> getUnitDefs() const;
534
535 public:
536 static const string CATEGORY;
537};
538
541class MX_CORE_API AttributeDef : public TypedElement
542{
543 public:
544 AttributeDef(ElementPtr parent, const string& name) :
545 TypedElement(parent, CATEGORY, name)
546 {
547 }
548 virtual ~AttributeDef() { }
549
552
554 void setAttrName(const string& name)
555 {
556 setAttribute(ATTRNAME_ATTRIBUTE, name);
557 }
558
560 bool hasAttrName() const
561 {
562 return hasAttribute(ATTRNAME_ATTRIBUTE);
563 }
564
566 const string& getAttrName() const
567 {
568 return getAttribute(ATTRNAME_ATTRIBUTE);
569 }
570
574
576 void setValueString(const string& value)
577 {
578 setAttribute(VALUE_ATTRIBUTE, value);
579 }
580
582 bool hasValueString() const
583 {
584 return hasAttribute(VALUE_ATTRIBUTE);
585 }
586
588 const string& getValueString() const
589 {
590 return getAttribute(VALUE_ATTRIBUTE);
591 }
592
596
598 template <class T> void setValue(const T& value, const string& type = EMPTY_STRING)
599 {
600 setType(!type.empty() ? type : getTypeString<T>());
601 setValueString(toValueString(value));
602 }
603
605 void setValue(const char* value, const string& type = EMPTY_STRING)
606 {
607 setValue(value ? string(value) : EMPTY_STRING, type);
608 }
609
611 bool hasValue() const
612 {
613 return hasAttribute(VALUE_ATTRIBUTE);
614 }
615
622 {
623 if (!hasValue())
624 return ValuePtr();
625 return Value::createValueFromStrings(getValueString(), getType());
626 }
627
631
633 void setElements(const string& elements)
634 {
635 setAttribute(ELEMENTS_ATTRIBUTE, elements);
636 }
637
639 bool hasElements() const
640 {
641 return hasAttribute(ELEMENTS_ATTRIBUTE);
642 }
643
645 const string& getElements() const
646 {
647 return getAttribute(ELEMENTS_ATTRIBUTE);
648 }
649
653
655 void setExportable(bool value)
656 {
657 setTypedAttribute<bool>(EXPORTABLE_ATTRIBUTE, value);
658 }
659
662 bool getExportable() const
663 {
664 return getTypedAttribute<bool>(EXPORTABLE_ATTRIBUTE);
665 }
666
668
669 public:
670 static const string CATEGORY;
671 static const string ATTRNAME_ATTRIBUTE;
672 static const string VALUE_ATTRIBUTE;
673 static const string ELEMENTS_ATTRIBUTE;
674 static const string EXPORTABLE_ATTRIBUTE;
675};
676
677MATERIALX_NAMESPACE_END
678
679#endif
shared_ptr< TargetDef > TargetDefPtr
A shared pointer to a TargetDef.
Definition: Definition.h:47
shared_ptr< const UnitTypeDef > ConstUnitTypeDefPtr
A shared pointer to a const UnitTypeDef.
Definition: Definition.h:69
shared_ptr< UnitDef > UnitDefPtr
A shared pointer to a UnitDef.
Definition: Definition.h:62
shared_ptr< Unit > UnitPtr
A shared pointer to a Unit.
Definition: Definition.h:57
shared_ptr< const UnitDef > ConstUnitDefPtr
A shared pointer to a const UnitDef.
Definition: Definition.h:64
shared_ptr< Implementation > ImplementationPtr
A shared pointer to an Implementation.
Definition: Definition.h:37
shared_ptr< const Member > ConstMemberPtr
A shared pointer to a const Member.
Definition: Definition.h:54
shared_ptr< const Unit > ConstUnitPtr
A shared pointer to a const Unit.
Definition: Definition.h:59
shared_ptr< TypeDef > TypeDefPtr
A shared pointer to a TypeDef.
Definition: Definition.h:42
shared_ptr< const TargetDef > ConstTargetDefPtr
A shared pointer to a const TargetDef.
Definition: Definition.h:49
shared_ptr< const Implementation > ConstImplementationPtr
A shared pointer to a const Implementation.
Definition: Definition.h:39
shared_ptr< AttributeDef > AttributeDefPtr
A shared pointer to an AttributeDef.
Definition: Definition.h:72
shared_ptr< const NodeDef > ConstNodeDefPtr
A shared pointer to a const NodeDef.
Definition: Definition.h:34
shared_ptr< const TypeDef > ConstTypeDefPtr
A shared pointer to a const TypeDef.
Definition: Definition.h:44
shared_ptr< const AttributeDef > AttributeDefDefPtr
A shared pointer to a const AttributeDef.
Definition: Definition.h:74
shared_ptr< Member > MemberPtr
A shared pointer to a Member.
Definition: Definition.h:52
shared_ptr< NodeDef > NodeDefPtr
A shared pointer to a NodeDef.
Definition: Definition.h:32
shared_ptr< UnitTypeDef > UnitTypeDefPtr
A shared pointer to a UnitTypeDef.
Definition: Definition.h:67
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
Interface element subclasses.
shared_ptr< InterfaceElement > InterfaceElementPtr
A shared pointer to an InterfaceElement.
Definition: Interface.h:41
shared_ptr< const InterfaceElement > ConstInterfaceElementPtr
A shared pointer to a const InterfaceElement.
Definition: Interface.h:43
Import and export declarations for the Core library.
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29
MX_CORE_API string toValueString(const T &data)
Convert the given data value to a value string.
An attribute definition element within a Document.
Definition: Definition.h:542
ValuePtr getValue() const
Return the typed value of an element as a generic value object, which may be queried to access its da...
Definition: Definition.h:621
void setAttrName(const string &name)
Set the element's attrname string.
Definition: Definition.h:554
const string & getValueString() const
Get the value string of a element.
Definition: Definition.h:588
const string & getAttrName() const
Return the element's attrname string.
Definition: Definition.h:566
void setValue(const char *value, const string &type=EMPTY_STRING)
Set the typed value of an element from a C-style string.
Definition: Definition.h:605
bool getExportable() const
Return the exportable boolean for the element.
Definition: Definition.h:662
bool hasElements() const
Return true if the element has an elements string.
Definition: Definition.h:639
void setExportable(bool value)
Set the exportable boolean for the element.
Definition: Definition.h:655
void setValueString(const string &value)
Set the value string of an element.
Definition: Definition.h:576
bool hasAttrName() const
Return true if this element has an attrname string.
Definition: Definition.h:560
void setValue(const T &value, const string &type=EMPTY_STRING)
Set the typed value of an element.
Definition: Definition.h:598
void setElements(const string &elements)
Set the element's elements string.
Definition: Definition.h:633
const string & getElements() const
Return the element's elements string.
Definition: Definition.h:645
bool hasValueString() const
Return true if the given element has a value string.
Definition: Definition.h:582
bool hasValue() const
Return true if the element possesses a typed value.
Definition: Definition.h:611
The base class for MaterialX elements.
Definition: Element.h:80
const string & getAttribute(const string &attrib) const
Return the value string of the given attribute.
Definition: Element.h:504
void setAttribute(const string &attrib, const string &value)
Set the value string of the given attribute.
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
Definition: Element.h:497
The base class for exceptions that are propagated from the MaterialX library to the client applicatio...
Definition: Exception.h:22
An implementation element within a Document.
Definition: Definition.h:194
const string & getNodeGraph() const
Return the nodegraph string for the Implementation.
Definition: Definition.h:262
bool hasFile() const
Return true if the given Implementation has a file string.
Definition: Definition.h:212
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
bool hasFunction() const
Return true if the given Implementation has a function string.
Definition: Definition.h:234
const string & getFunction() const
Return the function string for the Implementation.
Definition: Definition.h:240
void setFunction(const string &function)
Set the function string for the Implementation.
Definition: Definition.h:228
NodeDefPtr getNodeDef() const
Return the NodeDef element referenced by the Implementation.
bool hasNodeGraph() const
Return true if the given Implementation has a nodegraph string.
Definition: Definition.h:256
void setNodeGraph(const string &nodegraph)
Set the nodegraph string for the Implementation.
Definition: Definition.h:250
void setNodeDef(ConstNodeDefPtr nodeDef)
Set the NodeDef element referenced by the Implementation.
const string & getFile() const
Return the file string for the Implementation.
Definition: Definition.h:218
void setFile(const string &file)
Set the file string for the Implementation.
Definition: Definition.h:206
ConstInterfaceElementPtr getDeclaration(const string &target=EMPTY_STRING) const override
Return the first declaration of this interface, optionally filtered by the given target name.
The base class for interface elements such as Node, NodeDef, and NodeGraph.
Definition: Interface.h:286
A member element within a TypeDef.
Definition: Definition.h:420
A node definition element within a Document.
Definition: Definition.h:82
bool isVersionCompatible(const string &version) const
Return true if the given version string is compatible with this NodeDef.
void setNodeString(const string &node)
Set the node string of the NodeDef.
Definition: Definition.h:94
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
InterfaceElementPtr getImplementation(const string &target=EMPTY_STRING) const
Return the first implementation for this nodedef, optionally filtered by the given target name.
const string & getType() const override
Return the element's output type.
const string & getNodeGroup() const
Return the node group of the NodeDef.
Definition: Definition.h:131
bool hasNodeGroup() const
Return true if the given NodeDef has a node group.
Definition: Definition.h:125
const string & getNodeString() const
Return the node string of the NodeDef.
Definition: Definition.h:106
bool hasNodeString() const
Return true if the given NodeDef has a node string.
Definition: Definition.h:100
ConstInterfaceElementPtr getDeclaration(const string &target=EMPTY_STRING) const override
Return the first declaration of this interface, optionally filtered by the given target name.
void setNodeGroup(const string &category)
Set the node group of the NodeDef.
Definition: Definition.h:119
A definition of an implementation target.
Definition: Definition.h:399
StringVec getMatchingTargets() const
Return a vector of target names that is matching this targetdef either by itself of by its inheritanc...
A type definition element within a Document.
Definition: Definition.h:305
MemberPtr getMember(const string &name) const
Return the Member, if any, with the given name.
Definition: Definition.h:371
MemberPtr addMember(const string &name=EMPTY_STRING)
Add a Member to the TypeDef.
Definition: Definition.h:365
bool hasSemantic() const
Return true if the given TypeDef has a semantic string.
Definition: Definition.h:323
vector< MemberPtr > getMembers() const
Return a vector of all Member elements in the TypeDef.
Definition: Definition.h:377
bool hasContext() const
Return true if the given TypeDef has a context string.
Definition: Definition.h:345
void setContext(const string &context)
Set the context string of the TypeDef.
Definition: Definition.h:339
void setSemantic(const string &semantic)
Set the semantic string of the TypeDef.
Definition: Definition.h:317
void removeMember(const string &name)
Remove the Member, if any, with the given name.
Definition: Definition.h:383
const string & getContext() const
Return the context string of the TypeDef.
Definition: Definition.h:351
const string & getSemantic() const
Return the semantic string of the TypeDef.
Definition: Definition.h:329
The base class for typed elements.
Definition: Element.h:854
virtual const string & getType() const
Return the element's type string.
Definition: Element.h:884
void setType(const string &type)
Set the element's type string.
Definition: Element.h:872
A unit definition element within a Document.
Definition: Definition.h:450
void removeUnit(const string &name)
Remove the Unit, if any, with the given name.
Definition: Definition.h:509
void setUnitType(const string &type)
Set the element's unittype string.
Definition: Definition.h:462
UnitPtr getUnit(const string &name) const
Return the Unit, if any, with the given name.
Definition: Definition.h:497
bool hasUnitType() const
Return true if the given element has a unittype string.
Definition: Definition.h:468
UnitPtr addUnit(const string &name)
Add a Unit to the UnitDef.
Definition: Definition.h:487
vector< UnitPtr > getUnits() const
Return a vector of all Unit elements in the UnitDef.
Definition: Definition.h:503
const string & getUnitType() const
Return the element's type string.
Definition: Definition.h:474
A unit declaration within a UnitDef.
Definition: Definition.h:435
A unit type definition element within a Document.
Definition: Definition.h:524
vector< UnitDefPtr > getUnitDefs() const
Find all UnitDefs for the UnitTypeDef.
static ValuePtr createValueFromStrings(const string &value, const string &type)
Create a new value instance from value and type strings.