forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElement.h
1331 lines (1124 loc) · 40.7 KB
/
Element.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_ELEMENT_H
#define MATERIALX_ELEMENT_H
/// @file
/// Base and generic element classes
#include <MaterialXCore/Library.h>
#include <MaterialXCore/Traversal.h>
#include <MaterialXCore/Util.h>
#include <MaterialXCore/Value.h>
namespace MaterialX
{
class Element;
class TypedElement;
class ValueElement;
class Token;
class StringResolver;
class Document;
class Material;
class CopyOptions;
/// A shared pointer to an Element
using ElementPtr = shared_ptr<Element>;
/// A shared pointer to a const Element
using ConstElementPtr = shared_ptr<const Element>;
/// A shared pointer to a TypedElement
using TypedElementPtr = shared_ptr<TypedElement>;
/// A shared pointer to a const TypedElement
using ConstTypedElementPtr = shared_ptr<const TypedElement>;
/// A shared pointer to a ValueElement
using ValueElementPtr = shared_ptr<ValueElement>;
/// A shared pointer to a const ValueElement
using ConstValueElementPtr = shared_ptr<const ValueElement>;
/// A shared pointer to a Token
using TokenPtr = shared_ptr<Token>;
/// A shared pointer to a const Token
using ConstTokenPtr = shared_ptr<const Token>;
/// A shared pointer to a StringResolver
using StringResolverPtr = shared_ptr<StringResolver>;
/// A hash map from strings to elements
using ElementMap = std::unordered_map<string, ElementPtr>;
/// A standard function taking an ElementPtr and returning a boolean.
using ElementPredicate = std::function<bool(ElementPtr)>;
/// @class Element
/// The base class for MaterialX elements.
///
/// An Element is a named object within a Document, which may possess any
/// number of child elements and attributes.
class Element : public std::enable_shared_from_this<Element>
{
protected:
Element(ElementPtr parent, const string& category, const string& name) :
_category(category),
_name(name),
_parent(parent),
_root(parent ? parent->getRoot() : nullptr)
{
}
public:
virtual ~Element() { }
protected:
using DocumentPtr = shared_ptr<Document>;
using ConstDocumentPtr = shared_ptr<const Document>;
using ConstMaterialPtr = shared_ptr<const Material>;
template <class T> friend class ElementRegistry;
public:
/// Return true if the given element tree, including all descendants,
/// is identical to this one.
bool operator==(const Element& rhs) const;
/// Return true if the given element tree, including all descendants,
/// differs from this one.
bool operator!=(const Element& rhs) const;
/// @name Category
/// @{
/// Set the element's category string.
void setCategory(const string& category)
{
_category = category;
}
/// Return the element's category string. The category of a MaterialX
/// element represents its role within the document, with common examples
/// being "material", "nodegraph", and "image".
const string& getCategory() const
{
return _category;
}
/// @}
/// @name Name
/// @{
/// Set the element's name string. The name of a MaterialX element must be
/// unique among all elements at the same scope.
/// @throws Exception if an element at the same scope already possesses the
/// given name.
void setName(const string& name);
/// Return the element's name string.
const string& getName() const
{
return _name;
}
/// Return the element's hierarchical name path, relative to the root
/// document. The name of each ancestor will be prepended in turn,
/// separated by forward slashes.
/// @param relativeTo If a valid ancestor element is specified, then
/// the returned path will be relative to this ancestor.
string getNamePath(ConstElementPtr relativeTo = nullptr) const;
/// Return the element specified by the given hierarchical name path,
/// relative to the current element. If the name path is empty then the
/// current element is returned. If no element is found at the given path,
/// then an empty shared pointer is returned.
/// @param namePath The relative name path of the specified element.
ElementPtr getDescendant(const string& namePath);
/// @}
/// @name File Prefix
/// @{
/// Set the element's file prefix string.
void setFilePrefix(const string& prefix)
{
setAttribute(FILE_PREFIX_ATTRIBUTE, prefix);
}
/// Return true if the given element has a file prefix string.
bool hasFilePrefix() const
{
return hasAttribute(FILE_PREFIX_ATTRIBUTE);
}
/// Return the element's file prefix string.
const string& getFilePrefix() const
{
return getAttribute(FILE_PREFIX_ATTRIBUTE);
}
/// Return the file prefix string that is active at the scope of this
/// element, taking all ancestor elements into account.
const string& getActiveFilePrefix() const
{
for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
{
if (elem->hasFilePrefix())
{
return elem->getFilePrefix();
}
}
return EMPTY_STRING;
}
/// @}
/// @name Geom Prefix
/// @{
/// Set the element's geom prefix string.
void setGeomPrefix(const string& prefix)
{
setAttribute(GEOM_PREFIX_ATTRIBUTE, prefix);
}
/// Return true if the given element has a geom prefix string.
bool hasGeomPrefix() const
{
return hasAttribute(GEOM_PREFIX_ATTRIBUTE);
}
/// Return the element's geom prefix string.
const string& getGeomPrefix() const
{
return getAttribute(GEOM_PREFIX_ATTRIBUTE);
}
/// Return the geom prefix string that is active at the scope of this
/// element, taking all ancestor elements into account.
const string& getActiveGeomPrefix() const
{
for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
{
if (elem->hasGeomPrefix())
{
return elem->getGeomPrefix();
}
}
return EMPTY_STRING;
}
/// @}
/// @name Color Space
/// @{
/// Set the element's color space string.
void setColorSpace(const string& colorSpace)
{
setAttribute(COLOR_SPACE_ATTRIBUTE, colorSpace);
}
/// Return true if the given element has a color space string.
bool hasColorSpace() const
{
return hasAttribute(COLOR_SPACE_ATTRIBUTE);
}
/// Return the element's color space string.
const string& getColorSpace() const
{
return getAttribute(COLOR_SPACE_ATTRIBUTE);
}
/// Return the color space string that is active at the scope of this
/// element, taking all ancestor elements into account.
const string& getActiveColorSpace() const
{
for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
{
if (elem->hasColorSpace())
{
return elem->getColorSpace();
}
}
return EMPTY_STRING;
}
/// @}
/// @name Target
/// @{
/// Set the element's target string.
void setTarget(const string& target)
{
setAttribute(TARGET_ATTRIBUTE, target);
}
/// Return true if the given element has a target string.
bool hasTarget() const
{
return hasAttribute(TARGET_ATTRIBUTE);
}
/// Return the element's target string.
const string& getTarget() const
{
return getAttribute(TARGET_ATTRIBUTE);
}
/// @}
/// @name Inheritance
/// @{
/// Set the inherit string of this element.
void setInheritString(const string& inherit)
{
setAttribute(INHERIT_ATTRIBUTE, inherit);
}
/// Return true if this element has an inherit string.
bool hasInheritString() const
{
return hasAttribute(INHERIT_ATTRIBUTE);
}
/// Return the inherit string of this element.
const string& getInheritString() const
{
return getAttribute(INHERIT_ATTRIBUTE);
}
/// Set the element that this one directly inherits from.
void setInheritsFrom(ConstElementPtr super)
{
if (super)
{
setInheritString(super->getName());
}
else
{
removeAttribute(INHERIT_ATTRIBUTE);
}
}
/// Return the element, if any, that this one directly inherits from.
ElementPtr getInheritsFrom() const
{
return resolveRootNameReference<Element>(getInheritString());
}
/// Return true if this element has the given element as an inherited base,
/// taking the full inheritance chain into account.
bool hasInheritedBase(ConstElementPtr base) const;
/// Return true if the inheritance chain for this element contains a cycle.
bool hasInheritanceCycle() const;
/// @}
/// @name Namespace
/// @{
/// Set the namespace string of this element.
void setNamespace(const string& space)
{
setAttribute(NAMESPACE_ATTRIBUTE, space);
}
/// Return true if this element has a namespace string.
bool hasNamespace() const
{
return hasAttribute(NAMESPACE_ATTRIBUTE);
}
/// Return the namespace string of this element.
const string& getNamespace() const
{
return getAttribute(NAMESPACE_ATTRIBUTE);
}
/// Return a qualified version of the given name, taking the namespace at the
/// scope of this element into account.
string getQualifiedName(const string& name) const
{
for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
{
if (elem->hasNamespace())
{
return elem->getNamespace() + NAME_PREFIX_SEPARATOR + name;
}
}
return name;
}
/// @}
/// @name Version
/// @{
/// Set the version string of this element.
void setVersionString(const string& version)
{
setAttribute(VERSION_ATTRIBUTE, version);
}
/// Return true if this element has a version string.
bool hasVersionString() const
{
return hasAttribute(VERSION_ATTRIBUTE);
}
/// Return the version string of this element.
const string& getVersionString() const
{
return getAttribute(VERSION_ATTRIBUTE);
}
/// Return the major and minor versions as an integer pair.
virtual std::pair<int, int> getVersionIntegers() const;
/// @}
/// @name Default Version
/// @{
/// Set the default version flag of this element.
void setDefaultVersion(bool defaultVersion)
{
setTypedAttribute<bool>(DEFAULT_VERSION_ATTRIBUTE, defaultVersion);
}
/// Return the default version flag of this element.
bool getDefaultVersion() const
{
return getTypedAttribute<bool>(DEFAULT_VERSION_ATTRIBUTE);
}
/// @}
/// @name Subclass
/// @{
/// Return true if this element belongs to the given subclass.
/// If a category string is specified, then both subclass and category
/// matches are required.
template<class T> bool isA(const string& category = EMPTY_STRING) const
{
if (!asA<T>())
return false;
if (!category.empty() && getCategory() != category)
return false;
return true;
}
/// Dynamic cast to an instance of the given subclass.
template<class T> shared_ptr<T> asA();
/// Dynamic cast to a const instance of the given subclass.
template<class T> shared_ptr<const T> asA() const;
/// @}
/// @name Child Elements
/// @{
/// Add a child element of the given subclass and name.
/// @param name The name of the new child element.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @throws Exception if a child of this element already possesses the
/// given name.
/// @return A shared pointer to the new child element.
template<class T> shared_ptr<T> addChild(const string& name = EMPTY_STRING);
/// Add a child element of the given category and name.
/// @param category The category string of the new child element.
/// If the category string is recognized, then the correponding Element
/// subclass is generated; otherwise, a GenericElement is generated.
/// @param name The name of the new child element.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @throws Exception if a child of this element already possesses the
/// given name.
/// @return A shared pointer to the new child element.
ElementPtr addChildOfCategory(const string& category,
const string& name = EMPTY_STRING);
/// Return the child element, if any, with the given name.
ElementPtr getChild(const string& name) const
{
ElementMap::const_iterator it = _childMap.find(name);
if (it == _childMap.end())
return ElementPtr();
else
return it->second;
}
/// Return the child element, if any, with the given name and subclass.
/// If a child with the given name exists, but belongs to a different
/// subclass, then an empty shared pointer is returned.
template<class T> shared_ptr<T> getChildOfType(const string& name) const
{
ElementPtr child = getChild(name);
return child ? child->asA<T>() : shared_ptr<T>();
}
/// Return a constant vector of all child elements.
/// The returned vector maintains the order in which children were added.
const vector<ElementPtr>& getChildren() const
{
return _childOrder;
}
/// Return a vector of all child elements that are instances of the given
/// subclass, optionally filtered by the given category string. The returned
/// vector maintains the order in which children were added.
template<class T> vector< shared_ptr<T> > getChildrenOfType(const string& category = EMPTY_STRING) const
{
vector< shared_ptr<T> > children;
for (ElementPtr child : _childOrder)
{
shared_ptr<T> instance = child->asA<T>();
if (!instance)
continue;
if (!category.empty() && child->getCategory() != category)
continue;
children.push_back(instance);
}
return children;
}
/// Set the index of the child, if any, with the given name.
/// If the given index is out of bounds, then an exception is thrown.
void setChildIndex(const string& name, int index);
/// Return the index of the child, if any, with the given name.
/// If no child with the given name is found, then -1 is returned.
int getChildIndex(const string& name) const;
/// Remove the child element, if any, with the given name.
void removeChild(const string& name);
/// Remove the child element, if any, with the given name and subclass.
/// If a child with the given name exists, but belongs to a different
/// subclass, then this method has no effect.
template<class T> void removeChildOfType(const string& name)
{
if (getChildOfType<T>(name))
removeChild(name);
}
/// @}
/// @name Attributes
/// @{
/// Set the value string of the given attribute.
void setAttribute(const string& attrib, const string& value);
/// Return true if the given attribute is present.
bool hasAttribute(const string& attrib) const
{
return _attributeMap.count(attrib) != 0;
}
/// Return the value string of the given attribute. If the given attribute
/// is not present, then an empty string is returned.
const string& getAttribute(const string& attrib) const
{
StringMap::const_iterator it = _attributeMap.find(attrib);
if (it == _attributeMap.end())
return EMPTY_STRING;
else
return it->second;
}
/// Return a vector of stored attribute names, in the order they were set.
const StringVec& getAttributeNames() const
{
return _attributeOrder;
}
/// Set the value of an implicitly typed attribute. Since an attribute
/// stores no explicit type, the same type argument must be used in
/// corresponding calls to getTypedAttribute.
template<class T> void setTypedAttribute(const string& attrib, const T& data)
{
setAttribute(attrib, toValueString(data));
}
/// Return the the value of an implicitly typed attribute. If the given
/// attribute is not present, or cannot be converted to the given data
/// type, then the zero value for the data type is returned.
template<class T> const T getTypedAttribute(const string& attrib) const
{
try
{
return fromValueString<T>(getAttribute(attrib));
}
catch (ExceptionTypeError&)
{
}
return {};
}
/// Remove the given attribute, if present.
void removeAttribute(const string& attrib);
/// @}
/// @name Self And Ancestor Elements
/// @{
/// Return our self pointer.
ElementPtr getSelf()
{
return shared_from_this();
}
/// Return our self pointer.
ConstElementPtr getSelf() const
{
return shared_from_this();
}
/// Return our parent element.
ElementPtr getParent()
{
return _parent.lock();
}
/// Return our parent element.
ConstElementPtr getParent() const
{
return _parent.lock();
}
/// Return the root element of our tree.
ElementPtr getRoot();
/// Return the root element of our tree.
ConstElementPtr getRoot() const;
/// Return the root document of our tree.
DocumentPtr getDocument()
{
return getRoot()->asA<Document>();
}
/// Return the root document of our tree.
ConstDocumentPtr getDocument() const
{
return getRoot()->asA<Document>();
}
/// Return the first ancestor of the given subclass, or an empty shared
/// pointer if no ancestor of this subclass is found.
template<class T> shared_ptr<const T> getAncestorOfType() const
{
for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
{
shared_ptr<const T> typedElem = elem->asA<T>();
if (typedElem)
{
return typedElem;
}
}
return nullptr;
}
/// @}
/// @name Traversal
/// @{
/// Traverse the tree from the given element to each of its descendants in
/// depth-first order, using pre-order visitation.
/// @return A TreeIterator object.
/// @details Example usage with an implicit iterator:
/// @code
/// for (ElementPtr elem : inputElem->traverseTree())
/// {
/// cout << elem->asString() << endl;
/// }
/// @endcode
/// Example usage with an explicit iterator:
/// @code
/// for (mx::TreeIterator it = inputElem->traverseTree().begin(); it != mx::TreeIterator::end(); ++it)
/// {
/// mx::ElementPtr elem = it.getElement();
/// cout << elem->asString() << " at depth " << it.getElementDepth() << endl;
/// }
/// @endcode
TreeIterator traverseTree() const;
/// Traverse the dataflow graph from the given element to each of its
/// upstream sources in depth-first order, using pre-order visitation.
/// @param material An optional material element, whose data bindings will
/// be applied to the traversal.
/// @throws ExceptionFoundCycle if a cycle is encountered.
/// @return A GraphIterator object.
/// @details Example usage with an implicit iterator:
/// @code
/// for (Edge edge : inputElem->traverseGraph())
/// {
/// ElementPtr upElem = edge.getUpstreamElement();
/// ElementPtr downElem = edge.getDownstreamElement();
/// cout << upElem->asString() << " lies upstream from " << downElem->asString() << endl;
/// }
/// @endcode
/// Example usage with an explicit iterator:
/// @code
/// for (mx::GraphIterator it = inputElem->traverseGraph().begin(); it != mx::GraphIterator::end(); ++it)
/// {
/// mx::ElementPtr elem = it.getUpstreamElement();
/// cout << elem->asString() << " at depth " << it.getElementDepth() << endl;
/// }
/// @endcode
/// @sa getUpstreamEdge
/// @sa getUpstreamElement
GraphIterator traverseGraph(ConstMaterialPtr material = nullptr) const;
/// Return the Edge with the given index that lies directly upstream from
/// this element in the dataflow graph.
/// @param material An optional material element, whose data bindings will
/// be applied to the query.
/// @param index An optional index of the edge to be returned, where the
/// valid index range may be determined with getUpstreamEdgeCount.
/// @return The upstream Edge, if valid, or an empty Edge object.
virtual Edge getUpstreamEdge(ConstMaterialPtr material = nullptr,
size_t index = 0) const;
/// Return the number of queriable upstream edges for this element.
virtual size_t getUpstreamEdgeCount() const
{
return 0;
}
/// Return the Element with the given index that lies directly upstream
/// from this one in the dataflow graph.
/// @param material An optional material element, whose data bindings will
/// be applied to the query.
/// @param index An optional index of the element to be returned, where the
/// valid index range may be determined with getUpstreamEdgeCount.
/// @return The upstream Element, if valid, or an empty ElementPtr.
ElementPtr getUpstreamElement(ConstMaterialPtr material = nullptr,
size_t index = 0) const;
/// Traverse the inheritance chain from the given element to each element
/// from which it inherits.
/// @throws ExceptionFoundCycle if a cycle is encountered.
/// @return An InheritanceIterator object.
/// @details Example usage:
/// @code
/// ConstElementPtr derivedElem;
/// for (ConstElementPtr elem : inputElem->traverseInheritance())
/// {
/// if (derivedElem)
/// cout << derivedElem->asString() << " inherits from " << elem->asString() << endl;
/// derivedElem = elem;
/// }
/// @endcode
InheritanceIterator traverseInheritance() const;
/// @}
/// @name Source URI
/// @{
/// Set the element's source URI.
/// @param sourceUri A URI string representing the resource from which
/// this element originates. This string may be used by serialization
/// and deserialization routines to maintain hierarchies of include
/// references.
void setSourceUri(const string& sourceUri)
{
_sourceUri = sourceUri;
}
/// Return true if this element has a source URI.
bool hasSourceUri() const
{
return !_sourceUri.empty();
}
/// Return the element's source URI.
const string& getSourceUri() const
{
return _sourceUri;
}
/// Return the source URI that is active at the scope of this
/// element, taking all ancestor elements into account.
const string& getActiveSourceUri() const
{
for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
{
if (elem->hasSourceUri())
{
return elem->getSourceUri();
}
}
return EMPTY_STRING;
}
/// @}
/// @name Validation
/// @{
/// Validate that the given element tree, including all descendants, is
/// consistent with the MaterialX specification.
virtual bool validate(string* message = nullptr) const;
/// @}
/// @name Utility
/// @{
/// Copy all attributes and descendants from the given element to this one.
/// @param source The element from which content is copied.
/// @param copyOptions An optional pointer to a CopyOptions object.
/// If provided, then the given options will affect the behavior of the
/// copy function. Defaults to a null pointer.
void copyContentFrom(const ConstElementPtr& source, const CopyOptions* copyOptions = nullptr);
/// Clear all attributes and descendants from this element.
void clearContent();
/// Using the input name as a starting point, modify it to create a valid,
/// unique name for a child element.
string createValidChildName(string name) const
{
name = createValidName(name);
while (_childMap.count(name))
{
name = incrementName(name);
}
return name;
}
/// Construct a StringResolver at the scope of this element. The returned
/// object may be used to apply substring modifiers to data values in the
/// context of a specific element, geometry, and material.
/// @param geom An optional geometry name, which will be used to select the
/// applicable set of geometry token substitutions. By default, no
/// geometry token substitutions are applied. If the universal geometry
/// name "/" is given, then all geometry token substitutions are applied,
/// @param material An optional material element, which will be used to
/// select the applicable set of interface token substitutions.
/// @param target An optional target name, which will be used to filter
/// the shader references within the material that are considered.
/// @param type An optional shader type (e.g. "surfaceshader"), which will
/// be used to filter the shader references within the material that are
/// considered.
/// @return A shared pointer to a StringResolver.
/// @todo The StringResolver returned by this method doesn't yet take
/// variant assignments into account.
StringResolverPtr createStringResolver(const string& geom = EMPTY_STRING,
ConstMaterialPtr material = nullptr,
const string& target = EMPTY_STRING,
const string& type = EMPTY_STRING) const;
/// Return a single-line description of this element, including its category,
/// name, and attributes.
string asString() const;
/// @}
protected:
// Resolve a reference to a named element at the root scope of this document,
// taking the namespace at the scope of this element into account.
template<class T> shared_ptr<T> resolveRootNameReference(const string& name) const
{
ConstElementPtr root = getRoot();
shared_ptr<T> child = root->getChildOfType<T>(getQualifiedName(name));
return child ? child : root->getChildOfType<T>(name);
}
// Enforce a requirement within a validate method, updating the validation
// state and optional output text if the requirement is not met.
void validateRequire(bool expression, bool& res, string* message, string errorDesc) const;
public:
static const string NAME_ATTRIBUTE;
static const string FILE_PREFIX_ATTRIBUTE;
static const string GEOM_PREFIX_ATTRIBUTE;
static const string COLOR_SPACE_ATTRIBUTE;
static const string TARGET_ATTRIBUTE;
static const string VERSION_ATTRIBUTE;
static const string DEFAULT_VERSION_ATTRIBUTE;
static const string INHERIT_ATTRIBUTE;
static const string NAMESPACE_ATTRIBUTE;
protected:
virtual void registerChildElement(ElementPtr child);
virtual void unregisterChildElement(ElementPtr child);
// Return a non-const copy of our self pointer, for use in constructing
// graph traversal objects that require non-const storage.
ElementPtr getSelfNonConst() const
{
return std::const_pointer_cast<Element>(shared_from_this());
}
protected:
string _category;
string _name;
string _sourceUri;
ElementMap _childMap;
vector<ElementPtr> _childOrder;
StringMap _attributeMap;
StringVec _attributeOrder;
weak_ptr<Element> _parent;
weak_ptr<Element> _root;
private:
Element(const Element&) = delete;
Element& operator=(const Element&) = delete;
template <class T> static ElementPtr createElement(ElementPtr parent, const string& name)
{
return std::make_shared<T>(parent, name);
}
private:
using CreatorFunction = ElementPtr (*)(ElementPtr, const string&);
using CreatorMap = std::unordered_map<string, CreatorFunction>;
static CreatorMap _creatorMap;
};
/// @class TypedElement
/// The base class for typed elements.
class TypedElement : public Element
{
protected:
TypedElement(ElementPtr parent, const string& category, const string& name) :
Element(parent, category, name)
{
}
public:
virtual ~TypedElement() { }
protected:
using TypeDefPtr = shared_ptr<class TypeDef>;
public:
/// @}
/// @name Type String
/// Set the element's type string.
void setType(const string& type)
{
setAttribute(TYPE_ATTRIBUTE, type);
}
/// Return true if the given element has a type string.
bool hasType() const
{
return hasAttribute(TYPE_ATTRIBUTE);
}
/// Return the element's type string.
const string& getType() const
{
return getAttribute(TYPE_ATTRIBUTE);
}
/// Return true if the element is of multi-output type.
bool isMultiOutputType() const
{
return getType() == MULTI_OUTPUT_TYPE_STRING;
}
/// @}
/// @name TypeDef References
/// @{
/// Return the TypeDef declaring the type string of this element. If no
/// matching TypeDef is found, then an empty shared pointer is returned.
TypeDefPtr getTypeDef() const;
/// @}
public:
static const string TYPE_ATTRIBUTE;
};
/// @class ValueElement
/// The base class for elements that support typed values.
class ValueElement : public TypedElement
{
protected:
ValueElement(ElementPtr parent, const string& category, const string& name) :
TypedElement(parent, category, name)
{
}
public:
virtual ~ValueElement() { }
/// @name Value String
/// @{
/// Set the value string of an element.
void setValueString(const string& value)
{
setAttribute(VALUE_ATTRIBUTE, value);
}
/// Return true if the given element has a value string.
bool hasValueString() const
{
return hasAttribute(VALUE_ATTRIBUTE);
}
/// Get the value string of a element.
const string& getValueString() const
{
return getAttribute(VALUE_ATTRIBUTE);
}
/// Return the resolved value string of an element, applying any string
/// substitutions that are defined at the element's scope.
/// @param resolver An optional string resolver, which will be used to
/// apply string substitutions. By default, a new string resolver
/// will be created at this scope and applied to the return value.
string getResolvedValueString(StringResolverPtr resolver = nullptr) const;
/// @}
/// @name Interface Names
/// @{
/// Set the interface name of an element.
void setInterfaceName(const string& name)
{
setAttribute(INTERFACE_NAME_ATTRIBUTE, name);
}
/// Return true if the given element has an interface name.
bool hasInterfaceName() const
{
return hasAttribute(INTERFACE_NAME_ATTRIBUTE);
}
/// Return the interface name of an element.
const string& getInterfaceName() const
{