diff options
Diffstat (limited to 'src/progs/ingenuity/Loader.cpp')
-rw-r--r-- | src/progs/ingenuity/Loader.cpp | 216 |
1 files changed, 30 insertions, 186 deletions
diff --git a/src/progs/ingenuity/Loader.cpp b/src/progs/ingenuity/Loader.cpp index a0f099c2..59d718c7 100644 --- a/src/progs/ingenuity/Loader.cpp +++ b/src/progs/ingenuity/Loader.cpp @@ -25,219 +25,63 @@ using std::cout; using std::endl; namespace Ingenuity { -// LoadPatchEvent // - -void -LoadPatchEvent::execute() -{ - assert(m_patch_librarian != NULL); - m_patch_librarian->load_patch(m_patch_model, m_wait, m_merge); -} - - - -// SavePatchEvent // - -void -SavePatchEvent::execute() -{ - assert(m_patch_librarian != NULL); - m_patch_librarian->save_patch(m_patch_model, m_filename, m_recursive); -} - -/* -void -LoadSessionEvent::execute() -{ - std::ifstream is; - is.open(m_filename.c_str(), std::ios::in); - - if ( ! is.good()) { - cout << "[Loader] Unable to open session file " << m_filename << endl; - return; - } else { - cout << "[Loader] Loading session from " << m_filename << endl; - } - string s; - - is >> s; - if (s != "version") { - cout << "[Loader] Corrupt session file." << endl; - is.close(); - return; - } - - is >> s; - if (s != "1") { - cout << "[Loader] Unrecognised session file version." << endl; - is.close(); - return; - } - - while (!is.eof()) { - is >> s; - if (s == "") continue; - - if (s != "patch") { - //cerr << "[Loader] Corrupt session file, aborting session load." << endl; - break; - } else { - is >> s; - PatchModel* pm = new PatchModel("", 0); - if (s.substr(0, 1) != "/") - s = m_filename.substr(0, m_filename.find_last_of("/")+1) + s; - pm->filename(s); - pm->parent(NULL); - m_patch_librarian->load_patch(pm); - } - } - - is.close(); -} - - -void -SaveSessionEvent::execute() -{ - assert(m_filename != ""); - string dir = m_filename.substr(0, m_filename.find_last_of("/")); - - string patch_filename; - - std::ofstream os; - os.open(m_filename.c_str(), std::ios::out); - - if ( ! os.good()) { - cout << "[Loader] Unable to write to session file " << m_filename << endl; - return; - } else { - cout << "[Loader] Saving session to " << m_filename << endl; - } - - os << "version 1" << endl; - - for (map<string,PatchController*>::iterator i = app->patches().begin(); - i != app->patches().end(); ++i) - { - if ((*i).second->model()->parent() == NULL) { - patch_filename = (*i).second->model()->filename(); - - // Make path relative if possible - if (patch_filename.length() > dir.length() && - patch_filename.substr(0, dir.length()) == dir) - patch_filename = patch_filename.substr(dir.length()+1); - - os << "patch " << patch_filename << endl; - } - } - - os.close(); -} -*/ - - -//////// Loader ////////// - - Loader::Loader(CountedPtr<ModelEngineInterface> engine) -: m_patch_librarian(new PatchLibrarian(engine)), - m_event(NULL), - m_thread_exit_flag(false) +: _patch_librarian(new PatchLibrarian(engine)) { - assert(m_patch_librarian != NULL); - pthread_mutex_init(&m_mutex, NULL); - pthread_cond_init(&m_cond, NULL); - + assert(_patch_librarian != NULL); + // FIXME: rework this so the thread is only present when it's doing something (save mem) - launch(); + start(); } Loader::~Loader() { - m_thread_exit_flag = true; - pthread_join(m_thread, NULL); - delete m_patch_librarian; -} - - -void -Loader::set_event(LoaderEvent* ev) -{ - assert(ev != NULL); - - pthread_mutex_lock(&m_mutex); - assert(m_event == NULL); - m_event = ev; - pthread_cond_signal(&m_cond); - pthread_mutex_unlock(&m_mutex); + delete _patch_librarian; } void -Loader::launch() +Loader::_whipped() { - pthread_create(&m_thread, NULL, Loader::thread_function, this); -} + _mutex.lock(); + Closure& ev = _event; + ev(); + ev.disconnect(); -void* -Loader::thread_function(void* me) -{ - Loader* ct = static_cast<Loader*>(me); - return ct->m_thread_function(NULL); -} - - -void* -Loader::m_thread_function(void *) -{ - while ( ! m_thread_exit_flag) { - pthread_mutex_lock(&m_mutex); - pthread_cond_wait(&m_cond, &m_mutex); - - LoaderEvent* ev = m_event; - ev->execute(); - delete ev; - m_event = NULL; - - pthread_mutex_unlock(&m_mutex); - } - - pthread_exit(NULL); - return NULL; + _cond.signal(); + _mutex.unlock(); } void -Loader::load_patch(CountedPtr<PatchModel> model, bool wait, bool merge) +Loader::load_patch(const string& filename, + const string& parent_path, + const string& name, + size_t poly, + const MetadataMap& initial_data, + bool existing) { - set_event(new LoadPatchEvent(m_patch_librarian, model, wait, merge)); -} - - -void -Loader::save_patch(CountedPtr<PatchModel> model, const string& filename, bool recursive) -{ - cout << "[Loader] Saving patch " << filename << endl; - set_event(new SavePatchEvent(m_patch_librarian, model, filename, recursive)); -} + _mutex.lock(); + _event = sigc::hide_return(sigc::bind( + sigc::mem_fun(_patch_librarian, &PatchLibrarian::load_patch), + filename, parent_path, name, poly, initial_data, existing)); + + whip(); -/* -void -Loader::load_session(const string& filename) -{ - set_event(new LoadSessionEvent(m_patch_librarian, filename)); + _cond.wait(_mutex); + _mutex.unlock(); } void -Loader::save_session(const string& filename) +Loader::save_patch(CountedPtr<PatchModel> model, const string& filename, bool recursive) { - cout << "Saving session..." << endl; - set_event(new SaveSessionEvent(m_patch_librarian, filename)); + cerr << "FIXME: (loader) save patch\n"; + //cout << "[Loader] Saving patch " << filename << endl; + //set_event(new SavePatchEvent(m_patch_librarian, model, filename, recursive)); } -*/ } // namespace Ingenuity |