Destruct AssFiles on a background thread as it's rather slow with large files

Originally committed to SVN as r6407.
This commit is contained in:
Thomas Goyne 2012-02-01 00:47:28 +00:00
parent 64553dd633
commit 8bef1eb874
2 changed files with 40 additions and 6 deletions

View file

@ -68,16 +68,14 @@ namespace std {
}
}
/// @brief AssFile constructor
AssFile::AssFile ()
: commitId(0)
, loaded(false)
{
}
/// @brief AssFile destructor
AssFile::~AssFile() {
delete_clear(Line);
background_delete_clear(Line);
}
/// @brief Load generic subs
@ -386,7 +384,7 @@ void AssFile::AddLine(wxString data, int *version, AssAttachment **attach) {
}
void AssFile::Clear() {
delete_clear(Line);
background_delete_clear(Line);
loaded = false;
filename.clear();

View file

@ -46,6 +46,7 @@
#include <wx/icon.h>
#include <wx/menuitem.h>
#include <wx/thread.h>
#endif
class wxMouseEvent;
@ -97,18 +98,53 @@ template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b
#endif
#endif
/// Polymorphic delete functor
struct delete_ptr {
template<class T>
void operator()(T* ptr) const {
delete ptr;
}
};
/// Delete all of the items in a container of pointers and clear the container
template<class T>
void delete_clear(T& container) {
std::for_each(container.begin(), container.end(), delete_ptr());
container.clear();
if (!container.empty()) {
std::for_each(container.begin(), container.end(), delete_ptr());
container.clear();
}
}
/// Helper class for background_delete_clear
template<class Container>
class BackgroundDeleter : public wxThread {
Container cont;
wxThread::ExitCode Entry() {
delete_clear(cont);
return (wxThread::ExitCode)0;
}
public:
BackgroundDeleter(Container &source)
: wxThread(wxTHREAD_DETACHED)
{
using std::swap;
swap(cont, source);
SetPriority(WXTHREAD_MIN_PRIORITY);
Create();
Run();
}
};
/// Clear a container of pointers and delete the pointed to members on a
/// background thread
template<class T>
void background_delete_clear(T& container) {
if (!container.empty())
new BackgroundDeleter<T>(container);
}
template<class Out>
struct cast {
template<class In>