{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeOperators     #-}

module HaskellWorks.Data.FromForeignRegion
  ( FromForeignRegion(..)
  , ForeignRegion
  , mmapFromForeignRegion
  ) where

import Data.Word
import Foreign.ForeignPtr
import HaskellWorks.Data.Product

import qualified Data.ByteString          as BS
import qualified Data.ByteString.Internal as BSI
import qualified Data.Vector.Storable     as DVS
import qualified System.IO.MMap           as IO

type ForeignRegion = (ForeignPtr Word8, Int, Int)

-- | Class for datastructures that can be created from a foreign region
class FromForeignRegion a where
  -- | Create a value of type @a from a foreign region.
  fromForeignRegion :: ForeignRegion -> a

instance FromForeignRegion BS.ByteString where
  fromForeignRegion :: ForeignRegion -> ByteString
fromForeignRegion (ForeignPtr Word8
fptr, Int
offset, Int
size) = ForeignPtr Word8 -> Int -> Int -> ByteString
BSI.fromForeignPtr (forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr Word8
fptr) Int
offset Int
size
  {-# INLINE fromForeignRegion #-}

instance FromForeignRegion (DVS.Vector Word8) where
  fromForeignRegion :: ForeignRegion -> Vector Word8
fromForeignRegion (ForeignPtr Word8
fptr, Int
offset, Int
size) = forall a. Storable a => ForeignPtr a -> Int -> Int -> Vector a
DVS.unsafeFromForeignPtr (forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr Word8
fptr) Int
offset Int
size
  {-# INLINE fromForeignRegion #-}

instance FromForeignRegion (DVS.Vector Word16) where
  fromForeignRegion :: ForeignRegion -> Vector Word16
fromForeignRegion (ForeignPtr Word8
fptr, Int
offset, Int
size) = forall a. Storable a => ForeignPtr a -> Int -> Int -> Vector a
DVS.unsafeFromForeignPtr (forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr Word8
fptr) Int
offset ((Int
size forall a. Num a => a -> a -> a
+ Int
1) forall a. Integral a => a -> a -> a
`div` Int
2)
  {-# INLINE fromForeignRegion #-}

instance FromForeignRegion (DVS.Vector Word32) where
  fromForeignRegion :: ForeignRegion -> Vector Word32
fromForeignRegion (ForeignPtr Word8
fptr, Int
offset, Int
size) = forall a. Storable a => ForeignPtr a -> Int -> Int -> Vector a
DVS.unsafeFromForeignPtr (forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr Word8
fptr) Int
offset ((Int
size forall a. Num a => a -> a -> a
+ Int
3) forall a. Integral a => a -> a -> a
`div` Int
4)
  {-# INLINE fromForeignRegion #-}

instance FromForeignRegion (DVS.Vector Word64) where
  fromForeignRegion :: ForeignRegion -> Vector Word64
fromForeignRegion (ForeignPtr Word8
fptr, Int
offset, Int
size) = forall a. Storable a => ForeignPtr a -> Int -> Int -> Vector a
DVS.unsafeFromForeignPtr (forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr Word8
fptr) Int
offset ((Int
size forall a. Num a => a -> a -> a
+ Int
7) forall a. Integral a => a -> a -> a
`div` Int
8)
  {-# INLINE fromForeignRegion #-}

instance  ( FromForeignRegion a
          , FromForeignRegion b
          ) => FromForeignRegion (a :*: b) where
  fromForeignRegion :: ForeignRegion -> a :*: b
fromForeignRegion ForeignRegion
r = forall a. FromForeignRegion a => ForeignRegion -> a
fromForeignRegion ForeignRegion
r forall a b. a -> b -> a :*: b
:*: forall a. FromForeignRegion a => ForeignRegion -> a
fromForeignRegion ForeignRegion
r
  {-# INLINE fromForeignRegion #-}

mmapFromForeignRegion :: FromForeignRegion a => FilePath -> IO a
mmapFromForeignRegion :: forall a. FromForeignRegion a => FilePath -> IO a
mmapFromForeignRegion FilePath
filePath = do
  ForeignRegion
region <- forall a.
FilePath
-> Mode -> Maybe (Int64, Int) -> IO (ForeignPtr a, Int, Int)
IO.mmapFileForeignPtr FilePath
filePath Mode
IO.ReadOnly forall a. Maybe a
Nothing
  let !bs :: a
bs = forall a. FromForeignRegion a => ForeignRegion -> a
fromForeignRegion ForeignRegion
region
  forall (m :: * -> *) a. Monad m => a -> m a
return a
bs
{-# INLINE mmapFromForeignRegion #-}