packages feed

Hs2lib-0.4.8: WinDll/Lib/Tuples.hsc

{-# LANGUAGE FlexibleInstances        #-}
{-# LANGUAGE MultiParamTypeClasses    #-}
{-# LANGUAGE TypeSynonymInstances     #-}
{-# LANGUAGE ForeignFunctionInterface #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Windll
-- Copyright   :  (c) Tamar Christina 2009 - 2010
-- License     :  BSD3
-- 
-- Maintainer  :  tamar@zhox.com
-- Stability   :  experimental
-- Portability :  portable
--
-- Module containing definitions for tuples, since those can't be automatically
-- translated. We This file contains predefined mappings of tuples till 8-tuples.
-- If you need more you need to unfortunately add these yourself
--
-----------------------------------------------------------------------------

module WinDll.Lib.Tuples where

import WinDll.Lib.NativeMapping

import Foreign
import Foreign.C
import Foreign.Ptr
import Foreign.Marshal.Alloc
import Foreign.Marshal.Utils

import Control.Monad
import Control.Monad.Instances

#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)

#include "Tuples.h"

-- * The datatypes to replace the tupples with

data Tuple2 a b             = Tuple2 a b
data Tuple3 a b c           = Tuple3 a b c
data Tuple4 a b c d         = Tuple4 a b c d
data Tuple5 a b c d e       = Tuple5 a b c d e
data Tuple6 a b c d e f     = Tuple6 a b c d e f
data Tuple7 a b c d e f g   = Tuple7 a b c d e f g
data Tuple8 a b c d e f g h = Tuple8 a b c d e f g h

-- * Type namings

type Tuple2Ptr a b             = Ptr (Tuple2 a b)
type Tuple3Ptr a b c           = Ptr (Tuple3 a b c)
type Tuple4Ptr a b c d         = Ptr (Tuple4 a b c d)
type Tuple5Ptr a b c d e       = Ptr (Tuple5 a b c d e)
type Tuple6Ptr a b c d e f     = Ptr (Tuple6 a b c d e f)
type Tuple7Ptr a b c d e f g   = Ptr (Tuple7 a b c d e f g)
type Tuple8Ptr a b c d e f g h = Ptr (Tuple8 a b c d e f g h)

-- * Functor instances so that these new tuple types can
--   fit into the functor instance of the FFIType class.
instance Storable a => Functor (Tuple2 a) where
    fmap f (Tuple2 a b) = Tuple2 a (f b)
    
instance Storable a => Functor (Tuple3 a b) where
    fmap f (Tuple3 a b c) = Tuple3 a b (f c)
    
instance Storable a => Functor (Tuple4 a b c) where
    fmap f (Tuple4 a b c d) = Tuple4 a b c (f d)
    
instance Storable a => Functor (Tuple5 a b c d) where
    fmap f (Tuple5 a b c d e) = Tuple5 a b c d (f e)
    
instance Storable a => Functor (Tuple6 a b c d e) where
    fmap f (Tuple6 a b c d e f') = Tuple6 a b c d e (f f')
    
instance Storable a => Functor (Tuple7 a b c d e f) where
    fmap f (Tuple7 a b c d e f' g) = Tuple7 a b c d e f' (f g)
    
instance Storable a => Functor (Tuple8 a b c d e f g) where
    fmap f (Tuple8 a b c d e f' g h) = Tuple8 a b c d e f' g (f h)

-- * The isomorphic type conversions

instance (FFIType a b,FFIType c d) => FFIType (a,c) 
                                              (Tuple2 b d) where
    toFFI   (a,b)        = (Tuple2 (toFFI a) (toFFI b))
    fromFFI (Tuple2 a b) = (fromFFI a, fromFFI b)    
    
instance (FFIType a b,FFIType c d
         ,FFIType e f) => FFIType (a,c,e) 
                                  (Tuple3 b d f) where
    toFFI   (a,b,c)        = (Tuple3 (toFFI a) (toFFI b) (toFFI c))
    fromFFI (Tuple3 a b c) = (fromFFI a, fromFFI b, fromFFI c)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h) => FFIType (a,c,e,g) 
                                              (Tuple4 b d f h) where
    toFFI   (a,b,c,d)        = (Tuple4 (toFFI a) (toFFI b) (toFFI c) (toFFI d))
    fromFFI (Tuple4 a b c d) = (fromFFI a, fromFFI b, fromFFI c, fromFFI d)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h
         ,FFIType i j) => FFIType (a,c,e,g,i) 
                                  (Tuple5 b d f h j) where
    toFFI   (a,b,c,d,e)        = (Tuple5 (toFFI a) (toFFI b) (toFFI c) (toFFI d) 
                                         (toFFI e))
    fromFFI (Tuple5 a b c d e) = (fromFFI a, fromFFI b, fromFFI c, fromFFI d
                                 ,fromFFI e)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h
         ,FFIType i j,FFIType k l) => FFIType (a,c,e,g,i,k) (Tuple6 b d f h j l) where
    toFFI   (a,b,c,d,e,f)        = (Tuple6 (toFFI a) (toFFI b) (toFFI c) (toFFI d) 
                                           (toFFI e) (toFFI f))
    fromFFI (Tuple6 a b c d e f) = (fromFFI a, fromFFI b, fromFFI c, fromFFI d
                                   ,fromFFI e, fromFFI f)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h
         ,FFIType i j,FFIType k l
         ,FFIType m n) => FFIType (a,c,e,g,i,k,m) 
                                  (Tuple7 b d f h j l n) where
    toFFI   (a,b,c,d,e,f,g)        = (Tuple7 (toFFI a) (toFFI b) (toFFI c) (toFFI d) 
                                             (toFFI e) (toFFI f) (toFFI g))
    fromFFI (Tuple7 a b c d e f g) = (fromFFI a, fromFFI b, fromFFI c, fromFFI d
                                     ,fromFFI e, fromFFI f, fromFFI g)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h
         ,FFIType i j,FFIType k l
         ,FFIType m n,FFIType o p) => FFIType (a,c,e,g,i,k,m,o) 
                                              (Tuple8 b d f h j l n p) where
    toFFI   (a,b,c,d,e,f,g,h)        = (Tuple8 (toFFI a) (toFFI b) (toFFI c) (toFFI d) 
                                               (toFFI e) (toFFI f) (toFFI g) (toFFI h))
    fromFFI (Tuple8 a b c d e f g h) = (fromFFI a, fromFFI b, fromFFI c, fromFFI d
                                       ,fromFFI e, fromFFI f, fromFFI g, fromFFI h)


instance (FFIType a b,FFIType c d
         ,Storable b, Storable d) => FFIType (a,c) 
                                             (Tuple2Ptr b d) where
    toNative   x = new (toFFI x)
    fromNative x = fmap fromFFI (peek x)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,Storable b
         ,Storable d, Storable f) => FFIType (a,c,e) 
                                  (Tuple3Ptr b d f) where
    toNative   x = new (toFFI x)
    fromNative x = fmap fromFFI (peek x)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h
         ,Storable b,Storable d
         ,Storable f,Storable h) => FFIType (a,c,e,g) 
                                            (Tuple4Ptr b d f h) where
    toNative   x = new (toFFI x)
    fromNative x = fmap fromFFI (peek x)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h
         ,FFIType i j,Storable j
         ,Storable b,Storable d
         ,Storable f,Storable h) => FFIType (a,c,e,g,i) 
                                           (Tuple5Ptr b d f h j) where
    toNative   x = new (toFFI x)
    fromNative x = fmap fromFFI (peek x)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h
         ,FFIType i j,FFIType k l
         ,Storable j,Storable l
         ,Storable b,Storable d
         ,Storable f,Storable h) => FFIType (a,c,e,g,i,k) 
                                            (Tuple6Ptr b d f h j l) where
    toNative   x = new (toFFI x)
    fromNative x = fmap fromFFI (peek x)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h
         ,FFIType i j,FFIType k l
         ,FFIType m n,Storable n
         ,Storable j,Storable l
         ,Storable b,Storable d
         ,Storable f,Storable h) => FFIType (a,c,e,g,i,k,m) 
                                            (Tuple7Ptr b d f h j l n) where
    toNative   x = new (toFFI x)
    fromNative x = fmap fromFFI (peek x)
    
instance (FFIType a b,FFIType c d
         ,FFIType e f,FFIType g h
         ,FFIType i j,FFIType k l
         ,FFIType m n,FFIType o p
         ,Storable n,Storable p
         ,Storable j,Storable l
         ,Storable b,Storable d
         ,Storable f,Storable h) => FFIType (a,c,e,g,i,k,m,o) 
                                              (Tuple8Ptr b d f h j l n p) where
    toNative   x = new (toFFI x)
    fromNative x = fmap fromFFI (peek x)
    
-- * Storage instances

instance (Storable a, Storable b) => Storable (Tuple2 a b) where
    sizeOf    _ = #size Tuple2_t
    alignment _ = #alignment Tuple2_t
    
    poke ptr (Tuple2 a1 a2) = do
        (#poke Tuple2_t, tuple2_var1) ptr a1
        (#poke Tuple2_t, tuple2_var2) ptr a2
    peek ptr = do
        a1' <- (#peek Tuple2_t, tuple2_var1) ptr
        a2' <- (#peek Tuple2_t, tuple2_var2) ptr
        return $ (Tuple2 a1' a2')
        
instance (Storable a, Storable b, Storable c) => Storable (Tuple3 a b c) where
    sizeOf    _ = #size Tuple3_t
    alignment _ = #alignment Tuple3_t
    
    poke ptr (Tuple3 a1 a2 a3) = do
        (#poke Tuple3_t, tuple3_var1) ptr a1
        (#poke Tuple3_t, tuple3_var2) ptr a2
        (#poke Tuple3_t, tuple3_var3) ptr a3
    peek ptr = do
        a1' <- (#peek Tuple3_t, tuple3_var1) ptr
        a2' <- (#peek Tuple3_t, tuple3_var2) ptr
        a3' <- (#peek Tuple3_t, tuple3_var3) ptr
        return $ (Tuple3 a1' a2' a3')


instance (Storable a, Storable b, Storable c, Storable d) => Storable (Tuple4 a b c d) where
    sizeOf    _ = #size Tuple4_t
    alignment _ = #alignment Tuple4_t
    
    poke ptr (Tuple4 a1 a2 a3 a4) = do
        (#poke Tuple4_t, tuple4_var1) ptr a1
        (#poke Tuple4_t, tuple4_var2) ptr a2
        (#poke Tuple4_t, tuple4_var3) ptr a3
        (#poke Tuple4_t, tuple4_var4) ptr a4
    peek ptr = do
        a1' <- (#peek Tuple4_t, tuple4_var1) ptr
        a2' <- (#peek Tuple4_t, tuple4_var2) ptr
        a3' <- (#peek Tuple4_t, tuple4_var3) ptr
        a4' <- (#peek Tuple4_t, tuple4_var4) ptr
        return $ (Tuple4 a1' a2' a3' a4')


instance (Storable a, Storable b, Storable c, Storable d, Storable e) => Storable (Tuple5 a b c d e) where
    sizeOf    _ = #size Tuple5_t
    alignment _ = #alignment Tuple5_t
    
    poke ptr (Tuple5 a1 a2 a3 a4 a5) = do
        (#poke Tuple5_t, tuple5_var1) ptr a1
        (#poke Tuple5_t, tuple5_var2) ptr a2
        (#poke Tuple5_t, tuple5_var3) ptr a3
        (#poke Tuple5_t, tuple5_var4) ptr a4
        (#poke Tuple5_t, tuple5_var5) ptr a5
    peek ptr = do
        a1' <- (#peek Tuple5_t, tuple5_var1) ptr
        a2' <- (#peek Tuple5_t, tuple5_var2) ptr
        a3' <- (#peek Tuple5_t, tuple5_var3) ptr
        a4' <- (#peek Tuple5_t, tuple5_var4) ptr
        a5' <- (#peek Tuple5_t, tuple5_var5) ptr
        return $ (Tuple5 a1' a2' a3' a4' a5')


instance (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f) => Storable (Tuple6 a b c d e f) where
    sizeOf    _ = #size Tuple6_t
    alignment _ = #alignment Tuple6_t
    
    poke ptr (Tuple6 a1 a2 a3 a4 a5 a6) = do
        (#poke Tuple6_t, tuple6_var1) ptr a1
        (#poke Tuple6_t, tuple6_var2) ptr a2
        (#poke Tuple6_t, tuple6_var3) ptr a3
        (#poke Tuple6_t, tuple6_var4) ptr a4
        (#poke Tuple6_t, tuple6_var5) ptr a5
        (#poke Tuple6_t, tuple6_var6) ptr a6
    peek ptr = do
        a1' <- (#peek Tuple6_t, tuple6_var1) ptr
        a2' <- (#peek Tuple6_t, tuple6_var2) ptr
        a3' <- (#peek Tuple6_t, tuple6_var3) ptr
        a4' <- (#peek Tuple6_t, tuple6_var4) ptr
        a5' <- (#peek Tuple6_t, tuple6_var5) ptr
        a6' <- (#peek Tuple6_t, tuple6_var6) ptr
        return $ (Tuple6 a1' a2' a3' a4' a5' a6')


instance (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f, Storable g) => Storable (Tuple7 a b c d e f g) where
    sizeOf    _ = #size Tuple7_t
    alignment _ = #alignment Tuple7_t
    
    poke ptr (Tuple7 a1 a2 a3 a4 a5 a6 a7) = do
        (#poke Tuple7_t, tuple7_var1) ptr a1
        (#poke Tuple7_t, tuple7_var2) ptr a2
        (#poke Tuple7_t, tuple7_var3) ptr a3
        (#poke Tuple7_t, tuple7_var4) ptr a4
        (#poke Tuple7_t, tuple7_var5) ptr a5
        (#poke Tuple7_t, tuple7_var6) ptr a6
        (#poke Tuple7_t, tuple7_var7) ptr a7
    peek ptr = do
        a1' <- (#peek Tuple7_t, tuple7_var1) ptr
        a2' <- (#peek Tuple7_t, tuple7_var2) ptr
        a3' <- (#peek Tuple7_t, tuple7_var3) ptr
        a4' <- (#peek Tuple7_t, tuple7_var4) ptr
        a5' <- (#peek Tuple7_t, tuple7_var5) ptr
        a6' <- (#peek Tuple7_t, tuple7_var6) ptr
        a7' <- (#peek Tuple7_t, tuple7_var7) ptr
        return $ (Tuple7 a1' a2' a3' a4' a5' a6' a7')


instance (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f, Storable g, Storable h) => Storable (Tuple8 a b c d e f g h) where
    sizeOf    _ = #size Tuple8_t
    alignment _ = #alignment Tuple8_t
    
    poke ptr (Tuple8 a1 a2 a3 a4 a5 a6 a7 a8) = do
        (#poke Tuple8_t, tuple8_var1) ptr a1
        (#poke Tuple8_t, tuple8_var2) ptr a2
        (#poke Tuple8_t, tuple8_var3) ptr a3
        (#poke Tuple8_t, tuple8_var4) ptr a4
        (#poke Tuple8_t, tuple8_var5) ptr a5
        (#poke Tuple8_t, tuple8_var6) ptr a6
        (#poke Tuple8_t, tuple8_var7) ptr a7
        (#poke Tuple8_t, tuple8_var8) ptr a8
    peek ptr = do
        a1' <- (#peek Tuple8_t, tuple8_var1) ptr
        a2' <- (#peek Tuple8_t, tuple8_var2) ptr
        a3' <- (#peek Tuple8_t, tuple8_var3) ptr
        a4' <- (#peek Tuple8_t, tuple8_var4) ptr
        a5' <- (#peek Tuple8_t, tuple8_var5) ptr
        a6' <- (#peek Tuple8_t, tuple8_var6) ptr
        a7' <- (#peek Tuple8_t, tuple8_var7) ptr
        a8' <- (#peek Tuple8_t, tuple8_var8) ptr
        return $ (Tuple8 a1' a2' a3' a4' a5' a6' a7' a8')
        
instance (Storable a, Storable b) => Storable (a, b) where
    sizeOf    _ = #size Tuple2_t
    alignment _ = #alignment Tuple2_t
    
    poke ptr (a1, a2) = do
        (#poke Tuple2_t, tuple2_var1) ptr a1
        (#poke Tuple2_t, tuple2_var2) ptr a2
    peek ptr = do
        a1' <- (#peek Tuple2_t, tuple2_var1) ptr
        a2' <- (#peek Tuple2_t, tuple2_var2) ptr
        return $ (a1', a2')
        
instance (Storable a, Storable b, Storable c) => Storable (a, b, c) where
    sizeOf    _ = #size Tuple3_t
    alignment _ = #alignment Tuple3_t
    
    poke ptr (a1, a2, a3) = do
        (#poke Tuple3_t, tuple3_var1) ptr a1
        (#poke Tuple3_t, tuple3_var2) ptr a2
        (#poke Tuple3_t, tuple3_var3) ptr a3
    peek ptr = do
        a1' <- (#peek Tuple3_t, tuple3_var1) ptr
        a2' <- (#peek Tuple3_t, tuple3_var2) ptr
        a3' <- (#peek Tuple3_t, tuple3_var3) ptr
        return $ (a1', a2', a3')


instance (Storable a, Storable b, Storable c, Storable d) => Storable (a, b, c, d) where
    sizeOf    _ = #size Tuple4_t
    alignment _ = #alignment Tuple4_t
    
    poke ptr (a1, a2, a3, a4) = do
        (#poke Tuple4_t, tuple4_var1) ptr a1
        (#poke Tuple4_t, tuple4_var2) ptr a2
        (#poke Tuple4_t, tuple4_var3) ptr a3
        (#poke Tuple4_t, tuple4_var4) ptr a4
    peek ptr = do
        a1' <- (#peek Tuple4_t, tuple4_var1) ptr
        a2' <- (#peek Tuple4_t, tuple4_var2) ptr
        a3' <- (#peek Tuple4_t, tuple4_var3) ptr
        a4' <- (#peek Tuple4_t, tuple4_var4) ptr
        return $ (a1', a2', a3', a4')


instance (Storable a, Storable b, Storable c, Storable d, Storable e) => Storable (a, b, c, d, e) where
    sizeOf    _ = #size Tuple5_t
    alignment _ = #alignment Tuple5_t
    
    poke ptr (a1, a2, a3, a4, a5) = do
        (#poke Tuple5_t, tuple5_var1) ptr a1
        (#poke Tuple5_t, tuple5_var2) ptr a2
        (#poke Tuple5_t, tuple5_var3) ptr a3
        (#poke Tuple5_t, tuple5_var4) ptr a4
        (#poke Tuple5_t, tuple5_var5) ptr a5
    peek ptr = do
        a1' <- (#peek Tuple5_t, tuple5_var1) ptr
        a2' <- (#peek Tuple5_t, tuple5_var2) ptr
        a3' <- (#peek Tuple5_t, tuple5_var3) ptr
        a4' <- (#peek Tuple5_t, tuple5_var4) ptr
        a5' <- (#peek Tuple5_t, tuple5_var5) ptr
        return $ (a1', a2', a3', a4', a5')


instance (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f) => Storable (a, b, c, d, e, f) where
    sizeOf    _ = #size Tuple6_t
    alignment _ = #alignment Tuple6_t
    
    poke ptr (a1, a2, a3, a4, a5, a6) = do
        (#poke Tuple6_t, tuple6_var1) ptr a1
        (#poke Tuple6_t, tuple6_var2) ptr a2
        (#poke Tuple6_t, tuple6_var3) ptr a3
        (#poke Tuple6_t, tuple6_var4) ptr a4
        (#poke Tuple6_t, tuple6_var5) ptr a5
        (#poke Tuple6_t, tuple6_var6) ptr a6
    peek ptr = do
        a1' <- (#peek Tuple6_t, tuple6_var1) ptr
        a2' <- (#peek Tuple6_t, tuple6_var2) ptr
        a3' <- (#peek Tuple6_t, tuple6_var3) ptr
        a4' <- (#peek Tuple6_t, tuple6_var4) ptr
        a5' <- (#peek Tuple6_t, tuple6_var5) ptr
        a6' <- (#peek Tuple6_t, tuple6_var6) ptr
        return $ (a1', a2', a3', a4', a5', a6')


instance (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f, Storable g) => Storable (a, b, c, d, e, f, g) where
    sizeOf    _ = #size Tuple7_t
    alignment _ = #alignment Tuple7_t
    
    poke ptr (a1, a2, a3, a4, a5, a6, a7) = do
        (#poke Tuple7_t, tuple7_var1) ptr a1
        (#poke Tuple7_t, tuple7_var2) ptr a2
        (#poke Tuple7_t, tuple7_var3) ptr a3
        (#poke Tuple7_t, tuple7_var4) ptr a4
        (#poke Tuple7_t, tuple7_var5) ptr a5
        (#poke Tuple7_t, tuple7_var6) ptr a6
        (#poke Tuple7_t, tuple7_var7) ptr a7
    peek ptr = do
        a1' <- (#peek Tuple7_t, tuple7_var1) ptr
        a2' <- (#peek Tuple7_t, tuple7_var2) ptr
        a3' <- (#peek Tuple7_t, tuple7_var3) ptr
        a4' <- (#peek Tuple7_t, tuple7_var4) ptr
        a5' <- (#peek Tuple7_t, tuple7_var5) ptr
        a6' <- (#peek Tuple7_t, tuple7_var6) ptr
        a7' <- (#peek Tuple7_t, tuple7_var7) ptr
        return $ (a1', a2', a3', a4', a5', a6', a7')


instance (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f, Storable g, Storable h) => Storable (a, b, c, d, e, f, g, h) where
    sizeOf    _ = #size Tuple8_t
    alignment _ = #alignment Tuple8_t
    
    poke ptr (a1, a2, a3, a4, a5, a6, a7, a8) = do
        (#poke Tuple8_t, tuple8_var1) ptr a1
        (#poke Tuple8_t, tuple8_var2) ptr a2
        (#poke Tuple8_t, tuple8_var3) ptr a3
        (#poke Tuple8_t, tuple8_var4) ptr a4
        (#poke Tuple8_t, tuple8_var5) ptr a5
        (#poke Tuple8_t, tuple8_var6) ptr a6
        (#poke Tuple8_t, tuple8_var7) ptr a7
        (#poke Tuple8_t, tuple8_var8) ptr a8
    peek ptr = do
        a1' <- (#peek Tuple8_t, tuple8_var1) ptr
        a2' <- (#peek Tuple8_t, tuple8_var2) ptr
        a3' <- (#peek Tuple8_t, tuple8_var3) ptr
        a4' <- (#peek Tuple8_t, tuple8_var4) ptr
        a5' <- (#peek Tuple8_t, tuple8_var5) ptr
        a6' <- (#peek Tuple8_t, tuple8_var6) ptr
        a7' <- (#peek Tuple8_t, tuple8_var7) ptr
        a8' <- (#peek Tuple8_t, tuple8_var8) ptr
        return $ (a1', a2', a3', a4', a5', a6', a7', a8')