C ++ syntax game: imitate C #'s useing (...) {}

zhaozj2021-02-17  44

Games, you can use to exercise your brains. . ~~

//using.h

#pragma overce

// for iunknown

#include "atlbase.h"

#define using_scope_assert (x) atlassert (x);

#define using_scope_inline inline

#define using (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 ()

{

Sysfreeestring (BSTR);

}

}

USING_SCOPE_INLINE USINGSCOPE (BSTR BSTR)

{

PC = New BSTRCONTAINER (BSTR);

}

// vc7 m can be added to system :: idisposable *. Dispose () call

// to support all DOTNET System :: Idisposable implementors

}

//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 ();}}; useing_scope_inline usingscopet (iUnknown * punk) {pc = new iUnknownContainer (PUNK);} Class BstrContainer: Public Container: Public Container: Public Container: Public Container

{Public: BSTRCONTAINER (BSTR BSTR): Container

转载请注明原文地址:https://www.9cbs.com/read-28505.html

New Post(0)