Loading...
Searching...
No Matches
identity.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_USD_SDF_IDENTITY_H
25#define PXR_USD_SDF_IDENTITY_H
26
27#include "pxr/pxr.h"
28#include "pxr/usd/sdf/api.h"
30#include "pxr/usd/sdf/path.h"
31
32#include <memory>
33
34PXR_NAMESPACE_OPEN_SCOPE
35
36class Sdf_IdentityRegistry;
37class Sdf_IdRegistryImpl;
38
39SDF_DECLARE_HANDLES(SdfLayer);
40
47class Sdf_Identity {
48 Sdf_Identity(Sdf_Identity const &) = delete;
49 Sdf_Identity &operator=(Sdf_Identity const &) = delete;
50public:
52 SDF_API
53 const SdfLayerHandle &GetLayer() const;
54
56 const SdfPath &GetPath() const {
57 return _path;
58 }
59
60private:
61 // Ref-counting ops manage _refCount.
62 friend void TfDelegatedCountIncrement(Sdf_Identity*);
63 friend void TfDelegatedCountDecrement(Sdf_Identity*) noexcept;
64
65 friend class Sdf_IdentityRegistry;
66 friend class Sdf_IdRegistryImpl;
67
68 Sdf_Identity(Sdf_IdRegistryImpl *regImpl, const SdfPath &path)
69 : _refCount(0), _path(path), _regImpl(regImpl) {}
70
71 SDF_API
72 static void _UnregisterOrDelete(Sdf_IdRegistryImpl *reg, Sdf_Identity *id)
73 noexcept;
74 void _Forget();
75
76 mutable std::atomic_int _refCount;
77 SdfPath _path;
78 Sdf_IdRegistryImpl *_regImpl;
79};
80
81// Specialize TfDelegatedCountPtr operations.
82inline void TfDelegatedCountIncrement(PXR_NS::Sdf_Identity* p) {
83 ++p->_refCount;
84}
85inline void TfDelegatedCountDecrement(PXR_NS::Sdf_Identity* p) noexcept {
86 // Once the count hits zero, p is liable to be destroyed at any point,
87 // concurrently, by its owning registry if it happens to be doing a cleanup
88 // pass. Cache 'this' and the impl ptr in local variables so we have them
89 // before decrementing the count.
90 Sdf_Identity *self = p;
91 Sdf_IdRegistryImpl *reg = p->_regImpl;
92 if (--p->_refCount == 0) {
93 // Cannot use 'p' anymore here.
94 Sdf_Identity::_UnregisterOrDelete(reg, self);
95 }
96}
97
98class Sdf_IdentityRegistry {
99 Sdf_IdentityRegistry(const Sdf_IdentityRegistry&) = delete;
100 Sdf_IdentityRegistry& operator=(const Sdf_IdentityRegistry&) = delete;
101public:
102 Sdf_IdentityRegistry(const SdfLayerHandle &layer);
103 ~Sdf_IdentityRegistry();
104
106 const SdfLayerHandle &GetLayer() const {
107 return _layer;
108 }
109
114 Sdf_IdentityRefPtr Identify(const SdfPath &path);
115
117 void MoveIdentity(const SdfPath &oldPath, const SdfPath &newPath);
118
119private:
120 friend class Sdf_Identity;
121
122 friend class Sdf_IdRegistryImpl;
123
124 // Remove the identity mapping for \a path to \a id from the registry. This
125 // is invoked when an identity's refcount hits zero.
126 SDF_API
127 void _UnregisterOrDelete();
128
131 const SdfLayerHandle _layer;
132
133 // Private implementation.
134 const std::unique_ptr<Sdf_IdRegistryImpl> _impl;
135};
136
137PXR_NAMESPACE_CLOSE_SCOPE
138
139#endif // PXR_USD_SDF_IDENTITY_H
A scene description container that can combine with other such containers to form simple component as...
Definition: layer.h:100
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290