forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.cpp
2281 lines (1964 loc) · 69.7 KB
/
path.cpp
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
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#include "pxr/pxr.h"
#include "pxr/usd/sdf/path.h"
#include "pxr/usd/sdf/pathNode.h"
#include "pxr/usd/sdf/pathParser.h"
#include "pxr/base/vt/value.h"
#include "pxr/base/arch/hints.h"
#include "pxr/base/tf/iterator.h"
#include "pxr/base/tf/staticData.h"
#include "pxr/base/tf/stringUtils.h"
#include "pxr/base/tf/mallocTag.h"
#include "pxr/base/tf/stl.h"
#include "pxr/base/tf/type.h"
#include "pxr/base/trace/trace.h"
#include <algorithm>
#include <ostream>
using std::pair;
using std::string;
using std::vector;
PXR_NAMESPACE_OPEN_SCOPE
namespace {
// This is a simple helper class that records but defers issuing diagnostics
// until its destructor runs. It's used in the 'isValid' callbacks below to
// ensure that we do not issue diagnostics while the path internal locks are
// held, since that invokes unknown code (via diagnostic delegates) which could
// reenter here.
class _DeferredDiagnostics
{
public:
template <class ... Types>
void Warn(Types&& ... args) {
_Get().emplace_back(TF_DIAGNOSTIC_WARNING_TYPE,
_FormatString(std::forward<Types>(args)...));
}
template <class ... Types>
void CodingError(Types&& ... args) {
_Get().emplace_back(TF_DIAGNOSTIC_CODING_ERROR_TYPE,
_FormatString(std::forward<Types>(args)...));
}
~_DeferredDiagnostics() {
if (!_diagnostics) {
return;
}
for (auto const &pr: *_diagnostics) {
if (pr.first == TF_DIAGNOSTIC_WARNING_TYPE) {
TF_WARN(pr.second);
}
else if (pr.first == TF_DIAGNOSTIC_CODING_ERROR_TYPE) {
TF_CODING_ERROR(pr.second);
}
}
}
private:
template <class ... Types>
static std::string _FormatString(Types&& ... args) {
return TfStringPrintf(std::forward<Types>(args)...);
}
// Single-argument overload to avoid calling TfStringPrintf with
// just a format string, which causes warnings at build time.
static std::string _FormatString(char const *str) {
// printf converts "%%" to "%" even when no arguments are supplied,
// so for consistency we do the same here.
return TfStringReplace(std::string(str), "%%", "%");
}
std::vector<std::pair<TfDiagnosticType, std::string>> &_Get() {
if (!_diagnostics) {
_diagnostics = std::make_unique<
std::vector<std::pair<TfDiagnosticType, std::string>>
>();
}
return *_diagnostics;
}
std::unique_ptr<
std::vector<
std::pair<
TfDiagnosticType, std::string
>
>
> _diagnostics;
};
} // anon
static inline bool _IsValidIdentifier(TfToken const &name);
// XXX: Enable this define to make bad path strings
// cause runtime errors. This can be useful when trying to track down cases
// of bad path strings originating from python code.
// #define PARSE_ERRORS_ARE_ERRORS
TF_REGISTRY_FUNCTION(TfType)
{
TfType::Define<SdfPath>();
TfType::Define< vector<SdfPath> >()
.Alias(TfType::GetRoot(), "vector<SdfPath>");
}
SdfPath::SdfPath(const std::string &path) {
TfAutoMallocTag2 tag("Sdf", "SdfPath::SdfPath(string)");
TRACE_FUNCTION();
Sdf_PathParserContext context;
// Initialize the scanner, allowing it to be reentrant.
pathYylex_init(&context.scanner);
yy_buffer_state *b = pathYy_scan_bytes(path.c_str(), path.size(),
context.scanner);
if( pathYyparse(&context) != 0 ) {
#ifdef PARSE_ERRORS_ARE_ERRORS
TF_RUNTIME_ERROR("Ill-formed SdfPath <%s>: %s",
path.c_str(), context.errStr.c_str());
#else
TF_WARN("Ill-formed SdfPath <%s>: %s",
path.c_str(), context.errStr.c_str());
#endif
} else {
*this = std::move(context.path);
}
// Clean up.
pathYy_delete_buffer(b, context.scanner);
pathYylex_destroy(context.scanner);
}
const SdfPath &
SdfPath::EmptyPath()
{
static SdfPath theEmptyPath;
return theEmptyPath;
}
const SdfPath &
SdfPath::AbsoluteRootPath()
{
static SdfPath *theAbsoluteRootPath =
new SdfPath(Sdf_PathNode::GetAbsoluteRootNode(), nullptr);
return *theAbsoluteRootPath;
}
const SdfPath &
SdfPath::ReflexiveRelativePath()
{
static SdfPath *theReflexiveRelativePath =
new SdfPath(Sdf_PathNode::GetRelativeRootNode(), nullptr);
return *theReflexiveRelativePath;
}
size_t
SdfPath::GetPathElementCount() const
{
size_t primElems = _primPart ? _primPart->GetElementCount() : 0;
size_t propElems = _propPart ? _propPart->GetElementCount() : 0;
return primElems + propElems;
}
bool
SdfPath::IsAbsolutePath() const
{
return _primPart && _primPart->IsAbsolutePath();
}
bool
SdfPath::IsAbsoluteRootPath() const
{
return !_propPart && _primPart && _primPart->IsAbsoluteRoot();
}
bool
SdfPath::IsPrimPath() const
{
return !_propPart && _primPart &&
(_primPart->GetNodeType() == Sdf_PathNode::PrimNode ||
*this == ReflexiveRelativePath());
}
bool
SdfPath::IsAbsoluteRootOrPrimPath() const
{
return !_propPart && _primPart &&
(_primPart->GetNodeType() == Sdf_PathNode::PrimNode ||
*this == AbsoluteRootPath() ||
*this == ReflexiveRelativePath());
}
bool
SdfPath::IsRootPrimPath() const {
if (_propPart)
return false;
Sdf_PathNode const *primNode = _primPart.get();
return primNode && primNode->IsAbsolutePath() &&
primNode->GetElementCount() == 1;
}
bool
SdfPath::IsPropertyPath() const
{
if (Sdf_PathNode const *propNode = _propPart.get()) {
auto nodeType = propNode->GetNodeType();
return nodeType == Sdf_PathNode::PrimPropertyNode ||
nodeType == Sdf_PathNode::RelationalAttributeNode;
}
return false;
}
bool
SdfPath::IsPrimPropertyPath() const
{
if (Sdf_PathNode const *propNode = _propPart.get()) {
return propNode->GetNodeType() == Sdf_PathNode::PrimPropertyNode;
}
return false;
}
bool
SdfPath::IsNamespacedPropertyPath() const
{
if (Sdf_PathNode const *propNode = _propPart.get()) {
return propNode->IsNamespaced() &&
// Currently this subexpression is always true if IsNamespaced() is.
((propNode->GetNodeType() ==
Sdf_PathNode::PrimPropertyNode) ||
(propNode->GetNodeType() ==
Sdf_PathNode::RelationalAttributeNode));
}
return false;
}
bool
SdfPath::IsPrimVariantSelectionPath() const
{
if (_propPart)
return false;
if (Sdf_PathNode const *primNode = _primPart.get()) {
return primNode->GetNodeType() ==
Sdf_PathNode::PrimVariantSelectionNode;
}
return false;
}
bool
SdfPath::IsPrimOrPrimVariantSelectionPath() const
{
if (_propPart)
return false;
if (Sdf_PathNode const *primNode = _primPart.get()) {
auto nodeType = primNode->GetNodeType();
return
nodeType == Sdf_PathNode::PrimNode ||
nodeType == Sdf_PathNode::PrimVariantSelectionNode ||
*this == ReflexiveRelativePath();
}
return false;
}
bool
SdfPath::ContainsPrimVariantSelection() const
{
if (Sdf_PathNode const *primNode = _primPart.get()) {
return primNode->ContainsPrimVariantSelection();
}
return false;
}
bool
SdfPath::ContainsTargetPath() const
{
if (Sdf_PathNode const *propNode = _propPart.get()) {
return propNode->ContainsTargetPath();
}
return false;
}
bool
SdfPath::IsRelationalAttributePath() const {
if (Sdf_PathNode const *propNode = _propPart.get()) {
return propNode->GetNodeType() == Sdf_PathNode::RelationalAttributeNode;
}
return false;
}
bool
SdfPath::IsTargetPath() const {
if (Sdf_PathNode const *propNode = _propPart.get()) {
return propNode->GetNodeType() == Sdf_PathNode::TargetNode;
}
return false;
}
bool
SdfPath::IsMapperPath() const {
if (Sdf_PathNode const *propNode = _propPart.get()) {
return propNode->GetNodeType() == Sdf_PathNode::MapperNode;
}
return false;
}
bool
SdfPath::IsMapperArgPath() const {
if (Sdf_PathNode const *propNode = _propPart.get()) {
return propNode->GetNodeType() == Sdf_PathNode::MapperArgNode;
}
return false;
}
bool
SdfPath::IsExpressionPath() const {
if (Sdf_PathNode const *propNode = _propPart.get()) {
return propNode->GetNodeType() == Sdf_PathNode::ExpressionNode;
}
return false;
}
TfToken
SdfPath::GetAsToken() const
{
if (_primPart) {
return Sdf_PathNode::GetPathAsToken(_primPart.get(), _propPart.get());
}
return TfToken();
}
std::string
SdfPath::GetAsString() const
{
return GetAsToken().GetString();
}
TfToken const &
SdfPath::GetToken() const
{
if (_primPart) {
return Sdf_PathNode::GetPathToken(_primPart.get(), _propPart.get());
}
return SdfPathTokens->empty;
}
const std::string &
SdfPath::GetString() const
{
return GetToken().GetString();
}
const char *
SdfPath::GetText() const
{
return GetToken().GetText();
}
SdfPathVector
SdfPath::GetPrefixes() const {
SdfPathVector result;
GetPrefixes(&result);
return result;
}
void
SdfPath::GetPrefixes(SdfPathVector *prefixes) const
{
Sdf_PathNode const *prop = _propPart.get();
Sdf_PathNode const *prim = _primPart.get();
size_t elemCount = GetPathElementCount();
prefixes->resize(elemCount);
SdfPathVector::reverse_iterator iter = prefixes->rbegin();
while (prop && elemCount--) {
*iter++ = SdfPath(prim, prop);
prop = prop->GetParentNode();
}
while (prim && elemCount--) {
*iter++ = SdfPath(prim, prop);
prim = prim->GetParentNode();
}
}
SdfPathAncestorsRange
SdfPath::GetAncestorsRange() const
{
return SdfPathAncestorsRange(*this);
}
const std::string &
SdfPath::GetName() const
{
return GetNameToken().GetString();
}
const TfToken &
SdfPath::GetNameToken() const
{
if (_propPart) {
return _propPart.get()->GetName();
}
return _primPart ? _primPart.get()->GetName() : SdfPathTokens->empty;
}
string
SdfPath::GetElementString() const
{
return GetElementToken().GetString();
}
TfToken
SdfPath::GetElementToken() const
{
if (_propPart)
return _propPart.get()->GetElement();
return _primPart ? _primPart.get()->GetElement() : TfToken();
}
SdfPath
SdfPath::ReplaceName(TfToken const &newName) const
{
if (IsPrimPath())
return GetParentPath().AppendChild(newName);
else if (IsPrimPropertyPath())
return GetParentPath().AppendProperty(newName);
else if (IsRelationalAttributePath())
return GetParentPath().AppendRelationalAttribute(newName);
TF_CODING_ERROR("%s is not a prim, property, "
"or relational attribute path", GetText());
return SdfPath();
}
static Sdf_PathNode const *
_GetNextTargetNode(Sdf_PathNode const *curNode)
{
if (!curNode || !curNode->ContainsTargetPath())
return nullptr;
// Find nearest target or mapper node.
while (curNode
&& curNode->GetNodeType() != Sdf_PathNode::TargetNode
&& curNode->GetNodeType() != Sdf_PathNode::MapperNode) {
curNode = curNode->GetParentNode();
}
return curNode;
}
const SdfPath &
SdfPath::GetTargetPath() const
{
if (!_propPart)
return EmptyPath();
Sdf_PathNode const *targetNode = _GetNextTargetNode(_propPart.get());
return targetNode ? targetNode->GetTargetPath() : EmptyPath();
}
void
SdfPath::GetAllTargetPathsRecursively(SdfPathVector *result) const
{
if (!_propPart)
return;
for (Sdf_PathNode const *targetNode = _GetNextTargetNode(_propPart.get());
targetNode;
targetNode = _GetNextTargetNode(targetNode->GetParentNode())) {
SdfPath const &targetPath = targetNode->GetTargetPath();
result->push_back(targetPath);
targetPath.GetAllTargetPathsRecursively(result);
}
}
pair<string, string>
SdfPath::GetVariantSelection() const
{
pair<string, string> result;
if (IsPrimVariantSelectionPath()) {
const Sdf_PathNode::VariantSelectionType& sel =
_primPart.get()->GetVariantSelection();
result.first = sel.first.GetString();
result.second = sel.second.GetString();
}
return result;
}
bool
SdfPath::HasPrefix(const SdfPath &prefix) const
{
if (prefix.IsEmpty() || IsEmpty())
return false;
if (prefix._propPart) {
// The prefix is a property-like path, in order for it to be a prefix of
// this path, we must also have a property part, and our prim part must
// be the same as the prefix's prim part.
if (_primPart != prefix._primPart || !_propPart) {
return false;
}
// Now walk up property parts until we hit prefix._propPart or we
// recurse above its depth.
Sdf_PathNode const *propNode = _propPart.get();
Sdf_PathNode const *prefixPropNode = prefix._propPart.get();
while (propNode && propNode != prefixPropNode) {
propNode = propNode->GetParentNode();
}
return propNode == prefixPropNode;
}
else {
// The prefix is a prim-like path. Walk up nodes until we achieve the
// same depth as the prefix, then just check for equality.
Sdf_PathNode const *primNode = _primPart.get();
if (primNode->IsAbsolutePath() &&
prefix == SdfPath::AbsoluteRootPath()) {
return true;
}
Sdf_PathNode const *prefixPrimNode = prefix._primPart.get();
int prefixDepth = prefixPrimNode->GetElementCount();
int curDepth = primNode->GetElementCount();
if (curDepth < prefixDepth) {
return false;
}
while (curDepth > prefixDepth) {
primNode = primNode->GetParentNode();
--curDepth;
}
return primNode == prefixPrimNode;
}
}
SdfPath
SdfPath::GetParentPath() const {
if (IsEmpty()) {
return *this;
}
// If this is a property-like path, trim that first.
if (_propPart) {
Sdf_PathNode const *propNode = _propPart.get()->GetParentNode();
return SdfPath(_primPart, Sdf_PathPropNodeHandle(propNode));
}
// This is a prim-like path. If this is an absolute path (most common case)
// then it's just the parent path node. On the other hand if this path is a
// relative path, and is '.' or ends with '..', the logical parent path is
// made by appending a '..' component.
//
// XXX: NOTE that this is NOT the way that that Sdf_PathNode::GetParentNode
// works, and note that most of the code in SdfPath uses GetParentNode
// intentionally.
Sdf_PathNode const *primNode = _primPart.get();
if (ARCH_LIKELY(
primNode->IsAbsolutePath() ||
(primNode != Sdf_PathNode::GetRelativeRootNode() &&
primNode->GetName() != SdfPathTokens->parentPathElement))) {
return SdfPath(primNode->GetParentNode(), nullptr);
}
auto isValid = []() { return true; };
// Is relative root, or ends with '..'.
return SdfPath(Sdf_PathNode::FindOrCreatePrim(
primNode, SdfPathTokens->parentPathElement,
isValid),
Sdf_PathPropNodeHandle());
}
SdfPath
SdfPath::GetPrimPath() const {
Sdf_PathNode const *primNode = _primPart.get();
// Walk up looking for a prim node.
while (primNode && primNode->GetNodeType() != Sdf_PathNode::PrimNode) {
primNode = primNode->GetParentNode();
}
return SdfPath(primNode, nullptr);
}
SdfPath
SdfPath::GetPrimOrPrimVariantSelectionPath() const
{
Sdf_PathNode const *primNode = _primPart.get();
// Walk up looking for a prim or prim variant selection node.
while (primNode &&
(primNode->GetNodeType() != Sdf_PathNode::PrimNode &&
primNode->GetNodeType() != Sdf_PathNode::PrimVariantSelectionNode)){
primNode = primNode->GetParentNode();
}
return SdfPath(primNode, nullptr);
}
SdfPath
SdfPath::GetAbsoluteRootOrPrimPath() const {
return (*this == AbsoluteRootPath()) ? *this : GetPrimPath();
}
static inline SdfPath
_AppendNode(const SdfPath &path, Sdf_PathNode const *node) {
switch (node->GetNodeType()) {
case Sdf_PathNode::PrimNode:
return path.AppendChild(node->GetName());
case Sdf_PathNode::PrimPropertyNode:
return path.AppendProperty(node->GetName());
case Sdf_PathNode::PrimVariantSelectionNode:
{
const Sdf_PathNode::VariantSelectionType& selection =
node->GetVariantSelection();
return path.AppendVariantSelection(selection.first.GetString(),
selection.second.GetString());
}
case Sdf_PathNode::TargetNode:
return path.AppendTarget( node->GetTargetPath());
case Sdf_PathNode::RelationalAttributeNode:
return path.AppendRelationalAttribute(node->GetName());
case Sdf_PathNode::MapperNode:
return path.AppendMapper(node->GetTargetPath());
case Sdf_PathNode::MapperArgNode:
return path.AppendMapperArg(node->GetName());
case Sdf_PathNode::ExpressionNode:
return path.AppendExpression();
default:
// CODE_COVERAGE_OFF
// Should never get here. All reasonable cases are
// handled above.
TF_CODING_ERROR("Unexpected node type %i", node->GetNodeType());
return SdfPath::EmptyPath();
// CODE_COVERAGE_ON
}
}
SdfPath
SdfPath::StripAllVariantSelections() const {
if (!ContainsPrimVariantSelection())
return *this;
TRACE_FUNCTION();
std::vector<Sdf_PathNode const *> primNodes;
Sdf_PathNode const *curNode = _primPart.get();
while (curNode) {
if (curNode->GetNodeType() != Sdf_PathNode::PrimVariantSelectionNode)
primNodes.push_back(curNode);
curNode = curNode->GetParentNode();
}
SdfPath stripPath(*(primNodes.rbegin()), nullptr);
// Step through all primNodes except the last (which is the root node):
for (auto it = ++(primNodes.rbegin()); it != primNodes.rend(); ++it) {
stripPath = _AppendNode(stripPath, *it);
}
// Tack on any property portion.
stripPath._propPart = _propPart;
return stripPath;
}
SdfPath
SdfPath::AppendPath(const SdfPath &newSuffix) const {
if (*this == EmptyPath()) {
TF_CODING_ERROR("Cannot append to invalid path");
return EmptyPath();
}
if (newSuffix == EmptyPath()) {
TF_CODING_ERROR("Cannot append invalid path to <%s>",
GetAsString().c_str());
return EmptyPath();
}
if (newSuffix.IsAbsolutePath()) {
TF_WARN("Cannot append absolute path <%s> to another path <%s>.",
newSuffix.GetAsString().c_str(), GetAsString().c_str());
return EmptyPath();
}
if (newSuffix == ReflexiveRelativePath()) {
return *this;
}
Sdf_PathNode::NodeType primNodeType = _primPart->GetNodeType();
if (_propPart || (primNodeType != Sdf_PathNode::RootNode &&
primNodeType != Sdf_PathNode::PrimNode &&
primNodeType != Sdf_PathNode::PrimVariantSelectionNode)) {
TF_WARN("Cannot append a path to another path that is not "
"a root or a prim path.");
return EmptyPath();
}
// This list winds up in reverse order to what one might at first expect.
vector<Sdf_PathNode const *> tailNodes;
// Walk up to top of newSuffix.
Sdf_PathNode const *curNode = newSuffix._propPart.get();
while (curNode) {
tailNodes.push_back(curNode);
curNode = curNode->GetParentNode();
}
curNode = newSuffix._primPart.get();
while (curNode != Sdf_PathNode::GetRelativeRootNode()) {
tailNodes.push_back(curNode);
curNode = curNode->GetParentNode();
}
if ((tailNodes.back()->GetNodeType() == Sdf_PathNode::PrimPropertyNode) &&
*this == AbsoluteRootPath()) {
TF_WARN("Cannot append a property path to the absolute root path.");
return EmptyPath();
}
SdfPath result = *this;
// We have a list of new nodes (in reverse order) to append to our node.
for (auto it = tailNodes.rbegin();
(it != tailNodes.rend()) && (result != EmptyPath()); ++it) {
result = _AppendNode(result, *it);
}
return result;
}
// Use a simple per-thread cache for appending children to prim paths. This
// lets us avoid hitting the global table, reducing thread contention, for
// appending children repeatedly to a node.
namespace {
struct _PerThreadPrimPathCache
{
static constexpr unsigned Shift = 14;
static constexpr unsigned Size = 1 << Shift;
static constexpr unsigned ProbeShift = 1;
static constexpr unsigned Probes = 1 << ProbeShift;
struct _Entry {
Sdf_PathPrimNodeHandle parent;
Sdf_PathPrimNodeHandle primPart;
TfToken childName;
};
inline Sdf_PathPrimNodeHandle
Find(Sdf_PathPrimNodeHandle const &parent, TfToken const &childName,
int *outIndex) const {
// Hash and shift to find table index.
size_t h = childName.Hash();
uint32_t parentAsInt;
memcpy(&parentAsInt, &parent, sizeof(uint32_t));
boost::hash_combine(h, parentAsInt >> 8);
unsigned index = (h & (Size-1));
for (unsigned probe = 0; probe != Probes; ++probe) {
_Entry const &e = cache[(index + probe) & (Size - 1)];
if (e.parent == parent && e.childName == childName) {
// Cache hit.
return e.primPart;
}
if (!e.parent)
break;
}
// Not found -- arrange to replace original hash index.
*outIndex = index;
return Sdf_PathPrimNodeHandle();
}
inline void
Store(Sdf_PathPrimNodeHandle const &parent, TfToken const &childName,
Sdf_PathPrimNodeHandle primPart, int index) {
cache[index] = { parent, primPart, childName };
}
_Entry cache[Size];
};
}
namespace {
// XXX: Workaround for Windows issue USD-5306 -- this avoids destroying the
// per-thread caches to deal with static destruction order problems.
template <class T>
struct _FastThreadLocalBase
{
static T &Get() {
static thread_local T *theTPtr = nullptr;
if (ARCH_LIKELY(theTPtr)) {
return *theTPtr;
}
static thread_local
typename std::aligned_storage<sizeof(T)>::type storage;
void *addr = &storage;
T *p = new (addr) T;
theTPtr = p;
return *p;
}
};
}
using _PrimPathCache = _FastThreadLocalBase<_PerThreadPrimPathCache>;
static _PrimPathCache _primPathCache;
SdfPath
SdfPath::AppendChild(TfToken const &childName) const {
if (ARCH_UNLIKELY(_propPart)) {
TF_WARN("Cannot append child '%s' to path '%s'.",
childName.GetText(), GetText());
return EmptyPath();
}
auto &cache = _primPathCache.Get();
int storeIndex = 0;
SdfPath ret { cache.Find(_primPart, childName, &storeIndex), {} };
if (ret._primPart) {
return ret;
}
_DeferredDiagnostics dd;
auto isValid = [this, &childName, &dd]() {
if (!IsAbsoluteRootOrPrimPath()
&& !IsPrimVariantSelectionPath()
&& (*this != ReflexiveRelativePath())) {
dd.Warn("Cannot append child '%s' to path '%s'.",
childName.GetText(), GetText());
return false;
}
if (ARCH_UNLIKELY(childName == SdfPathTokens->parentPathElement)) {
return false;
}
else if (ARCH_UNLIKELY(!_IsValidIdentifier(childName))) {
dd.Warn("Invalid prim name '%s'", childName.GetText());
return false;
}
return true;
};
Sdf_PathPrimNodeHandle childNode =
Sdf_PathNode::FindOrCreatePrim(_primPart.get(), childName, isValid);
if (ARCH_UNLIKELY(!childNode)) {
if (childName == SdfPathTokens->parentPathElement) {
return GetParentPath();
}
}
return { std::move(childNode), {} };
}
// Use a simple per-thread cache for appending prim properties. This lets us
// avoid hitting the global table, reducing thread contention and increasing
// speed. We don't do this for the other property-type paths, like target paths
// or relational attribute paths because those operations are done much less
// frequently than appending properties to prim paths.
namespace {
struct _PerThreadPropertyPathCache
{
static constexpr unsigned Shift = 10;
static constexpr unsigned Size = 1 << Shift;
static constexpr unsigned ProbeShift = 1;
static constexpr unsigned Probes = 1 << ProbeShift;
struct _Entry {
TfToken propName;
Sdf_PathPropNodeHandle propPart;
};
inline Sdf_PathPropNodeHandle
Find(TfToken const &propName, int *outIndex) const {
// Hash and shift to find table index.
size_t h = propName.Hash();
unsigned index = (h >> (8*sizeof(h) - Shift));
for (unsigned probe = 0; probe != Probes; ++probe) {
_Entry const &e = cache[(index + probe) & (Size - 1)];
if (e.propName == propName) {
// Cache hit.
return e.propPart;
}
if (e.propName.IsEmpty())
break;
}
// Not found -- arrange to replace original hash index.
*outIndex = index;
return Sdf_PathPropNodeHandle();
}
inline void
Store(TfToken const &propName, Sdf_PathPropNodeHandle propPart, int index) {
cache[index] = { propName, propPart };
}
_Entry cache[Size];
};
}
using _PropPathCache = Sdf_FastThreadLocalBase<_PerThreadPropertyPathCache>;
static _PropPathCache _propPathCache;
SdfPath
SdfPath::AppendProperty(TfToken const &propName) const {
SdfPath ret;
if (ARCH_UNLIKELY(_propPart)) {
TF_WARN("Can only append a property '%s' to a prim path (%s)",
propName.GetText(), GetText());
return ret;
}
_DeferredDiagnostics dd;
auto isValid = [this, &propName, &dd]() {
if (!IsValidNamespacedIdentifier(propName.GetString())) {
//TF_WARN("Invalid property name.");
return false;
}
if (!IsPrimVariantSelectionPath() &&
!IsPrimPath() && (*this != ReflexiveRelativePath())) {
dd.Warn("Can only append a property '%s' to a prim path (%s)",
propName.GetText(), GetText());
return false;
}
return true;
};
auto &cache = _propPathCache.Get();
int storeIndex = 0;
Sdf_PathPropNodeHandle propPart = cache.Find(propName, &storeIndex);
if (!propPart) {
propPart = Sdf_PathNode::FindOrCreatePrimProperty(
_primPart.get(), propName, isValid);
if (propPart) {
cache.Store(propName, propPart, storeIndex);
}
}
if (propPart) {
ret._primPart = _primPart;
ret._propPart = std::move(propPart);
}
return ret;
}
SdfPath
SdfPath::AppendVariantSelection(const string &variantSet,
const string &variant) const
{
_DeferredDiagnostics dd;
auto isValid = [this, &variantSet, &variant, &dd]() {
if (!IsPrimOrPrimVariantSelectionPath()) {
dd.CodingError("Cannot append variant selection %s = %s to <%s>; "
"can only append a variant selection to a prim or "
"prim variant selection path.",
variantSet.c_str(), variant.c_str(),
GetText());
return false;
}
return true;
};
return SdfPath(Sdf_PathNode::FindOrCreatePrimVariantSelection(
_primPart.get(),
TfToken(variantSet),
TfToken(variant),
isValid));
}
SdfPath
SdfPath::AppendTarget(const SdfPath &targetPath) const {
_DeferredDiagnostics dd;
auto isValid = [this, &targetPath, &dd]() {
if (!IsPropertyPath()) {
dd.Warn("Can only append a target to a property path.");
return false;
}
if (targetPath == EmptyPath()) {
dd.Warn("Target path cannot be invalid.");
return false;
}
return true;
};
if (Sdf_PathPropNodeHandle tgtNode = Sdf_PathNode::FindOrCreateTarget(
_propPart.get(), targetPath, isValid)) {
return SdfPath(_primPart, tgtNode);