7#ifndef PXR_USD_AR_THREAD_LOCAL_SCOPED_CACHE_H
8#define PXR_USD_AR_THREAD_LOCAL_SCOPED_CACHE_H
11#include "pxr/usd/ar/api.h"
13#include "pxr/base/vt/value.h"
16#include <tbb/enumerable_thread_specific.h>
20PXR_NAMESPACE_OPEN_SCOPE
48template <
class CachedType>
52 using CachePtr = std::shared_ptr<CachedType>;
54 void BeginCacheScope(
VtValue* cacheScopeData)
59 if (!cacheScopeData ||
61 !cacheScopeData->
IsHolding<CachePtr>())) {
66 _CachePtrStack& cacheStack = _threadCacheStack.local();
67 if (cacheScopeData->
IsHolding<CachePtr>()) {
68 cacheStack.push_back(cacheScopeData->
UncheckedGet<CachePtr>());
71 if (cacheStack.empty()) {
72 cacheStack.push_back(std::make_shared<CachedType>());
75 cacheStack.push_back(cacheStack.back());
78 *cacheScopeData = cacheStack.back();
81 void EndCacheScope(
VtValue* cacheScopeData)
83 _CachePtrStack& cacheStack = _threadCacheStack.local();
85 cacheStack.pop_back();
89 CachePtr GetCurrentCache()
91 _CachePtrStack& cacheStack = _threadCacheStack.local();
92 return (cacheStack.empty() ? CachePtr() : cacheStack.back());
96 using _CachePtrStack = std::vector<CachePtr>;
97 using _ThreadLocalCachePtrStack =
98 tbb::enumerable_thread_specific<_CachePtrStack>;
99 _ThreadLocalCachePtrStack _threadCacheStack;
102PXR_NAMESPACE_CLOSE_SCOPE
Low-level utilities for informing users of various internal and external diagnostic conditions.
Utility class for custom resolver implementations.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
bool IsEmpty() const
Returns true iff this value is empty.
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.