hs-bindgen-runtime
Safe HaskellSafe-Inferred
LanguageGHC2021

HsBindgen.Runtime.HasCBitfield

Description

Declarations with C bitfields

Most users do not directly need to use HasCField, and can use record dot syntax instead. For example, given

Given

struct DriverFlags {
  unsigned int safe      : 1;
  unsigned int allocates : 1;
};

hs-bindgen will generate code such that if

flagsPtr :: Ptr DriverFlags

then

flagsPtr.driverFlags_allocates :: BitfieldPtr CUInt

Module HsBindgen.Runtime.BitfieldPtr can be used to interact with these BitfieldPtrs; for example:

BitfieldPtr.peek flagsPtr.driverFlags_allocates

Bitfields can be chained with regular fields; for example, given

struct Driver {
  struct DriverFlags flags;
  ..
};

then if

driverPtr :: Ptr Driver

then

driverPtr.driver_flags.driverFlags_allocates :: BitfieldPtr CUInt

See also HsBindgen.Runtime.HasCField.

This module is intended to be imported qualified.

import HsBindgen.Runtime.Prelude
import HsBindgen.Runtime.HasCBitfield qualified as HasCBitfield
Synopsis

Documentation

class HasCBitfield (a :: Type) (field :: Symbol) where Source #

Evidence that a C object a has a bit-field with the name field.

Bit-fields can be part of structs and unions.

Struct

If we have the C struct S:

struct S {
  int x : 2;
  int y : 3;
}

And an accompanying Haskell datatype S:

data S = S { s_x :: CInt, s_y :: CInt }

Then we can define two instances

HasCBitfield S "s_x"
HasCBitfield S "s_y"

Union

If we have the C union U:

union U {
  int x : 2;
  int y : 3;
}

And an accompanying Haskell datatype U:

data U = U ... {- details elided -}
... {- getters and setters elided -}

Then we can define two instances

HasCBitfield U "u_x"
HasCBitfield U "u_y"

Associated Types

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

The type of the bit field

Methods

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

The offset (in number of bits) of the bit-field with respect to the parent object.

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

The width (in number of bits) of the bit-field.

offset :: forall a field. HasCBitfield a field => Proxy a -> Proxy field -> Int Source #

The offset (in number of bits) of the bit-field with respect to the parent object.

width :: forall a field. HasCBitfield a field => Proxy a -> Proxy field -> Int Source #

The width (in number of bits) of the bit-field.

toPtr :: forall a field. HasCBitfield a field => Proxy field -> Ptr a -> BitfieldPtr (CBitfieldType a field) Source #

Convert a pointer to a C object to a pointer to one of the object's bit-fields.

peek :: forall a field. (HasCBitfield a field, Bitfield (CBitfieldType a field)) => Proxy field -> Ptr a -> IO (CBitfieldType a field) Source #

Using a pointer to a C object, read from one of the object's bit-fields.

poke :: forall a field. (HasCBitfield a field, Bitfield (CBitfieldType a field)) => Proxy field -> Ptr a -> CBitfieldType a field -> IO () Source #

Using a pointer to a C object, write to one of the object's bit-fields.