doc_manager.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:16k
- /*
- * ===========================================================================
- * PRODUCTION $Log: doc_manager.cpp,v $
- * PRODUCTION Revision 1000.5 2004/06/01 20:43:48 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.50
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: doc_manager.cpp,v 1000.5 2004/06/01 20:43:48 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Mike DiCuccio
- *
- * File Description:
- * Central document arbiter for GBENCH
- */
- #include <ncbi_pch.hpp>
- #include <corelib/ncbimtx.hpp>
- #include <gui/core/data_store.hpp>
- #include <gui/core/doc_exception.hpp>
- #include <gui/core/doc_manager.hpp>
- #include <gui/core/docmgr_view.hpp>
- #include <gui/core/idocument.hpp>
- #include <gui/core/plugin_registry.hpp>
- #include <gui/core/plugin_utils.hpp>
- #include <gui/core/message_queue.hpp>
- #include <gui/core/iview.hpp>
- #include <gui/config/settings.hpp>
- #include <gui/core/selection_buffer.hpp>
- #include <gui/utils/message_box.hpp>
- #include <gui/utils/clipboard.hpp>
- #include <gui/plugin/PluginMessage.hpp>
- #include <gui/plugin/PluginMRUList.hpp>
- #include <objects/seqloc/Seq_id.hpp>
- #include <objects/seqalign/Seq_align.hpp>
- #include <objects/seq/Seq_annot.hpp>
- #include <objmgr/object_manager.hpp>
- #include <serial/typeinfo.hpp>
- #include <algorithm>
- #include "seqid_doc.hpp"
- #include "serialobj_doc.hpp"
- BEGIN_NCBI_SCOPE
- //
- // statics
- //
- DEFINE_STATIC_MUTEX(s_DocMgrMutex);
- CRef<CObjectManager> CDocManager::sm_ObjMgr;
- CDocManager::TDocList CDocManager::sm_Documents;
- CDocManager::TViewList CDocManager::sm_Views;
- CRef<CSelectionBuffer> CDocManager::sm_SelBuffer;
- CRef<CSettings> CDocManager::sm_Settings;
- CDocManager::TScopeIndex CDocManager::sm_ScopeIdx;
- CRef<CPluginMRUList> CDocManager::sm_PluginMessageCache;
- //
- // destructor
- // this is defined so that we can specify the order of destruction of our
- // member objects. scopes must be deleted before the object manager itself!
- //
- void CDocManager::ShutDown()
- {
- CMutexGuard LOCK(s_DocMgrMutex);
- // shut down our settings
- GetSettings().ShutDown();
- // clear the data store
- CDataStore::Clear();
- // clear the clipboard
- CClipboard::Clear();
- // clear the plugin registry
- CPluginRegistry::Clear();
- // clear our views
- sm_Views.clear();
- // clear the plugin message queue
- CPluginMessageQueue::Clear();
- // clear our documents - there are hidden scopes here
- // because of a circular CRef<>, we need to clear the views first
- NON_CONST_ITERATE (TDocList, iter, sm_Documents) {
- (*iter)->Clear();
- }
- sm_Documents.clear();
- sm_ScopeIdx.clear();
- // now, free to object manager
- // (hopefully we've rooted out all lingering scopes by this point)
- sm_ObjMgr.Reset(NULL);
- }
- CDocManager::TDocList& CDocManager::GetDocuments()
- {
- CMutexGuard LOCK(s_DocMgrMutex);
- return sm_Documents;
- }
- //
- // attach a view to the document manager
- //
- void CDocManager::AttachView(CDocMgrView* view)
- {
- CMutexGuard LOCK(s_DocMgrMutex);
- CRef<CDocMgrView> ref(view);
- sm_Views.push_back(ref);
- }
- //
- // force update() to be called on all views
- //
- void CDocManager::UpdateAllViews(TUpdateFlags flags)
- {
- CMutexGuard LOCK(s_DocMgrMutex);
- NON_CONST_ITERATE (TViewList, iter, sm_Views) {
- if (flags & (*iter)->GetEventMask()) {
- (*iter)->Update(flags & (*iter)->GetEventMask());
- }
- }
- }
- //
- // event will be broadcasted to all documents and then views
- //
- void CDocManager::NotifyAllViews(const CEvent * event)
- {
- CMutexGuard LOCK(s_DocMgrMutex);
- NON_CONST_ITERATE (TDocList, iter, sm_Documents) {
- IEventReceiver * vr = (*iter)->QueryInterfaceReceiver();
- if (vr) vr->OnEvent(event);
- }
- }
- //
- // x_CreateObjectManager()
- // function to wrap object manager and scope creation
- //
- void CDocManager::x_CreateObjectManager(void)
- {
- // Check to see if we have an object manager(if not, we need to create one)
- if (sm_ObjMgr) {
- return;
- }
- // Create object manager
- sm_ObjMgr = new CObjectManager();
- if ( !sm_ObjMgr ) {
- NCBI_THROW(CDocException, eObjectManagerError,
- "CDocManager::CDocManager(): can't create object manager");
- }
- }
- //
- // access the object manager
- //
- CObjectManager& CDocManager::GetObjectManager()
- {
- if ( !sm_ObjMgr ) {
- CMutexGuard LOCK(s_DocMgrMutex);
- x_CreateObjectManager();
- }
- return *sm_ObjMgr;
- }
- //
- // access the selection buffer
- //
- CSelectionBuffer& CDocManager::GetSelBuffer()
- {
- if (sm_SelBuffer) {
- return *sm_SelBuffer;
- }
- CMutexGuard LOCK(s_DocMgrMutex);
- // check again after locking to make sure we didn't block on a thread that
- // was creating our selection buffer
- if (sm_SelBuffer) {
- return *sm_SelBuffer;
- }
- sm_SelBuffer = new CSelectionBuffer();
- return *sm_SelBuffer;
- }
- //
- // GetSettings()
- // access the global settings repository
- //
- CSettings& CDocManager::GetSettings(void)
- {
- if ( !sm_Settings ) {
- CMutexGuard LOCK(s_DocMgrMutex);
- if ( !sm_Settings ) {
- sm_Settings.Reset(new CSettings());
- }
- }
- return *sm_Settings;
- }
- IDocument* CDocManager::GetDocumentFromScope(CScope& scope)
- {
- CMutexGuard LOCK(s_DocMgrMutex);
- TScopeIndex::iterator iter = sm_ScopeIdx.find(CRef<CScope>(&scope));
- if (iter == sm_ScopeIdx.end()) {
- return NULL;
- }
- return iter->second;
- }
- const IDocument* CDocManager::GetDocumentFromScope(const CScope& scope)
- {
- CRef<CScope> nc_ref(&const_cast<CScope&>(scope));
- CMutexGuard LOCK(s_DocMgrMutex);
- TScopeIndex::const_iterator iter = sm_ScopeIdx.find(nc_ref);
- if (iter == sm_ScopeIdx.end()) {
- return NULL;
- }
- return iter->second;
- }
- //
- // GetDocument()
- // this actually creates and manages documents for the user
- //
- IDocument* CDocManager::CreateDocument(CScope& scope, const CSeq_id& id)
- {
- CMutexGuard LOCK(s_DocMgrMutex);
- CRef<IDocument> doc(new CSeqIdDocument(scope, id));
- sm_Documents.push_back(doc);
- sm_ScopeIdx[ CRef<CScope>(&scope) ] = doc;
- return doc.GetPointer();
- }
- IDocument* CDocManager::CreateDocument(CScope& scope, const CSerialObject& obj)
- {
- CMutexGuard LOCK(s_DocMgrMutex);
- CRef<IDocument> doc(new CSerialObjDocument(scope, obj));
- sm_Documents.push_back(doc);
- sm_ScopeIdx[ CRef<CScope>(&scope) ] = doc;
- return doc.GetPointer();
- }
- //
- // ReleaseDocument()
- // this is responsible for making sure that documents can indeed
- // be deleted
- //
- bool CDocManager::ReleaseDocument(IDocument* doc)
- {
- CMutexGuard LOCK(s_DocMgrMutex);
- CRef<IDocument> ref(doc);
- TDocList::iterator iter =
- std::find(sm_Documents.begin(), sm_Documents.end(), ref);
- if (iter != sm_Documents.end()) {
- // clear all views from the document first
- doc->Clear();
- sm_Documents.erase(iter);
- // also remove it from the scope index
- NON_CONST_ITERATE (TScopeIndex, iter, sm_ScopeIdx) {
- if (iter->second.GetPointer() == doc) {
- sm_ScopeIdx.erase(iter);
- break;
- }
- }
- UpdateAllViews();
- LOG_POST(Error << "unloaded document: " << ref->GetShortTitle());
- return true;
- } else {
- return false;
- }
- }
- //
- // CachePluginMessage
- // Cache the plugin messgae for the Most Recent Used list
- //
- void CDocManager::AddMRUEntry(objects::CPluginMessage& msg, string label)
- {
- // Cache messages whose reply is not formatted
- if (msg.GetReply().IsSetFormatted()) {
- return;
- }
-
- CMutexGuard LOCK(s_DocMgrMutex);
- // sm_PluginMessageCache.Add(msg, CPluginUtils::GetPluginMessageLabel(msg));
- GetMRUCache().Add(msg, label);
- }
- CPluginMRUList& CDocManager::GetMRUCache(void)
- {
- if ( !sm_PluginMessageCache ) {
- CMutexGuard LOCK(s_DocMgrMutex);
- if ( !sm_PluginMessageCache ) {
- sm_PluginMessageCache.Reset(new CPluginMRUList("mru-cache"));
- }
- }
- return *sm_PluginMessageCache;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: doc_manager.cpp,v $
- * Revision 1000.5 2004/06/01 20:43:48 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.50
- *
- * Revision 1.50 2004/06/01 18:02:19 dicuccio
- * Changed GetPluginMessageCache() -> GetMRUCache()
- *
- * Revision 1.49 2004/05/21 22:27:40 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.48 2004/05/18 11:13:24 friedman
- * Handle adding recently loaded documents into the MRU list.
- *
- * Revision 1.47 2004/05/07 15:40:33 dicuccio
- * gui/objutils/clipboard -> gui/objutils/obj_clipboard
- *
- * Revision 1.46 2004/05/03 12:48:46 dicuccio
- * gui/utils --> gui/objutils where needed
- *
- * Revision 1.45 2004/04/21 17:12:33 dicuccio
- * Added const version of GetDocumentFromScope()
- *
- * Revision 1.44 2004/04/16 14:37:28 dicuccio
- * Remove dead code in Update()
- *
- * Revision 1.43 2004/04/07 12:45:06 dicuccio
- * Formatting changes. Added scope -> document index. Added
- * GetDocumentFromScope()
- *
- * Revision 1.42 2004/03/30 17:09:01 tereshko
- * Added NotifyAllViews function
- *
- * Revision 1.41 2004/02/17 20:35:23 rsmith
- * moved core/settings.[ch]pp and core/system_path.[ch]pp to config and utils, respectively.
- *
- * Revision 1.40 2004/02/03 21:24:11 dicuccio
- * Clear the clipboard on ShutDown()
- *
- * Revision 1.39 2004/01/07 17:39:01 vasilche
- * Fixed include path to genbank loader.
- *
- * Revision 1.38 2003/12/22 19:18:48 dicuccio
- * COmmented out unnecessary view update
- *
- * Revision 1.37 2003/11/14 17:50:58 dicuccio
- * Fix silly compiler error introduced in last commit.
- *
- * Revision 1.36 2003/11/14 17:44:18 dicuccio
- * Changed ShutDown() to clear more components
- *
- * Revision 1.35 2003/11/04 17:15:52 dicuccio
- * Added more to ShutDown(): clear the object store; clear the registry
- *
- * Revision 1.34 2003/10/07 13:46:14 dicuccio
- * Drop documents in reverse order (safety concern if documents are
- * interdependent)
- *
- * Revision 1.33 2003/09/16 14:01:40 dicuccio
- * Removed CSeqannotDocument - replaced with generic CSerialObjDocument
- *
- * Revision 1.32 2003/09/04 14:01:51 dicuccio
- * Introduce IDocument and IView as abstract base classes for CDocument and CView
- *
- * Revision 1.31 2003/06/30 13:32:34 dicuccio
- * Removed GenBank data loader - this now lives in a plugin of its own
- *
- * Revision 1.30 2003/06/29 13:10:17 dicuccio
- * Use DEFINE_STATIC_MUTEX() instead of static member variables
- *
- * Revision 1.29 2003/06/25 17:02:54 dicuccio
- * Split CPluginHandle into a handle (pointer-to-implementation) and
- * implementation file. Lots of #include file clean-ups.
- *
- * Revision 1.28 2003/06/18 13:54:48 rsmith
- * cnstr/destr of sm_Views or sm_Documents uses s_DocMgrMutex. Set order
- * accordingly. Useless to guard GetSettings since a reference is returned.
- *
- * Revision 1.27 2003/06/02 16:06:17 dicuccio
- * Rearranged src/objects/ subtree. This includes the following shifts:
- * - src/objects/asn2asn --> arc/app/asn2asn
- * - src/objects/testmedline --> src/objects/ncbimime/test
- * - src/objects/objmgr --> src/objmgr
- * - src/objects/util --> src/objmgr/util
- * - src/objects/alnmgr --> src/objtools/alnmgr
- * - src/objects/flat --> src/objtools/flat
- * - src/objects/validator --> src/objtools/validator
- * - src/objects/cddalignview --> src/objtools/cddalignview
- * In addition, libseq now includes six of the objects/seq... libs, and libmmdb
- * replaces the three libmmdb? libs.
- *
- * Revision 1.26 2003/05/30 14:15:41 dicuccio
- * Renamed MessageBox to NcbiMessageBox because brain-dead MSVC thinks this is
- * ::MessageBox and rewrites the symbol as MessageBoxA, which results in an
- * unresolved external and conflict with the Win32 API :(.
- *
- * Revision 1.25 2003/05/30 12:56:50 dicuccio
- * Converted code to use MessageBox() instead of
- * fl_ask()/fl_message()/fl_alert()
- *
- * Revision 1.24 2003/05/09 14:29:35 dicuccio
- * Remember to update all docmanager views after deleting a document. Changed
- * log messaged for deleted document
- *
- * Revision 1.23 2003/05/08 20:10:24 dicuccio
- * Added LOG_POST for unloading documents
- *
- * Revision 1.22 2003/05/08 16:18:59 dicuccio
- * Changed CFastMutex --> CMutex - guards against recursive locking
- *
- * Revision 1.21 2003/04/30 13:53:14 dicuccio
- * Made access of CDocManager internals thread safe
- *
- * Revision 1.20 2003/04/29 14:37:17 dicuccio
- * Removed old typing system
- *
- * Revision 1.19 2003/04/24 16:27:09 dicuccio
- * Added new marshalling functions for creating documents from given object
- * components.
- *
- * Revision 1.18 2003/04/16 11:38:13 dicuccio
- * Reordered shutdown procedure - clear current doc manager views first
- *
- * Revision 1.17 2003/03/17 14:49:54 dicuccio
- * Removed header file dependency on view.hpp
- *
- * Revision 1.16 2003/03/10 22:58:51 kuznets
- * iterate -> ITERATE
- *
- * Revision 1.15 2003/02/20 19:49:54 dicuccio
- * Created new plugin architecture, based on ASN.1 spec. Moved GBENCH frameowrk
- * over to use new plugin architecture.
- *
- * Revision 1.14 2003/02/05 19:50:52 dicuccio
- * Wrapped creation of a data loader in a try{}...catch(){} to avoid fatal
- * exceptions if there is no network available.
- *
- * Revision 1.13 2003/01/15 21:07:52 dicuccio
- * Added CSettings member variable. Removed a number of dead _TRACE()
- * statements
- *
- * Revision 1.12 2003/01/13 13:10:06 dicuccio
- * Namespace clean-up. Retired namespace gui -> converted all to namespace
- * ncbi. Moved all FLUID-generated code into namespace ncbi.
- *
- * Revision 1.11 2003/01/10 17:36:41 dicuccio
- * Changed TPluginList --> TPlugins
- *
- * Revision 1.10 2002/12/30 17:53:26 dicuccio
- * Modified function of ReleaseDocument() - don't call UpdateAllViews() (this
- * function is called from a menu callback; we delay this call to avoid
- * inadvertently returning to a destroyed object).
- *
- * Revision 1.9 2002/12/20 19:16:49 dicuccio
- * Better handling of document release - must free views before releasing
- * document; also, notify all views and documents of a release through a new
- * event
- *
- * Revision 1.8 2002/12/12 15:17:30 dicuccio
- * Added a selection buffer specific to the document manager - provides global
- * selections
- *
- * Revision 1.7 2002/11/29 16:34:59 dicuccio
- * Reformatted tabs -> spaces
- *
- * Revision 1.6 2002/11/29 16:03:06 dicuccio
- * Removed a bunch of dead code. Modified the use of NCBI_THROW to be correct
- * and avoid compile errors on Windows.
- *
- * Revision 1.5 2002/11/25 20:53:05 dicuccio
- * Changed singleton implementation of CDocManager to pure static implementation
- * (more in line with rest of framework)
- *
- * Revision 1.4 2002/11/14 16:15:48 dicuccio
- * Removed unecessary _TRACE() macros
- *
- * Revision 1.3 2002/11/09 20:50:09 dicuccio
- * Changed IDocument::x_GetViews() (non-const) and x_AttachView() from private
- * to public.
- *
- * Revision 1.2 2002/11/07 18:38:21 dicuccio
- * Changed the auto_ptr<> implementation to be a static auto_ptr<>.
- * Changed code to use ERR_POST() and _TRACE().
- *
- * Revision 1.1 2002/11/06 15:49:47 dicuccio
- * Initial revision
- *
- * ===========================================================================
- */