All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 USD_PRIMDATAHANDLE_H
25 #define USD_PRIMDATAHANDLE_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_IssueFatalPrimAccessError(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_IssueFatalPrimAccessError(p);
93 #endif
94  return p;
95  }
96 
97  // Explicit bool conversion operator. Returns \c true if this handle points
98  // to a valid prim instance that is not marked dead, \c false otherwise.
99  explicit operator bool() const {
100  element_type *p = _p.get();
101  return p && !Usd_IsDead(p);
102  }
103 
104  // Return a text description of this prim data, used primarily for
105  // diagnostic purposes.
106  std::string GetDescription(SdfPath const &proxyPrimPath) const;
107 
108 private:
109  // Equality comparison.
110  friend bool operator==(const Usd_PrimDataHandle &lhs,
111  const Usd_PrimDataHandle &rhs) {
112  return lhs._p == rhs._p;
113  }
114 
115  // Inequality comparison.
116  friend bool operator!=(const Usd_PrimDataHandle &lhs,
117  const Usd_PrimDataHandle &rhs) {
118  return !(lhs == rhs);
119  }
120 
121  // Swap \p lhs and \p rhs.
122  friend void swap(Usd_PrimDataHandle &lhs, Usd_PrimDataHandle &rhs) {
123  lhs.swap(rhs);
124  }
125 
126  // Provide hash_value.
127  friend size_t hash_value(const Usd_PrimDataHandle &h) {
128  return boost::hash_value(h._p.get());
129  }
130 
131  friend element_type *get_pointer(const Usd_PrimDataHandle &h) {
132  return h._p.get();
133  }
134 
135  Usd_PrimDataConstIPtr _p;
136 };
137 
138 
139 PXR_NAMESPACE_CLOSE_SCOPE
140 
141 #endif // USD_PRIMDATAHANDLE_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:287
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.