⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resource_pool.hpp

📁 ncbi源码
💻 HPP
字号:
/* * =========================================================================== * PRODUCTION $Log: resource_pool.hpp,v $ * PRODUCTION Revision 1000.1  2004/04/13 17:22:55  gouriano * PRODUCTION PRODUCTION: UPGRADED [CATCHUP_003] Dev-tree R1.5 * PRODUCTION * =========================================================================== */#ifndef UTIL___RESOURCEPOOL__HPP#define UTIL___RESOURCEPOOL__HPP/*  $Id: resource_pool.hpp,v 1000.1 2004/04/13 17:22:55 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. * * =========================================================================== * * Author:  Anatoliy Kuznetsov *    General purpose resource pool. */#include <corelib/ncbistd.hpp>#include <vector>BEGIN_NCBI_SCOPE/** @addtogroup ResourcePool * * @{ *//// General purpose resource pool.////// Intended use is to store heavy reusable objects./// Pool frees all vacant objects only upon pools destruction./// Subsequent Get/Put calls does not result in objects reallocations and/// reinitializations. (So the prime target is performance optimization)./// Class is intentinally light weight and unsophisticated.template<class Value>class CResourcePool{public:     typedef Value                       TValue;    typedef Value*                      TValuePtr;    typedef vector<Value*>              TPoolList;public:    CResourcePool()    {}    ~CResourcePool()    {        ITERATE(typename TPoolList, it, m_FreeObjects) {            Value* v = *it;            delete v;        }    }    /// Get object from the pool.     ///    /// Pool makes no reinitialization or constructor     /// call and object is returned in the same state it was put.    /// If pool has no vacant objects, new is called to produce an object.    /// Caller is responsible for deletion or returning object back to the pool.    Value* Get()    {        Value* v;        if (m_FreeObjects.empty()) {            v = new Value;        } else {            typename TPoolList::iterator it = m_FreeObjects.end();            v = *(--it);            m_FreeObjects.pop_back();        }        return v;    }    /// Put object into the pool.     ///    /// Pool does not check if object is actually    /// originated in the very same pool. It's ok to get an object from one pool    /// and return it to another pool.    /// Method does NOT immidiately destroy the object v.     void Put(Value* v)    {        _ASSERT(v);        m_FreeObjects.push_back(v);    }    void Return(Value* v) { Put(v); }    /// Makes the pool to forget the object.    ///    /// Method scans the free objects list, finds the object and removes    /// it from the structure. It is important that the object is not    /// deleted and it is responsibility of the caller to destroy it.    ///    /// @return NULL if object does not belong to the pool or     ///    object's pointer otherwise.    Value* Forget(Value* v)    {        NON_CONST_ITERATE(typename TPoolList, it, m_FreeObjects) {            Value* vp = *it;            if (v == vp) {                m_FreeObjects.erase(it);                return v;            }        }        return 0;    }    /// Makes pool to forget all objects    ///    /// Method removes all objects from the internal list but does NOT    /// deallocate the objects.    void ForgetAll()    {        m_FreeObjects.clear();    }    /// Get internal list of free objects    TPoolList& GetFreeList() { return m_FreeObjects; }    /// Get internal list of free objects    const TPoolList& GetFreeList() const { return m_FreeObjects; }protected:    CResourcePool(const CResourcePool&);    CResourcePool& operator=(const CResourcePool&);protected:    TPoolList   m_FreeObjects;};/// Guard object. Returns object pointer to the pool upon destruction./// @sa CResourcePooltemplate<class Pool>class CResourcePoolGuard{public:    CResourcePoolGuard(Pool& pool, typename Pool::TValue* v)    : m_Pool(pool),      m_Value(v)    {}    ~CResourcePoolGuard()    {        m_Pool.Return(m_Value);    }private:    Pool&                     m_Pool;    typename Pool::TValue*    m_Value;};/* @} */END_NCBI_SCOPE/* * =========================================================================== * $Log: resource_pool.hpp,v $ * Revision 1000.1  2004/04/13 17:22:55  gouriano * PRODUCTION: UPGRADED [CATCHUP_003] Dev-tree R1.5 * * Revision 1.5  2004/03/10 16:51:09  kuznets * Fixed compilation problems (GCC) * * Revision 1.4  2004/03/10 16:16:48  kuznets * Add accessors to internal list of free objects * * Revision 1.3  2004/02/23 19:18:20  kuznets * +CResourcePool::Forget to manually remove objects from the pool. * * Revision 1.2  2004/02/17 19:06:59  kuznets * GCC warning fix * * Revision 1.1  2004/02/13 20:24:47  kuznets * Initial revision. CResourcePool implements light weight solution for pooling * of heavy weight objects (intended as optimization tool). * * * =========================================================================== */#endif  /* UTIL___RESOURCEPOOL__HPP */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -