Skip to content

Commit

Permalink
Improve get function
Browse files Browse the repository at this point in the history
  • Loading branch information
sakertooth committed Nov 21, 2024
1 parent 5810d3d commit f9d7968
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions include/SampleCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,16 @@ class SampleCache
static auto get(const T& entry, std::unordered_map<T, std::weak_ptr<SampleBuffer>, Hash>& map, Args... args)
{
const auto it = map.find(entry);
const auto item = it == map.end() ? nullptr : it->second.lock();

if (it == map.end())
if (!item)
{
const auto buffer = std::make_shared<SampleBuffer>(std::forward<Args>(args)...);
map.insert(std::make_pair(entry, buffer));
map.emplace(entry, buffer);
return buffer;
}

const auto entryLock = it->second.lock();
if (!entryLock)
{
const auto buffer = std::make_shared<SampleBuffer>(std::forward<Args>(args)...);
map[entry] = buffer;
return buffer;
}

return entryLock;
return item;
}

inline static std::unordered_map<AudioFileEntry, std::weak_ptr<SampleBuffer>, Hash> s_audioFileMap;
Expand Down

0 comments on commit f9d7968

Please sign in to comment.