7 #ifndef PXR_IMAGING_HD_INSTANCE_REGISTRY_H
8 #define PXR_IMAGING_HD_INSTANCE_REGISTRY_H
16 #include <tbb/concurrent_unordered_map.h>
42 template <
typename VALUE>
62 typedef tbb::concurrent_unordered_map<KeyType, ValueHolder>
Dictionary;
77 , _registryLock(std::move(registryLock))
78 , _container(container)
79 , _isFirstInstance(!
bool(_value))
90 , _isFirstInstance(!
bool(_value))
101 if (_container) (*_container)[_key] =
ValueHolder(value);
107 return _isFirstInstance;
115 bool _isFirstInstance;
128 template <
typename VALUE>
138 : _dictionary(other._dictionary)
164 template <
typename Callback>
173 size_t size()
const {
return _dictionary.size(); }
178 template <
typename T>
179 static bool _IsUnique(std::shared_ptr<T>
const &
value) {
180 return value.use_count() == 1;
192 template <
typename VALUE>
204 typename InstanceType::Dictionary::iterator it = _dictionary.find(key);
205 if (it == _dictionary.end()) {
207 it = _dictionary.insert(
211 it->second.ResetRecycleCounter();
212 return InstanceType(key, it->second.value, std::move(lock), &_dictionary);
215 template <
typename VALUE>
227 typename InstanceType::Dictionary::iterator it = _dictionary.find(key);
228 if (it == _dictionary.end()) {
230 return InstanceType(key, VALUE(), std::move(lock),
nullptr);
233 it->second.ResetRecycleCounter();
234 return InstanceType(key, it->second.value,std::move(lock),&_dictionary);
238 template <
typename VALUE>
243 return GarbageCollect([](
void*){}, recycleCount);
246 template <
typename VALUE>
247 template <
typename Callback>
255 if (recycleCount < 0) {
256 return _dictionary.size();
259 size_t inUseCount = 0;
260 for (
typename InstanceType::Dictionary::iterator it = _dictionary.begin();
261 it != _dictionary.end();) {
264 bool isUnique = _IsUnique(it->second.value);
265 if (isUnique && (++it->second.recycleCounter > recycleCount)) {
266 std::forward<Callback>(callback)(it->second.value.get());
267 it = _dictionary.unsafe_erase(it);
276 template <
typename VALUE>
289 #endif // PXR_IMAGING_HD_INSTANCE_REGISTRY_H
size_t GarbageCollect(int recycleCount=0)
tbb::concurrent_unordered_map< KeyType, ValueHolder > Dictionary
KeyType const & GetKey() const
Returns the key.
GLsizei const GLfloat * value
#define HF_MALLOC_TAG_FUNCTION()
const_iterator begin() const
bool IsFirstInstance() const
Returns true if the value has not been initialized.
HdInstanceRegistry()=default
HdInstance< VALUE > InstanceType
HdInstance(KeyType const &key)
void SetValue(ValueType const &value)
Update the value in dictionary indexed by the key.
HdInstance(KeyType const &key, ValueType const &value, RegistryLock &®istryLock, Dictionary *container)
InstanceType::Dictionary::const_iterator const_iterator
#define HD_TRACE_FUNCTION()
std::unique_lock< RegistryMutex > RegistryLock
void ResetRecycleCounter()
HdInstanceRegistry(const HdInstanceRegistry &other)
InstanceType GetInstance(typename InstanceType::KeyType const &key)
Returns a shared instance for given key.
ValueType const & GetValue() const
Returns the value.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
#define PXR_NAMESPACE_CLOSE_SCOPE
InstanceType FindInstance(typename InstanceType::KeyType const &key, bool *found)
ValueHolder(ValueType const &value=ValueType())
const_iterator end() const