HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
identity.h
Go to the documentation of this file.
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 
35 
37 class Sdf_IdRegistryImpl;
38 
40 
41 /// \class Sdf_Identity
42 ///
43 /// Identifies the logical object behind an SdfSpec.
44 ///
45 /// This is simply the layer the spec belongs to and the path to the spec.
46 ///
47 class Sdf_Identity {
48  Sdf_Identity(Sdf_Identity const &) = delete;
49  Sdf_Identity &operator=(Sdf_Identity const &) = delete;
50 public:
51  /// Returns the layer that this identity refers to.
52  SDF_API
53  const SdfLayerHandle &GetLayer() const;
54 
55  /// Returns the path that this identity refers to.
56  const SdfPath &GetPath() const {
57  return _path;
58  }
59 
60 private:
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 
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 hboost::intrusive_ptr operations.
81 inline void intrusive_ptr_add_ref(PXR_NS::Sdf_Identity* p) {
82  ++p->_refCount;
83 }
84 inline 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 
99  Sdf_IdentityRegistry& operator=(const Sdf_IdentityRegistry&) = delete;
100 public:
101  Sdf_IdentityRegistry(const SdfLayerHandle &layer);
103 
104  /// Returns the layer that owns this registry.
105  const SdfLayerHandle &GetLayer() const {
106  return _layer;
107  }
108 
109  /// Return the identity associated with \a path, issuing a new
110  /// one if necessary. The registry will track the identity
111  /// and update it if the logical object it represents moves
112  /// in namespace.
114 
115  /// Update identity in response to a namespace edit.
116  void MoveIdentity(const SdfPath &oldPath, const SdfPath &newPath);
117 
118 private:
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 
128  /// The layer that owns this registry, and on behalf of which
129  /// this registry tracks identities.
130  const SdfLayerHandle _layer;
131 
132  // Private implementation.
133  const std::unique_ptr<Sdf_IdRegistryImpl> _impl;
134 };
135 
137 
138 #endif // PXR_USD_SDF_IDENTITY_H
Definition: layer.h:97
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
void intrusive_ptr_release(PXR_NS::Sdf_Identity *p)
Definition: identity.h:84
hboost::intrusive_ptr< Sdf_Identity > Sdf_IdentityRefPtr
friend void intrusive_ptr_release(Sdf_Identity *)
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
atomic< int > atomic_int
Definition: atomic.h:25
Sdf_IdentityRefPtr Identify(const SdfPath &path)
SDF_API const SdfLayerHandle & GetLayer() const
Returns the layer that this identity refers to.
void MoveIdentity(const SdfPath &oldPath, const SdfPath &newPath)
Update identity in response to a namespace edit.
void intrusive_ptr_add_ref(PXR_NS::Sdf_Identity *p)
Definition: identity.h:81
const SdfLayerHandle & GetLayer() const
Returns the layer that owns this registry.
Definition: identity.h:105
Definition: path.h:290
friend class Sdf_IdRegistryImpl
Definition: identity.h:121
#define SDF_API
Definition: api.h:40
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
SDF_DECLARE_HANDLES(SdfLayer)
friend void intrusive_ptr_add_ref(Sdf_Identity *)
const SdfPath & GetPath() const
Returns the path that this identity refers to.
Definition: identity.h:56
friend class Sdf_IdRegistryImpl
Definition: identity.h:66