[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: [ghudson@MIT.EDU: Re: svn commit: rev 3110 - trunk/subversion/include]

From: Greg Stein <gstein_at_lyra.org>
Date: 2002-09-03 21:36:08 CEST

On Tue, Sep 03, 2002 at 10:13:22AM -0500, Karl Fogel wrote:
> "Gerald Richter - ecos gmbh" <richter@ecos.de> writes:
> > I am running out of time at the moment, but I think at least for mod_dav it
> > makes more sense to have the ctx members, than have a userdata argument to
> > function, because mod_dav is really object oriented, only implemented in C.
> > So most of these structure are really objects (and that's what they become
> > in Perl).
> >
> > I will provide you with some sample code as soon as I have a little free
> > time. (Hopefully later today or tomorrow moring)
>
> Okay, looking forward to the detailed response...

Actually, I think it can be summarized pretty simply:

* FOO returns you an object OBJ, expecting you to pass it back to its
  functions at various (later) points in time

* you want associate your own context with OBJ for other purposes

* where does your context pointer go?

In Gerald's recommendation, OBJ should have a context pointer in it for this
purpose. Thus, given an OBJ, you can recover your context.

Another direction is that you are acting as FOO, and need to return an OBJ,
yet you want some private context. If the definition of OBJ doesn't allow
this, then it gets trickier.

I believe both directions are solved without the embedded context pointer,
so I'm going to go ahead and back it out. For the first case, you wrap each
"object" in another object:

typedef struct {
  some_ptr my_data;
  int something_else;
  
  dav_resource *actual_resource;

} my_dav_resource;

  ...
  my_dav_resource *rsrc;

  rsrc = apr_palloc(r->pool, sizeof(*rsrc));
  rsrc->actual_resoure = hooks->get_resource(...);
  rsrc->my_data = perl_object_wrapper(...);
  
  return rsrc;
  ...

For the second case, you use embedded structures:

typedef struct {
  dav_resource actual_resource;
  
  some_ptr my_data;
  int something_else;
} my_dav_resource;

  ...
  my_dav_resource *rsrc;
  
  rsrc = apr_palloc(r->pool, sizeof(*rsrc));
  ...
  return &rsrc->actual_resource;
  ...
  
void takes_a_resource(const dav_resource *r)
{
  my_dav_resource *rsrc = (my_dav_resource *)r;
  ...

On this second example, you could potentially need more code if the embedded
structure isn't the first field:

  my_dav_resource *rsrc = (my_dav_resource *)((const char *)r -
                             offsetof(my_dav_resource, actual_resource));

This concept would apply to the mod_dav structures, too.

Gerald: do you have any issues with this wrapper structure style?

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Tue Sep 3 21:37:30 2002

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.