summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjunjihashimoto <>2020-02-13 10:04:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2020-02-13 10:04:00 (GMT)
commitec45b737891027d8193c9a179d83806b9c4a51a8 (patch)
tree23af7efe4df0038b9d136acc704d33223714ed9a
parentfeadad85e485e8509e60b05e9172a7b4e0472957 (diff)
version 0.1.1.00.1.1.0
-rw-r--r--HsWebots.cabal4
-rw-r--r--src/Webots/Supervisor.hs46
2 files changed, 29 insertions, 21 deletions
diff --git a/HsWebots.cabal b/HsWebots.cabal
index ef2b417..f4d266d 100644
--- a/HsWebots.cabal
+++ b/HsWebots.cabal
@@ -4,10 +4,10 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
--- hash: 3b6e21d4ea12644e25ba1c2ef63bf2354c2a9c2624c04cf194be97b696647a81
+-- hash: d7399ff5367c55e2b2afaaae32dcf8116890e0a71844b15a4b105fd4f5564ee8
name: HsWebots
-version: 0.1.0.1
+version: 0.1.1.0
synopsis: Webots bindings for Haskell
description: Please see the README on GitHub at <https://github.com/junjihashimoto/HsWebots#readme>
category: Robotics
diff --git a/src/Webots/Supervisor.hs b/src/Webots/Supervisor.hs
index 3b88c7b..dfe208a 100644
--- a/src/Webots/Supervisor.hs
+++ b/src/Webots/Supervisor.hs
@@ -141,10 +141,14 @@ wb_supervisor_node_get_type :: WbNodeRef -> IO WbNodeType
wb_supervisor_node_get_type node =
[C.exp| WbNodeType { wb_supervisor_node_get_type($(WbNodeRef node)) } |]
-wb_supervisor_node_get_field :: WbNodeRef -> String -> IO WbFieldRef
-wb_supervisor_node_get_field node field_name =
- withCString field_name $ \field_name' ->
- [C.exp| WbFieldRef { wb_supervisor_node_get_field($(WbNodeRef node), $(const char* field_name')) } |]
+wb_supervisor_node_get_field :: WbNodeRef -> String -> IO (Maybe WbFieldRef)
+wb_supervisor_node_get_field node field_name = do
+ ptr <- withCString field_name $ \field_name' ->
+ [C.exp| WbFieldRef { wb_supervisor_node_get_field($(WbNodeRef node), $(const char* field_name')) } |]
+ if ptr == nullPtr
+ then return Nothing
+ else return $ Just ptr
+
wb_supervisor_node_remove :: WbNodeRef -> IO ()
wb_supervisor_node_remove node =
@@ -500,10 +504,8 @@ fieldGetSfString ptr =
fieldGetMfNode :: WbFieldRef -> CInt -> IO WbNodeRef
fieldGetMfNode ptr i = [C.exp| WbNodeRef { wb_supervisor_field_get_mf_node($(WbFieldRef ptr), $(int i)) } |]
-getField :: WbNodeRef -> String -> IO WbFieldRef
-getField ptr str =
- withCString str $ \cstr ->
- [C.exp| WbFieldRef { wb_supervisor_node_get_field($(WbNodeRef ptr), $(char* cstr)) } |]
+getField :: WbNodeRef -> String -> IO (Maybe WbFieldRef)
+getField ptr str = wb_supervisor_node_get_field ptr str
getId :: WbNodeRef -> IO CInt
getId ptr =
@@ -535,14 +537,20 @@ whenM cond block = if cond then block else return ()
getRootNodes :: IO [(WbNodeRef,CInt,String)]
getRootNodes = do
root <- getRootNode
- children <- getField root "children"
- cnt <- fieldGetCount children
- forM [0..(cnt-1)] $ \i -> do
- node <- fieldGetMfNode children i
- nodeId <- getId node
- typeName <- getTypeName node
- return (node,nodeId,typeName)
-
-getNodeFieldByString :: WbNodeRef -> String -> IO String
-getNodeFieldByString node label = getField node label >>= fieldGetSfString
-
+ mchildren <- getField root "children"
+ case mchildren of
+ Nothing -> return []
+ Just children -> do
+ cnt <- fieldGetCount children
+ forM [0..(cnt-1)] $ \i -> do
+ node <- fieldGetMfNode children i
+ nodeId <- getId node
+ typeName <- getTypeName node
+ return (node,nodeId,typeName)
+
+getNodeFieldByString :: WbNodeRef -> String -> IO (Maybe String)
+getNodeFieldByString node label = do
+ m <- getField node label
+ case m of
+ Nothing -> return Nothing
+ Just field -> fieldGetSfString field >>= return.Just