Loading...
Searching...
No Matches
resolverContext.h
Go to the documentation of this file.
1//
2// Copyright 2020 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_AR_RESOLVER_CONTEXT_H
8#define PXR_USD_AR_RESOLVER_CONTEXT_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/usd/ar/api.h"
14#include "pxr/usd/ar/ar.h"
15
16#include "pxr/base/tf/hash.h"
18
19#ifdef PXR_PYTHON_SUPPORT_ENABLED
20// XXX: This include is a hack to avoid build errors due to
21// incompatible macro definitions in pyport.h on macOS.
22#include <locale>
23#include "pxr/base/tf/pyLock.h"
24#endif
25
26#include "pxr/base/tf/pyObjWrapper.h"
27
28#include <algorithm>
29#include <memory>
30#include <string>
31#include <type_traits>
32#include <typeinfo>
33#include <vector>
34
35PXR_NAMESPACE_OPEN_SCOPE
36
42template <class T>
44{
45 static const bool value = false;
46};
47
49template <class Context>
50std::string ArGetDebugString(const Context& context);
51
52// Metafunctions for determining if a variadic list of objects
53// are valid for use with the ArResolverContext c'tor.
55
56template <class ...Objects> struct Ar_AllValidForContext;
57
58template <class Object, class ...Other>
59struct Ar_AllValidForContext<Object, Other...>
60{
61 static const bool value =
62 (std::is_same<Object, ArResolverContext>::value ||
64 Ar_AllValidForContext<Other...>::value;
65};
66
67template <>
68struct Ar_AllValidForContext<>
69{
70 static const bool value = true;
71};
72
108{
109public:
112 {
113 }
114
130 template <
131 class ...Objects,
132 typename std::enable_if<Ar_AllValidForContext<Objects...>::value>::type*
133 = nullptr>
134 ArResolverContext(const Objects&... objs)
135 {
136 _AddObjects(objs...);
137 }
138
148 AR_API
149 explicit ArResolverContext(const std::vector<ArResolverContext>& ctxs);
150
152 bool IsEmpty() const
153 {
154 return _contexts.empty();
155 }
156
159 AR_API
160 bool Contains(const ArResolverContext& ctx) const;
161
164 template <
165 class ContextObj,
166 std::enable_if_t<ArIsContextObject<ContextObj>::value>* = nullptr
167 >
168 bool Contains(const ContextObj& contextObj) const
169 {
170 const ContextObj* testObj = Get<ContextObj>();
171 return testObj && *testObj == contextObj;
172 }
173
177 template <class ContextObj>
178 const ContextObj* Get() const
179 {
180 for (const auto& context : _contexts) {
181 if (context->IsHolding(typeid(ContextObj))) {
182 return &_GetTyped<ContextObj>(*context)._context;
183 }
184 }
185 return nullptr;
186 }
187
189 AR_API
190 std::string GetDebugString() const;
191
194 AR_API
195 bool operator==(const ArResolverContext& rhs) const;
196
197 bool operator!=(const ArResolverContext& rhs) const
198 {
199 return !(*this == rhs);
200 }
201
202 AR_API
203 bool operator<(const ArResolverContext& rhs) const;
204
206
208 friend size_t hash_value(const ArResolverContext& context)
209 {
210 return TfHash()(context._contexts);
211 }
212
213private:
214 // Type-erased storage for context objects.
215 struct _Untyped;
216 template <class Context> struct _Typed;
217
218 void _AddObjects()
219 {
220 // Empty base case for unpacking parameter pack
221 }
222
223 template <class Object, class ...Other>
224 void _AddObjects(const Object& obj, const Other&... other)
225 {
226 _Add(obj);
227 _AddObjects(other...);
228 }
229
230 AR_API
231 void _Add(const ArResolverContext& ctx);
232
233 template <class Object>
234 void _Add(const Object& obj)
235 {
236 _Add(std::shared_ptr<_Untyped>(new _Typed<Object>(obj)));
237 }
238
239 AR_API
240 void _Add(std::shared_ptr<_Untyped>&& context);
241
242 template <class Context>
243 static const _Typed<Context>& _GetTyped(const _Untyped& untyped)
244 {
245 return static_cast<const _Typed<Context>&>(untyped);
246 }
247
248 struct _Untyped
249 {
250 AR_API
251 virtual ~_Untyped();
252
253 bool IsHolding(const std::type_info& ti) const
254 {
255 return TfSafeTypeCompare(ti, GetTypeid());
256 }
257
258 virtual _Untyped* Clone() const = 0;
259 virtual const std::type_info& GetTypeid() const = 0;
260 virtual bool LessThan(const _Untyped& rhs) const = 0;
261 virtual bool Equals(const _Untyped& rhs) const = 0;
262 virtual size_t Hash() const = 0;
263 virtual std::string GetDebugString() const = 0;
264 virtual TfPyObjWrapper GetPythonObj() const = 0;
265 };
266
267 template <class Context>
268 struct _Typed : public _Untyped
269 {
270 virtual ~_Typed() { }
271
272 _Typed(const Context& context) : _context(context)
273 {
274 }
275
276 virtual _Untyped* Clone() const
277 {
278 return new _Typed<Context>(_context);
279 }
280
281 virtual const std::type_info& GetTypeid() const
282 {
283 return typeid(Context);
284 }
285
286 virtual bool LessThan(const _Untyped& rhs) const
287 {
288 return _context < _GetTyped<Context>(rhs)._context;
289 }
290
291 virtual bool Equals(const _Untyped& rhs) const
292 {
293 return _context == _GetTyped<Context>(rhs)._context;
294 }
295
296 virtual size_t Hash() const
297 {
298 return hash_value(_context);
299 }
300
301 virtual std::string GetDebugString() const
302 {
303 return ArGetDebugString(_context);
304 }
305
306 virtual TfPyObjWrapper GetPythonObj() const
307 {
308 #ifdef PXR_PYTHON_SUPPORT_ENABLED
309 TfPyLock lock;
310 return pxr_boost::python::object(_context);
311#else
312 return {};
313#endif
314 }
315
316 Context _context;
317 };
318
319 template <class HashState>
320 friend void TfHashAppend(
321 HashState& h, const std::shared_ptr<_Untyped>& context)
322 {
323 h.Append(context->Hash());
324 }
325
326#ifdef PXR_PYTHON_SUPPORT_ENABLED
327 friend class Ar_ResolverContextPythonAccess;
328#endif
329
330 std::vector<std::shared_ptr<_Untyped>> _contexts;
331};
332
333
334// Default implementation for streaming out held contexts.
335AR_API
336std::string Ar_GetDebugString(const std::type_info&, void const*);
337
338template <class Context>
339std::string ArGetDebugString(const Context& context)
340{
341 return Ar_GetDebugString(typeid(Context),
342 static_cast<void const*>(&context));
343}
344
345PXR_NAMESPACE_CLOSE_SCOPE
346
347#endif
An asset resolver context allows clients to provide additional data to the resolver for use during re...
ArResolverContext(const Objects &... objs)
Construct a resolver context using the given objects objs.
const ContextObj * Get() const
Returns pointer to the context object of the given type held in this resolver context.
AR_API std::string GetDebugString() const
Returns a debug string representing the contained context objects.
AR_API bool Contains(const ArResolverContext &ctx) const
Returns true if this resolver context contains all of the context objects in the given ctx.
AR_API ArResolverContext(const std::vector< ArResolverContext > &ctxs)
Construct a resolver context using the ArResolverContexts in ctxs.
bool IsEmpty() const
Returns whether this resolver context is empty.
bool Contains(const ContextObj &contextObj) const
Returns true if this resolver context contains the given context object contextObj.
ArResolverContext()
Construct an empty asset resolver context.
friend size_t hash_value(const ArResolverContext &context)
Returns hash value for this asset resolver context.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition hash.h:472
Convenience class for accessing the Python Global Interpreter Lock.
Definition pyLock.h:105
Boost Python object wrapper.
std::string ArGetDebugString(const Context &context)
Default implementation for providing debug info on the contained context.
Safely compare C++ RTTI type structures.
bool TfSafeTypeCompare(const std::type_info &t1, const std::type_info &t2)
Safely compare std::type_info structures.
Metafunction to determine whether the templated object type is a valid context object.