/*------------------------------------------------------------------------ * (The MIT License) * * Copyright (c) 2008-2011 Rhomobile, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * http://rhomobile.com *------------------------------------------------------------------------*/ #pragma once #include "common/IRhoThreadImpl.h" namespace rho { namespace common { //From boost class CNonCopyable { protected: CNonCopyable() {} ~CNonCopyable() {} private: // emphasize the following members are private CNonCopyable( const CNonCopyable& ); const CNonCopyable& operator=( const CNonCopyable& ); }; template class CBaseAutoPointer //: public CNonCopyable { public: CBaseAutoPointer() { m_ptr = 0; } PTRTYPE* operator &(){ Close(); return &m_ptr; } void Set( PTRTYPE ptr ){ Close(); m_ptr = ptr; } PTRTYPE operator ->(){ return m_ptr; } operator PTRTYPE(){ return m_ptr; } virtual void FreePtr() = 0; void Close() { if ( m_ptr ) { FreePtr(); m_ptr = 0; } } PTRTYPE ReleasePtr() { PTRTYPE ptr = m_ptr; m_ptr = 0; return ptr; } protected: PTRTYPE m_ptr; }; template class CAutoPtr : public CBaseAutoPointer { public: CAutoPtr( PTRTYPE ptr ){ CBaseAutoPointer::Set(ptr); } CAutoPtr(){} ~CAutoPtr(){ CBaseAutoPointer::Close(); } CAutoPtr( const CAutoPtr& orig){ *this = orig; } CAutoPtr& operator=( const CAutoPtr& orig) { if ( CBaseAutoPointer::m_ptr ) FreePtr(); CBaseAutoPointer::m_ptr = orig.m_ptr; const_cast(orig).m_ptr = 0; return *this; } bool operator==(const CAutoPtr& orig)const{ return true;} //operator PTRTYPE(){ return m_ptr; } virtual void FreePtr() { delete CBaseAutoPointer::m_ptr; } }; template class CAutoPointer1 : public CBaseAutoPointer, public BaseClass { public: ~CAutoPointer1(){ CBaseAutoPointer::Close(); } virtual void FreePtr() { (*((BaseClass*)this))(CBaseAutoPointer::m_ptr); } }; /* class CFunctor_NetApiBufferFree { public: void operator() ( void* pObject ){ NetApiBufferFree(pObject); } };*/ template class CAutoPointer2 : public CBaseAutoPointer { public: ~CAutoPointer2(){ CBaseAutoPointer::Close(); } virtual void FreePtr() { if(pFunc) (*pFunc)(CBaseAutoPointer::m_ptr); } }; template class CAutoPointer : public CBaseAutoPointer { struct CBaseDeleter{ virtual void Delete(PTRTYPE) = 0; }; template struct CDeleter : public CBaseDeleter { CDeleter( FUNCTYPE pFunc ): m_pFunc(pFunc){;} void Delete(PTRTYPE ptr) { m_pFunc(ptr); } FUNCTYPE m_pFunc; }; public: template CAutoPointer( FUNCTYPE pFunc ) { static CDeleter oDeleter(pFunc); CBaseAutoPointer::m_ptr = 0; m_pDeleter = &oDeleter; } ~CAutoPointer(){ CBaseAutoPointer::Close(); } virtual void FreePtr() { if(m_pDeleter) m_pDeleter->Delete(CBaseAutoPointer::m_ptr); } private: CBaseDeleter* m_pDeleter; }; template class CStaticClassFunctor : public rho::common::IRhoRunnable { public: CStaticClassFunctor( FUNCTYPE pFunc, PARAMTYPE param ): m_pFunc(pFunc), m_param(param){} virtual void runObject() { m_pFunc(m_param); } ~CStaticClassFunctor() { } private: FUNCTYPE m_pFunc; PARAMTYPE m_param; }; template class CInstanceClassFunctorBase : public rho::common::IRhoRunnable { public: CInstanceClassFunctorBase( PARAMTYPE param ): m_param(param){} PARAMTYPE& getLastParam(){ return m_param; } protected: PARAMTYPE m_param; }; template class CInstanceClassFunctor1 : public CInstanceClassFunctorBase { public: CInstanceClassFunctor1( OBJTYPE obj, FUNCTYPE pFunc, PARAMTYPE param ): CInstanceClassFunctorBase(param), m_obj(obj), m_pFunc(pFunc){} virtual void runObject() { (m_obj->*m_pFunc)(CInstanceClassFunctorBase::m_param); } ~CInstanceClassFunctor1() { } protected: OBJTYPE m_obj; FUNCTYPE m_pFunc; }; template CInstanceClassFunctorBase* rho_makeInstanceClassFunctor1( OBJTYPE obj,FUNCTYPE pFunc, PARAMTYPE param ) { return new rho::common::CInstanceClassFunctor1(obj, pFunc, param); } template class CInstanceClassFunctor2 : public CInstanceClassFunctorBase { public: CInstanceClassFunctor2( OBJTYPE obj, FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2 ): CInstanceClassFunctorBase(param2), m_obj(obj), m_pFunc(pFunc), m_param1(param1){} virtual void runObject() { (m_obj->*m_pFunc)(m_param1, CInstanceClassFunctorBase::m_param); } ~CInstanceClassFunctor2() { } protected: OBJTYPE m_obj; FUNCTYPE m_pFunc; PARAMTYPE1 m_param1; }; template CInstanceClassFunctorBase* rho_makeInstanceClassFunctor2( OBJTYPE obj,FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2 ) { return new rho::common::CInstanceClassFunctor2(obj, pFunc, param1, param2); } template class CInstanceClassFunctor3 : public CInstanceClassFunctorBase { public: CInstanceClassFunctor3( OBJTYPE obj, FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2, PARAMTYPE3 param3 ): CInstanceClassFunctorBase(param3), m_obj(obj), m_pFunc(pFunc), m_param1(param1), m_param2(param2){} virtual void runObject() { (m_obj->*m_pFunc)(m_param1, m_param2, CInstanceClassFunctorBase::m_param); } ~CInstanceClassFunctor3() { } protected: OBJTYPE m_obj; FUNCTYPE m_pFunc; PARAMTYPE1 m_param1; PARAMTYPE2 m_param2; }; template CInstanceClassFunctorBase* rho_makeInstanceClassFunctor3( OBJTYPE obj,FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2, PARAMTYPE3 param3 ) { return new rho::common::CInstanceClassFunctor3(obj, pFunc, param1, param2, param3); } template class CInstanceClassFunctor4 : public CInstanceClassFunctorBase { public: CInstanceClassFunctor4( OBJTYPE obj, FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2, PARAMTYPE3 param3, PARAMTYPE4 param4 ): CInstanceClassFunctorBase(param4), m_obj(obj), m_pFunc(pFunc), m_param1(param1), m_param2(param2), m_param3(param3){} virtual void runObject() { (m_obj->*m_pFunc)(m_param1, m_param2, m_param3, CInstanceClassFunctorBase::m_param); } ~CInstanceClassFunctor4() { } protected: OBJTYPE m_obj; FUNCTYPE m_pFunc; PARAMTYPE1 m_param1; PARAMTYPE2 m_param2; PARAMTYPE3 m_param3; }; template CInstanceClassFunctorBase* rho_makeInstanceClassFunctor4( OBJTYPE obj,FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2, PARAMTYPE3 param3, PARAMTYPE4 param4 ) { return new rho::common::CInstanceClassFunctor4(obj, pFunc, param1, param2, param3, param4); } template class CInstanceClassFunctor5 : public CInstanceClassFunctorBase { public: CInstanceClassFunctor5( OBJTYPE obj, FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2, PARAMTYPE3 param3, PARAMTYPE4 param4, PARAMTYPE5 param5 ): CInstanceClassFunctorBase(param5), m_obj(obj), m_pFunc(pFunc), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4){} virtual void runObject() { (m_obj->*m_pFunc)(m_param1, m_param2, m_param3, m_param4, CInstanceClassFunctorBase::m_param); } ~CInstanceClassFunctor5() { } protected: OBJTYPE m_obj; FUNCTYPE m_pFunc; PARAMTYPE1 m_param1; PARAMTYPE2 m_param2; PARAMTYPE3 m_param3; PARAMTYPE4 m_param4; }; template CInstanceClassFunctorBase* rho_makeInstanceClassFunctor5( OBJTYPE obj,FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2, PARAMTYPE3 param3, PARAMTYPE4 param4, PARAMTYPE5 param5 ) { return new rho::common::CInstanceClassFunctor5(obj, pFunc, param1, param2, param3, param4, param5); } template class CInstanceClassFunctor6 : public CInstanceClassFunctorBase { public: CInstanceClassFunctor6( OBJTYPE obj, FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2, PARAMTYPE3 param3, PARAMTYPE4 param4, PARAMTYPE5 param5, PARAMTYPE6 param6 ): CInstanceClassFunctorBase(param6), m_obj(obj), m_pFunc(pFunc), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4), m_param5(param5){} virtual void runObject() { (m_obj->*m_pFunc)(m_param1, m_param2, m_param3, m_param4, m_param5, CInstanceClassFunctorBase::m_param); } ~CInstanceClassFunctor6() { } protected: OBJTYPE m_obj; FUNCTYPE m_pFunc; PARAMTYPE1 m_param1; PARAMTYPE2 m_param2; PARAMTYPE3 m_param3; PARAMTYPE4 m_param4; PARAMTYPE5 m_param5; }; template CInstanceClassFunctorBase* rho_makeInstanceClassFunctor6( OBJTYPE obj,FUNCTYPE pFunc, PARAMTYPE1 param1, PARAMTYPE2 param2, PARAMTYPE3 param3, PARAMTYPE4 param4, PARAMTYPE5 param5, PARAMTYPE6 param6 ) { return new rho::common::CInstanceClassFunctor6(obj, pFunc, param1, param2, param3, param4, param5, param6); } } }