C-structs-0.2.0.1: C-Structs implementation for Haskell
Copyright(c) Simon Plakolb 2020
LicenseMIT
Maintainers.plakolb@gmail.com
Stabilitybeta
Safe HaskellNone
LanguageHaskell2010

Foreign.C.Structs

Description

The Foreign.C.Structs module allows you to construct C structs of arbitrary Storable types. It also defined them as instances of the Storable type-class. You can thus create pointers to an instance of such a struct and interface with another language.

Currently up to six records are supported. Each number of records needs its own type. The types are named after the number of records they support: Struct2, Struct3 .. StructN

If a Struct type with more fields is required, it can be created using Template Haskell and the structT function:

structT 8 -- creates a Struct with 8 fields

Field access is provided threefold:

  • Record syntax
2nd :: Struct2 a b -> b
  • Pattern matching
(Struct2 a b)
  • Template Haskell acs function.
$(acs 2 2) :: Struct2 a b -> b
Synopsis

Documentation

data Struct2 a b Source #

A Struct2 can hold two records of any Storable types a and b. It is itself an instance of Storable and can be used inside a Ptr. The Struct2 constructor takes two arguments. The record functions s21st and s22nd provide access to the fields values.

Constructors

Struct2 

Fields

Instances

Instances details
(Eq a, Eq b) => Eq (Struct2 a b) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

(==) :: Struct2 a b -> Struct2 a b -> Bool #

(/=) :: Struct2 a b -> Struct2 a b -> Bool #

(Show a, Show b) => Show (Struct2 a b) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

showsPrec :: Int -> Struct2 a b -> ShowS #

show :: Struct2 a b -> String #

showList :: [Struct2 a b] -> ShowS #

(Storable a, Storable b) => Storable (Struct2 a b) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

sizeOf :: Struct2 a b -> Int #

alignment :: Struct2 a b -> Int #

peekElemOff :: Ptr (Struct2 a b) -> Int -> IO (Struct2 a b) #

pokeElemOff :: Ptr (Struct2 a b) -> Int -> Struct2 a b -> IO () #

peekByteOff :: Ptr b0 -> Int -> IO (Struct2 a b) #

pokeByteOff :: Ptr b0 -> Int -> Struct2 a b -> IO () #

peek :: Ptr (Struct2 a b) -> IO (Struct2 a b) #

poke :: Ptr (Struct2 a b) -> Struct2 a b -> IO () #

data Struct3 a b c Source #

A Struct3 can hold three records of any Storable types a, b and c. It is itself an instance of Storable and can be used inside a Ptr. The Struct3 constructor takes three arguments. The record functions s31st, s32nd and s33rd provide access to the fields values.

Constructors

Struct3 

Fields

Instances

Instances details
(Eq a, Eq b, Eq c) => Eq (Struct3 a b c) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

(==) :: Struct3 a b c -> Struct3 a b c -> Bool #

(/=) :: Struct3 a b c -> Struct3 a b c -> Bool #

(Show a, Show b, Show c) => Show (Struct3 a b c) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

showsPrec :: Int -> Struct3 a b c -> ShowS #

show :: Struct3 a b c -> String #

showList :: [Struct3 a b c] -> ShowS #

(Storable a, Storable b, Storable c) => Storable (Struct3 a b c) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

sizeOf :: Struct3 a b c -> Int #

alignment :: Struct3 a b c -> Int #

peekElemOff :: Ptr (Struct3 a b c) -> Int -> IO (Struct3 a b c) #

pokeElemOff :: Ptr (Struct3 a b c) -> Int -> Struct3 a b c -> IO () #

peekByteOff :: Ptr b0 -> Int -> IO (Struct3 a b c) #

pokeByteOff :: Ptr b0 -> Int -> Struct3 a b c -> IO () #

peek :: Ptr (Struct3 a b c) -> IO (Struct3 a b c) #

poke :: Ptr (Struct3 a b c) -> Struct3 a b c -> IO () #

data Struct4 a b c d Source #

A Struct4 can hold four records of any Storable types a, b, c and d. It is itself an instance of Storable and can be used inside a Ptr. The Struct4 constructor takes four arguments. The record functions s41st, s42nd, s43rd and s44th provide access to the fields values.

Constructors

Struct4 

Fields

Instances

Instances details
(Eq a, Eq b, Eq c, Eq d) => Eq (Struct4 a b c d) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

(==) :: Struct4 a b c d -> Struct4 a b c d -> Bool #

(/=) :: Struct4 a b c d -> Struct4 a b c d -> Bool #

(Show a, Show b, Show c, Show d) => Show (Struct4 a b c d) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

showsPrec :: Int -> Struct4 a b c d -> ShowS #

show :: Struct4 a b c d -> String #

showList :: [Struct4 a b c d] -> ShowS #

(Storable a, Storable b, Storable c, Storable d) => Storable (Struct4 a b c d) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

sizeOf :: Struct4 a b c d -> Int #

alignment :: Struct4 a b c d -> Int #

peekElemOff :: Ptr (Struct4 a b c d) -> Int -> IO (Struct4 a b c d) #

pokeElemOff :: Ptr (Struct4 a b c d) -> Int -> Struct4 a b c d -> IO () #

peekByteOff :: Ptr b0 -> Int -> IO (Struct4 a b c d) #

pokeByteOff :: Ptr b0 -> Int -> Struct4 a b c d -> IO () #

peek :: Ptr (Struct4 a b c d) -> IO (Struct4 a b c d) #

poke :: Ptr (Struct4 a b c d) -> Struct4 a b c d -> IO () #

data Struct5 a b c d e Source #

A Struct5 can hold five records of any Storable types a, b, c, d and e. It is itself an instance of Storable and can be used inside a Ptr. The Struct5 constructor takes five arguments. The record functions s51st, s52nd, s53rd, s54th and s55th provide access to the fields values.

Constructors

Struct5 

Fields

Instances

Instances details
(Eq a, Eq b, Eq c, Eq d, Eq e) => Eq (Struct5 a b c d e) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

(==) :: Struct5 a b c d e -> Struct5 a b c d e -> Bool #

(/=) :: Struct5 a b c d e -> Struct5 a b c d e -> Bool #

(Show a, Show b, Show c, Show d, Show e) => Show (Struct5 a b c d e) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

showsPrec :: Int -> Struct5 a b c d e -> ShowS #

show :: Struct5 a b c d e -> String #

showList :: [Struct5 a b c d e] -> ShowS #

(Storable a, Storable b, Storable c, Storable d, Storable e) => Storable (Struct5 a b c d e) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

sizeOf :: Struct5 a b c d e -> Int #

alignment :: Struct5 a b c d e -> Int #

peekElemOff :: Ptr (Struct5 a b c d e) -> Int -> IO (Struct5 a b c d e) #

pokeElemOff :: Ptr (Struct5 a b c d e) -> Int -> Struct5 a b c d e -> IO () #

peekByteOff :: Ptr b0 -> Int -> IO (Struct5 a b c d e) #

pokeByteOff :: Ptr b0 -> Int -> Struct5 a b c d e -> IO () #

peek :: Ptr (Struct5 a b c d e) -> IO (Struct5 a b c d e) #

poke :: Ptr (Struct5 a b c d e) -> Struct5 a b c d e -> IO () #

data Struct6 a b c d e f Source #

A Struct6 can hold six records of any Storable types a, b, c, d, e and f. It is itself an instance of Storable and can be used inside a Ptr. The Struct6 constructor takes six arguments. The record functions s61st, s62nd, s63rd, s64th, s65th and s66th provide access to the fields values.

Constructors

Struct6 

Fields

Instances

Instances details
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f) => Eq (Struct6 a b c d e f) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

(==) :: Struct6 a b c d e f -> Struct6 a b c d e f -> Bool #

(/=) :: Struct6 a b c d e f -> Struct6 a b c d e f -> Bool #

(Show a, Show b, Show c, Show d, Show e, Show f) => Show (Struct6 a b c d e f) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

showsPrec :: Int -> Struct6 a b c d e f -> ShowS #

show :: Struct6 a b c d e f -> String #

showList :: [Struct6 a b c d e f] -> ShowS #

(Storable a, Storable b, Storable c, Storable d, Storable e, Storable f) => Storable (Struct6 a b c d e f) Source # 
Instance details

Defined in Foreign.C.Structs.Types

Methods

sizeOf :: Struct6 a b c d e f -> Int #

alignment :: Struct6 a b c d e f -> Int #

peekElemOff :: Ptr (Struct6 a b c d e f) -> Int -> IO (Struct6 a b c d e f) #

pokeElemOff :: Ptr (Struct6 a b c d e f) -> Int -> Struct6 a b c d e f -> IO () #

peekByteOff :: Ptr b0 -> Int -> IO (Struct6 a b c d e f) #

pokeByteOff :: Ptr b0 -> Int -> Struct6 a b c d e f -> IO () #

peek :: Ptr (Struct6 a b c d e f) -> IO (Struct6 a b c d e f) #

poke :: Ptr (Struct6 a b c d e f) -> Struct6 a b c d e f -> IO () #

structT :: Int -> DecsQ Source #

All StructN types and their instances of Storable are declared using structT. It can theoretically create C structs with an infinite number of fields. The parameter of structT is the number of fields the struct type should have. Its constructor and type will both be named StructN where N is equal to the argument to structT.

acs :: Int -> Int -> ExpQ Source #

Access function for fields of a StructN where N is the number of fields in the struct. N is the first argument passed to acs, while the second is the field number. The first field has number 1, the second 2 and so on.

s = Struct4 1 2 3 4
$(acs 4 3) s

next :: (Storable a, Storable b, Storable c) => Ptr a -> b -> IO (Ptr c) Source #

Jumps to the next pointer location in the struct.

sizeof :: [Int] -> [Int] -> Int Source #

Due to alignment constraints the size of C structs is dependent on the order of fields and their respectible sizes. The function sizeof can calculate the resulting size given a list of all alignments and sizes.

fmax :: Integral a => [a] -> a Source #

Alias for foldr max 0.