C++语法游戏之,模拟C#的using(...){}

lostinetdotcom 2003-05-18 05:33:59
C++语法游戏之,模拟C#的using(...){}
游戏而已,,可以用来锻炼脑筋。。~~


//using.h

#pragma once

//for IUnknown
#include "atlbase.h"

#define USING_SCOPE_ASSERT(x) ATLASSERT(x);

#define USING_SCOPE_INLINE inline

#define Using(expression) if(UsingScope UsingScope_LocalVar=(expression))

class UsingScope
{
class Container
{
public:
virtual ~Container()
{
}
};

Container* pc;
public:
USING_SCOPE_INLINE operator bool ()
{
return true;
}

USING_SCOPE_INLINE ~UsingScope()
{
delete pc;
}

template<typename T>
class SimplePointerContainer:public Container
{
T* p;
public:
SimplePointerContainer(T* p)
{
USING_SCOPE_ASSERT(p!=NULL);
this->p=p;
}
virtual ~SimplePointerContainer()
{
delete p;
}
};

template<typename T>
USING_SCOPE_INLINE UsingScope(T* p)
{
pc=new SimplePointerContainer<T>(p);
}

class IUnknownContainer:public Container
{
IUnknown* punk;
public:
IUnknownContainer(IUnknown* punk)
{
USING_SCOPE_ASSERT(punk!=NULL);
this->punk=punk;
}
virtual ~IUnknownContainer()
{
punk->Release();
}
};
USING_SCOPE_INLINE UsingScope(IUnknown* punk)
{
pc=new IUnknownContainer(punk);
}

class BSTRContainer:public Container
{
BSTR bstr;
public:
BSTRContainer(BSTR bstr)
{
USING_SCOPE_ASSERT(bstr!=NULL);
this->bstr=bstr;
}
virtual ~BSTRContainer()
{
SysFreeString(bstr);
}
};
USING_SCOPE_INLINE UsingScope(BSTR bstr)
{
pc=new BSTRContainer(bstr);
}

//vc7 m 可以为System::IDisposable* 添加 .Dispose() 的调用
//以支持所有的dotnet System::IDisposable的实现者
};




//usingT.h

#include "Using.h"

#define UsingT(type,varname,expression) if(UsingScopeT<type> varname=(expression))

template<typename T>
class UsingScopeT
{
template<typename T>
class Container
{
public:
T* p;
Container(T* p)
{
USING_SCOPE_ASSERT(p!=NULL);
this->p=p;
}
virtual ~Container(){}
};

Container<T>* pc;
public:
USING_SCOPE_INLINE operator bool ()
{
return true;
}

USING_SCOPE_INLINE T* operator -> ()
{
return (pc->p);
}
USING_SCOPE_INLINE operator T*() const
{
return (T*)(pc->p);
}
USING_SCOPE_INLINE T& operator*() const
{
return *(pc->p);
}
USING_SCOPE_INLINE T** operator&()
{
return &(pc->p);
}
USING_SCOPE_INLINE bool operator==(T* pT) const
{
return (pc->p) == pT;
}

USING_SCOPE_INLINE ~UsingScopeT()
{
delete pc;
}

template<typename T>
class SimplePointerContainer:public Container<T>
{
public:
SimplePointerContainer(T* p):Container<T>(p)
{
}
virtual ~SimplePointerContainer()
{
delete p;
}
};
template<typename T>
USING_SCOPE_INLINE UsingScopeT(T* p)
{
pc=new SimplePointerContainer<T>(p);
}

class IUnknownContainer:public Container<IUnknown>
{
public:
IUnknownContainer(IUnknown* punk):Container<IUnknown>(punk)
{
}
virtual ~IUnknownContainer()
{
p->Release();
}
};
USING_SCOPE_INLINE UsingScopeT(IUnknown* punk)
{
pc=new IUnknownContainer(punk);
}

class BSTRContainer:public Container<OLECHAR>
{
public:
BSTRContainer(BSTR bstr):Container<OLECHAR>(bstr)
{
}
virtual ~BSTRContainer()
{
SysFreeString(p);
}
};
USING_SCOPE_INLINE UsingScopeT(BSTR bstr)
{
pc=new BSTRContainer(bstr);
}

//vc7 m 可以为System::IDisposable* 添加 .Dispose() 的调用
//以支持所有的dotnet System::IDisposable的实现者
};




//Using.Cpp

// Using.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "atlbase.h"

#include "Using.h"
#include "UsingT.h"

class ClassA
{
public:
ClassA()
{
_tprintf(_T("ClassA()\n"));
}
virtual ~ClassA()
{
_tprintf(_T("~ClassA()\n"));
}

virtual void Write()
{
_tprintf(_T("ClassA::Write\n"));
}
};
class ClassB:public ClassA
{
public:
virtual void Write()
{
_tprintf(_T("ClassB::Write\n"));
}
};

ClassA* CreateClassA()
{
return new ClassB();
}

IUnknown* ReturnUnknown()
{
HRESULT hres;
IUnknown* p;
CLSID cid;
hres=CLSIDFromProgID(L"JScript",&cid);
hres=CoGetClassObject(cid,CLSCTX_INPROC_SERVER,NULL,IID_IClassFactory,(void**)&p);
return p;
}

void DoIt()
{
CoInitialize(NULL);

IUnknown* punk;
Using(punk=ReturnUnknown())
{
ClassA* pa;
Using(pa=CreateClassA())
{
_tprintf(_T("DoWork\n"));
pa->Write();
}
}

UsingT(ClassB,pb,new ClassB())
{
pb->Write();
ClassA* pa=pb;
if(pa==pb)
pa->Write();
}

UsingT(IUnknown,p,ReturnUnknown())
{
//p->..
}

CoUninitialize();
}


int main(int argc, char* argv[])
{
DoIt();
return 0;
}

...全文
10 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lostinetdotcom 2003-05-18
换了个更好的实现
下载:
http://www.lostinet.com/files/SqlScope.rar

//UsingT.h

#include "Using.h"

#define UsingT(type,varname,expression) for(type* varname=expression;varname;)for(UsingScopeT<type> UsingScopeT_LocalVar=(varname);varname;varname=NULL)

template<typename T>
class UsingScopeT
{
template<typename T>
class Container
{
public:
T* p;
Container(T* p)
{
USING_SCOPE_ASSERT(p!=NULL);
this->p=p;
}
virtual ~Container(){}
};

Container<T>* pc;
public:
USING_SCOPE_INLINE ~UsingScopeT()
{
delete pc;
}

template<typename T>
class SimplePointerContainer:public Container<T>
{
public:
SimplePointerContainer(T* p):Container<T>(p)
{
}
virtual ~SimplePointerContainer()
{
delete p;
}
};
template<typename T>
USING_SCOPE_INLINE UsingScopeT(T* p)
{
pc=new SimplePointerContainer<T>(p);
}

class IUnknownContainer:public Container<IUnknown>
{
public:
IUnknownContainer(IUnknown* punk):Container<IUnknown>(punk)
{
}
virtual ~IUnknownContainer()
{
p->Release();
}
};
USING_SCOPE_INLINE UsingScopeT(IUnknown* punk)
{
pc=new IUnknownContainer(punk);
}

class BSTRContainer:public Container<OLECHAR>
{
public:
BSTRContainer(BSTR bstr):Container<OLECHAR>(bstr)
{
}
virtual ~BSTRContainer()
{
SysFreeString(p);
}
};
USING_SCOPE_INLINE UsingScopeT(BSTR bstr)
{
pc=new BSTRContainer(bstr);
}

//vc7 m 可以为System::IDisposable* 添加 .Dispose() 的调用
//以支持所有的dotnet System::IDisposable的实现者
};
回复
lostinetdotcom 2003-05-18
我参与的问题
回复
相关推荐
发帖
工具平台和程序库
创建于2007-09-28

2.4w+

社区成员

C/C++ 工具平台和程序库
申请成为版主
帖子事件
创建了帖子
2003-05-18 05:33
社区公告
暂无公告