All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
primDataHandle.h
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"
12#include "pxr/base/tf/delegatedCountPtr.h"
13#include "pxr/base/tf/hash.h"
14
15PXR_NAMESPACE_OPEN_SCOPE
16
17class 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.
23void TfDelegatedCountIncrement(const class Usd_PrimData *prim) noexcept;
24void TfDelegatedCountDecrement(const class Usd_PrimData *prim) noexcept;
25
26// Forward declarations for Usd_PrimDataHandle's use. Defined in primData.h.
27USD_API
28void Usd_ThrowExpiredPrimAccessError(Usd_PrimData const *p);
29bool Usd_IsDead(Usd_PrimData const *p);
30
31// convenience typedefs for raw ptrs.
32typedef Usd_PrimData *Usd_PrimDataPtr;
33typedef const Usd_PrimData *Usd_PrimDataConstPtr;
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.
42class Usd_PrimDataHandle
43{
44public:
45 // smart ptr element_type typedef.
46 typedef Usd_PrimDataConstIPtr::element_type element_type;
47
48 // Construct a null handle.
49 Usd_PrimDataHandle() {}
50 // Convert/construct a handle from a prim data delegated count ptr.
51 Usd_PrimDataHandle(const Usd_PrimDataIPtr &primData)
52 : _p(primData) {}
53 // Convert/construct a handle from a prim data delegated count ptr.
54 Usd_PrimDataHandle(const Usd_PrimDataConstIPtr &primData)
55 : _p(primData) {}
56 // Convert/construct a handle from a prim data raw ptr.
57 Usd_PrimDataHandle(Usd_PrimDataPtr primData)
58 : _p(TfDelegatedCountIncrementTag, primData) {}
59 // Convert/construct a handle from a prim data raw ptr.
60 Usd_PrimDataHandle(Usd_PrimDataConstPtr primData)
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.
71 element_type *operator->() const {
72 element_type *p = _p.get();
73#ifdef USD_CHECK_ALL_PRIM_ACCESSES
74 if (!p || Usd_IsDead(p)) {
75 Usd_ThrowExpiredPrimAccessError(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
92private:
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
115 friend element_type *get_pointer(const Usd_PrimDataHandle &h) {
116 return h._p.get();
117 }
118
120};
121
122
123PXR_NAMESPACE_CLOSE_SCOPE
124
125#endif // PXR_USD_USD_PRIM_DATA_HANDLE_H
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
Stores a pointer to a ValueType which uses TfDelegatedCountIncrement and TfDelegatedCountDecrement to...
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:460
size_t hash_value(const half h)
Overload hash_value for half.
Definition: half.h:28