| Safe Haskell | Safe-Inferred |
|---|---|
| Language | GHC2021 |
HsBindgen.Runtime.HasCField
Contents
Description
Declarations with C fields
Most users do not directly need to use HasCField, and can use record dot
syntax instead. For example, given
struct DriverInfo {
char* name;
int version;
};
struct Driver {
struct DriverInfo info;
};hs-bindgen will generate code such that if
driverPtr :: Ptr Driver
then
driverPtr.driver_version :: Ptr DriverInfo driverPtr.driver_version.driverInfo_version :: Ptr CInt
Note that chaining like this can only be done for nested structs; no actual dereferencing takes place! For example, if we additionally had
struct DriverCode {
..
};
struct Driver {
..
struct DriverCode* code;
};
then
driverPtr.driver_code :: Ptr (Ptr DriverCode)
(note the double Ptr), which must be dereferenced before any fields of
DriverCode can be accessed.
This module is intended to be imported qualified.
import HsBindgen.Runtime.Prelude import HsBindgen.Runtime.HasCField qualified as HasCField
Synopsis
- class HasCField (a :: Type) (field :: Symbol) where
- offset :: forall a field. HasCField a field => Proxy a -> Proxy field -> Int
- fromPtr :: forall a field. HasCField a field => Proxy field -> Ptr a -> Ptr (CFieldType a field)
- peek :: (HasCField a field, Storable (CFieldType a field)) => Proxy field -> Ptr a -> IO (CFieldType a field)
- poke :: (HasCField a field, Storable (CFieldType a field)) => Proxy field -> Ptr a -> CFieldType a field -> IO ()
- readRaw :: (HasCField a field, ReadRaw (CFieldType a field)) => Proxy field -> Ptr a -> IO (CFieldType a field)
- writeRaw :: (HasCField a field, WriteRaw (CFieldType a field)) => Proxy field -> Ptr a -> CFieldType a field -> IO ()
Fields
class HasCField (a :: Type) (field :: Symbol) where Source #
Evidence that a C object a has a field with the name field.
Fields can be part of structs and unions. Typedefs are a degenerate use case.
Struct
If we have the C struct S:
struct S {
int x;
int y;
}And an accompanying Haskell datatype S:
data S = S { s_x :: CInt, s_y :: CInt }Then we can define two instances
HasCField S "s_x" HasCField S "s_y"
Union
If we have the C union U:
union U {
int x;
int y;
}And an accompanying Haskell datatype U:
data U = U ... {- details elided -}
... {- getters and setters elided -}Then we can define two instances
HasCField U "u_x" HasCField U "u_y"
Typedef
If we have the C typedef T:
typedef int T;
And an accompanying Haskell newtype T:
newtype T = T { unwrapT :: Int }Then we can define the instance:
HasCField T "unwrapT"
Methods
offset# :: Proxy# a -> Proxy# field -> Int Source #
The offset (in number of bytes) of the field with respect to the parent object.
Instances
offset :: forall a field. HasCField a field => Proxy a -> Proxy field -> Int Source #
The offset (in number of bytes) of the field with respect to the parent object.
fromPtr :: forall a field. HasCField a field => Proxy field -> Ptr a -> Ptr (CFieldType a field) Source #
Convert a pointer to a C object to a pointer to one of the object's fields.
peek :: (HasCField a field, Storable (CFieldType a field)) => Proxy field -> Ptr a -> IO (CFieldType a field) Source #
Using a pointer to a C object, read from one of the object's fields.
poke :: (HasCField a field, Storable (CFieldType a field)) => Proxy field -> Ptr a -> CFieldType a field -> IO () Source #
Using a pointer to a C object, write to one of the object's fields.
readRaw :: (HasCField a field, ReadRaw (CFieldType a field)) => Proxy field -> Ptr a -> IO (CFieldType a field) Source #
Read a field using a pointer to a C object
writeRaw :: (HasCField a field, WriteRaw (CFieldType a field)) => Proxy field -> Ptr a -> CFieldType a field -> IO () Source #
Write a field using a pointer to a C object