• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

C++语法游戏 : 模仿C#的 using(...){}

发布: 2007-7-01 20:40 | 作者: admin | 来源: | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网 游戏而已,,可以用来锻炼脑筋。。~~ //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() 的调用 //以支持所有的do.net 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; }

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网