HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primDataHandle.h
Go to the documentation of this file.
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_USD_PRIM_DATA_HANDLE_H
8 #define PXR_USD_USD_PRIM_DATA_HANDLE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usd/api.h"
13 #include "pxr/base/tf/hash.h"
14 
16 
17 class SdfPath;
18 
19 // To start we always validate.
20 #define USD_CHECK_ALL_PRIM_ACCESSES
21 
22 // Forward declare TfDelegatedCountPtr requirements. Defined in primData.h.
23 void TfDelegatedCountIncrement(const class Usd_PrimData *prim) noexcept;
24 void TfDelegatedCountDecrement(const class Usd_PrimData *prim) noexcept;
25 
26 // Forward declarations for Usd_PrimDataHandle's use. Defined in primData.h.
27 USD_API
29 bool Usd_IsDead(Usd_PrimData const *p);
30 
31 // convenience typedefs for raw ptrs.
34 
35 // convenience typedefs for TfDelegatedCountPtr.
38 
39 // Private helper class that holds a reference to prim data. UsdObject (and by
40 // inheritance its subclasses) hold an instance of this class. It lets
41 // UsdObject detect prim expiry, and provides access to cached prim data.
43 {
44 public:
45  // smart ptr element_type typedef.
47 
48  // Construct a null handle.
50  // Convert/construct a handle from a prim data delegated count ptr.
52  : _p(primData) {}
53  // Convert/construct a handle from a prim data delegated count ptr.
55  : _p(primData) {}
56  // Convert/construct a handle from a prim data raw ptr.
58  : _p(TfDelegatedCountIncrementTag, primData) {}
59  // Convert/construct a handle from a prim data raw ptr.
61  : _p(TfDelegatedCountIncrementTag, primData) {}
62 
63  // Reset this handle to null.
64  void reset() { _p.reset(); }
65 
66  // Swap this handle with \p other.
67  void swap(Usd_PrimDataHandle &other) { _p.swap(other._p); }
68 
69  // Dereference this handle. If USD_CHECK_ALL_PRIM_ACCESSES is defined, this
70  // will issue a fatal error if the handle is invalid.
72  element_type *p = _p.get();
73 #ifdef USD_CHECK_ALL_PRIM_ACCESSES
74  if (!p || Usd_IsDead(p)) {
76  }
77 #endif
78  return p;
79  }
80 
81  // Explicit bool conversion operator. Returns \c true if this handle points
82  // to a valid prim instance that is not marked dead, \c false otherwise.
83  explicit operator bool() const {
84  element_type *p = _p.get();
85  return p && !Usd_IsDead(p);
86  }
87 
88  // Return a text description of this prim data, used primarily for
89  // diagnostic purposes.
90  std::string GetDescription(SdfPath const &proxyPrimPath) const;
91 
92 private:
93  // Equality comparison.
94  friend bool operator==(const Usd_PrimDataHandle &lhs,
95  const Usd_PrimDataHandle &rhs) {
96  return lhs._p == rhs._p;
97  }
98 
99  // Inequality comparison.
100  friend bool operator!=(const Usd_PrimDataHandle &lhs,
101  const Usd_PrimDataHandle &rhs) {
102  return !(lhs == rhs);
103  }
104 
105  // Swap \p lhs and \p rhs.
106  friend void swap(Usd_PrimDataHandle &lhs, Usd_PrimDataHandle &rhs) {
107  lhs.swap(rhs);
108  }
109 
110  // Provide hash_value.
111  friend size_t hash_value(const Usd_PrimDataHandle &h) {
112  return TfHash()(h._p.get());
113  }
114 
116  return h._p.get();
117  }
118 
120 };
121 
122 
124 
125 #endif // PXR_USD_USD_PRIM_DATA_HANDLE_H
void TfDelegatedCountDecrement(const class Usd_PrimData *prim) noexcept
#define USD_API
Definition: api.h:23
Usd_PrimDataHandle(const Usd_PrimDataIPtr &primData)
friend element_type * get_pointer(const Usd_PrimDataHandle &h)
friend bool operator==(const Usd_PrimDataHandle &lhs, const Usd_PrimDataHandle &rhs)
Usd_PrimDataHandle(const Usd_PrimDataConstIPtr &primData)
USD_API void Usd_ThrowExpiredPrimAccessError(Usd_PrimData const *p)
Usd_PrimDataHandle(Usd_PrimDataConstPtr primData)
void reset() noexcept(DecrementIsNoExcept())
friend size_t hash_value(const Usd_PrimDataHandle &h)
OutGridT const XformOp bool bool
Definition: hash.h:472
RawPtrType get() const noexcept
Return the underlying pointer.
void swap(Usd_PrimDataHandle &other)
void TfDelegatedCountIncrement(const class Usd_PrimData *prim) noexcept
Usd_PrimDataConstIPtr::element_type element_type
std::string GetDescription(SdfPath const &proxyPrimPath) const
bool Usd_IsDead(Usd_PrimData const *p)
Definition: primData.h:323
Definition: path.h:273
Usd_PrimDataHandle(Usd_PrimDataPtr primData)
Usd_PrimData * Usd_PrimDataPtr
const Usd_PrimData * Usd_PrimDataConstPtr
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
friend bool operator!=(const Usd_PrimDataHandle &lhs, const Usd_PrimDataHandle &rhs)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
friend void swap(Usd_PrimDataHandle &lhs, Usd_PrimDataHandle &rhs)
element_type * operator->() const
PXR_NAMESPACE_OPEN_SCOPE constexpr struct TfDelegatedCountIncrementTagType TfDelegatedCountIncrementTag
void swap(TfDelegatedCountPtr &other) noexcept
Swap this object's held pointer with other's.