C2HS编组双指针
C2HS marshalling double pointer
当有函数如:
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))
但是我该怎么做 return IO (SomeTypeFPtr, String)
(或更好 IO (Either String SomeTypeFPtr))
因为 String
表示错误)?
我假设我应该 use/write 使用不同的编组器而不是 peek
来转换结果类型,但我不太明白如何去做。
我想我已经弄明白了,我只是写了以下编组程序:
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 #-}
当有函数如:
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))
但是我该怎么做 return IO (SomeTypeFPtr, String)
(或更好 IO (Either String SomeTypeFPtr))
因为 String
表示错误)?
我假设我应该 use/write 使用不同的编组器而不是 peek
来转换结果类型,但我不太明白如何去做。
我想我已经弄明白了,我只是写了以下编组程序:
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 #-}