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 intrusive_ptr_add_ref(Sdf_Identity*);
63 friend void intrusive_ptr_release(Sdf_Identity*);
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 void _Forget();
74
75 mutable std::atomic_int _refCount;
76 SdfPath _path;
77 Sdf_IdRegistryImpl *_regImpl;
78};
79
80// Specialize boost::intrusive_ptr operations.
81inline void intrusive_ptr_add_ref(PXR_NS::Sdf_Identity* p) {
82 ++p->_refCount;
83}
84inline void intrusive_ptr_release(PXR_NS::Sdf_Identity* p) {
85 // Once the count hits zero, p is liable to be destroyed at any point,
86 // concurrently, by its owning registry if it happens to be doing a cleanup
87 // pass. Cache 'this' and the impl ptr in local variables so we have them
88 // before decrementing the count.
89 Sdf_Identity *self = p;
90 Sdf_IdRegistryImpl *reg = p->_regImpl;
91 if (--p->_refCount == 0) {
92 // Cannot use 'p' anymore here.
93 Sdf_Identity::_UnregisterOrDelete(reg, self);
94 }
95}
96
97class Sdf_IdentityRegistry {
98 Sdf_IdentityRegistry(const Sdf_IdentityRegistry&) = delete;
99 Sdf_IdentityRegistry& operator=(const Sdf_IdentityRegistry&) = delete;
100public:
101 Sdf_IdentityRegistry(const SdfLayerHandle &layer);
102 ~Sdf_IdentityRegistry();
103
105 const SdfLayerHandle &GetLayer() const {
106 return _layer;
107 }
108
113 Sdf_IdentityRefPtr Identify(const SdfPath &path);
114
116 void MoveIdentity(const SdfPath &oldPath, const SdfPath &newPath);
117
118private:
119 friend class Sdf_Identity;
120
121 friend class Sdf_IdRegistryImpl;
122
123 // Remove the identity mapping for \a path to \a id from the registry. This
124 // is invoked when an identity's refcount hits zero.
125 SDF_API
126 void _UnregisterOrDelete();
127
130 const SdfLayerHandle _layer;
131
132 // Private implementation.
133 const std::unique_ptr<Sdf_IdRegistryImpl> _impl;
134};
135
136PXR_NAMESPACE_CLOSE_SCOPE
137
138#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:291