宏观驱逐舰 [英] Macro Destroyer

查看:48
本文介绍了宏观驱逐舰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



今天我正在做一些Win32编程,必须包含文件

" windows.h"。无论如何,我正在考虑编写一个程序,你会这样工作:


macrodestroyer.exe windows.h


什么这个程序将通过文件搜索,用全局const变量和内联函数替换所有宏

。所以例如,如果你有
有:


#define MAX_LOADSTRING 100


它会变成:


unsigned const MAX_LOADSTRING = 100;


如果你有:


#define Minus5 (t)(t-5)


然后将变成


模板< class T,class R>

内联T减5(R r)

{

返回(t-5);

}

或者其他类似的东西。


无论如何...


这样做的主要好处是:


A)遵循范围规则,你可以包装windows.h。在命名空间中,例如:


命名空间Win {#include" windows.h" }

B)你不会遇到这样的问题:


SomeMacro(++ i);


,因为它可能会不止一次增加。


另外,我会转:


#define LRESULT long;


进入:


typdef长LRESULT;

无论如何,在我开始这个小宠物项目之前,这是完成

之前?


任何想法,评论都可以吗?

-JKop


I was doing some Win32 programming today, having to include the file
"windows.h". Anyway, I''m thinking of writing a program that''ll work like so:

macrodestroyer.exe windows.h

What this program will do is scour through the file, replacing all macros
with global const variables and inline functions. So for instance, if you
have:

#define MAX_LOADSTRING 100

It''ll become:

unsigned const MAX_LOADSTRING = 100;

And if you have:

#define Minus5(t) (t-5)

Then''d be turned into

template <class T,class R>
inline T Minus5(R r)
{
return (t-5);
}
Or something along those lines.

Anyway...

The main benefits of this would be:

A) Follows scoping rules, and you could wrap "windows.h" in a namespace, eg:

namespace Win { #include "windows.h" }
B) You wouldn''t have the problem of:

SomeMacro(++i);

in that it may increment it more than once.

Also, I would turn:

#define LRESULT long;

into:

typdef long LRESULT;
Anyway, before I get going on this little pet project, has this been done
before?

Any ideas, comments at all?
-JKop

推荐答案

这是好的项目,但是很可能没有工作的机会。原因

这是我的不同含义:


#define MACROTYPE类型*





typedef类型MYTYPE


这两种形式的不同之处在于:


MACROTYPE a,b;


是:

类型* a,b; // b不是指向类型的指针


但是在实际的typedef(MYTYPE)


MYTYPE a,b; // a和b是指针。


我真的不知道是什么东西在windows.h中存在,所以

你的项目有机会: )

It is OK project, but it is highly chance that is not work OK. Reason for
this my be different meaning of:

#define MACROTYPE type*

and

typedef type MYTYPE

This two forms are different in term of:

MACROTYPE a, b;

is:
type *a, b; // b is not pointer to type

but in real typedef (MYTYPE)

MYTYPE a, b; // a and b are pointers.

I realy do not know is something like this is present in windows.h so than
your project have chance :)


hari4063发布:
hari4063 posted:
这是好项目,但是很有可能无法正常工作。原因
对此我的意思不同:

#define MACROTYPE类型*



typedef类型MYTYPE

这两种形式的不同之处在于:

MACROTYPE a,b;

是:
类型* a,b; // b不是指向类型的指针

但是在实际的typedef(MYTYPE)

MYTYPE a,b; // a和b是指针。

我真的不知道是什么东西在windows.h中存在,所以
比你的项目有机会:))
It is OK project, but it is highly chance that is not work OK. Reason
for this my be different meaning of:

#define MACROTYPE type*

and

typedef type MYTYPE

This two forms are different in term of:

MACROTYPE a, b;

is:
type *a, b; // b is not pointer to type

but in real typedef (MYTYPE)

MYTYPE a, b; // a and b are pointers.

I realy do not know is something like this is present in windows.h so
than your project have chance :)


这是一件非常好的事情,我没有想到这一点。


我会调查它......
-JKop


That''s a very good thing to point out, I hadn''t thought of that.

I''ll look into it...
-JKop


" JKop" < NU ** @ NULL.NULL>在消息中写道

news:tn ******************* @ news.indigo.ie ...
"JKop" <NU**@NULL.NULL> wrote in message
news:tn*******************@news.indigo.ie...

我今天正在做一些Win32编程,必须包含文件
windows.h。无论如何,我正在考虑编写一个像
一样工作的程序,所以:

macrodestroyer.exe windows.h

这个程序会做什么遍历文件,用全局const变量和内联函数替换所有宏。例如,如果你有:
[为讨论重新排序的例子]

#define MAX_LOADSTRING 100
它将成为:
无符号const MAX_LOADSTRING = 100;
嗯,为什么*未签名*? (值可能是-100)

我倾向于选择枚举{MAX_LOADSTRING = 100};反正这样的事情。


[不正确的尾随;从下一行删除] #define LRESULT长期进入:
typdef long LRESULT;


请注意,之前的两个案例并不总是很容易区分。

考虑:

#define THIS_STATUS_CODE SOME_PREVIOUS_ID

这应该是一个typedef还是一个const?


您可能需要解析#include -d文件以找出...

当定义宏时,甚至可能看不到SOME_PREVIOUS_ID

这个问题更加严重。

你将如何翻译:

#define ab

// ...后来很多行或文件:

#define b int //或者可能是0x00000000而不是''int''


另外,在< windows.h>中,你会发现很多宏,例如:

#define GetWindowText GetWindowTextA

哪个最好被翻译成(例如):

inline BOOL GetWindowText(HANDLE h,CHAR * p)

{return GetWindowTextA(h,p); }


这些可以另外有条件地编译:

#ifndef UNICODE

#define GetWindowText GetWindowTextA

#else

#define GetWindowText GetWindowTextW

#endif

#define减5(t)(t-5)

模板<类T,R类>
内联T Minus5(R r)
{
返回(t- 5);
}
请注意,这可能并不总是等价的。

特别是,它可能会产生额外的会员副本。


这样做的主要好处是:

A)遵循范围规则,你可以包装windows.h。在命名空间中,例如:

命名空间Win {#include" windows.h" }

B)你不会遇到这样的问题:

SomeMacro(++ i);

因为它可以增加它不止一次。
是的,但是有人会不小心编译代码

与原始版本< windows.h>并最终

面对奇怪和意外的错误。


无论如何,在我开始这个小宠物项目之前,这已经完成了吗?

任何想法,评论都可以吗?

I was doing some Win32 programming today, having to include the file
"windows.h". Anyway, I''m thinking of writing a program that''ll work like
so:

macrodestroyer.exe windows.h

What this program will do is scour through the file, replacing all macros
with global const variables and inline functions. So for instance, if you
have: [examples reordered for discussion]
#define MAX_LOADSTRING 100
It''ll become:
unsigned const MAX_LOADSTRING = 100; Hum, why *unsigned* ? (the value could be -100)
I tend to prefer enum { MAX_LOADSTRING = 100 }; for such things anyway.

[ incorrect trailing ; removed from the next line ] #define LRESULT long
into:
typdef long LRESULT;
Note that the two previous cases will not always be easy to distinguish.
Consider:
#define THIS_STATUS_CODE SOME_PREVIOUS_ID
Should this become a typedef or a const?

You may need to parse #include-d files to find out...
The problem is aggravated by the fact that SOME_PREVIOUS_ID
may not even have been seen when the macro is defined.
How will you translate:
#define a b
// ... many lines or files later:
#define b int // or could be 0x00000000 instead of ''int''

Also, in <windows.h>, you will find a lot of macros such as:
#define GetWindowText GetWindowTextA
Which would best be translated into something like (e.g.):
inline BOOL GetWindowText(HANDLE h, CHAR* p)
{ return GetWindowTextA(h,p); }

These may additionally be conditionally compiled:
#ifndef UNICODE
#define GetWindowText GetWindowTextA
#else
#define GetWindowText GetWindowTextW
#endif
#define Minus5(t) (t-5)

Then''d be turned into

template <class T,class R>
inline T Minus5(R r)
{
return (t-5);
} Beware that this may not always be equivalent.
In particular, it may generate additional member copies.

The main benefits of this would be:
A) Follows scoping rules, and you could wrap "windows.h" in a namespace,
eg:

namespace Win { #include "windows.h" }
B) You wouldn''t have the problem of:

SomeMacro(++i);

in that it may increment it more than once. Yes, but then someone will accidentally compile the code
with the original version of <windows.h> and eventually
face strange and unexpected bugs.

Anyway, before I get going on this little pet project, has this been done
before?

Any ideas, comments at all?



正如您所知,这不是一项微不足道的任务:您需要

a预处理器一个解析器,可能需要在处理之前一起扫描所有文件。


我同情......

但是我很久以前就已放弃(本地)修改供应商提供的文件
。更好地将它们包装并封装到你自己的文件/类中。

问候,

Ivan

-
http://ivan.vecerina.com/contact/?subject = NG_POST < - 电子邮件联系表格


As you can tell, this is not a trivial task: you need
a preprocessor, a parser, and probably need to scan
all files together prior to processing.

I sympathize...
but I''ve given up long ago (locally) modifying files supplied
by a vendor. Better wrap and encapsulate them into
your own files/classes.
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



这篇关于宏观驱逐舰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆