Next: , Previous: , Up: lang   [Index]


1.13 C language flags and flags combinations

The following bindings are exported by the library (vicare language-extensions c-enumerations).

Syntax: define-c-flags ?name (?flag0 ?flag ...) (?symbol0 ?symbol ...)

Define a new enumeration type and a couple of functions to convert between enumeration sets of this type and other objects. Usage example:

(define A 1)
(define B 2)
(define C 3)

(define-c-flags things
  (A B C)
  (a b c))

(things->value (things a)) ⇒ 1
(things->value (things b)) ⇒ 2
(things->value (things c)) ⇒ 3

(value->things 1) ⇒ (things a)
(value->things 2) ⇒ (things b)
(value->things 3) ⇒ (things c)

when used in a library, we can do the following to export the interface:

(library (things)
  (export things things->value value->things)
  (import (rnrs))
  (define A 1)
  (define B 2)
  (define C 3)
  (define-c-flags things
    (A B C)
    (a b c)))

The typical use of this syntax is to define an interface for C language foreign constants.

?name must be an identifier to which an enumeration set constructor syntax is bound; the syntax accepts only a single symbol as argument and it expands to an enumeration set with that symbol as element.

The enumeration type is enum-?name and the original constructor syntax accepting any number of arguments is %?name.

The ?flag arguments are meant to be identifiers bound to any Scheme value, but they can be any value. The ?symbol arguments must be Scheme symbols which are used to define (in the given order) the universe of the enumeration.

The function which converts from an enumeration set (holding a single argument) to the corresponding value is bound to ?name->value. The function which converts from a value to the enumeration set (holding a single argument) is bound to value->?name.

Syntax: define-c-ior-flags ?name (?flag0 ?flag ...) (?symbol0 ?symbol ...)

Define a new enumeration type and a couple of functions to convert between enumeration sets of this type and a bitwise, inclusive, OR combination of exact integers. Usage example:

(define A (bitwise-arithmetic-shift-left 1 0))
(define B (bitwise-arithmetic-shift-left 1 1))
(define C (bitwise-arithmetic-shift-left 1 2))

(define-c-ior-flags things
  (A B C)
  (a b c))

(things->value (things a))      ⇒ 1
(things->value (things b))      ⇒ 2
(things->value (things a b))    ⇒ 3
(things->value (things c))      ⇒ 4
(things->value (things a c))    ⇒ 5
(things->value (things b c))    ⇒ 6
(things->value (things a b c))  ⇒ 7

(value->things 1) ⇒ (things a)
(value->things 2) ⇒ (things b)
(value->things 5) ⇒ (things a c)

when used in a library, we can do the following to export the interface:

(library (things)
  (export things things->value value->things)
  (import (rnrs))
  (define A (bitwise-arithmetic-shift-left 1 0))
  (define B (bitwise-arithmetic-shift-left 1 1))
  (define C (bitwise-arithmetic-shift-left 1 2))
  (define-c-ior-flags things
    (A B C)
    (a b c)))

The typical use of this syntax is to define an interface for C language foreign constants used as flags in inclusive OR combinations.

?name must be an identifier to which an enumeration set constructor syntax is bound; the syntax accepts any number of symbols as arguments and it expands to an enumeration set with that symbols as elements. The enumeration type is enum-?name.

The ?flag arguments must be identifiers bound to exact integers. The ?symbol arguments must be Scheme symbols which are used to define (in the given order) the universe of the enumeration.

The function which converts from an enumeration set to the corresponding bitwise, inclusive OR combination is bound to ?name->value. An assertion violation is raised if this function is applied to an enumeration set of the wrong type.

The function which converts from a bitwise, inclusive OR combination to the enumeration set is bound to value->?name. If this function is applied to an integer holding bits outside the ones coded in the definition of the enumeration, no error is raised.


Next: , Previous: , Up: lang   [Index]