hs-bindgen-runtime
Safe HaskellSafe-Inferred
LanguageGHC2021

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

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"

Associated Types

type CFieldType (a :: Type) (field :: Symbol) :: Type Source #

Methods

offset# :: Proxy# a -> Proxy# field -> Int Source #

The offset (in number of bytes) of the field with respect to the parent object.

Instances

Instances details
HasCField CTm "tm_hour" Source # 
Instance details

Defined in HsBindgen.Runtime.Internal.LibC.Auxiliary

Associated Types

type CFieldType CTm "tm_hour" Source #

Methods

offset# :: Proxy# CTm -> Proxy# "tm_hour" -> Int Source #

HasCField CTm "tm_isdst" Source # 
Instance details

Defined in HsBindgen.Runtime.Internal.LibC.Auxiliary

Associated Types

type CFieldType CTm "tm_isdst" Source #

Methods

offset# :: Proxy# CTm -> Proxy# "tm_isdst" -> Int Source #

HasCField CTm "tm_mday" Source # 
Instance details

Defined in HsBindgen.Runtime.Internal.LibC.Auxiliary

Associated Types

type CFieldType CTm "tm_mday" Source #

Methods

offset# :: Proxy# CTm -> Proxy# "tm_mday" -> Int Source #

HasCField CTm "tm_min" Source # 
Instance details

Defined in HsBindgen.Runtime.Internal.LibC.Auxiliary

Associated Types

type CFieldType CTm "tm_min" Source #

Methods

offset# :: Proxy# CTm -> Proxy# "tm_min" -> Int Source #

HasCField CTm "tm_mon" Source # 
Instance details

Defined in HsBindgen.Runtime.Internal.LibC.Auxiliary

Associated Types

type CFieldType CTm "tm_mon" Source #

Methods

offset# :: Proxy# CTm -> Proxy# "tm_mon" -> Int Source #

HasCField CTm "tm_sec" Source # 
Instance details

Defined in HsBindgen.Runtime.Internal.LibC.Auxiliary

Associated Types

type CFieldType CTm "tm_sec" Source #

Methods

offset# :: Proxy# CTm -> Proxy# "tm_sec" -> Int Source #

HasCField CTm "tm_wday" Source # 
Instance details

Defined in HsBindgen.Runtime.Internal.LibC.Auxiliary

Associated Types

type CFieldType CTm "tm_wday" Source #

Methods

offset# :: Proxy# CTm -> Proxy# "tm_wday" -> Int Source #

HasCField CTm "tm_yday" Source # 
Instance details

Defined in HsBindgen.Runtime.Internal.LibC.Auxiliary

Associated Types

type CFieldType CTm "tm_yday" Source #

Methods

offset# :: Proxy# CTm -> Proxy# "tm_yday" -> Int Source #

HasCField CTm "tm_year" Source # 
Instance details

Defined in HsBindgen.Runtime.Internal.LibC.Auxiliary

Associated Types

type CFieldType CTm "tm_year" Source #

Methods

offset# :: Proxy# CTm -> Proxy# "tm_year" -> Int Source #

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