Squeak FFI中的typedef如何处理 [英] How one deals with typedefs in Squeak FFI
问题描述
我想与一个库(HDF5)交互,该库在函数原型和结构定义中都专门使用其自己的typedef.
I want to interface with a library (HDF5) which uses exclusively its own typedefs in both function prototypes and structure definitions.
typedef struct {
H5L_type_t type; /* Type of link */
hbool_t corder_valid; /* Indicate if creation order is valid */
int64_t corder; /* Creation order */
H5T_cset_t cset; /* Character set of link name */
union {
haddr_t address; /* Address hard link points to */
size_t val_size; /* Size of a soft link or UD link value */
} u;
} H5L_info_t;
在带有DLLCC的Visualworks中,我可以处理这样的typedef:
In Visualworks with DLLCC I can deal with such typedefs like this:
H5Interface>>hbool_t
<C: typedef unsigned int hbool_t>
然后我可以在原型和结构中都使用hbool_t
,对于枚举也是如此.
And I can then use hbool_t
in both prototypes and structure, same for enum.
但是Squeak FFI似乎只了解几种原子类型,并将其他任何内容解释为结构.显然,必须由他人来进行翻译,而且如果翻译不是自动化的,那么它就容易出错,而且对将来外部库的发展也不可靠.
But Squeak FFI seems to understand only a few atomic types and interpret anything else as a structure. Obviously, someone has to do the translation, and if it is not automated, then it's both errorprone and not robust to future evolutions of external library.
那么避免这种脆弱性的推荐方法是什么?
So what is the recommended way for avoiding such fragility?
推荐答案
自mt.92起,您还可以将ExternalTypeAlias
子类化,以清晰的方式记录类型别名.参见 source.squeak.org/FFI/FFI-Kernel-mt.92 .diff
Since mt.92, you can also subclass ExternalTypeAlias
to document type aliasing in a clear fashion. See source.squeak.org/FFI/FFI-Kernel-mt.92.diff
您的示例别名可以使用新的基类:
You example alias could use the new base class:
ExternalTypeAlias subclass: #'Hbool_t'
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'FFI-Tests'
类端只需要原始类型名称,这就是您要使用的别名:
The class-side just needs the original type name, which is the one you are aliasing:
Hbool_t class>>originalTypeName
^ 'ulong'
请注意,Hbool_t defineFields
还将定义访问器#value
和#value:
以在运行时解析别名.
Note that Hbool_t defineFields
will also define the accessors #value
and #value:
to resolve the alias at runtime.
还请注意,具有使用该别名的字段的外部结构甚至会为原子类型的别名创建实例:
Also note that external structures with fields using that alias will create instances even for aliases to atomic types:
H5L_info_t>>corder_valid
<generated>
^ Hbool_t fromHandle: (handle unsignedLongAt: 123456)
这篇关于Squeak FFI中的typedef如何处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!