primDataHandle.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_USD_PRIM_DATA_HANDLE_H
25 #define PXR_USD_USD_PRIM_DATA_HANDLE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/api.h"
29 #include <boost/functional/hash.hpp>
30 #include <boost/intrusive_ptr.hpp>
31 
32 PXR_NAMESPACE_OPEN_SCOPE
33 
34 class SdfPath;
35 
36 // To start we always validate.
37 #define USD_CHECK_ALL_PRIM_ACCESSES
38 
39 // Forward declare boost::intrusive_ptr requirements. Defined in primData.h.
40 void intrusive_ptr_add_ref(const class Usd_PrimData *prim);
41 void intrusive_ptr_release(const class Usd_PrimData *prim);
42 
43 // Forward declarations for Usd_PrimDataHandle's use. Defined in primData.h.
44 USD_API
45 void Usd_ThrowExpiredPrimAccessError(Usd_PrimData const *p);
46 bool Usd_IsDead(Usd_PrimData const *p);
47 
48 // convenience typedefs for raw ptrs.
49 typedef Usd_PrimData *Usd_PrimDataPtr;
50 typedef const Usd_PrimData *Usd_PrimDataConstPtr;
51 
52 // convenience typedefs for intrusive_ptr.
53 typedef boost::intrusive_ptr<Usd_PrimData> Usd_PrimDataIPtr;
54 typedef boost::intrusive_ptr<const Usd_PrimData> Usd_PrimDataConstIPtr;
55 
56 // Private helper class that holds a reference to prim data. UsdObject (and by
57 // inheritance its subclasses) hold an instance of this class. It lets
58 // UsdObject detect prim expiry, and provides access to cached prim data.
59 class Usd_PrimDataHandle
60 {
61 public:
62  // smart ptr element_type typedef.
63  typedef Usd_PrimDataConstIPtr::element_type element_type;
64 
65  // Construct a null handle.
66  Usd_PrimDataHandle() {}
67  // Convert/construct a handle from a prim data intrusive ptr.
68  Usd_PrimDataHandle(const Usd_PrimDataIPtr &primData)
69  : _p(primData) {}
70  // Convert/construct a handle from a prim data intrusive ptr.
71  Usd_PrimDataHandle(const Usd_PrimDataConstIPtr &primData)
72  : _p(primData) {}
73  // Convert/construct a handle from a prim data raw ptr.
74  Usd_PrimDataHandle(Usd_PrimDataPtr primData)
75  : _p(Usd_PrimDataConstIPtr(primData)) {}
76  // Convert/construct a handle from a prim data raw ptr.
77  Usd_PrimDataHandle(Usd_PrimDataConstPtr primData)
78  : _p(Usd_PrimDataConstIPtr(primData)) {}
79 
80  // Reset this handle to null.
81  void reset() { _p.reset(); }
82 
83  // Swap this handle with \p other.
84  void swap(Usd_PrimDataHandle &other) { _p.swap(other._p); }
85 
86  // Dereference this handle. If USD_CHECK_ALL_PRIM_ACCESSES is defined, this
87  // will issue a fatal error if the handle is invalid.
88  element_type *operator->() const {
89  element_type *p = _p.get();
90 #ifdef USD_CHECK_ALL_PRIM_ACCESSES
91  if (!p || Usd_IsDead(p)) {
92  Usd_ThrowExpiredPrimAccessError(p);
93  }
94 #endif
95  return p;
96  }
97 
98  // Explicit bool conversion operator. Returns \c true if this handle points
99  // to a valid prim instance that is not marked dead, \c false otherwise.
100  explicit operator bool() const {
101  element_type *p = _p.get();
102  return p && !Usd_IsDead(p);
103  }
104 
105  // Return a text description of this prim data, used primarily for
106  // diagnostic purposes.
107  std::string GetDescription(SdfPath const &proxyPrimPath) const;
108 
109 private:
110  // Equality comparison.
111  friend bool operator==(const Usd_PrimDataHandle &lhs,
112  const Usd_PrimDataHandle &rhs) {
113  return lhs._p == rhs._p;
114  }
115 
116  // Inequality comparison.
117  friend bool operator!=(const Usd_PrimDataHandle &lhs,
118  const Usd_PrimDataHandle &rhs) {
119  return !(lhs == rhs);
120  }
121 
122  // Swap \p lhs and \p rhs.
123  friend void swap(Usd_PrimDataHandle &lhs, Usd_PrimDataHandle &rhs) {
124  lhs.swap(rhs);
125  }
126 
127  // Provide hash_value.
128  friend size_t hash_value(const Usd_PrimDataHandle &h) {
129  return boost::hash_value(h._p.get());
130  }
131 
132  friend element_type *get_pointer(const Usd_PrimDataHandle &h) {
133  return h._p.get();
134  }
135 
136  Usd_PrimDataConstIPtr _p;
137 };
138 
139 
140 PXR_NAMESPACE_CLOSE_SCOPE
141 
142 #endif // PXR_USD_USD_PRIM_DATA_HANDLE_H
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
std::enable_if< std::is_same< Half, half >::value, size_t >::type hash_value(const Half &h)
Overload hash_value for half.
Definition: half.h:50
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.