CbkDispatcher.h
资源名称:cppapp.zip [点击查看]
上传用户:smh666999
上传日期:2007-01-14
资源大小:553k
文件大小:4k
源码类别:
BREW编程
开发平台:
Visual C++
- #ifndef _CBK_DISPATCHER_h_
- #define _CBK_DISPATCHER_h_
- #include "AEEError.h"
- #include "BrewVector.h"
- class IRelease
- {
- public:
- virtual void releaseResource() = 0;
- virtual int getUID() = 0;
- virtual ~IRelease() {};
- protected:
- IRelease(){};
- private:
- IRelease( const IRelease &value );
- const IRelease &operator = ( const IRelease &rhs );
- };
- class IExecute
- {
- public:
- virtual void onCbk() = 0;
- protected:
- IExecute(){};
- virtual ~IExecute() {};
- private:
- IExecute( const IExecute &value );
- const IExecute &operator = ( const IExecute &rhs );
- };
- class ResourceRegistry
- {
- public:
- void registerResource(IRelease* resource)
- {
- for(int i = resources_.size() - 1; i >= 0; --i)
- {
- if (resources_[i] && resources_[i] == resource)
- {
- return;
- }
- }
- resources_.append(resource);
- if (resources_.size() > swapLimit_)
- swap();
- }
- bool unregisterResource(IRelease* resource)
- {
- for(int i = resources_.size() - 1; i >= 0; --i)
- {
- IRelease* t = resources_[i];
- if (t && t == resource)
- {
- resources_.setAt(0,i) ;
- return true;
- }
- }
- return false;
- }
- IRelease* getRegistered(int uid)
- {
- for(int i = resources_.size() - 1; i >= 0; --i)
- {
- IRelease* t = resources_[i];
- if (t && (uid == t->getUID()))
- {
- return t;
- }
- }
- return 0;
- }
- ~ResourceRegistry() //unusable if using cppapp - manual destruction
- {
- unregisterResources();
- }
- ResourceRegistry(int swapLimit): swapLimit_(swapLimit)
- {}
- private:
- void unregisterResources()
- {
- for(int i=0, sz = resources_.size(); i < sz; ++i)
- {
- IRelease* t = (resources_[i]);
- if (t)
- {
- t->releaseResource();
- resources_.setAt(0,i) ;
- }
- }
- }
- void swap()
- {
- BrewPVector<IRelease> v;
- for(int i=0, sz = resources_.size(); i < sz; ++i)
- {
- if (resources_[i])
- {
- v.append(resources_[i]);
- }
- }
- BrewPVector<IRelease> resources_;
- resources_.ensureCapacity(v.size());
- for(int j=0, szj = v.size(); j < szj; ++j)
- {
- if (v[j])
- {
- resources_.append(v[j]);
- }
- }
- }
- private:
- BrewPVector<IRelease> resources_;
- int swapLimit_;
- private:
- ResourceRegistry( const ResourceRegistry &value );
- const ResourceRegistry &operator = ( const ResourceRegistry &rhs );
- };
- template <class P, class T, class U, class W, class R = ResourceRegistry>
- class CbkDispatcher : public IRelease, public IExecute
- {
- public:
- virtual void onCbk( )
- {
- p_();
- close();
- }
- virtual void releaseResource( )
- {
- delete this;
- }
- virtual int getUID( )
- {
- return W::getID() + 10;
- }
- static int getID()
- {
- return W::getID() + 10;
- }
- static int getDispatcher(IShell* shell, R* r, P& p, W* w)
- {
- CbkDispatcher* hd = 0;
- if (T::isReusable())
- {
- hd = static_cast<CbkDispatcher*>(r->getRegistered(CbkDispatcher::getID()));
- }
- if (!hd)
- {
- hd = new CbkDispatcher(shell, r, p);
- }
- int err = hd->initRequest(w);
- if (err != SUCCESS)
- {
- delete hd;
- }
- return err;
- }
- ~CbkDispatcher()
- {
- releaseResources();
- }
- private:
- CbkDispatcher( IShell* shell, R* r, P& p):
- shell_(shell),
- u_(0),
- p_(p),
- r_(r)
- {
- }
- void releaseResources()
- {
- releasePipe();
- r_->unregisterResource(this);
- }
- void releasePipe()
- {
- delete u_;
- u_ = 0;
- }
- void close() const
- {
- if (! T::isReusable())
- delete this;
- else
- u_->reset();
- }
- int initRequest(W* w)
- {
- int err = SUCCESS;
- bool isFresh = false;
- if (!u_)
- {
- isFresh = true;
- u_ = new U(shell_, this, w );
- if ((err = u_->init()) != SUCCESS)
- {
- releasePipe();
- return err;
- }
- }
- err = u_->initConnection();
- if (err != SUCCESS)
- {
- releasePipe();
- return err;
- }
- if (isFresh)
- r_->registerResource(this);
- return err;
- }
- private:
- IShell* shell_;
- U* u_;
- P& p_;
- R* r_;
- private:
- CbkDispatcher( const CbkDispatcher &Value );
- const CbkDispatcher & operator = ( const CbkDispatcher &Rhs );
- //CbkDispatcher(){};
- };
- #endif