[Gambas-devel] A few comment about Tommy's changes
Benoît Minisini
gambas at ...1...
Mon Aug 1 21:11:02 CEST 2011
> Benoit,
> Thanks for correcting my errors.
> I'm working on point 1 and 4, it's not so difficult, but if you could
> explain me why shouldn't I use pointer (point 2) if it works, and what to
> use instead? The same with point 3, as I wrote some time ago, I'm not a
> good C programmer, I'm still learning. And after I clean up my code, I
> promise not to mess anymore ;)
>
> Tomek.
>
Pointer can points at any memory address. By assuming that they are pointing
to a GluQuadric, you can make crash the interpreter, or worse make it doing
random things just by using a random pointer. THIS IS BAD!
Let's take GluQuadric as an example:
1) You must create a Gambas class to manage it. Let's keep the same name,
GluQuadric.
2) Look at the source code of the 'Rect' class, and use it as a template.
'Rect' is defined in /trunk/main/lib/draw/crect.c and
/trunk/main/lib/draw/crect.h.
So you should create a 'cgluquadric.c' and a 'cgluquadric.h' file. I added a
"c" in front of the file name to remember that these files implement a Gambas
class.
3) The Gambas GluQuadric object is defined by a C structure that always starts
with a GB_BASE field. All Gambas objects are structure that begins with that
structure. GB_BASE includes a pointer to the object class and the object
reference count.
typedef
struct {
GB_BASE ob;
GluQuadric *quadric;
}
CGLUQUADRIC;
4) Contrary to the 'Rect' class, you cannot create a 'GluQuadric' from scratch
(afaik). So you must add the GB_NOT_CREATABLE() keyword in the GB_DESC class
description.
GB_DESC GluQuadricDesc[] =
{
GB_DECLARE("GluQuadric", sizeof(CRECT)),
GB_NOT_CREATABLE(),
// No _new method is needed I think
...
GB_END_DECLARE
};
5) But you will add a public C function in the gluquadric.c file that will
create a GluQuadric Gambas object from a 'GluQuadric *' C pointer, as returned
by the 'gluNewQuadric' OpenGL function.
CGLUQUADRIC *GLUQUADRIC_create(GluQuadric *quadric)
{
CGLUQUADRIC *ob = GB.New(GB.FindClass("GluQuadric"), NULL, NULL);
ob->quadric = quadric;
return ob;
}
6) Then you will implement GLUNEWQUADRIC that way:
#include "cgluquadric.h"
BEGIN_METHOD_VOID(GLUNEWQUADRIC)
CGLUQUADRIC *ob = GLUQUADRIC_create(gluNewQuadric());
GB.ReturnObject(ob);
END_METHOD
7) And you will declare it that way:
GB_METHOD("NewQuadric", "GluQuadric", GLUNEWQUADRIC, NULL),
8) All other functions using GluQuadric must be modified, so that instead of
taking a pointer, they take a GluQuadric Gambas object.
Then the pointer you will receive in the function will be either NULL, either
a pointer to a CGLUQUADRIC structure.
More information about the Devel
mailing list