posts - 311, comments - 0, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

(地基工)对象池管理类

Posted on 2011-10-27 10:25 点点滴滴 阅读(324) 评论(0)  编辑 收藏 引用
#ifndef WS_OBJECTPOOL_H_
#define WS_OBJECTPOOL_H_

#include 
"WSDataObject.h"
#include 
"templates\WSSmartAssert.h"
#include 
"WSTaggedListenerManager.h"
#include 
"WSParamHolder.h"
#include 
"WSAny.h"
namespace WS {
    typedef ParamHolder ObjectCreationParameters;
    
class ObjectPool;
    
//class DataObjectManager;
    class DataObject;
    
class PoolableObjectManager;
    
enum PoolableObjectType {
        POT_NONE,
        POT_SCENEMANAGER,
        POT_SCENENODE,
        POT_LIGHT,
        POT_TERRAINGROUP,
        POT_TERRAINPAGE,
        POT_ADGRAPHICENTITY,  
//高级图形实体,相当于Ogre::Entity
        POT_PARTICLESYSTEM,
        POT_SPECIALEFFECT,
        POT_VIEWPORT,
        POT_CAMERA,
        POT_SKELETON,
        POT_UNITENTITY,
        POT_PLAY,
        POT_ITEM,
        POT_CREATURE,
    };
    typedef 
set<PoolableObjectType>::type PoolableObjectTypeSet;
    
class _WSExport PoolableObject
    {
        friend 
class ObjectPool;
    
public:
        
enum PoolableObjectState  {PO_IDLE, PO_USE,};

        PoolableObject();
        
virtual ~PoolableObject();
        ObjectPool
* getObjectPool() {return mObjectPool;}
        
void setObjectPool(ObjectPool* val){mObjectPool = val;}
        
        
virtual bool getIsAutoRelease() const { return mIsAutoRelease; }
        
virtual void setIsAutoRelease(bool isAutoRelease) { mIsAutoRelease = isAutoRelease; }

        
virtual Real getAutoReleaseTime() const { return mAutoReleaseTime; };
        
virtual void setAutoReleaseTime(Real autoReleaseTime) { mAutoReleaseTime = autoReleaseTime; };
        
virtual void update(const Real timeElapsed);

        
virtual bool getNeedTouch() const { return mNeedTouch; }
        
virtual void setNeedTouch(bool needTouch) { mNeedTouch = needTouch; }

        
virtual void unUse(){mState = PO_IDLE;}
        
virtual const int getState(){return mState;}

        
virtual PoolableObjectType getPoolableObjectType() const { return mPoolableObjectType; }
        
virtual void setPoolableObjectType(PoolableObjectType poolableObjectType) { mPoolableObjectType = poolableObjectType; }

        
virtual void touch() { mUnTouchTime = 0; }    
        
virtual  const String& getPoolableObjectID() constreturn mPoolableObjectID; }
        
virtual void setPoolableObjectID (const String &poolableObjectID){ mPoolableObjectID = poolableObjectID; }
        
virtual String getClassName(){ return "";};

        
//此属性可以给编辑器交互的时候使用
        virtual PropertyValueMap* getPropertyValueParams() const { return mPropertyValueParams; }
        
virtual void setPropertyValueParams(PropertyValueMap *propertyValueParams) { mPropertyValueParams = propertyValueParams; }    


        
//释放自己
        virtual void releaseSelf();
        
void setPoolableObjectManager(PoolableObjectManager *poolableObjectManager) { mPoolableObjectManager = poolableObjectManager; }
        PoolableObjectManager 
* getPoolableObjectManager() const { return mPoolableObjectManager;}
    
protected:

        
virtual void _onBorrowed();
        
virtual void _onReturned();
    
protected:
        ObjectPool
* mObjectPool;
        
bool mIsAutoRelease;
        Real mAutoReleaseTime;    
        PoolableObjectState mState;
        
bool mNeedTouch;
        Real mUnTouchTime;
        String mPoolableObjectID;
        PoolableObjectType mPoolableObjectType;

        PropertyValueMap 
*mPropertyValueParams;
        
bool mIsTriggerCreate;
        PoolableObjectManager 
*mPoolableObjectManager;
    
private:
        
//int _addRef();
        
//int _release();
        
//int mUseCount;
    };

    
class _WSExport PoolableObjectFactory
    {
    
public:
        friend 
class ObjectPool;
        
//typedef std::vector<PoolableObject*> PoolableObjectVector;
    public:
        
virtual ~PoolableObjectFactory();
        
virtual PoolableObject* makeObject() = 0;
        
virtual void destroyObject(PoolableObject* obj) {delete obj; };
        
//bool validateObject(PoolableObject* obj){return false;}
        
//void activateObject(PoolableObject* obj){}
        
//void passivateObject(PoolableObject* obj){}
    
//protected:
    
//    //void addObject(PoolableObject* obj);
    
//protected:
    
//    PoolableObjectVector mPoolableObjects;
    };
    
    typedef map
<String, PoolableObjectFactory*>::type PoolableObjectFactoryList;


    
class _WSExport PoolableObjectFactoryManager: public Singleton<PoolableObjectFactoryManager>
    {
    
public:
        PoolableObjectFactoryManager();
        
virtual ~PoolableObjectFactoryManager();
        
static PoolableObjectFactoryManager* getSingletonPtr(void);        
        
static PoolableObjectFactoryManager& getSingleton(void);
        
void addFactory(const String &name, PoolableObjectFactory* factory);
        
void removeFactory(const String &name);
        PoolableObjectFactory 
* getFactory(const String &name);
    
protected:
        PoolableObjectFactoryList mFactorys;
        
    };

    
class _WSExport ObjectPool
    {
    
public:
        typedef deque
<PoolableObject*>::type PoolableObjectQueue;
    
public:
        ObjectPool(PoolableObjectFactory
* factory = NULL);
        
virtual ~ObjectPool();
        
virtual PoolableObject* borrowObject(PoolableObjectManager *poolableObjectManager = NULL);
        
virtual void returnObject(PoolableObject* obj);        
//         int getNumIdle();
//         int getNumActive();
//         void addObject();        
//         void clear();
//         void close();
        void setFactory(PoolableObjectFactory* factory);
        
void update(const Real);
        
void destoryAllPoolableObjects();
    
protected:        
        PoolableObjectFactory
* mFactory;
        PoolableObjectQueue mPoolableObjects;        
        
//PoolableObjectQueue mIdlePoolableObjects;        
        
//PoolableObjectQueue mUsedPoolableObjects;
        
//PoolableObjectQueue mReturnPoolableObjects;
    };
    typedef map
<String, ObjectPool*>::type ObjectPoolList;    

    
class _WSExport ObjectPoolManager: public Singleton<ObjectPoolManager>
    {
    
public:
        ObjectPoolManager();
        
virtual ~ObjectPoolManager();
        
static ObjectPoolManager* getSingletonPtr(void);        
        
static ObjectPoolManager& getSingleton(void);

        
void addObjectPool(const String &className, ObjectPool* objectPool);
        
void removeObjectPool(const String &className);
        ObjectPool 
* getObjectPool(const String &className);
        
void destoryAllPoolableObjects();
    
protected:
        ObjectPoolList mObjectPools;
    };


    
class _WSExport PoolableObjectManagerListener
    {
    
public:
        
enum MethodType{
            MT_CREATE,
            MT_INIT,
            MT_DESTROY,
            MT_MODIFY,
        };
        
virtual ~PoolableObjectManagerListener() {}
        
virtual void onObjectCreated(PoolableObject*const ObjectCreationParameters& = ObjectCreationParameters()) {}
        
virtual void onObjectInitialized(PoolableObject*const ObjectCreationParameters& = ObjectCreationParameters()) {}
        
virtual void onDestroyObject(PoolableObject*) {}
        
virtual void onObjectModify(PoolableObject*const ObjectCreationParameters& = ObjectCreationParameters()) {}        
    };

//PoolableObjectManager可以存放任意类型的对象

    
class _WSExport PoolableObjectManager: public ListenerManager<PoolableObjectManagerListener>
    {
    
public:
        friend 
class PoolableObject;
        PoolableObjectManager(
const String &className = String(""), DataObjectManager *dataObjectManager = NULL);        
        
//PoolableObjectManager(ObjectPool* defaultObjectPool, DataObjectManager *dataObjectManager);

        
        
virtual ~PoolableObjectManager();

        
virtual void initObjectFromDataObject(PoolableObject* poolableObject, DataObject *dataObject) = 0;
        
virtual void initObjectFromDataObject(PoolableObject* poolableObject, const String& dataObjectID);

        
virtual PoolableObject* borrowObject();
        
virtual PoolableObject* borrowObjectByClassName(const String &className = "");        
        
virtual PoolableObject* borrowObjectByID(const String& poolableObjectID, const String &className = "");        
        
virtual PoolableObject* borrowObject(const String& dataObjectID,const String& poolableObjectID, const String &className = "");
        
virtual PoolableObject* borrowObject(DataObject *dataObject);
        
virtual PoolableObject* borrowObject(const String& dataObjectID);
        
virtual void returnObject(PoolableObject* obj);
        
virtual void returnObject(const String &poolableObjectID);
        
virtual void returnAllObjects();
        
virtual void delayReturn(PoolableObject* obj);
        
virtual void update(const Real);
    
public:
        typedef map
<String, PoolableObject*>::type PoolableObjectList;
        typedef MapIterator
<PoolableObjectList> PoolableObjectIterator;
        PoolableObjectIterator getPoolableObjectIterator(
void) {
            
return PoolableObjectIterator(mPoolableObjects.begin(), mPoolableObjects.end());
        }
        
const PoolableObjectList& getPoolableObjects() const { return mPoolableObjects; }
        
virtual PoolableObject* getPoolableObject(const String &poolableObjectID);

        
bool hasPoolableObject(const String& poolableObjectID);
    
public:
        
//void addFactory(const String &name, PoolableObjectFactory* factory);
        
//void removeFactory(const String &name);
        void addObjectPool(const String &className, ObjectPool* objectPool = NULL);
        
void removeObjectPool(const String &className);
    
protected:
        
//virtual PoolableObject* makeObject(){ return factory->makeObject();    }
        
//virtual PoolableObject* makeObjectFromDataObject(DataObject *dataObject){return factory->makeObjectFromDataObject(dataObject);}
        
//virtual PoolableObjectFactory* getFactory() { return mFactory; };
        
//virtual ObjectPool* getObjectPool() {return mObjectPool;};
        typedef ListenerManager<PoolableObjectManagerListener> listener_mgr_type;
        
void listeners_onObjectCreated(PoolableObject* obj,const ObjectCreationParameters& params = ObjectCreationParameters());
        
void listeners_onObjectInitialized(PoolableObject* obj,const ObjectCreationParameters& params = ObjectCreationParameters());
        
void listeners_onDestroyObject(PoolableObject* obj);
        String getNextID();
    
protected:    
        int32 mIDCounter;
        
//PoolableObjectFactory* mDefaultFactory; 
        
//PoolableObjectFactoryList mFactorys;

        ObjectPool
* mDefaultObjectPool;    //=mObjectPools[0]
        ObjectPoolList mObjectPools;

        DataObjectManager 
*mDataObjectManager;


        ObjectPoolManager 
*mObjectPoolManager;
        
    
private:
        PoolableObjectList mPoolableObjects;

    
    
public:    
        typedef vector
<PoolableObjectManagerListener*>::type PoolableObjectManagerListenerList;
        
static void addSingletonListener(PoolableObjectManagerListener* listener);
        
static void removeSingletonListener(PoolableObjectManagerListener* listener);    
        
static PoolableObjectManagerListenerList& getSingletonListeners();
        
static bool hasSingletonListeners() { return mSingletonListeners.size() > 0; }
        
static void triggerSingletonListeners(PoolableObject* obj, PoolableObjectManagerListener::MethodType methodType, const ObjectCreationParameters& params = ObjectCreationParameters());
    
protected:

        
//全局监听器,可以用于和编辑器的接口
        static PoolableObjectManagerListenerList mSingletonListeners;
    };



#endif
#include <WSPrerequisites.h>
#include 
"WSObjectPool.h"
#include 
"WSDataObject.h"
#include 
"WSStringConverter.h"
#include 
"WSUniqueName.h"

namespace WS
{
    PoolableObjectManager::PoolableObjectManagerListenerList PoolableObjectManager::mSingletonListeners;
    PoolableObject::PoolableObject()
        :mObjectPool(
0)
        
//,mUseCount(0)
        ,mIsAutoRelease(false)
        ,mAutoReleaseTime(FLT_MAX)
        ,mState(PO_IDLE)
        ,mNeedTouch(
false)
        ,mUnTouchTime(
0.0f)
        ,mPoolableObjectID(uniqueName::create( 
"PO" ))
        ,mPoolableObjectType(POT_NONE)
        ,mIsTriggerCreate(
false)
        ,mPoolableObjectManager(NULL)
    {

    }

    PoolableObject::
~PoolableObject()
    {        
        PoolableObjectManager::triggerSingletonListeners(
this, PoolableObjectManagerListener::MT_DESTROY);
    }

    
void PoolableObject::releaseSelf()
    {
        
if (mPoolableObjectManager)
            mPoolableObjectManager
->returnObject(this);
    }
    
//int PoolableObject::_addRef()
    
//{
    
//    ++mUseCount;
    
//    return mUseCount;
    
//}

    
//int PoolableObject::_release()
    
//{
    
//    if (mUseCount > 0)
    
//    {
    
//        --mUseCount;
    
//    }
    
//    
    
//    if ((mUseCount == 0) && mObjectPool)
    
//    {
    
//        _onReturned();            
    
//    }
    
//    return mUseCount;
    
//}

    
void PoolableObject::update(const Real timeElapsed)
    {
        
if (!mIsTriggerCreate)
        {
            PoolableObjectManager::triggerSingletonListeners(
this, PoolableObjectManagerListener::MT_CREATE);
            mIsTriggerCreate 
= true;
        }
        
        
if (mIsAutoRelease)
        {
            mAutoReleaseTime 
-= timeElapsed;
            
if (mAutoReleaseTime <= 0)
            {
                mObjectPool
->returnObject(this);
                
return;
            }
        }

        
if (mNeedTouch)
        {
            mUnTouchTime 
+= timeElapsed;
            
if (mUnTouchTime > 300 )
            {
                mObjectPool
->returnObject(this);
                
return;
            }
        }
        
    }

    
void PoolableObject::_onBorrowed()
    {
        mState 
= PO_USE;
        mAutoReleaseTime 
= FLT_MAX;
        mIsAutoRelease 
= false;
        mNeedTouch 
= false;
        mUnTouchTime 
= 0.0f;
    }

    
void PoolableObject::_onReturned() 
    {
        mState 
= PO_IDLE;
        PoolableObjectManager::triggerSingletonListeners(
this, PoolableObjectManagerListener::MT_DESTROY);
    }

    PoolableObjectFactory::
~PoolableObjectFactory()
    {
    }


    PoolableObject
* ObjectPool::borrowObject(PoolableObjectManager *poolableObjectManager)
    {

        PoolableObject 
*pPoolableObject = NULL;
        
for (PoolableObjectQueue::iterator iter = mPoolableObjects.begin(); iter != mPoolableObjects.end(); ++iter)
        {
            
if ((*iter)->mState == PoolableObject::PO_IDLE)
            {
                pPoolableObject 
= *iter;
            }
        }

        
if(!pPoolableObject)
        {
            pPoolableObject 
= mFactory->makeObject();
            pPoolableObject
->setObjectPool(this);        
            mPoolableObjects.push_back(pPoolableObject);
        }
        pPoolableObject
->setPoolableObjectManager(poolableObjectManager);
        pPoolableObject
->_onBorrowed();
        
return pPoolableObject;
    }

    ObjectPool::ObjectPool(PoolableObjectFactory
* factory)
        :mFactory(factory)
    {
        
    }

    ObjectPool::
~ObjectPool()
    {
        
for (PoolableObjectQueue::iterator iter = mPoolableObjects.begin(); iter != mPoolableObjects.end(); ++iter)
        {
            mFactory
->destroyObject(*iter);
        }
        mPoolableObjects.clear();
        
//mIdlePoolableObjects.clear();
        
//mUsedPoolableObjects.clear();
        
//mReturnPoolableObjects.clear();
    }

    
void ObjectPool::returnObject(PoolableObject* obj)
    {    
        obj
->_onReturned();
        
//mReturnPoolableObjects.push_back(obj);

//         PoolableObjectQueue::iterator iter = find(mUsedPoolableObjects.begin(), mUsedPoolableObjects.end(), obj);
//         if (iter != mUsedPoolableObjects.end())
//         {
//             mUsedPoolableObjects.erase(iter);
//         }
//         mIdlePoolableObjects.push_back(obj);
    }

    
void ObjectPool::update(const Real timeElapsed)
    {
        
//for (PoolableObjectQueue::iterator iter = mReturnPoolableObjects.begin(); iter != mReturnPoolableObjects.end(); ++iter)
        
//{
        
//    PoolableObjectQueue::iterator iter1 = find(mUsedPoolableObjects.begin(), mUsedPoolableObjects.end(), *iter);
        
//    if (iter1 != mUsedPoolableObjects.end())
        
//    {
        
//        mIdlePoolableObjects.push_back(*iter1);
        
//        mUsedPoolableObjects.erase(iter1);
        
//        
        
//    }
        
//}
        
//mReturnPoolableObjects.clear();

        
for (PoolableObjectQueue::iterator iter = mPoolableObjects.begin(); iter != mPoolableObjects.end(); ++iter)
        {
            
if ((*iter)->mState == PoolableObject::PO_USE)
            {
                (
*iter)->update(timeElapsed);                
            }
        }
    }

    
void ObjectPool::setFactory(PoolableObjectFactory* factory)
    {
        mFactory 
= factory;
    }



    
void ObjectPool::destoryAllPoolableObjects()
    {
        PoolableObjectQueue::reverse_iterator it 
= mPoolableObjects.rbegin();
        PoolableObjectQueue::reverse_iterator itEnd 
= mPoolableObjects.rend();
        
for (; it != itEnd; ++it)
        {
            mFactory
->destroyObject(*it); //returnObject(it->second);
        }
        mPoolableObjects.clear();
    }

    template
<> ObjectPoolManager* Singleton<ObjectPoolManager>::ms_Singleton = 0;
    ObjectPoolManager
* ObjectPoolManager::getSingletonPtr(void)
    {
        
return ms_Singleton; 
    }
    ObjectPoolManager
& ObjectPoolManager::getSingleton(void)
    {
        assert( ms_Singleton );  
return ( *ms_Singleton );
    }

    ObjectPoolManager::ObjectPoolManager()
    {

    }

    ObjectPoolManager::
~ObjectPoolManager()
    {
        
//因为有可能对象池是不同的Dll创建的,不能在这里统一释放
        
//for (ObjectPoolList::iterator it = mObjectPools.begin(); it != mObjectPools.end(); ++it)
        
//{
        
//    delete it->second;
        
//}
    }

    
void ObjectPoolManager::addObjectPool(const String &className, ObjectPool* objectPool)
    {
        mObjectPools[className] 
= objectPool;
    }

    
void ObjectPoolManager::removeObjectPool(const String &className)
    {
        mObjectPools.erase(mObjectPools.find(className));
    }

    ObjectPool
* ObjectPoolManager::getObjectPool(const String &className)
    {
        
return mObjectPools[className];
    }

    
void ObjectPoolManager::destoryAllPoolableObjects()
    {
        ObjectPoolList::reverse_iterator it 
= mObjectPools.rbegin();
        ObjectPoolList::reverse_iterator itEnd 
= mObjectPools.rend(); 
        
for (; it != itEnd; ++it)
        {
            it
->second->destoryAllPoolableObjects();
        }
    }

    PoolableObjectManager::PoolableObjectManager(
const String &className, DataObjectManager *dataObjectManager):mIDCounter(0), 
        mDefaultObjectPool(NULL), mDataObjectManager(dataObjectManager), mObjectPoolManager(ObjectPoolManager::getSingletonPtr())
    {
        
if (className != "")
        {
            addObjectPool(className, mObjectPoolManager
->getObjectPool(className));
        }
    }

    
//PoolableObjectManager::PoolableObjectManager(ObjectPool* defaultObjectPool, DataObjectManager *dataObjectManager):
    
//    mIDCounter(0), mDefaultObjectPool(defaultObjectPool), mDataObjectManager(dataObjectManager)
    
//{         
    
//    //if (defaultFactory)
    
//    //{
    
//    //    mDefaultObjectPool->setFactory(mDefaultFactory);
    
//    //    mFactorys["Default"]
    
//    //}
    
//    
    
//}

    PoolableObjectManager::
~PoolableObjectManager()
    {
        returnAllObjects();
    }

    PoolableObject
* PoolableObjectManager::borrowObjectByID(const String& poolableObjectID, const String &className)
    {
        PoolableObject
* obj;
        
if (className == "")
        {
            obj 
= mDefaultObjectPool->borrowObject();
        }
        
else
        {    
            ObjectPoolList::iterator it 
= mObjectPools.find(className);
            
if (it == mObjectPools.end())
            {
                ObjectPool 
*objPool = mObjectPoolManager->getObjectPool(className);
                addObjectPool(className, objPool);
                obj 
= objPool->borrowObject(this);
            }
            
else
            {
                obj 
= it->second->borrowObject(this);
            }            
        }
        
if (poolableObjectID == "" || obj == NULL)
            
return NULL;
        mPoolableObjects[poolableObjectID] 
= obj;
        obj
->setPoolableObjectID(poolableObjectID);
        listeners_onObjectCreated(obj, ObjectCreationParameters());
        
return obj;
    }

    PoolableObject
* PoolableObjectManager::borrowObject(const String& dataObjectID,const String& poolableObjectID, const String &className)
    {
        PoolableObject
* obj = borrowObjectByID(poolableObjectID,className);
        initObjectFromDataObject(obj, dataObjectID);
        
return obj;
    }

    PoolableObject
* PoolableObjectManager::borrowObject()
    {
        
return borrowObjectByID(getNextID());
    }

    PoolableObject
* PoolableObjectManager::borrowObjectByClassName(const String &className)    
    {
         
return borrowObjectByID(getNextID(), className);
    }

    PoolableObject
* PoolableObjectManager::borrowObject(DataObject *dataObject)
    {
        PoolableObject
* obj = borrowObject();
        initObjectFromDataObject(obj, dataObject);
        
return obj;
    }

    PoolableObject
* PoolableObjectManager::borrowObject(const String& dataObjectID)
    {
        PoolableObject
* obj = borrowObject();
        initObjectFromDataObject(obj, dataObjectID);
        
return obj;
    }
    
    
void  PoolableObjectManager::returnObject(PoolableObject* obj)
    {    
        
if (!obj) return;
        listeners_onDestroyObject(obj);
        PoolableObjectList::iterator it 
= mPoolableObjects.find(obj->getPoolableObjectID());
        
if (it == mPoolableObjects.end()) return;
        mPoolableObjects.erase(it);        
        
        mObjectPools.find(obj
->getClassName())->second->returnObject(obj);
        
        
//mDefaultObjectPool->returnObject(obj);    
        
    }
    
    
void PoolableObjectManager::delayReturn(PoolableObject* obj)
    {
        obj
->unUse();
    }

    
void PoolableObjectManager::returnObject(const String &poolableObjectID)
    {
        returnObject(mPoolableObjects[poolableObjectID]);        
    }

    
void PoolableObjectManager::returnAllObjects()
    {
         PoolableObjectList::iterator it 
= mPoolableObjects.begin();
        PoolableObjectList::iterator itEnd 
= mPoolableObjects.end(); 
        
for (; it != itEnd; ++it)
        {
            listeners_onDestroyObject(it
->second);
            mObjectPools.find(it
->second->getClassName())->second->returnObject(it->second);
        }
        mPoolableObjects.clear();
    }

    
void PoolableObjectManager::update(const Real timeElapsed)
    {
        PoolableObjectList::iterator it 
= mPoolableObjects.begin();
        PoolableObjectList::iterator itEnd 
= mPoolableObjects.end();
        
for (; it != itEnd; ++it )
        {
            
if ( NULL == it->second)
                
continue;
            it
->second->update(timeElapsed);            
        }

        
for (it = mPoolableObjects.begin(); it != mPoolableObjects.end();)
        {
            
if ( NULL == it->second)
                
continue;
            PoolableObject
* pObject = it->second;
            
if (pObject->getState() == PoolableObject::PO_IDLE)
            {
                listeners_onDestroyObject(pObject);                
                it 
= mPoolableObjects.erase(it);    
                mObjectPools.find(pObject
->getClassName())->second->returnObject(pObject);                            
            }    
            
else
            {
                it 
++;
            }
        }
        
//mDefaultObjectPool->update(timeElapsed);
    }
    
    
void PoolableObjectManager::initObjectFromDataObject(PoolableObject* poolableObject,const String& dataObjectID)
    {
        DataObjectPtr dataObject 
= mDataObjectManager->getByID(dataObjectID);
        
if (!dataObject.get())
        {
            WS_EXCEPT2(Exception2::ERR_ITEM_NOT_FOUND, 
                mDataObjectManager
->getResourceType() + " with ID " + dataObjectID + "' not Found!"
                
"PoolableObjectManager::initObjectFromDataObject");
        }
        initObjectFromDataObject(poolableObject, dataObject.
get());

        listeners_onObjectInitialized(poolableObject, ObjectCreationParameters());
    }

    String PoolableObjectManager::getNextID()
    {
        
return "AutoID" + StringConverter::toString(mIDCounter++);        
    }

    PoolableObject
* PoolableObjectManager::getPoolableObject(const String& poolableObjectID)
    {
        PoolableObjectList::iterator it 
= mPoolableObjects.find(poolableObjectID);
        
if (it != mPoolableObjects.end())
            
return it->second;
        
else
            
return NULL;
    }

    
bool PoolableObjectManager::hasPoolableObject(const String& poolableObjectID)
    {
        
return mPoolableObjects.find(poolableObjectID) != mPoolableObjects.end();        
    }

    
void PoolableObjectManager::listeners_onObjectCreated(PoolableObject* obj, const ObjectCreationParameters& params)
    {
        assert( obj );
        
if (!obj)
            
return;
        ConstVectorIterator
<listener_mgr_type::ListenerList> it(mListeners);
        
while (it.hasMoreElements())
            it.getNext()
->onObjectCreated(obj,params);

        
//PoolableObjectManagerListenerList::iterator it1 = mSingletonListeners.begin();
        
//PoolableObjectManagerListenerList::iterator itEnd1 = mSingletonListeners.end();
        
//for (; it1 != itEnd1; ++it1)
        
//{
        
//    (*it1)->onObjectCreated(obj,params);
        
//}
    }
    
void PoolableObjectManager::listeners_onObjectInitialized(PoolableObject* obj, const ObjectCreationParameters& params)
    {
        assert( obj );
        
if (!obj)
            
return;
        ConstVectorIterator
<listener_mgr_type::ListenerList> it(mListeners);
        
while (it.hasMoreElements())
            it.getNext()
->onObjectInitialized(obj, params);

        
//PoolableObjectManagerListenerList::iterator it1 = mSingletonListeners.begin();
        
//PoolableObjectManagerListenerList::iterator itEnd1 = mSingletonListeners.end();
        
//for (; it1 != itEnd1; ++it1)
        
//{
        
//    (*it1)->onObjectInitialized(obj,params);
        
//}
    }
    
void PoolableObjectManager::listeners_onDestroyObject(PoolableObject* obj)
    {
        assert( obj );
        
if (!obj)
            
return;
        ConstVectorIterator
<listener_mgr_type::ListenerList> it(mListeners);
        
while (it.hasMoreElements())
            it.getNext()
->onDestroyObject(obj);

        
//PoolableObjectManagerListenerList::iterator it1 = mSingletonListeners.begin();
        
//PoolableObjectManagerListenerList::iterator itEnd1 = mSingletonListeners.end();
        
//for (; it1 != itEnd1; ++it1)
        
//{
        
//    (*it1)->onDestroyObject(obj);
        
//}
    }

    
//void PoolableObjectManager::addFactory(const String &name, PoolableObjectFactory* factory)
    
//{
    
//    mFactorys[name] = factory;
    
//}

    
//void PoolableObjectManager::removeFactory(const String &name)
    
//{
    
//    mFactorys.erase(mFactorys.find(name));
    
//}

    
void PoolableObjectManager::addObjectPool(const String &className, ObjectPool* objectPool)
    {
        
if (NULL == objectPool)
            objectPool 
= ObjectPoolManager::getSingleton().getObjectPool(className);
        
if (mObjectPools.size() == 0)
            mDefaultObjectPool 
= objectPool;
        mObjectPools[className] 
= objectPool;
    }

    
void PoolableObjectManager::removeObjectPool(const String &className)
    {
        mObjectPools.erase(mObjectPools.find(className));
    }

    
void PoolableObjectManager::addSingletonListener(PoolableObjectManagerListener* listener)
    {
        mSingletonListeners.push_back(listener);
    }

    
void PoolableObjectManager::removeSingletonListener(PoolableObjectManagerListener* listener)
    {
        PoolableObjectManagerListenerList::iterator it 
= find(mSingletonListeners.begin(), mSingletonListeners.end(), listener);
        mSingletonListeners.erase(it);
    }

    PoolableObjectManager::PoolableObjectManagerListenerList
& PoolableObjectManager::getSingletonListeners()
    {
        
return mSingletonListeners;
    }

    
void PoolableObjectManager::triggerSingletonListeners(PoolableObject* obj, PoolableObjectManagerListener::MethodType methodType, const ObjectCreationParameters& params)
    {
        
if (!PoolableObjectManager::hasSingletonListeners()) 
            
return;
        PoolableObjectManagerListenerList::iterator it1 
= mSingletonListeners.begin();
        PoolableObjectManagerListenerList::iterator itEnd1 
= mSingletonListeners.end();
        
for (; it1 != itEnd1; ++it1)
        {
            
switch (methodType) 
            {
            
case PoolableObjectManagerListener::MT_CREATE:
                (
*it1)->onObjectCreated(obj, params);
                
break;
            
case PoolableObjectManagerListener::MT_INIT:
                (
*it1)->onObjectInitialized(obj, params);
                
break;
            
case PoolableObjectManagerListener::MT_DESTROY:
                (
*it1)->onDestroyObject(obj);
                
break;
            
case PoolableObjectManagerListener::MT_MODIFY:
                (
*it1)->onObjectModify(obj, params);
                
break;
                
            }

            
        }
    }


    template
<> PoolableObjectFactoryManager* Singleton<PoolableObjectFactoryManager>::ms_Singleton = 0;
    PoolableObjectFactoryManager
* PoolableObjectFactoryManager::getSingletonPtr(void)
    {
        
return ms_Singleton; 
    }
    PoolableObjectFactoryManager
& PoolableObjectFactoryManager::getSingleton(void)
    {
        assert( ms_Singleton );  
return ( *ms_Singleton );
    }

    PoolableObjectFactoryManager::PoolableObjectFactoryManager()
    {

    }

    PoolableObjectFactoryManager::
~PoolableObjectFactoryManager()
    {
        
//因为有可能工厂是不同的Dll创建的,不能在这里统一释放
        
//for (PoolableObjectFactoryList::iterator it = mFactorys.begin(); it != mFactorys.end(); ++it)
        
//{
        
//    delete it->second;
        
//}
    }

    
void PoolableObjectFactoryManager::addFactory(const String &name, PoolableObjectFactory* factory)
    {
        mFactorys[name] 
= factory;
    }

    
void PoolableObjectFactoryManager::removeFactory(const String &name)
    {
        mFactorys.erase(mFactorys.find(name));
    }

    PoolableObjectFactory
* PoolableObjectFactoryManager::getFactory(const String &name)
    {
        
return mFactorys[name];
    }

    


}
实现