Version franaise
Home About Download Resources Contact us
Browse thread
Converting variants with only constant constructors to integers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: W Dan Meyer <wojciech.meyer@g...>
Subject: Re: [Caml-list] Converting variants with only constant constructors to integers

Török Edwin <edwintorok@gmail.com> writes:

>
> Yes, I probably should have started with that.
> I have some flags like:
> type myflags = Property1 | Property2 | Property3;;

Hmm, you can take a look at lablgl sources, they use a lot this kind of
constructs.

Here is the answer:
CAMLprim value ml_glPushAttrib (value list)
{
    GLbitfield mask = 0;

    while (list != Val_int(0)) {
	switch (Field(list,0)) {
	case MLTAG_accum_buffer:mask |= GL_ACCUM_BUFFER_BIT; break;
	case MLTAG_color_buffer:mask |= GL_COLOR_BUFFER_BIT; break;
	case MLTAG_current:	mask |= GL_CURRENT_BIT; break;
	case MLTAG_depth_buffer:mask |= GL_DEPTH_BUFFER_BIT; break;
	case MLTAG_enable:	mask |= GL_ENABLE_BIT; break;
	case MLTAG_eval:	mask |= GL_EVAL_BIT; break;
	case MLTAG_fog:		mask |= GL_FOG_BIT; break;
	case MLTAG_hint:	mask |= GL_HINT_BIT; break;
	case MLTAG_lighting:	mask |= GL_LIGHTING_BIT; break;
	case MLTAG_line:	mask |= GL_LINE_BIT; break;
	case MLTAG_list:	mask |= GL_LIST_BIT; break;
	case MLTAG_pixel_mode:	mask |= GL_PIXEL_MODE_BIT; break;
	case MLTAG_point:	mask |= GL_POINT_BIT; break;
	case MLTAG_polygon:	mask |= GL_POLYGON_BIT; break;
	case MLTAG_polygon_stipple:mask |= GL_POLYGON_STIPPLE_BIT; break;
	case MLTAG_scissor:	mask |= GL_SCISSOR_BIT; break;
	case MLTAG_stencil_buffer:mask |= GL_STENCIL_BUFFER_BIT; break;
	case MLTAG_texture:	mask |= GL_TEXTURE_BIT; break;
	case MLTAG_transform:	mask |= GL_TRANSFORM_BIT; break;
	case MLTAG_viewport:	mask |= GL_VIEWPORT_BIT; break;
	}
	list = Field(list,1);
    }
    glPushAttrib (mask);
    return Val_unit;
}

where is the MLTAG is define like this:

struct record {
    value key; 
    GLenum data;
};

static struct record input_table[] = {
#include "gl_tags.c"
};

where:

    {MLTAG_color, GL_COLOR},
    {MLTAG_depth, GL_DEPTH},
    {MLTAG_accum, GL_ACCUM},
    {MLTAG_stencil, GL_STENCIL},
    {MLTAG_points, GL_POINTS},

where:

#define MLTAG_color	Val_int(-899911325)
#define MLTAG_depth	Val_int(-685117181)
(...)

aha!
-rwxr-xr-x 1 spec spec 86334 2010-05-31 13:51 var2switch
-rwxr-xr-x 1 spec spec 81183 2010-05-31 13:51 var2def

plus this:
gl_tags.var:
(* GLenum *)

color
depth
accum
stencil
points
lines
polygon
triangles
quads
line_strip
line_loop
triangle_strip
triangle_fan
quad_strip     

(...)

gives an answer: they are generated from OCaml code.

> Best regards,
> --Edwin

Wojciech