Howdy,

On Sun 14 Sep 2008 00:42, "Maciek Godek" <[EMAIL PROTECTED]> writes:

> Hi,
> Using some hints you gave me, I've implemented a really tiny
> object system

Neat!

> your opinion ("why it's still better to use goops" :D)

Use what you want :)

But:

> storing objects as vectors allows for an efficient and convenient
> object treating from C level, so boatmen should be satisfied.

GOOPS objects are internally implemented similar to vectors:

   http://wingolog.org/archives/2008/04/11/allocate-memory-part-of-n

The only trick is that the mapping between slot names and indices in the
slot array is not part of GOOPS' specification -- it depends on many
things. But in normal cases, the first slot that you name in the class
definition is stored in slot 0, etc:

    guile> (define-class <circle> () (radius #:init-keyword #:radius))
    guile> (make <circle> #:radius 10)
    $1 = #<<circle> b7f8be20>
    guile> (struct-ref $2 0)
    $2 = 10

Of course from scheme the way to take advantage of this is to use the
accessors, which compile down to this. But from C, probably your best
bet is to introspect which index a slot is stored in at runtime, then
cache that. From Guile-GNOME:

    /* from goops.c */
    static int
    gtype_struct_offset (SCM class)
    {
        register SCM slots = SCM_SLOT (scm_class_of (class), 
scm_si_getters_n_setters);
      for (; !scm_is_null (slots); slots = SCM_CDR (slots))
          if (SCM_CAAR (slots) == scm_sym_gtype)
              return scm_to_int (SCM_CDDR (SCM_CAR (slots)));
              
      scm_c_gruntime_error ("%gtype-class-bind",
                            "`gtype' not allocated a slot in struct!",
                            SCM_LIST1 (class));
      return -1;
    }

This could certainly be improved, somehow, on an API level.

Andy
-- 
http://wingolog.org/


Reply via email to