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 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
36 class Sdf_IdentityRegistry;
37 class Sdf_IdRegistryImpl;
38 
39 SDF_DECLARE_HANDLES(SdfLayer);
40 
47 class Sdf_Identity {
48  Sdf_Identity(Sdf_Identity const &) = delete;
49  Sdf_Identity &operator=(Sdf_Identity const &) = delete;
50 public:
52  SDF_API
53  const SdfLayerHandle &GetLayer() const;
54 
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 
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.
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 
97 class Sdf_IdentityRegistry : public boost::noncopyable {
98 public:
99  Sdf_IdentityRegistry(const SdfLayerHandle &layer);
100  ~Sdf_IdentityRegistry();
101 
103  const SdfLayerHandle &GetLayer() const {
104  return _layer;
105  }
106 
111  Sdf_IdentityRefPtr Identify(const SdfPath &path);
112 
114  void MoveIdentity(const SdfPath &oldPath, const SdfPath &newPath);
115 
116 private:
117  friend class Sdf_Identity;
118 
119  friend class Sdf_IdRegistryImpl;
120 
121  // Remove the identity mapping for \a path to \a id from the registry. This
122  // is invoked when an identity's refcount hits zero.
123  SDF_API
124  void _UnregisterOrDelete();
125 
128  const SdfLayerHandle _layer;
129 
130  // Private implementation.
131  const std::unique_ptr<Sdf_IdRegistryImpl> _impl;
132 };
133 
134 PXR_NAMESPACE_CLOSE_SCOPE
135 
136 #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:94
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290