--- gigedit/trunk/src/gigedit/mainwindow.cpp 2019/02/12 18:54:58 3469 +++ gigedit/trunk/src/gigedit/mainwindow.cpp 2019/02/14 19:10:49 3470 @@ -1892,13 +1892,19 @@ } } + +LoaderSaverBase::LoaderSaverBase(const Glib::ustring filename, gig::File* gig) : + filename(filename), gig(gig), thread(0), progress(0.f) +{ +} + void loader_progress_callback(gig::progress_t* progress) { - Loader* loader = static_cast(progress->custom); + LoaderSaverBase* loader = static_cast(progress->custom); loader->progress_callback(progress->factor); } -void Loader::progress_callback(float fraction) +void LoaderSaverBase::progress_callback(float fraction) { { Glib::Threads::Mutex::Lock lock(progressMutex); @@ -1911,19 +1917,17 @@ // make sure stack is 16-byte aligned for SSE instructions __attribute__((force_align_arg_pointer)) #endif -void Loader::thread_function() +void LoaderSaverBase::thread_function() { printf("thread_function self=%p\n", static_cast(Glib::Threads::Thread::self())); printf("Start %s\n", filename.c_str()); try { - RIFF::File* riff = new RIFF::File(filename); - gig = new gig::File(riff); gig::progress_t progress; progress.callback = loader_progress_callback; progress.custom = this; - gig->GetInstrument(0, &progress); + thread_function_sub(progress); printf("End\n"); finished_dispatcher(); } catch (RIFF::Exception e) { @@ -1935,159 +1939,94 @@ } } -Loader::Loader(const char* filename) - : filename(filename), gig(0), thread(0), progress(0.f) -{ -} - -void Loader::launch() +void LoaderSaverBase::launch() { #ifdef OLD_THREADS - thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true); + thread = Glib::Thread::create(sigc::mem_fun(*this, &LoaderSaverBase::thread_function), true); #else - thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Loader::thread_function)); + thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &LoaderSaverBase::thread_function)); #endif printf("launch thread=%p\n", static_cast(thread)); } -float Loader::get_progress() +float LoaderSaverBase::get_progress() { - float res; - { - Glib::Threads::Mutex::Lock lock(progressMutex); - res = progress; - } - return res; + Glib::Threads::Mutex::Lock lock(progressMutex); + return progress; } -Glib::Dispatcher& Loader::signal_progress() +Glib::Dispatcher& LoaderSaverBase::signal_progress() { return progress_dispatcher; } -Glib::Dispatcher& Loader::signal_finished() +Glib::Dispatcher& LoaderSaverBase::signal_finished() { return finished_dispatcher; } -Glib::Dispatcher& Loader::signal_error() +Glib::Dispatcher& LoaderSaverBase::signal_error() { return error_dispatcher; } -void saver_progress_callback(gig::progress_t* progress) -{ - Saver* saver = static_cast(progress->custom); - saver->progress_callback(progress->factor); -} -void Saver::progress_callback(float fraction) +Loader::Loader(const char* filename) : + LoaderSaverBase(filename, 0) { - { - Glib::Threads::Mutex::Lock lock(progressMutex); - progress = fraction; - } - progress_dispatcher.emit(); } -#if defined(WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) -// make sure stack is 16-byte aligned for SSE instructions -__attribute__((force_align_arg_pointer)) -#endif -void Saver::thread_function() +void Loader::thread_function_sub(gig::progress_t& progress) { - printf("thread_function self=%p\n", - static_cast(Glib::Threads::Thread::self())); - printf("Start %s\n", filename.c_str()); - try { - gig::progress_t progress; - progress.callback = saver_progress_callback; - progress.custom = this; - - // if no filename was provided, that means "save", if filename was provided means "save as" - if (filename.empty()) { - if (!Settings::singleton()->saveWithTemporaryFile) { - // save directly over the existing .gig file - // (requires less disk space than solution below - // but may be slower) - gig->Save(&progress); - } else { - // save the file as separate temporary file first, - // then move the saved file over the old file - // (may result in performance speedup during save) - gig::String tmpname = filename + ".TMP"; - gig->Save(tmpname, &progress); - #if defined(WIN32) - if (!DeleteFile(filename.c_str())) { - throw RIFF::Exception("Could not replace original file with temporary file (unable to remove original file)."); - } - #else // POSIX ... - if (unlink(filename.c_str())) { - throw RIFF::Exception("Could not replace original file with temporary file (unable to remove original file): " + gig::String(strerror(errno))); - } - #endif - if (rename(tmpname.c_str(), filename.c_str())) { - #if defined(WIN32) - throw RIFF::Exception("Could not replace original file with temporary file (unable to rename temp file)."); - #else - throw RIFF::Exception("Could not replace original file with temporary file (unable to rename temp file): " + gig::String(strerror(errno))); - #endif - } - } - } else { - gig->Save(filename, &progress); - } + RIFF::File* riff = new RIFF::File(filename); + gig = new gig::File(riff); - printf("End\n"); - finished_dispatcher.emit(); - } catch (RIFF::Exception e) { - error_message = e.Message; - error_dispatcher.emit(); - } catch (...) { - error_message = _("Unknown exception occurred"); - error_dispatcher.emit(); - } + gig->GetInstrument(0, &progress); } -Saver::Saver(gig::File* file, Glib::ustring filename) - : gig(file), filename(filename), thread(0), progress(0.f) + +Saver::Saver(gig::File* file, Glib::ustring filename) : + LoaderSaverBase(filename, file) { } -void Saver::launch() +void Saver::thread_function_sub(gig::progress_t& progress) { -#ifdef OLD_THREADS - thread = Glib::Thread::create(sigc::mem_fun(*this, &Saver::thread_function), true); + // if no filename was provided, that means "save", if filename was provided means "save as" + if (filename.empty()) { + if (!Settings::singleton()->saveWithTemporaryFile) { + // save directly over the existing .gig file + // (requires less disk space than solution below + // but may be slower) + gig->Save(&progress); + } else { + // save the file as separate temporary file first, + // then move the saved file over the old file + // (may result in performance speedup during save) + gig::String tmpname = filename + ".TMP"; + gig->Save(tmpname, &progress); +#if defined(WIN32) + if (!DeleteFile(filename.c_str())) { + throw RIFF::Exception("Could not replace original file with temporary file (unable to remove original file)."); + } +#else // POSIX ... + if (unlink(filename.c_str())) { + throw RIFF::Exception("Could not replace original file with temporary file (unable to remove original file): " + gig::String(strerror(errno))); + } +#endif + if (rename(tmpname.c_str(), filename.c_str())) { +#if defined(WIN32) + throw RIFF::Exception("Could not replace original file with temporary file (unable to rename temp file)."); #else - thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Saver::thread_function)); + throw RIFF::Exception("Could not replace original file with temporary file (unable to rename temp file): " + gig::String(strerror(errno))); #endif - printf("launch thread=%p\n", static_cast(thread)); -} - -float Saver::get_progress() -{ - float res; - { - Glib::Threads::Mutex::Lock lock(progressMutex); - res = progress; + } + } + } else { + gig->Save(filename, &progress); } - return res; -} - -Glib::Dispatcher& Saver::signal_progress() -{ - return progress_dispatcher; } -Glib::Dispatcher& Saver::signal_finished() -{ - return finished_dispatcher; -} - -Glib::Dispatcher& Saver::signal_error() -{ - return error_dispatcher; -} ProgressDialog::ProgressDialog(const Glib::ustring& title, Gtk::Window& parent) : Gtk::Dialog(title, parent, true)