hs-ffmpeg-0.3.2: src/Media/FFMpeg/Common.hsc
-- -*- haskell -*-
{-# LANGUAGE ForeignFunctionInterface, ScopedTypeVariables #-}
{- |Module 'Media.FFMpeg.Common' common definitions and helpers
(c) 2009 Vasyl Pasternak
-}
module Media.FFMpeg.Common
(
ExternalPointer (..)
,cToInt
,cFromInt
,cToEnum
,cFromEnum
,justPtr
,combineBitMasks
)where
--
-- |ExternalPointer class, used to define method withThis
-- similar to 'Foreign.Marshal.Utils.with' but without need Storable specifier
--
import Foreign.Ptr (Ptr, nullPtr)
import Data.Bits (Bits, (.|.))
class ExternalPointer a where
withThis :: a -> (Ptr b -> IO c) -> IO c
--
-- Some conversion functions
--
cToInt :: (Integral a) => a -> Int
cToInt = fromIntegral
cFromInt :: (Num a) => Int -> a
cFromInt = fromIntegral
cToEnum :: (Integral a, Enum b) => a -> b
cToEnum = toEnum . fromIntegral
cFromEnum :: (Enum a, Num b) => a -> b
cFromEnum = fromIntegral . fromEnum
justPtr :: Ptr a -> Maybe (Ptr a)
justPtr p | p == nullPtr = Nothing
| otherwise = Just p
combineBitMasks :: (Enum a, Bits b) => [a] -> b
combineBitMasks = foldl (.|.) 0 . map cFromEnum