All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 SDF_IDENTITY_H
25 #define SDF_IDENTITY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/tf/hashmap.h"
29 #include "pxr/usd/sdf/api.h"
30 #include "pxr/usd/sdf/declareHandles.h"
31 #include "pxr/usd/sdf/path.h"
32 
33 #include <boost/intrusive_ptr.hpp>
34 #include <boost/noncopyable.hpp>
35 #include <tbb/spin_mutex.h>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 class Sdf_IdentityRegistry;
40 SDF_DECLARE_HANDLES(SdfLayer);
41 
48 class Sdf_Identity : public boost::noncopyable {
49 public:
51  SDF_API
52  const SdfLayerHandle &GetLayer() const;
53 
55  const SdfPath &GetPath() const {
56  return _path;
57  }
58 
59 private:
60  // Ref-counting ops manage _refCount.
61  friend void intrusive_ptr_add_ref(Sdf_Identity*);
62  friend void intrusive_ptr_release(Sdf_Identity*);
63 
64  friend class Sdf_IdentityRegistry;
65 
66  SDF_API
67  Sdf_Identity(Sdf_IdentityRegistry *registry, const SdfPath &path);
68  SDF_API
69  ~Sdf_Identity();
70 
71  void _Forget();
72 
73  mutable std::atomic_int _refCount;
74  Sdf_IdentityRegistry *_registry;
75  SdfPath _path;
76 };
77 
78 // Specialize boost::intrusive_ptr operations.
79 inline void intrusive_ptr_add_ref(PXR_NS::Sdf_Identity* p) {
80  ++p->_refCount;
81 }
82 inline void intrusive_ptr_release(PXR_NS::Sdf_Identity* p) {
83  if (--p->_refCount == 0) {
84  delete p;
85  }
86 }
87 
88 class Sdf_IdentityRegistry : public boost::noncopyable {
89 public:
90  Sdf_IdentityRegistry(const SdfLayerHandle &layer);
91  ~Sdf_IdentityRegistry();
92 
94  const SdfLayerHandle &GetLayer() const {
95  return _layer;
96  }
97 
102  Sdf_IdentityRefPtr Identify(const SdfPath &path);
103 
105  void MoveIdentity(const SdfPath &oldPath, const SdfPath &newPath);
106 
107 private:
108  // When an identity expires, it will remove itself from the registry.
109  friend class Sdf_Identity;
110 
111  // Remove the identity mapping for \a path to \a id from the registry.
112  // This is only called by Sdf_Identity's destructor.
113  void _Remove(const SdfPath &path, Sdf_Identity *id);
114 
117  const SdfLayerHandle _layer;
118 
120  typedef TfHashMap<SdfPath, Sdf_Identity*, SdfPath::Hash> _IdMap;
121  _IdMap _ids;
122 
124  Sdf_IdentityRefPtr _lastId;
125 
126  // This mutex synchronizes access to _ids.
127  tbb::spin_mutex _idsMutex;
128 };
129 
130 PXR_NAMESPACE_CLOSE_SCOPE
131 
132 #endif // SDF_IDENTITY_H
A unit of scene description that you combine with other units of scene description to form a shot...
Definition: layer.h:93
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287