msg_pool.hpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:7k
- /*
- * ===========================================================================
- * PRODUCTION $Log: msg_pool.hpp,v $
- * PRODUCTION Revision 1000.5 2004/04/16 16:54:00 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [CATCHUP_003] Dev-tree R1.15
- * PRODUCTION
- * ===========================================================================
- */
- #ifndef GUI_UTILS___MSG_POOL__HPP
- #define GUI_UTILS___MSG_POOL__HPP
- /* $Id: msg_pool.hpp,v 1000.5 2004/04/16 16:54:00 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: Lou Friedman
- *
- * File Description:
- * Implement a messging mechanism between instants of an object. The object
- * must inherit from CObject and implement a MessageEventHandler.
- */
- #include <corelib/ncbiobj.hpp>
- #include <algorithm>
- #include <vector>
- #include <list>
- #include <string>
- /** @addtogroup GUI_UTILS
- *
- * @{
- */
- BEGIN_NCBI_SCOPE
- ///////////////////////////////////////////////////////////////////////
- //
- // CMsgPool - Is a templated object that implement the messgaing
- // mechanism for object passed in to the template. The object
- // must inherit from CObject and implement a MessageEventHandler.
- // A message pool must have a sting name upon creation.
- template <class Object>
- class CMsgPool : public CObject
- {
- public:
- typedef CRef<Object> TMember;
- typedef list<TMember> TMemberList;
- // defautl ctor
- CMsgPool(const string& name)
- : m_Name(name) { }
- // retrieve the name of this pool
- void GetName(const string& name) const { m_Name = name; }
- string& SetName() { return m_Name; }
- // add a member to this pool
- void Join(Object& member)
- {
- typename TMemberList::iterator pos;
- pos = find(m_Members.begin(), m_Members.end(), CRef<Object>(&member) );
- if (pos == m_Members.end()) {
- m_Members.push_back(TMember(&member));
- }
- }
- // remove a member from this pool
- void Leave(Object& member)
- {
- typename TMemberList::iterator pos;
- pos = find(m_Members.begin(), m_Members.end(), TMember(&member));
- if (pos != m_Members.end()) {
- m_Members.erase(pos);
- }
- }
- // post a messgae to this pool
- template <class Msg>
- void PostMessage(Msg msg, const void *user_data)
- {
- NON_CONST_ITERATE(typename TMemberList, iter, m_Members) {
- (*iter)->MessageEventHandler(msg, user_data);
- }
- }
- private:
- string m_Name;
- TMemberList m_Members;
- };
- ///////////////////////////////////////////////////////////////////////
- //
- // CMsgPoolMgr is a templated object that manages the message pools for
- // passed in object.
- template <class Object>
- class CMsgPoolMgr : public CObject
- {
- public:
- typedef CMsgPool<Object> TPool;
- typedef CRef<TPool> TPoolRef;
- typedef list<TPoolRef> TPoolList;
- // create a message pool with a given name
- TPool* CreatePool(const string& name)
- {
- // if pool exists, return the pool
- if (TPool* pool = FindPool(name)) {
- return pool;
- }
- // create new pool
- TPoolRef pool (new TPool(name));
- m_Pools.push_back(pool);
- return pool.GetPointer();
- }
- // retrieve a message pool by name
- TPool* FindPool(const string& name)
- {
- TPoolRef pool = x_FindPool(name);
- return pool.GetPointer();
- }
- // delete a named message pool
- void DeletePool(const string& name)
- {
- TPoolRef pool = x_FindPool(name);
- m_Pools.remove(pool);
- }
- // clear all pools from this manager
- void Clear(void)
- {
- m_Pools.clear();
- }
- private:
- // our message pools
- TPoolList m_Pools;
- // internal retrieval function
- TPoolRef x_FindPool(const string& name)
- {
- NON_CONST_ITERATE(typename TPoolList, iter, m_Pools) {
- if (NStr::CompareCase((*iter)->SetName(), name) == 0) {
- return *iter;
- }
- }
-
- return null;
- }
-
- };
- END_NCBI_SCOPE
-
- /* @} */
- /*
- * ===========================================================================
- * $Log: msg_pool.hpp,v $
- * Revision 1000.5 2004/04/16 16:54:00 gouriano
- * PRODUCTION: UPGRADED [CATCHUP_003] Dev-tree R1.15
- *
- * Revision 1.15 2004/04/16 14:27:17 dicuccio
- * Added doxygen module tag
- *
- * Revision 1.14 2004/01/21 12:34:36 dicuccio
- * Removed unused variable
- *
- * Revision 1.13 2003/12/31 20:31:47 dicuccio
- * Renamed prototypes in CMessagePoolMgr
- *
- * Revision 1.12 2003/12/22 19:15:48 dicuccio
- * Pass const void* to PostMessage()
- *
- * Revision 1.11 2003/11/19 20:52:49 friedman
- * CreateMessagePool checks for an existing message pool and returns it if detected
- *
- * Revision 1.10 2003/11/14 18:03:17 ucko
- * Fixed CMsgPoolMgr::x_FindMessagePool now that CMsgPool::Name is {Get,Set}Name.
- *
- * Revision 1.9 2003/11/14 17:41:49 dicuccio
- * Added Clear() to the pool manager - called at shut down. Added code comments.
- * Changed include guard to match file location.
- *
- * Revision 1.8 2003/11/04 18:28:40 ucko
- * CMsgPoolMgr::x_FindMessagePool: return null rather than NULL, which
- * the compiler may not be willing to cast to CRef implicitly.
- *
- * Revision 1.7 2003/11/04 16:47:37 dicuccio
- * Fixed include guards. Use explicit values on return rather than implict conversion
- *
- * Revision 1.6 2003/11/04 13:54:51 dicuccio
- * Fixed compilation error on MSVC - ambiguous pointer<->CRef<> comparison
- *
- * Revision 1.5 2003/11/04 12:22:25 friedman
- * Code cleanup
- *
- * Revision 1.4 2003/10/20 18:50:02 friedman
- * Added typename to template<>::itearators to remove GCC 3.3.1 warnings
- *
- * Revision 1.3 2003/09/14 14:10:10 ucko
- * +<algorithm> for find<> (not already included when using GCC 2.95)
- *
- * Revision 1.2 2003/09/12 19:46:04 ucko
- * Tweak x_FindMessagePool to make GCC (and others?) happy.
- *
- * Revision 1.1 2003/09/12 17:38:40 friedman
- * Initial revision
- *
- * ===========================================================================
- */
- #endif // GUI_UTILS___MSG_POOL__HPP