1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-17 23:10:17 +02:00

Move instead of copy strings at the end of FileLister::browse

This takes a little trickery, which I wrapped and documented.
This commit is contained in:
Maarten ter Huurne 2014-08-13 06:46:08 +02:00
parent f1e92425ac
commit 46c64ea984

View File

@ -60,6 +60,18 @@ void FileLister::setShowFiles(bool showFiles)
this->showFiles = showFiles; this->showFiles = showFiles;
} }
static void moveNames(set<string, case_less>&& from, vector<string>& to)
{
to.reserve(from.size());
for (string const& name : from) {
// Casting away the const is necessary to make the move work.
// It will leave the set in an invalid state where it contains multiple
// empty strings, but since the set is an rvalue we don't care.
to.emplace_back(move(const_cast<string&>(name)));
}
to.shrink_to_fit();
}
void FileLister::browse(const string& path, bool clean) void FileLister::browse(const string& path, bool clean)
{ {
set<string, case_less> directorySet; set<string, case_less> directorySet;
@ -68,6 +80,8 @@ void FileLister::browse(const string& path, bool clean)
directorySet.insert(directories.begin(), directories.end()); directorySet.insert(directories.begin(), directories.end());
fileSet.insert(files.begin(), files.end()); fileSet.insert(files.begin(), files.end());
} }
directories.clear();
files.clear();
if (showDirectories || showFiles) { if (showDirectories || showFiles) {
DIR *dirp; DIR *dirp;
@ -146,8 +160,8 @@ void FileLister::browse(const string& path, bool clean)
closedir(dirp); closedir(dirp);
} }
directories = vector<string>(directorySet.begin(), directorySet.end()); moveNames(move(directorySet), directories);
files = vector<string>(fileSet.begin(), fileSet.end()); moveNames(move(fileSet), files);
} }
unsigned int FileLister::size() unsigned int FileLister::size()