简体   繁体   English

在define宏中使用typedef定义函数

[英]defining a function using a typedef inside a define macro

I have seen typedef and #define used in seperate scenarios. 我已经看到在单独的场景中使用了typedef和#define。 However, I have never come across code with one inside the other. 但是,我从来没有遇到过一个内含一个的代码。 Namely, the following 即以下

#define DECL_STDXL_FUNC(apiname, apitype, args)    \
  typedef XLstatus (_XL_EXPORT_API *apitype) args

This is later used to declare a function prototype (apparently) like this 以后用来声明一个函数原型(显然)是这样的

DECL_STDXL_FUNC ( xlGetChannelTime,   XLGETCHANNELTIME, (
              XLportHandle        portHandle, 
              XLaccess            accessMask,
              XLuint64            *pChannelTime )
);

Basically, the header file is supposed to define functions that I only have the dll for. 基本上,头文件应该定义我仅具有dll的功能。 This suggests to me that I need to use _declspec(dllimport) and the correspond export of that. 这向我建议我需要使用_declspec(dllimport)及其相应的导出。 But the above just does not make any sense to me. 但是以上对我来说毫无意义。 Can anybody help make sense of this definition? 任何人都可以帮助理解这个定义吗? If you need more clarification on the code, I can absolutely provide it. 如果您需要有关代码的更多说明,我绝对可以提供。 This is by far the most frustrating and convoluted way I've ever seen a function prototype written. 到目前为止,这是我看过的函数原型编写以来最令人沮丧和费解的方法。

Run it through the preprocessor, at least mentally (this is what I'm doing): 至少在精神上通过预处理器运行它(这是我正在做的):

#define DECL_STDXL_FUNC(apiname, apitype, args)    \
  typedef XLstatus (_XL_EXPORT_API *apitype) args

Interesting: the apiname argument to the macro is unused. 有趣:宏的apiname参数未使用。

DECL_STDXL_FUNC ( xlGetChannelTime,   XLGETCHANNELTIME, (
              XLportHandle        portHandle, 
              XLaccess            accessMask,
              XLuint64            *pChannelTime )
);

When translated, this becomes: 翻译后,将变为:

typedef XLstatus (_XL_EXPORT_API *XLGETCHANNELTIME)(XLportHandle portHandle, 
              XLaccess accessMask, XLuint64 *pChannelTime);

That is, XLGETCHANNELTIME becomes a name for a specific pointer to function type. 即, XLGETCHANNELTIME成为函数类型的特定指针的名称。 The _XL_EXPORT_API is presumably related to importing from or exporting to shared library (DLL) symbol lists. _XL_EXPORT_API可能与共享库(DLL)符号列表的导入或导出有关。 And the various XLlowercase names are types defined by the code. 并且各种XLlowercase名称是由代码定义的类型。

You'll be able to define variables of this type: 您将能够定义这种类型的变量:

XLGETCHANNELTIME get_channel_time = …;

which will presumably need to be initialized by some sort of symbol lookup function ( dlopen() in the most common Unix-like system). 大概需要通过某种符号查找功能(在最常见的类Unix系统中使用dlopen()来初始化它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM