[Gambas-devel] What's in GB_BASE or Is it save to copy it for another object?
Tobias Boege
tobias at ...692...
Sun Aug 11 02:21:09 CEST 2013
Hi Benoit,
I'm currently implementing an AVL tree class (named AvlTree) in gb.data and
for my plans I need to give it a Current property which is an AvlTree
itself, which is given in theory but I fear that it's not that easy in
Gambas:
---
typedef struct {
NODE *left, *right; /* Children or NULL */
GB_STRING key; /* The key string */
GB_VARIANT_VALUE value; /* Payload */
} NODE;
typedef struct {
GB_BASE ob;
NODE *root, *current;
} CAVLTREE;
GB_DESC CAvlTree[] = {
GB_DECLARE("AvlTree", sizeof(CAVLTREE)),
...
GB_PROPERTY_READ("Current", "AvlTree", AvlTree_Current),
...
GB_END_DECLARE
};
---
To implement AvlTree_Current I imagine something as simple as:
---
BEGIN_PROPERTY(AvlTree_Current)
CAVLTREE *subtree;
GB.Alloc(&subtree, sizeof(*subtree));
memcpy(subtree, _object, sizeof(*subtree));
subtree->root = subtree->current;
GB.Ref(_object);
GB.ReturnObject(subtree);
END_PROPERTY
---
This copies the original AvlTree and just assigns it a new root. I think all
my routines will be fine with this but what about the Gambas machinery?
Is it safe for a data container (it won't have any events, but it will be
enumerable, though (the next element will, however, be located just in
time)) to copy its GB_BASE into another object? The resulting subtree is
explicitly supposed to share elements with the parent tree.
I now need to know: Are there any implications I don't know of? Is this
considered bad taste/not future-proof?
You see, the bulk of the information lies in the NODE structure which is to
be shared between both objects anyway and the class will be really simple -
the only exception is that an element of an object of this class is going to
be another instance of the same class sharing data with its parent.
Thinking about it a little further, this question is only about using
memcpy() to copy GB_BASE (if it's safe - because it's more efficient) or
using the GB.New() API. So what would you advise?
Regards,
Tobi
More information about the Devel
mailing list