Next Previous Up Top Contents Index

referenced-type

c-type-cast

Function

Summary

Converts a value to a value of a specified type, according to the semantics of a C type cast.

Signature

c-type-cast type value => value 

Arguments

type
See Description.

value
An instance of <object>.

Values

value
An instance of <object>.

Library

c-ffi

Module

c-ffi

Description

Returns the value of the second argument, converted to the type specified by the first argument, in accordance with the semantics of a C type cast. This is convenient to use when translating C code to Dylan. It may also be helpful for converting a value to the form required by a C-function wrapper argument.

The first argument can be either a C type designator or one of the Dylan classes <boolean>, <character>, <machine-word>, or any subclass of <number>. For a C type designator, the value is converted to the Dylan class which it maps to. <C-[un]signed-short> and <C-[un]signed-char> truncate the value as well as ensuring that it is an <integer>.

For example, with a function declared in C as

  Foo(long x);

and called as

  Foo((long) p);

if the Dylan declaration is

  define C-function Foo
    parameter x :: <C-both-long>;
    c-name: "Foo";
  end;

then the equivalent call will be:

  Foo(c-type-cast(<C-both-long>, p));

which will ensure that the C semantics are preserved without needing to analyze exactly what the type cast is doing.

The functions pointer-value and pointer-value-setter perform the primitive Dylan-to-C and C-to-Dylan conversions as documented with the designator class of the pointer's contents type (see Table 1.1). The C-FFI signals an error if it cannot convert the object you attempt to store in the pointer to a compatible type.

These two functions are part of a protocol for extending the C type conversions. You can define new methods for pointer-value and pointer-value-setter for types defined by define C-subtype that are subtypes of <C-pointer>.


C FFI and Win 32 Reference - 31 MAR 2000

Next Previous Up Top Contents Index