hs-bindgen-runtime
Safe HaskellSafe-Inferred
LanguageGHC2021

HsBindgen.Runtime.ConstantArray

Description

C arrays of known, constant size

This module is intended to be imported qualified.

import HsBindgen.Runtime.Prelude
import HsBindgen.Runtime.ConstantArray qualified as CA
Synopsis

Documentation

data ConstantArray (n :: Nat) a Source #

A C array of known size

Instances

Instances details
HasField "toFirstElemPtr" (Ptr (ConstantArray n a)) (Ptr a) Source #

toFirstElemPtr for overloaded record dot syntax

Instance details

Defined in HsBindgen.Runtime.ConstantArray

Methods

getField :: Ptr (ConstantArray n a) -> Ptr a #

(Storable a, KnownNat n) => Storable (ConstantArray n a) Source # 
Instance details

Defined in HsBindgen.Runtime.ConstantArray

Methods

sizeOf :: ConstantArray n a -> Int #

alignment :: ConstantArray n a -> Int #

peekElemOff :: Ptr (ConstantArray n a) -> Int -> IO (ConstantArray n a) #

pokeElemOff :: Ptr (ConstantArray n a) -> Int -> ConstantArray n a -> IO () #

peekByteOff :: Ptr b -> Int -> IO (ConstantArray n a) #

pokeByteOff :: Ptr b -> Int -> ConstantArray n a -> IO () #

peek :: Ptr (ConstantArray n a) -> IO (ConstantArray n a) #

poke :: Ptr (ConstantArray n a) -> ConstantArray n a -> IO () #

(Show a, Storable a) => Show (ConstantArray n a) Source # 
Instance details

Defined in HsBindgen.Runtime.ConstantArray

(Storable a, Eq a) => Eq (ConstantArray n a) Source # 
Instance details

Defined in HsBindgen.Runtime.ConstantArray

Methods

(==) :: ConstantArray n a -> ConstantArray n a -> Bool #

(/=) :: ConstantArray n a -> ConstantArray n a -> Bool #

(Storable a, KnownNat n) => ReadRaw (ConstantArray n a) Source # 
Instance details

Defined in HsBindgen.Runtime.ConstantArray

Methods

readRaw :: Ptr (ConstantArray n a) -> IO (ConstantArray n a) Source #

(Storable a, KnownNat n) => StaticSize (ConstantArray n a) Source # 
Instance details

Defined in HsBindgen.Runtime.ConstantArray

(Storable a, KnownNat n) => WriteRaw (ConstantArray n a) Source # 
Instance details

Defined in HsBindgen.Runtime.ConstantArray

Methods

writeRaw :: Ptr (ConstantArray n a) -> ConstantArray n a -> IO () Source #

toVector :: forall a n arrayLike. Coercible arrayLike (ConstantArray n a) => arrayLike -> (Proxy n, Vector a) Source #

( O(1) ): Get the underlying Vector representation

This makes the full Vector API available.

fromVector :: forall a n arrayLike. (Coercible arrayLike (ConstantArray n a), Storable a, KnownNat n, HasCallStack) => Proxy n -> Vector a -> arrayLike Source #

( O(1) ): Construct from a Vector representation

This makes the full Vector API available.

Precondition: the vector must have the right number of elements.

Pointers

In example C code below, p1 points to the array xs as a whole, while p2 points to the first element of xs.

extern int xs[3];
void foo () {
  int (*p1)[3] = &xs;
  int *p2 = &(xs[0]);
}

Though the types of p1 and p2 differ, the values of the pointers (the address they point to) is the same. An array is just a block of contiguous memory storing array elements. p1 points to where xs starts, and p2 points to where the first element of xs starts, and these addresses are the same. In Haskell, the corresponding types for p1 and p2 respectively are Ptr (ConstantArray n CInt) and Ptr CInt respectively.

Functions like peek require a Ptr (ConstantArray n a) argument. If the user only has access to a Ptr a but they know that is pointing to the first element in an array, then they can use toPtr to convert the pointer before using peekArray on it. Conversely, if the user has access to a Ptr (ConstantArray n a) but they want to convert it to a Ptr a, then they can use toFirstElemPtr.

NOTE: with overloaded record dot syntax, syntax like .toFirstElemPtr is also supported.

Relevant functions in this module also support pointers of newtypes around ConstantArray, hence the addition of Coercible constraints in many places. For example, we can use toPtr at a ConstantArray type or we can use toPtr at a newtype around a ConstantArray.

newtype A n = A (ConstantArray n CInt)
toPtr @(ConstantArray 3 CInt) ::
  Proxy 3 -> Ptr CInt -> Ptr (ConstantArray 3 CInt)
toPtr @(A 3) ::
  Proxy 3 -> Ptr CInt -> Ptr (A 3)

toPtr :: forall arrayLike n a. Coercible arrayLike (ConstantArray n a) => Proxy n -> Ptr a -> Ptr arrayLike Source #

( O(1) ): Use a pointer to the first element of an array as a pointer to the whole of said array.

NOTE: this function does not check that the pointer is actually a pointer to the first element of an array.

toFirstElemPtr :: forall arrayLike n a. Coercible arrayLike (ConstantArray n a) => Ptr arrayLike -> (Proxy n, Ptr a) Source #

( O(1) ): Use a pointer to a whole array as a pointer to the first element of said array.

withPtr :: forall b n a r. (Coercible b (ConstantArray n a), Storable a) => b -> (Ptr b -> IO r) -> IO r Source #

( O(n) ): Retrieve the underlying pointer

Construction

repeat :: forall n a. (KnownNat n, Storable a) => a -> ConstantArray n a Source #

( O(n) )

fromList :: forall n a. (KnownNat n, Storable a, HasCallStack) => [a] -> ConstantArray n a Source #

( O(n) ): Construct from a list

Precondition: the list must have the right number of elements.

Query

toList :: Storable a => ConstantArray n a -> [a] Source #

( O(n) )

Auxiliary

intVal :: forall n. KnownNat n => Proxy n -> Int Source #