    {-# OPTIONS_HADDOCK hide #-}
    --------------------------------------------------------------------------------
    -- |
    -- Module      :  Graphics.Rendering.OpenGL.GL.PeekPoke
    -- Copyright   :  (c) Sven Panne 2002-2019
    -- License     :  BSD3
    --
    -- Maintainer  :  Sven Panne <svenpanne@gmail.com>
    -- Stability   :  stable
    -- Portability :  portable
    --
    -- This is a purely internal module with peek- and poke-related utilities.
    --
    --------------------------------------------------------------------------------

    module Graphics.Rendering.OpenGL.GL.PeekPoke (
       poke1, poke2, poke3, poke4,
       peek1, peek2, peek3, peek4,
       peek1M, peek2M, peek3M, peek4M
    ) where

    import Foreign.Ptr
    import Foreign.Storable

    --------------------------------------------------------------------------------
    -- The implementation is little bit verbose/redundant, but seems to generate
    -- better code than mapM/zipWithM_.

    --------------------------------------------------------------------------------

    {-# INLINE poke1 #-}
    poke1 :: Storable a => Ptr a -> a -> IO ()
    poke1 :: forall a. Storable a => Ptr a -> a -> IO ()
    poke1 Ptr a
    ptr a
    x =
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    0 a
    x

    {-# INLINE poke2 #-}
    poke2 :: Storable a => Ptr a -> a -> a -> IO ()
    poke2 :: forall a. Storable a => Ptr a -> a -> a -> IO ()
    poke2 Ptr a
    ptr a
    x a
    y = do
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    0 a
    x
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    1 a
    y

    {-# INLINE poke3 #-}
    poke3 :: Storable a => Ptr a -> a -> a -> a -> IO ()
    poke3 :: forall a. Storable a => Ptr a -> a -> a -> a -> IO ()
    poke3 Ptr a
    ptr a
    x a
    y a
    z = do
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    0 a
    x
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    1 a
    y
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    2 a
    z

    {-# INLINE poke4 #-}
    poke4 :: Storable a => Ptr a -> a -> a -> a -> a -> IO ()
    poke4 :: forall a. Storable a => Ptr a -> a -> a -> a -> a -> IO ()
    poke4 Ptr a
    ptr a
    x a
    y a
    z a
    w = do
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    0 a
    x
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    1 a
    y
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    2 a
    z
       Ptr a -> Int -> a -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff Ptr a
    ptr Int
    3 a
    w

    --------------------------------------------------------------------------------

    {-# INLINE peek1 #-}
    peek1 :: Storable a => (a -> b) -> Ptr a -> IO b
    peek1 :: forall a b. Storable a => (a -> b) -> Ptr a -> IO b
    peek1 a -> b
    f Ptr a
    ptr = do
       x <- Ptr a -> Int -> IO a
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff Ptr a
    ptr Int
    0
       return $ f x

    {-# INLINE peek2 #-}
    peek2 :: Storable a => (a -> a -> b) -> Ptr a -> IO b
    peek2 :: forall a b. Storable a => (a -> a -> b) -> Ptr a -> IO b
    peek2 a -> a -> b
    f = (a -> a -> IO b) -> Ptr a -> IO b
    forall a b. Storable a => (a -> a -> IO b) -> Ptr a -> IO b
    peek2M ((a -> a -> IO b) -> Ptr a -> IO b)
    -> (a -> a -> IO b) -> Ptr a -> IO b
    forall a b. (a -> b) -> a -> b
    $ \a
    x a
    y -> b -> IO b
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a -> a -> b
    f a
    x a
    y)

    {-# INLINE peek3 #-}
    peek3 :: Storable a => (a -> a -> a -> b) -> Ptr a -> IO b
    peek3 :: forall a b. Storable a => (a -> a -> a -> b) -> Ptr a -> IO b
    peek3 a -> a -> a -> b
    f = (a -> a -> a -> IO b) -> Ptr a -> IO b
    forall a b. Storable a => (a -> a -> a -> IO b) -> Ptr a -> IO b
    peek3M ((a -> a -> a -> IO b) -> Ptr a -> IO b)
    -> (a -> a -> a -> IO b) -> Ptr a -> IO b
    forall a b. (a -> b) -> a -> b
    $ \a
    x a
    y a
    z -> b -> IO b
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a -> a -> a -> b
    f a
    x a
    y a
    z)

    {-# INLINE peek4 #-}
    peek4 :: Storable a => (a -> a -> a -> a -> b) -> Ptr a -> IO b
    peek4 :: forall a b. Storable a => (a -> a -> a -> a -> b) -> Ptr a -> IO b
    peek4 a -> a -> a -> a -> b
    f = (a -> a -> a -> a -> IO b) -> Ptr a -> IO b
    forall a b.
    Storable a =>
    (a -> a -> a -> a -> IO b) -> Ptr a -> IO b
    peek4M ((a -> a -> a -> a -> IO b) -> Ptr a -> IO b)
    -> (a -> a -> a -> a -> IO b) -> Ptr a -> IO b
    forall a b. (a -> b) -> a -> b
    $ \a
    x a
    y a
    z a
    w -> b -> IO b
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a -> a -> a -> a -> b
    f a
    x a
    y a
    z a
    w)

    --------------------------------------------------------------------------------

    {-# INLINE peek1M #-}
    peek1M :: Storable a => (a -> IO b) -> Ptr a -> IO b
    peek1M :: forall a b. Storable a => (a -> IO b) -> Ptr a -> IO b
    peek1M a -> IO b
    f Ptr a
    ptr = do
       x <- Ptr a -> Int -> IO a
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff Ptr a
    ptr Int
    0
       f x

    {-# INLINE peek2M #-}
    peek2M :: Storable a => (a -> a -> IO b) -> Ptr a -> IO b
    peek2M :: forall a b. Storable a => (a -> a -> IO b) -> Ptr a -> IO b
    peek2M a -> a -> IO b
    f Ptr a
    ptr = do
       x <- Ptr a -> Int -> IO a
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff Ptr a
    ptr Int
    0
       y <- peekElemOff ptr 1
       f x y

    {-# INLINE peek3M #-}
    peek3M :: Storable a => (a -> a -> a -> IO b) -> Ptr a -> IO b
    peek3M :: forall a b. Storable a => (a -> a -> a -> IO b) -> Ptr a -> IO b
    peek3M a -> a -> a -> IO b
    f Ptr a
    ptr = do
       x <- Ptr a -> Int -> IO a
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff Ptr a
    ptr Int
    0
       y <- peekElemOff ptr 1
       z <- peekElemOff ptr 2
       f x y z

    {-# INLINE peek4M #-}
    peek4M :: Storable a => (a -> a -> a -> a -> IO b) -> Ptr a -> IO b
    peek4M :: forall a b.
    Storable a =>
    (a -> a -> a -> a -> IO b) -> Ptr a -> IO b
    peek4M a -> a -> a -> a -> IO b
    f Ptr a
    ptr = do
       x <- Ptr a -> Int -> IO a
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff Ptr a
    ptr Int
    0
       y <- peekElemOff ptr 1
       z <- peekElemOff ptr 2
       w <- peekElemOff ptr 3
       f x y z w