Loading...
Searching...
No Matches
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 "pxr/base/tf/delegatedCountPtr.h"
30#include "pxr/base/tf/hash.h"
31
32PXR_NAMESPACE_OPEN_SCOPE
33
34class SdfPath;
35
36// To start we always validate.
37#define USD_CHECK_ALL_PRIM_ACCESSES
38
39// Forward declare TfDelegatedCountPtr requirements. Defined in primData.h.
40void TfDelegatedCountIncrement(const class Usd_PrimData *prim) noexcept;
41void TfDelegatedCountDecrement(const class Usd_PrimData *prim) noexcept;
42
43// Forward declarations for Usd_PrimDataHandle's use. Defined in primData.h.
44USD_API
45void Usd_ThrowExpiredPrimAccessError(Usd_PrimData const *p);
46bool Usd_IsDead(Usd_PrimData const *p);
47
48// convenience typedefs for raw ptrs.
49typedef Usd_PrimData *Usd_PrimDataPtr;
50typedef const Usd_PrimData *Usd_PrimDataConstPtr;
51
52// convenience typedefs for TfDelegatedCountPtr.
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.
59class Usd_PrimDataHandle
60{
61public:
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 delegated count ptr.
68 Usd_PrimDataHandle(const Usd_PrimDataIPtr &primData)
69 : _p(primData) {}
70 // Convert/construct a handle from a prim data delegated count 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(TfDelegatedCountIncrementTag, primData) {}
76 // Convert/construct a handle from a prim data raw ptr.
77 Usd_PrimDataHandle(Usd_PrimDataConstPtr primData)
78 : _p(TfDelegatedCountIncrementTag, 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
109private:
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 TfHash()(h._p.get());
130 }
131
132 friend element_type *get_pointer(const Usd_PrimDataHandle &h) {
133 return h._p.get();
134 }
135
137};
138
139
140PXR_NAMESPACE_CLOSE_SCOPE
141
142#endif // PXR_USD_USD_PRIM_DATA_HANDLE_H
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
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:477
size_t hash_value(const half h)
Overload hash_value for half.
Definition: half.h:45