【问题标题】:C2HS marshalling double pointerC2HS编组双指针
【发布时间】:2016-04-12 14:02:48
【问题描述】:

当有这样的功能时:

some_type_t* some_type_create(const char* name, char** errptr);

有没有办法让 C2HS 生成具有以下签名的 Haskell 绑定?

someTypeCreate :: String -> IO (SomeTypeFPtr, String)

这是我目前所能得到的:

{#fun some_type_create as ^ 
    {`String', alloca- `Ptr CChar' peek*} -> `SomeTypeFPtr' #}

它以我得到的方式工作

someTypeCreate :: String -> IO (SomeTypeFPtr, (Ptr CChar))

但是如何让它返回IO (SomeTypeFPtr, String)
(或者更好的IO (Either String SomeTypeFPtr)),因为String 代表错误)?

我假设我应该使用/编写一个不同的编组器来使用,而不是 peek,它会转换结果类型,但我不太明白该怎么做。

【问题讨论】:

    标签: haskell c2hs


    【解决方案1】:

    我想我已经想通了,我刚刚写了以下编组器:

    nullableM :: (Ptr a -> IO b) -> Ptr a -> IO (Maybe b)
    nullableM f ptr = if ptr == nullPtr
        then return Nothing
        else liftM Just $ f ptr
    {-# INLINE nullableM #-}
    
    toStringMaybe :: CString -> IO (Maybe String)
    toStringMaybe = nullableM peekCString 
    {-# INLINE toStringMaybe #-}
    
    peekStringMaybe :: Ptr CString -> IO (Maybe String)
    peekStringMaybe x = peek x >>= toStringMaybe
    {-# INLINE peekStringMaybe #-}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多