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
 
 
  
  
 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; } 
                 
                
               
              
             
            
           
          
         
        
      
     
     
    
    
   
   
  
  
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值