summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/audiocachefile.cc25
-rw-r--r--src/audiocachefile.h2
-rw-r--r--test/audiocachefiletest.cc6
3 files changed, 14 insertions, 19 deletions
diff --git a/src/audiocachefile.cc b/src/audiocachefile.cc
index 91fcecf..e9ed322 100644
--- a/src/audiocachefile.cc
+++ b/src/audiocachefile.cc
@@ -130,25 +130,19 @@ AudioCacheFile& AudioCacheFiles::getFile(const std::string& filename)
{
std::lock_guard<std::mutex> lock(mutex);
- AudioCacheFile* cache_audio_file = nullptr;
-
auto it = audiofiles.find(filename);
if(it == audiofiles.end())
{
- cache_audio_file = new AudioCacheFile(filename, read_buffer);
- audiofiles.insert(std::make_pair(filename, cache_audio_file));
- }
- else
- {
- cache_audio_file = it->second;
+ it = audiofiles.emplace(std::piecewise_construct, std::make_tuple(filename),
+ std::make_tuple(filename, std::ref(read_buffer))).first;
}
- assert(cache_audio_file);
+ auto& cache_audio_file = it->second;
// Increase ref count.
- ++cache_audio_file->ref;
+ ++cache_audio_file.ref;
- return *cache_audio_file;
+ return cache_audio_file;
}
void AudioCacheFiles::releaseFile(const std::string& filename)
@@ -162,14 +156,13 @@ void AudioCacheFiles::releaseFile(const std::string& filename)
return; // not open
}
- auto audiofile = it->second;
+ auto& audiofile = it->second;
- assert(audiofile->ref); // If ref is not > 0 it shouldn't be in the map.
+ assert(audiofile.ref); // If ref is not > 0 it shouldn't be in the map.
- --audiofile->ref;
- if(audiofile->ref == 0)
+ --audiofile.ref;
+ if(audiofile.ref == 0)
{
- delete audiofile;
audiofiles.erase(it);
}
}
diff --git a/src/audiocachefile.h b/src/audiocachefile.h
index 8e40a2e..343133e 100644
--- a/src/audiocachefile.h
+++ b/src/audiocachefile.h
@@ -95,7 +95,7 @@ public:
void releaseFile(const std::string& filename);
protected:
- std::map<std::string, AudioCacheFile*> audiofiles;
+ std::map<std::string, AudioCacheFile> audiofiles;
std::mutex mutex;
std::vector<sample_t> read_buffer;
};
diff --git a/test/audiocachefiletest.cc b/test/audiocachefiletest.cc
index b7839b7..6f84ba2 100644
--- a/test/audiocachefiletest.cc
+++ b/test/audiocachefiletest.cc
@@ -39,12 +39,14 @@ public:
//void release(const std::string& filename);
int getRef(const std::string& filename)
{
- if(audiofiles.find(filename) == audiofiles.end())
+ auto it = audiofiles.find(filename);
+
+ if(it == audiofiles.end())
{
return -1;
}
- return audiofiles[filename]->ref;
+ return (it->second).ref;
}
};