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