{-# LINE 1 "src/Box2D/Id.hsc" #-}
{- | Low-level bindings to @box2d/id.h@: opaque handles to Box2D objects.

The C API passes these as integer-only structs of at most 8 bytes by value.
On the 64-bit ABIs this package supports (x86-64 SysV and Win64, AArch64)
such a struct travels in a general-purpose register exactly like a
@uint32_t@/@uint64_t@, so the handles are newtypes over words and cross the
FFI directly, with no shim. The payload is the raw struct bytes and carries
no stable meaning; the constructors are exported only so foreign imports can
see through the newtypes. A zero word is the null id, which is also what
zero-initialisation produces on the C side. Struct sizes are pinned by
@_Static_assert@s in @cbits/box2d_shim.c@.

'ContactId' is 12 bytes and does not fit a word, so it keeps a struct-shaped
representation; nothing passes it by value yet.
-}
module Box2D.Id
  ( WorldId (..)
  , BodyId (..)
  , ShapeId (..)
  , ChainId (..)
  , JointId (..)
  , ContactId (..)
  , nullWorldId
  , nullBodyId
  , nullShapeId
  , nullChainId
  , nullJointId
  , nullContactId
  ) where

import Foreign



-- | Handle to a world instance.
newtype WorldId = WorldId Word32
  deriving (WorldId -> WorldId -> Bool
(WorldId -> WorldId -> Bool)
-> (WorldId -> WorldId -> Bool) -> Eq WorldId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WorldId -> WorldId -> Bool
== :: WorldId -> WorldId -> Bool
$c/= :: WorldId -> WorldId -> Bool
/= :: WorldId -> WorldId -> Bool
Eq, Int -> WorldId -> ShowS
[WorldId] -> ShowS
WorldId -> String
(Int -> WorldId -> ShowS)
-> (WorldId -> String) -> ([WorldId] -> ShowS) -> Show WorldId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WorldId -> ShowS
showsPrec :: Int -> WorldId -> ShowS
$cshow :: WorldId -> String
show :: WorldId -> String
$cshowList :: [WorldId] -> ShowS
showList :: [WorldId] -> ShowS
Show)

-- | Handle to a body instance.
newtype BodyId = BodyId Word64
  deriving (BodyId -> BodyId -> Bool
(BodyId -> BodyId -> Bool)
-> (BodyId -> BodyId -> Bool) -> Eq BodyId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BodyId -> BodyId -> Bool
== :: BodyId -> BodyId -> Bool
$c/= :: BodyId -> BodyId -> Bool
/= :: BodyId -> BodyId -> Bool
Eq, Int -> BodyId -> ShowS
[BodyId] -> ShowS
BodyId -> String
(Int -> BodyId -> ShowS)
-> (BodyId -> String) -> ([BodyId] -> ShowS) -> Show BodyId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BodyId -> ShowS
showsPrec :: Int -> BodyId -> ShowS
$cshow :: BodyId -> String
show :: BodyId -> String
$cshowList :: [BodyId] -> ShowS
showList :: [BodyId] -> ShowS
Show)

-- | Handle to a shape instance.
newtype ShapeId = ShapeId Word64
  deriving (ShapeId -> ShapeId -> Bool
(ShapeId -> ShapeId -> Bool)
-> (ShapeId -> ShapeId -> Bool) -> Eq ShapeId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ShapeId -> ShapeId -> Bool
== :: ShapeId -> ShapeId -> Bool
$c/= :: ShapeId -> ShapeId -> Bool
/= :: ShapeId -> ShapeId -> Bool
Eq, Int -> ShapeId -> ShowS
[ShapeId] -> ShowS
ShapeId -> String
(Int -> ShapeId -> ShowS)
-> (ShapeId -> String) -> ([ShapeId] -> ShowS) -> Show ShapeId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ShapeId -> ShowS
showsPrec :: Int -> ShapeId -> ShowS
$cshow :: ShapeId -> String
show :: ShapeId -> String
$cshowList :: [ShapeId] -> ShowS
showList :: [ShapeId] -> ShowS
Show)

-- | Handle to a chain instance.
newtype ChainId = ChainId Word64
  deriving (ChainId -> ChainId -> Bool
(ChainId -> ChainId -> Bool)
-> (ChainId -> ChainId -> Bool) -> Eq ChainId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChainId -> ChainId -> Bool
== :: ChainId -> ChainId -> Bool
$c/= :: ChainId -> ChainId -> Bool
/= :: ChainId -> ChainId -> Bool
Eq, Int -> ChainId -> ShowS
[ChainId] -> ShowS
ChainId -> String
(Int -> ChainId -> ShowS)
-> (ChainId -> String) -> ([ChainId] -> ShowS) -> Show ChainId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChainId -> ShowS
showsPrec :: Int -> ChainId -> ShowS
$cshow :: ChainId -> String
show :: ChainId -> String
$cshowList :: [ChainId] -> ShowS
showList :: [ChainId] -> ShowS
Show)

-- | Handle to a joint instance.
newtype JointId = JointId Word64
  deriving (JointId -> JointId -> Bool
(JointId -> JointId -> Bool)
-> (JointId -> JointId -> Bool) -> Eq JointId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JointId -> JointId -> Bool
== :: JointId -> JointId -> Bool
$c/= :: JointId -> JointId -> Bool
/= :: JointId -> JointId -> Bool
Eq, Int -> JointId -> ShowS
[JointId] -> ShowS
JointId -> String
(Int -> JointId -> ShowS)
-> (JointId -> String) -> ([JointId] -> ShowS) -> Show JointId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JointId -> ShowS
showsPrec :: Int -> JointId -> ShowS
$cshow :: JointId -> String
show :: JointId -> String
$cshowList :: [JointId] -> ShowS
showList :: [JointId] -> ShowS
Show)

-- | Handle to a contact instance.
data ContactId = ContactId Int32 Word16 Word32
  deriving (ContactId -> ContactId -> Bool
(ContactId -> ContactId -> Bool)
-> (ContactId -> ContactId -> Bool) -> Eq ContactId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ContactId -> ContactId -> Bool
== :: ContactId -> ContactId -> Bool
$c/= :: ContactId -> ContactId -> Bool
/= :: ContactId -> ContactId -> Bool
Eq, Int -> ContactId -> ShowS
[ContactId] -> ShowS
ContactId -> String
(Int -> ContactId -> ShowS)
-> (ContactId -> String)
-> ([ContactId] -> ShowS)
-> Show ContactId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ContactId -> ShowS
showsPrec :: Int -> ContactId -> ShowS
$cshow :: ContactId -> String
show :: ContactId -> String
$cshowList :: [ContactId] -> ShowS
showList :: [ContactId] -> ShowS
Show)

-- The word's alignment is at least the struct's, so alloca'd space is always
-- aligned enough for the aliased peek/poke.

instance Storable WorldId where
  sizeOf :: WorldId -> Int
sizeOf WorldId
_ = (Int
4)
{-# LINE 64 "src/Box2D/Id.hsc" #-}
  alignment _ = alignment (0 :: Word32)
  peek :: Ptr WorldId -> IO WorldId
peek Ptr WorldId
p = Word32 -> WorldId
WorldId (Word32 -> WorldId) -> IO Word32 -> IO WorldId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word32 -> IO Word32
forall a. Storable a => Ptr a -> IO a
peek (Ptr WorldId -> Ptr Word32
forall a b. Ptr a -> Ptr b
castPtr Ptr WorldId
p)
  poke :: Ptr WorldId -> WorldId -> IO ()
poke Ptr WorldId
p (WorldId Word32
w) = Ptr Word32 -> Word32 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr WorldId -> Ptr Word32
forall a b. Ptr a -> Ptr b
castPtr Ptr WorldId
p) Word32
w

instance Storable BodyId where
  sizeOf :: BodyId -> Int
sizeOf BodyId
_ = (Int
8)
{-# LINE 70 "src/Box2D/Id.hsc" #-}
  alignment _ = alignment (0 :: Word64)
  peek :: Ptr BodyId -> IO BodyId
peek Ptr BodyId
p = Word64 -> BodyId
BodyId (Word64 -> BodyId) -> IO Word64 -> IO BodyId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word64 -> IO Word64
forall a. Storable a => Ptr a -> IO a
peek (Ptr BodyId -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr Ptr BodyId
p)
  poke :: Ptr BodyId -> BodyId -> IO ()
poke Ptr BodyId
p (BodyId Word64
w) = Ptr Word64 -> Word64 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr BodyId -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr Ptr BodyId
p) Word64
w

instance Storable ShapeId where
  sizeOf :: ShapeId -> Int
sizeOf ShapeId
_ = (Int
8)
{-# LINE 76 "src/Box2D/Id.hsc" #-}
  alignment _ = alignment (0 :: Word64)
  peek :: Ptr ShapeId -> IO ShapeId
peek Ptr ShapeId
p = Word64 -> ShapeId
ShapeId (Word64 -> ShapeId) -> IO Word64 -> IO ShapeId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word64 -> IO Word64
forall a. Storable a => Ptr a -> IO a
peek (Ptr ShapeId -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr Ptr ShapeId
p)
  poke :: Ptr ShapeId -> ShapeId -> IO ()
poke Ptr ShapeId
p (ShapeId Word64
w) = Ptr Word64 -> Word64 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr ShapeId -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr Ptr ShapeId
p) Word64
w

instance Storable ChainId where
  sizeOf :: ChainId -> Int
sizeOf ChainId
_ = (Int
8)
{-# LINE 82 "src/Box2D/Id.hsc" #-}
  alignment _ = alignment (0 :: Word64)
  peek :: Ptr ChainId -> IO ChainId
peek Ptr ChainId
p = Word64 -> ChainId
ChainId (Word64 -> ChainId) -> IO Word64 -> IO ChainId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word64 -> IO Word64
forall a. Storable a => Ptr a -> IO a
peek (Ptr ChainId -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr Ptr ChainId
p)
  poke :: Ptr ChainId -> ChainId -> IO ()
poke Ptr ChainId
p (ChainId Word64
w) = Ptr Word64 -> Word64 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr ChainId -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr Ptr ChainId
p) Word64
w

instance Storable JointId where
  sizeOf :: JointId -> Int
sizeOf JointId
_ = (Int
8)
{-# LINE 88 "src/Box2D/Id.hsc" #-}
  alignment _ = alignment (0 :: Word64)
  peek :: Ptr JointId -> IO JointId
peek Ptr JointId
p = Word64 -> JointId
JointId (Word64 -> JointId) -> IO Word64 -> IO JointId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word64 -> IO Word64
forall a. Storable a => Ptr a -> IO a
peek (Ptr JointId -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr Ptr JointId
p)
  poke :: Ptr JointId -> JointId -> IO ()
poke Ptr JointId
p (JointId Word64
w) = Ptr Word64 -> Word64 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr JointId -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr Ptr JointId
p) Word64
w

instance Storable ContactId where
  sizeOf :: ContactId -> Int
sizeOf ContactId
_ = (Int
12)
{-# LINE 94 "src/Box2D/Id.hsc" #-}
  alignment _ = 4
{-# LINE 95 "src/Box2D/Id.hsc" #-}
  peek p =
    ContactId
      <$> (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 98 "src/Box2D/Id.hsc" #-}
      <*> (\hsc_ptr -> peekByteOff hsc_ptr 4) p
{-# LINE 99 "src/Box2D/Id.hsc" #-}
      <*> (\hsc_ptr -> peekByteOff hsc_ptr 8) p
{-# LINE 100 "src/Box2D/Id.hsc" #-}
  poke p (ContactId i w g) = do
    (\hsc_ptr -> pokeByteOff hsc_ptr 0) p i
{-# LINE 102 "src/Box2D/Id.hsc" #-}
    (\hsc_ptr -> pokeByteOff hsc_ptr 4) p w
{-# LINE 103 "src/Box2D/Id.hsc" #-}
    (\hsc_ptr -> pokeByteOff hsc_ptr 6) p (0 :: Int16)
{-# LINE 104 "src/Box2D/Id.hsc" #-}
    (\hsc_ptr -> pokeByteOff hsc_ptr 8) p g
{-# LINE 105 "src/Box2D/Id.hsc" #-}

nullWorldId :: WorldId
nullWorldId :: WorldId
nullWorldId = Word32 -> WorldId
WorldId Word32
0

nullBodyId :: BodyId
nullBodyId :: BodyId
nullBodyId = Word64 -> BodyId
BodyId Word64
0

nullShapeId :: ShapeId
nullShapeId :: ShapeId
nullShapeId = Word64 -> ShapeId
ShapeId Word64
0

nullChainId :: ChainId
nullChainId :: ChainId
nullChainId = Word64 -> ChainId
ChainId Word64
0

nullJointId :: JointId
nullJointId :: JointId
nullJointId = Word64 -> JointId
JointId Word64
0

nullContactId :: ContactId
nullContactId :: ContactId
nullContactId = Int32 -> Word16 -> Word32 -> ContactId
ContactId Int32
0 Word16
0 Word32
0