[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
27.1.6.2 Struct and Union types
C struct and Union types can be declared in Bigloo using <struct-type> clauses:
<struct-type> →(struct
(
<typed-ident> <string>)^+
)
<union-type> →(union
(
<typed-ident> <string>)^+
)
This clause declared a C struct but C structure values cannot
be handled by Bigloo. Instead Bigloo is able to handle
pointers to C structure. Thus, in order to help the
definition of extern types, when a struct named struct is
defined, if it does not exists yet, Bigloo automatically defines
a type pointer to the structure. This type is named
struct*
.
When a pointer to a structure type is defined, Bigloo
automatically produces functions to manipulate objects of this
type. Let us suppose the type definition of struct*
:
(type struct (struct (id1::type1 name1) ... (idn::typen namen))
The following functions are created:
- A creator:
(struct*::struct* ::type_1 ... ::type_n)
This function allocates a fresh struct
*
(in the same heap as any Scheme value) and fills the fields of the C structure with the proper values provided in the call. - A type checker:
(struct*?::bool obj::obj)
This function returns
#t
if and only if the argument obj is of type struct*
. - A null checker:
(struct*-null?::bool ::struct*)
This function returns
#t
if and only if its argument isNull
. - A null creator:
(make-null-struct::struct*)
This function creates a
NULL
value of type struct*. - An equality checker:
(=struct*?::bool ::struct* ::struct*)
This function returns
#t
if and only if its arguments are equal. - Accessors and mutators:
(struct*-id_1::type_1 ::struct*) (struct*-id_1-set!::obj ::struct* ::type_1) ...
These functions read and store field values.
Here is an example of structure usage:
(module foo (extern (include "named_point_declaration.h") (type named-point (struct (x::double "x") (y::double "y") (name::string "name")) "struct named_point") (c-print-point::int (named-point*) "ppoint"))) (define (scheme-print-point point) (print "point*-name: " point " x: " (named-point*-x point) " y: " (named-point*-y point))) (let ((orig (named-point* 0.0 0.0 "orig"))) (if (named-point*-null? orig) (error "bigloo" "cannot allocate point" orig) (begin (c-print-point orig) (scheme-print-point orig))))
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.