Copyright | (c) Mateusz Kłoczko 2016 |
---|---|
License | MIT |
Maintainer | mateusz.p.kloczko@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- class GStorable' f where
- gpeekByteOff' :: [Int] -> Int -> Ptr b -> Int -> IO (f a)
- gpokeByteOff' :: [Int] -> Int -> Ptr b -> Int -> f a -> IO ()
- glistSizeOf' :: f a -> [Size]
- glistAlignment' :: f a -> [Alignment]
- class GStorable a where
- gsizeOf :: a -> Int
- galignment :: a -> Int
- gpeekByteOff :: Ptr b -> Int -> IO a
- gpokeByteOff :: Ptr b -> Int -> a -> IO ()
- class Storable a where
- class GStorableSum' f where
- seeFirstByte' :: f p -> Int -> Word8
- gsizeOfSum' :: f p -> Int
- alignOfSum' :: f p -> Int
- gpeekByteOffSum' :: Int -> Ptr b -> Int -> IO (f p)
- gpokeByteOffSum' :: Ptr b -> Int -> f p -> IO ()
- class GStorableChoice' (choice :: Bool) a where
- chSizeOf :: proxy choice -> a -> Int
- chAlignment :: proxy choice -> a -> Int
- chPeekByteOff :: proxy choice -> Ptr b -> Int -> IO a
- chPokeByteOff :: proxy choice -> Ptr b -> Int -> a -> IO ()
- type GStorableChoice a = GStorableChoice' (IsSumType (Rep a)) a
- internalTagValue :: (KnownNat (SumArity (Rep a)), GStorableSum' (Rep a), Generic a) => a -> Word8
- internalSizeOf :: forall f p. GStorable' f => f p -> Int
- internalAlignment :: forall f p. GStorable' f => f p -> Alignment
- internalPeekByteOff :: forall f p b. (KnownNat (NoFields f), GStorable' f) => Ptr b -> Offset -> IO (f p)
- internalPokeByteOff :: forall f p b. (KnownNat (NoFields f), GStorable' f) => Ptr b -> Offset -> f p -> IO ()
- internalOffsets :: forall f p. GStorable' f => f p -> [Offset]
Documentation
class GStorable' f where Source #
:: [Int] | List of fields' offsets for the type/struct. |
-> Int | The index. Used to obtain the correct offset |
-> Ptr b | The pointer to the type/struct. |
-> Int | Global offset. |
-> IO (f a) | The result, wrapped in GHC.Generic metadata. | Write the element at a given offset. Additional information about the offests of the subfields are needed. |
Read the element at a given offset. Additional information about the offests of the subfields are needed.
:: [Int] | List of fields' offsets for the type/struct. |
-> Int | The index. Used to obtain the correct offset. |
-> Ptr b | The pointer to the type/struct. |
-> Int | Global offset. |
-> f a | The element to write, wrapped in GHC.Generic metadata. |
-> IO () |
:: f a | GHC.Generic information about a given type/struct. |
-> [Size] | List of sizes. |
Calculates the sizes of type's/struct's fields.
:: f a | GHC.Generic information about a given type/struct. |
-> [Alignment] | List of alignments. |
Calculates the alignments of type's/struct's fields.
Instances
GStorable' (U1 :: Type -> Type) Source # | |
Storable a => GStorable' (K1 i a :: Type -> Type) Source # | |
(KnownNat (NoFields f), KnownNat (NoFields g), GStorable' f, GStorable' g) => GStorable' (f :*: g) Source # | |
GStorable' f => GStorable' (M1 i t f) Source # | |
class GStorable a where Source #
The class uses the default Generic based implementations to
provide Storable instances for types made from primitive types.
Sum types work with sumtypes
cabal flag enabled - or
just with -DGSTORABLE_SUMTYPES cpp flag.
Nothing
:: a | Element of a given type. Can be undefined. |
-> Int | Size. |
Calculate the size of the type.
default gsizeOf :: (ConstraintsSize a, GStorableChoice a) => a -> Int Source #
:: a | Element of a given type. Can be undefined |
-> Int | Alignment. |
Calculate the alignment of the type.
default galignment :: (ConstraintsAlignment a, GStorableChoice a) => a -> Int Source #
Read the variable from a given pointer.
default gpeekByteOff :: (GStorableChoice a, ConstraintsPeek a) => Ptr b -> Int -> IO a Source #
Write the variable to a pointer.
default gpokeByteOff :: (GStorableChoice a, ConstraintsPoke a) => Ptr b -> Int -> a -> IO () Source #
The member functions of this class facilitate writing values of primitive types to raw memory (which may have been allocated with the above mentioned routines) and reading values from blocks of raw memory. The class, furthermore, includes support for computing the storage requirements and alignment restrictions of storable types.
Memory addresses are represented as values of type
, for some
Ptr
aa
which is an instance of class Storable
. The type argument to
Ptr
helps provide some valuable type safety in FFI code (you can't
mix pointers of different types without an explicit cast), while
helping the Haskell type system figure out which marshalling method is
needed for a given pointer.
All marshalling between Haskell and a foreign language ultimately
boils down to translating Haskell data structures into the binary
representation of a corresponding data structure of the foreign
language and vice versa. To code this marshalling in Haskell, it is
necessary to manipulate primitive data types stored in unstructured
memory blocks. The class Storable
facilitates this manipulation on
all types for which it is instantiated, which are the standard basic
types of Haskell, the fixed size Int
types (Int8
, Int16
,
Int32
, Int64
), the fixed size Word
types (Word8
, Word16
,
Word32
, Word64
), StablePtr
, all types from Foreign.C.Types,
as well as Ptr
.
sizeOf, alignment, (peek | peekElemOff | peekByteOff), (poke | pokeElemOff | pokeByteOff)
Computes the storage requirements (in bytes) of the argument. The value of the argument is not used.
Computes the alignment constraint of the argument. An
alignment constraint x
is fulfilled by any address divisible
by x
. The value of the argument is not used.
peekElemOff :: Ptr a -> Int -> IO a #
Read a value from a memory area regarded as an array
of values of the same kind. The first argument specifies
the start address of the array and the second the index into
the array (the first element of the array has index
0
). The following equality holds,
peekElemOff addr idx = IOExts.fixIO $ \result -> peek (addr `plusPtr` (idx * sizeOf result))
Note that this is only a specification, not necessarily the concrete implementation of the function.
pokeElemOff :: Ptr a -> Int -> a -> IO () #
Write a value to a memory area regarded as an array of values of the same kind. The following equality holds:
pokeElemOff addr idx x = poke (addr `plusPtr` (idx * sizeOf x)) x
peekByteOff :: Ptr b -> Int -> IO a #
Read a value from a memory location given by a base address and offset. The following equality holds:
peekByteOff addr off = peek (addr `plusPtr` off)
pokeByteOff :: Ptr b -> Int -> a -> IO () #
Write a value to a memory location given by a base address and offset. The following equality holds:
pokeByteOff addr off x = poke (addr `plusPtr` off) x
Read a value from the given memory location.
Note that the peek and poke functions might require properly
aligned addresses to function correctly. This is architecture
dependent; thus, portable code should ensure that when peeking or
poking values of some type a
, the alignment
constraint for a
, as given by the function
alignment
is fulfilled.
Write the given value to the given memory location. Alignment
restrictions might apply; see peek
.
Instances
class GStorableSum' f where Source #
Work on the sum type.
seeFirstByte' :: f p -> Int -> Word8 Source #
gsizeOfSum' :: f p -> Int Source #
The size of the biggest subtree
alignOfSum' :: f p -> Int Source #
Alignment of the biggest subtree
gpeekByteOffSum' :: Int -> Ptr b -> Int -> IO (f p) Source #
Peek the type based on the tag.
Instances
GStorableSum' (V1 :: Type -> Type) Source # | |
GStorableSum' (U1 :: Type -> Type) Source # | |
GStorableSum' (K1 i a :: Type -> Type) Source # | |
Defined in Foreign.Storable.Generic.Internal | |
(KnownNat (SumArity g), KnownNat (SumArity f), GStorableSum' f, GStorableSum' g) => GStorableSum' (f :+: g) Source # | |
Defined in Foreign.Storable.Generic.Internal | |
GStorableSum' (f :*: g) Source # | |
Defined in Foreign.Storable.Generic.Internal | |
GStorableSum' f => GStorableSum' (M1 D t f) Source # | |
Defined in Foreign.Storable.Generic.Internal | |
(KnownNat (NoFields f), GStorable' f, GStorableSum' f) => GStorableSum' (M1 C t f) Source # | |
Defined in Foreign.Storable.Generic.Internal | |
GStorableSum' f => GStorableSum' (M1 S t f) Source # | |
Defined in Foreign.Storable.Generic.Internal |
class GStorableChoice' (choice :: Bool) a where Source #
Choose a GStorable implementation - whether a sum type (with tag) or raw product type (without the tag).
chSizeOf :: proxy choice -> a -> Int Source #
chAlignment :: proxy choice -> a -> Int Source #
chPeekByteOff :: proxy choice -> Ptr b -> Int -> IO a Source #
chPokeByteOff :: proxy choice -> Ptr b -> Int -> a -> IO () Source #
type GStorableChoice a = GStorableChoice' (IsSumType (Rep a)) a Source #
internalTagValue :: (KnownNat (SumArity (Rep a)), GStorableSum' (Rep a), Generic a) => a -> Word8 Source #
Get the tag value from the generic representation.
:: forall f p. GStorable' f | |
=> f p | Generic representation |
-> Int | Resulting size |
Calculates the size of generic data-type.
:: forall f p. GStorable' f | |
=> f p | Generic representation |
-> Alignment | Resulting alignment |
Calculates the alignment of generic data-type.
:: forall f p b. (KnownNat (NoFields f), GStorable' f) | |
=> Ptr b | Pointer to peek |
-> Offset | Offset |
-> IO (f p) | Resulting generic representation |
View the variable under a pointer, with offset.
:: forall f p b. (KnownNat (NoFields f), GStorable' f) | |
=> Ptr b | Pointer to write to |
-> Offset | Offset |
-> f p | Written generic representation |
-> IO () |
Write the variable under the pointer, with offset.
internalOffsets :: forall f p. GStorable' f => f p -> [Offset] Source #
Obtain the list of offsets