Created
April 18, 2014 09:31
-
-
Save xylcbd/11034138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <functional> | |
class CScopedRelease | |
{ | |
public: | |
explicit CScopedRelease(std::function<void ()> pfnRelease) | |
: m_pfnRelease(pfnRelease) | |
{ | |
} | |
~CScopedRelease() | |
{ | |
if(m_pfnRelease) | |
{ | |
m_pfnRelease(); | |
} | |
} | |
private: | |
std::function<void ()> m_pfnRelease; | |
}; | |
#define SCOPEDRELEASE_NAME_CAT(name, line) name##line | |
#define SCOPEDRELEASE_NAME(name,line) SCOPEDRELEASE_NAME_CAT(name,line) | |
#define SCOPEDRELEASE(callfunc) \ | |
CScopedRelease SCOPEDRELEASE_NAME(AUTORELEASE,__LINE__)(callfunc); | |
#define DELFUNC(x) [&x]()->void{delete x;x=nullptr;} | |
#define DELARRAYFUNC(x) [&x]()->void{delete x;x=nullptr;} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//c++ 98/03 __cplusplus = 199711L | |
//c++11 __cplusplus = 201103L | |
#if __cplusplus < 199711L | |
#error "must be compiled by c++11 support compiler" | |
#endif | |
#include <algorithm> | |
#include <iostream> | |
#include "ScopedRelease.h" | |
using namespace std; | |
struct ITest | |
{ | |
public: | |
virtual void open() = 0; | |
virtual void print() = 0; | |
virtual void close() = 0; | |
}; | |
struct CTestBase : public ITest | |
{ | |
public: | |
CTestBase(){} | |
CTestBase(int i){} | |
~CTestBase(){} | |
public: | |
virtual void open() override | |
{ | |
} | |
virtual void print() override | |
{ | |
cout<<"hello world"<<endl; | |
} | |
virtual void close() override | |
{ | |
} | |
}; | |
struct CTest : public CTestBase | |
{ | |
public: | |
using CTestBase::CTestBase; | |
// CTest() : CTestBase(){} | |
// CTest(int i) : CTestBase(i){} | |
CTest() {} | |
CTest(int i) {} | |
~CTest(){} | |
public: | |
virtual void open() override | |
{ | |
} | |
virtual void print() override | |
{ | |
cout<<"hello kitty"<<endl; | |
} | |
virtual void close() override | |
{ | |
} | |
}; | |
//extern template class CTestTemplate<float>; | |
int main(int argc,char* argv[]) | |
{ | |
auto showfunc = [](const int& val){cout<<val<<" ";}; | |
int vallist[] = {1,2,3,4,5}; | |
for_each(vallist,vallist+sizeof(vallist)/(sizeof(int)),showfunc); | |
cout<<endl<<endl; | |
int* pData = new int[100]; | |
SCOPEDRELEASE(DELARRAYFUNC(pData)); | |
for (int i=0;i<100;i++){pData[i] = i;} | |
for_each(pData,pData+100,showfunc); | |
cout<<endl<<endl; | |
ITest* p1 = new CTestBase(); | |
ITest* p2 = new CTest(123); | |
SCOPEDRELEASE(DELFUNC(p1)); | |
SCOPEDRELEASE(DELFUNC(p2)); | |
p1->print(); | |
p2->print(); | |
cout<<endl<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment