#pragma once#ifndef null#define NULL 0#endifnamespace rangehelper{template <class t>class CAutoDeletePtr {// Auto-delete pointer public:cautodeleteptr (): M_ptr (NULL) {}cautodeleteptr (t* ptr) {m_ptr = ptr;} ~cautodeleteptr () {free ();} void free () {if (!m_ptr) return;delete []m_ptr;m_ptr = NULL;} BOOL Allocate (size_t nelements) {free (); m_ptr = new T[nelements];if (!m_ptr) return False;return true;} void Attach (t* p) {ASSERT (P! = NULL); m_ptr = P;} t* Detach () {t* p;p = m_p;m_p = Null;return (p);} T operator[] (int i) Const{assert ((i>=0) && afxisvalidaddress (m_ptr, sizeof (T) *i)); return m_ptr[i];} t& operator[] (int i) {ASSERT ((i>=0) && afxisvalidaddress (m_ptr, sizeof (T) *i); return m_ptr[i];} cautodeleteptr<t>& operator= (const t* PTR) {if (m_ptr! = null) && (m_ptr! = ptr)) {if (m_ptr! = null) {del ete []m_ptr;m_ptr = NULL;}} m_ptr = Const_cast<t*> (PTR); return *this;} operator t* () const {return m_ptr;} Private:t *m_ptr;}; Template <class T>class CAUTORELEASEVMSelf-freeing virtual memory {PUBLIC:CAUTORELEASEVM (): M_ptr (NULL), m_ncnt (0) {}CAUTORELEASEVM (t* vmdata, ULONG ncnt) {ASSERT (vmdata); ASSERT (ncnt>0); m_ptr= vmdata;m_ncnt= ncnt;} CAUTORELEASEVM (ULONG nelementcnt, DWORD Flallocationtype=mem_commit, DWORD Flprotect=page_readwrite): M_ptr (NULL) { ALLOCATEVM (nelementcnt, Flallocationtype, flprotect);} ~CAUTORELEASEVM () {FREEVM ();} void Freevm () {if (m_ptr) {virtualfree (m_ptr, 0, mem_release); m_ncnt= 0;m_ptr= NULL;}} BOOL ALLOCATEVM (ULONG nelementcnt, DWORD Flallocationtype=mem_commit, DWORD Flprotect=page_readwrite) {FREEVM (); m_ptr = (t*) VirtualAlloc (NULL, nelementcnt*sizeof (T), Flallocationtype, flprotect); m_ncnt= Nelementcnt;return (m_ptr!=null );} BOOL Reallocatevm (ULONG nelementcnt, BOOL Bcopyold=true, DWORD Flallocationtype=mem_commit, DWORD Flprotect=page_ READWRITE) {if (!bcopyold) return ALLOCATEVM (nelementcnt, Flallocationtype, Flprotect); T *tmp = (t*) VirtualAlloc (NULL, nelementcnt*sizeof (T), Flallocationtype, Flprotect), if (!tmp) return False;memmove (TMP, M_ptr, Min (nelementcnt, m_ncnt) *sizeof (T)); m_ptr = tmp;m_ncnt= Nelementcnt;return TRUE;} BOOL IsOK () {return (m_ptr!=null);} size_t getdatabytes () {size_t ndatabytes = 0;if (IsOK ()) {ndatabytes = Gettypesize () *m_ncnt;} return ndatabytes;} size_t gettypesize () {return sizeof (t);} ULONG Getdatacount () {ULong Ndatacount = 0;if (IsOK ()) {ndatacount = m_ncnt;} return ndatacount;} T operator[] (size_t i) Const{assert ((i>=0) && m_ptr && afxisvalidaddress (m_ptr, sizeof (t) *i)); return m_ptr[i];} t& operator[] (size_t i) {ASSERT (i>=0) && m_ptr && afxisvalidaddress (m_ptr, sizeof (t) *i)); return m_ptr[i];} operator t* () const {return m_ptr;} private:t*m_ptr;size_tm_ncnt;}; Class cautoclosehandle//automatically closes the handle class {Public:cautoclosehandle (handle* handle=null): M_handle (HANDLE) {}cautoclosehandle & Operator= (handle* RH) {if (m_handle) {CloseHandle (m_handle); m_handle = NULL;} M_handle = Rh;return *this;} ~cautoclosehandle () {CloseHandle (*m_handle);} Private:handle *m_handle;}; Template <clT>class cautodeletearray:public carray<t*, T*>{public:~cautodeletearray () {RemoveAll ();} void RemoveAt (Int_ptr nIndex, int_ptr ncount = 1) {ASSERT (ncount>0); for (int_ptr I=nindex; I!=min (Nindex+ncount, GetCount ()); ++i) {T *pitem = GetAt (i);d elete Pitem;} Carray::removeat (NIndex, ncount);} void RemoveAll () {for (int_ptr i=0; I!=getcount (); ++i) {T *pitem = GetAt (i);d elete Pitem;} Carray::removeall ();}};/ *//crangefxnhelper use example Class A{public:void func1 () {cout << 1 << Endl;} void Func2 () {cout << 2 << Endl;} void Test () {crangefxnhelper<a> A (this, &a::func1, &A::FUNC2);}}; */template <class t>class crangefxnhelper//Local function call {typedef void (T::* memfxnptr) (void);//member function public: Crangefxnhelper (t* pclassname,memfxnptr lpfxnenter,memfxnptr lpfxnleave = NULL): M_pclassname (pClassName), m_ Lpfxnleave (Lpfxnleave) {ASSERT (Pclassname! = NULL); ASSERT (Lpfxnenter! = NULL);(m_pclassname->*lpfxnenter) ();} ~crangefxnhelper () {if (M_lpfxnleave! = NULL) (M_pclassnaMe->*m_lpfxnleave) ();} Private:t *m_pclassname; Memfxnptr M_lpfxnleave;}; Template <class t>class cautorevertbool//Auto Inverse boolean variable {public:cautorevertbool (t *pobjbool, T Initval): M_pobjbool ( Pobjbool), M_initval (initval) {ASSERT (Pobjbool! = NULL); *pobjbool = Initval;} ~cautorevertbool () {*m_pobjbool =!m_initval;} Private:t *m_pobjbool; T M_initval;}; Class Cautodisableenablechildctls{public:cautodisableenablechildctls (HWND hwndparent, UINT Nfirstexcludechildctrlid=-1, ...): M_hwndparent (hwndparent), m_bdisabled (FALSE) {Addexcludectrlids ( Nfirstexcludechildctrlid);} ~cautodisableenablechildctls () {if (m_bdisabled) Switchstatus ();} void Switchstatus () {struct Stenumchild {bool*pchangestatus;//change state carray<uint, uint>*parrayexcludeid;// No need to make a list of state changes Carray
VC + + self-releasing pointer, self-freeing virtual memory, self-closing handle, local scope callback function call and other auxiliary development classes