gpt4 book ai didi

haskell - marshaller 中的 c2hs 与 gtk2hsC2hs

转载 作者:行者123 更新时间:2023-12-02 13:39:44 25 4
gpt4 key购买 nike

我尝试了解 c2hsgtk2hsC2hs 之间的区别(用于 gtk2hsc2hs 的修改版本) )。考虑以下编码:

module Matrix where

data Matrix = Matrix { xx :: !Double, yx :: !Double,
xy :: !Double, yy :: !Double,
x0 :: !Double, y0 :: !Double }
deriving (Show, Eq)

{#pointer *cairo_matrix_t as MatrixPtr -> Matrix#}

instance Storable Matrix where
sizeOf _ = {#sizeof cairo_matrix_t#}
alignment _ = alignment (undefined :: CDouble)
peek p = do
xx <- {#get cairo_matrix_t->xx#} p
yx <- {#get cairo_matrix_t->yx#} p
xy <- {#get cairo_matrix_t->xy#} p
yy <- {#get cairo_matrix_t->yy#} p
x0 <- {#get cairo_matrix_t->x0#} p
y0 <- {#get cairo_matrix_t->y0#} p
return $ Matrix (realToFrac xx) (realToFrac yx)
(realToFrac xy) (realToFrac yy)
(realToFrac x0) (realToFrac y0)

poke p (Matrix xx yx xy yy x0 y0) = do
{#set cairo_matrix_t->xx#} p (realToFrac xx)
{#set cairo_matrix_t->yx#} p (realToFrac yx)
{#set cairo_matrix_t->xy#} p (realToFrac xy)
{#set cairo_matrix_t->yy#} p (realToFrac yy)
{#set cairo_matrix_t->x0#} p (realToFrac x0)
{#set cairo_matrix_t->y0#} p (realToFrac y0)
return ()

{#context lib="cairo" #}

{#fun cairo_pattern_set_matrix as patternSetMatrix { unPattern `Pattern', `Matrix'} -> `()'#}
{#fun cairo_pattern_get_matrix as patternGetMatrix { unPattern `Pattern', alloca- `Matrix' peek*} -> `()'#}

第二个 fun 行(例如 cairo_pattern_get_matrix)适用于这两种工具(尽管生成的代码不同),而第一行(例如 cairo_pattern_set_matrix >) 仅适用于 gtk2hsC2hs

使用c2hs时的错误消息是:

Test.chs:35: (column 75) [ERROR]  >>> Missing "in" marshaller!
There is no default marshaller for this combination of Haskell and C type:
Haskell type: Matrix
C type : (MatrixPtr)

我不太清楚这里的问题,但从我的角度来看,这应该是有效的。

我用于c2hs:

C->Haskell Compiler, version 0.28.3 Switcheroo, 25 November 2017
build platform is "x86_64-linux" <1, True, True, 1>

gtk2hsC2hs:

C->Haskell Compiler, version 0.13.13 (gtk2hs branch) "Bin IO", 27 May 2012

提前致谢

最佳答案

好的,以下似乎有效:

{#fun cairo_pattern_set_matrix as patternSetMatrix 
{ `Pattern', with* `Matrix'} -> `()'#}

因此,c2hsgtk2hsC2hsnewtype 和编码(marshal)拆收器的处理是不同的。我猜,这是因为后者是基于古代版本的。

关于haskell - marshaller 中的 c2hs 与 gtk2hsC2hs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49789724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com