This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
identity.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_SDF_IDENTITY_H
8#define PXR_USD_SDF_IDENTITY_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/sdf/api.h"
13#include "pxr/usd/sdf/path.h"
14
15#include <memory>
16
17PXR_NAMESPACE_OPEN_SCOPE
18
19class Sdf_IdentityRegistry;
20class Sdf_IdRegistryImpl;
21
22SDF_DECLARE_HANDLES(SdfLayer);
23
30class Sdf_Identity {
31 Sdf_Identity(Sdf_Identity const &) = delete;
32 Sdf_Identity &operator=(Sdf_Identity const &) = delete;
33public:
35 SDF_API
36 const SdfLayerHandle &GetLayer() const;
37
39 const SdfPath &GetPath() const {
40 return _path;
41 }
42
43private:
44 // Ref-counting ops manage _refCount.
45 friend void TfDelegatedCountIncrement(Sdf_Identity*);
46 friend void TfDelegatedCountDecrement(Sdf_Identity*) noexcept;
47
48 friend class Sdf_IdentityRegistry;
49 friend class Sdf_IdRegistryImpl;
50
51 Sdf_Identity(Sdf_IdRegistryImpl *regImpl, const SdfPath &path)
52 : _refCount(0), _path(path), _regImpl(regImpl) {}
53
54 SDF_API
55 static void _UnregisterOrDelete(Sdf_IdRegistryImpl *reg, Sdf_Identity *id)
56 noexcept;
57 void _Forget();
58
59 mutable std::atomic_int _refCount;
60 SdfPath _path;
61 Sdf_IdRegistryImpl *_regImpl;
62};
63
64// Specialize TfDelegatedCountPtr operations.
65inline void TfDelegatedCountIncrement(PXR_NS::Sdf_Identity* p) {
66 ++p->_refCount;
67}
68inline void TfDelegatedCountDecrement(PXR_NS::Sdf_Identity* p) noexcept {
69 // Once the count hits zero, p is liable to be destroyed at any point,
70 // concurrently, by its owning registry if it happens to be doing a cleanup
71 // pass. Cache 'this' and the impl ptr in local variables so we have them
72 // before decrementing the count.
73 Sdf_Identity *self = p;
74 Sdf_IdRegistryImpl *reg = p->_regImpl;
75 if (--p->_refCount == 0) {
76 // Cannot use 'p' anymore here.
77 Sdf_Identity::_UnregisterOrDelete(reg, self);
78 }
79}
80
81class Sdf_IdentityRegistry {
82 Sdf_IdentityRegistry(const Sdf_IdentityRegistry&) = delete;
83 Sdf_IdentityRegistry& operator=(const Sdf_IdentityRegistry&) = delete;
84public:
85 Sdf_IdentityRegistry(const SdfLayerHandle &layer);
86 ~Sdf_IdentityRegistry();
87
89 const SdfLayerHandle &GetLayer() const {
90 return _layer;
91 }
92
97 Sdf_IdentityRefPtr Identify(const SdfPath &path);
98
100 void MoveIdentity(const SdfPath &oldPath, const SdfPath &newPath);
101
102private:
103 friend class Sdf_Identity;
104
105 friend class Sdf_IdRegistryImpl;
106
107 // Remove the identity mapping for \a path to \a id from the registry. This
108 // is invoked when an identity's refcount hits zero.
109 SDF_API
110 void _UnregisterOrDelete();
111
114 const SdfLayerHandle _layer;
115
116 // Private implementation.
117 const std::unique_ptr<Sdf_IdRegistryImpl> _impl;
118};
119
120PXR_NAMESPACE_CLOSE_SCOPE
121
122#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:84
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274