游戏而已,,可以用来锻炼脑筋。。~~
//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
class SimplePointerContainer:public Container
{
T* p;
public:
SimplePointerContainer(T* p)
{
USING_SCOPE_ASSERT(p!=NULL);
this-p=p;
}
virtual ~SimplePointerContainer()
{
delete p;
}
};
template
USING_SCOPE_INLINE UsingScope(T* p)
{
pc=new SimplePointerContainer(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 varname=(expression))
template
class UsingScopeT
{
template
class Container
{
public:
T* p;
Container(T* p)
{
USING_SCOPE_ASSERT(p!=NULL);
this-p=p;
}
virtual ~Container(){}
};
Container* 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
class SimplePointerContainer:public Container
{
public:
SimplePointerContainer(T* p):Container(p)
{
}
virtual ~SimplePointerContainer()
{
delete p;
}
};
template
USING_SCOPE_INLINE UsingScopeT(T* p)
{
pc=new SimplePointerContainer(p);
}
class IUnknownContainer:public Container
{
public:
IUnknownContainer(IUnknown* punk):Container(punk)
{
}
virtual ~IUnknownContainer()
{
p-Release();
}
};
USING_SCOPE_INLINE UsingScopeT(IUnknown* punk)
{
pc=new IUnknownContainer(punk);
}
class BSTRContainer:public Container
{
public:
BSTRContainer(BSTR bstr):Container(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;
}