【问题标题】:c2hs fun marshallingc2hs 有趣的编组
【发布时间】:2011-03-01 03:05:31
【问题描述】:

我正在使用c2hsffi 绑定到Assimp library。我有一个数据类型AiScene,定义如下(细节不重要):

data AiScene = AiScene
  { mFlags         :: SceneFlags
  , mMeshes        :: [AiMesh]
  , mMaterials     :: [AiMaterial]
  , mAnimations    :: [AiAnimation]
  , mTextures      :: [AiTexture]
  , mLights        :: [AiLight]
  , mCameras       :: [AiCamera]
  }
{#pointer *aiScene as AiScenePtr -> AiScene#}

现在我正在尝试编写与以下函数的绑定:

const aiScene* aiImportFile( 
    const char* pFile, 
    unsigned int pFlags);

这是我目前所拥有的:

{#fun aiImportFile as ^
  {`String', cFromEnum `SceneFlags'} -> `AiScene' peek*#}

不幸的是,我收到了类型错误...

Couldn't match expected type `AiScene' against inferred type `()'
  Expected type: IO AiScene
  Inferred type: IO ()

现在,如果我进入生成的源并更改类型:

aiImportFile'_ :: ((Ptr CChar) -> (CUInt -> (IO (Ptr ()))))      -- original
aiImportFile'_ :: ((Ptr CChar) -> (CUInt -> (IO (Ptr AiScene)))) -- fixed

然后问题就消失了。我怎样才能让c2hs 自动执行此操作/我做错了什么?

谢谢!

编辑:我忘了提到的一件事是我为AiScene 定义了一个Storable 的实例。

【问题讨论】:

  • 您的编组器在 c2hs-0.16.2、OS X 10.6 上为我工作。你用的是什么版本的 c2hs?
  • @John 有趣。我也在使用 0.16.2,但在 Linux 上。我想知道问题可能是什么。跟阴谋集团有关吗?
  • 我认为这与阴谋集团无关。好像国外的导入调用是错误的,这很奇怪。我想知道头文件是否在 CPP 通行证期间被修改?运行c2hs --keep 并检查aiImportFile 的声明是否正确。您也可以尝试使用 {#call ...#} 代替函数挂钩,并手动编写编组包装器。
  • @John 当我运行c2hs --keep 时,Fun.i 的声明与在头文件中出现的完全相同。如果我决定放弃并使用call,那么我想我会改用hsc2hs。这将是不幸的,因为唯一的问题是一种指针类型。
  • @John 好的,我想我现在已经解决了。我定义了以下内容:with' x y = with x (y . castPtr)peek' = peek . castPtr 并且类型错误消失了。我想找到根本原因,但现在应该这样做(我还没有实际测试过)。感谢您的帮助!

标签: haskell


【解决方案1】:

社区维基回答后代的解决方案:

我定义了以下内容:with' x y = with x (y . castPtr)peek' = peek . castPtr 并且类型错误消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2021-12-19
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2014-11-25
    相关资源
    最近更新 更多