structType: Create a type description for a C-level structure or array

View source: R/struct.R

structTypeR Documentation

Create a type description for a C-level structure or array

Description

The structType function allows us to create new type definitions for C-level structures. We describe the types of each of the struct's elements and can then use this new type in subsequent interfaces to native routines as return types or parameter types.

arrayType is used to create a description of a C-level array by describing the type of the elements and the length of the array.

stringArrayType is a function for creating an FFI type describing the specific data type which is an array of char elements in C.

Usage

structType(types)
stringArrayType(len)

Arguments

types

a list of FFI types, either the built-in ones or other compositions for structs

len

the number of char elements in the array

Value

An external pointer object referencing the newly created ffi_type object.

Note

At present, names of the elements are ignored as it is only the layout of the structure's elements that is relevant. In the future, we might utilize the names to provide facilities to access individual elements. But this will probably be done at a higher level.

Author(s)

Duncan Temple Lang

References

libffi

See Also

voidType and the other built-in types.

Examples


 myStruct.type = structType(list(s = sint16Type, i = sint32Type, d = doubleType, str = stringType))

 cif = CIF(myStruct.type)
 callCIF(cif, "getStruct")

 cif = CIF(voidType, list(myStruct.type))
 invisible(replicate(10, callCIF(cif, "doStruct", list(-10L, 99L, 3.1415, "an R string"))))



  setClass("MyStructRef", contains = "RCStructReference")
  MyStruct.FFIType = structType(list(s = sint16Type,
                                    i = sint32Type,
                                    d = doubleType,
                                    string = stringType))

  setMethod("$", "MyStructRef",
            function(x, name) {
                 getStructField(x, name, MyStruct.FFIType)
            }) 



omegahat/Rffi documentation built on Nov. 29, 2023, 12:48 a.m.