24 #ifndef PXR_IMAGING_HD_INSTANCE_REGISTRY_H
25 #define PXR_IMAGING_HD_INSTANCE_REGISTRY_H
33 #include <tbb/concurrent_unordered_map.h>
59 template <
typename VALUE>
79 typedef tbb::concurrent_unordered_map<KeyType, ValueHolder>
Dictionary;
94 , _registryLock(std::move(registryLock))
95 , _container(container)
96 , _isFirstInstance(!bool(_value))
106 , _container(nullptr)
107 , _isFirstInstance(!bool(_value))
118 if (_container) (*_container)[_key] =
ValueHolder(value);
124 return _isFirstInstance;
132 bool _isFirstInstance;
145 template <
typename VALUE>
155 : _dictionary(other._dictionary)
181 template <
typename Callback>
190 size_t size()
const {
return _dictionary.size(); }
195 template <
typename T>
196 static bool _IsUnique(std::shared_ptr<T>
const &
value) {
197 return value.unique();
209 template <
typename VALUE>
221 typename InstanceType::Dictionary::iterator it = _dictionary.find(key);
222 if (it == _dictionary.end()) {
224 it = _dictionary.insert(
228 it->second.ResetRecycleCounter();
229 return InstanceType(key, it->second.value, std::move(lock), &_dictionary);
232 template <
typename VALUE>
244 typename InstanceType::Dictionary::iterator it = _dictionary.find(key);
245 if (it == _dictionary.end()) {
247 return InstanceType(key, VALUE(), std::move(lock),
nullptr);
250 it->second.ResetRecycleCounter();
251 return InstanceType(key, it->second.value,std::move(lock),&_dictionary);
255 template <
typename VALUE>
260 return GarbageCollect([](
void*){}, recycleCount);
263 template <
typename VALUE>
264 template <
typename Callback>
272 if (recycleCount < 0) {
273 return _dictionary.size();
276 size_t inUseCount = 0;
277 for (
typename InstanceType::Dictionary::iterator it = _dictionary.begin();
278 it != _dictionary.end();) {
281 bool isUnique = _IsUnique(it->second.value);
282 if (isUnique && (++it->second.recycleCounter > recycleCount)) {
283 std::forward<Callback>(callback)(it->second.value.get());
284 it = _dictionary.unsafe_erase(it);
293 template <
typename VALUE>
306 #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