[geos-devel] userData for Geometry

Ferdinando Villa ferdinando.villa at uvm.edu
Tue Apr 13 10:03:49 EDT 2004

Using the void* and relying on a virtual for proper deletion is a pretty
unsafe way of doing it (not to mention ugly). A better alternative could
be something like this - a bit messier, but once done, it takes care of
it all:
1. define these within the class that has the void*

class _handler
  void* obj;
  virtual ~_handler() {}
template <typename T>
class handle : public _handler
  handle(T* o) { obj = o; }
  handle(const handle& lh)
  { obj = lh.obj; }
  virtual ~handle() { T* t = (T*)obj; delete t; }

2. have a pointer to _handler initialized to zero instead of void*, and
delete it in the destructor if it's non-zero;

_handler _h

yourclass(): _h(0) ...
~yourclass() { if (_h) delete _h; }

3. add a set/get method like 

template <typename T>
void set_thingy(T* lh)
  _h = new handle<T>(lh); 

template <typename T>
T* get_thingy()
  return reinterpret_cast<T*>(_h->obj);

Even better if the _h pointer is a smart pointer, which allows the
containing object to be copied and passed around safely, but that's more
setup. You can call the set_methods as a regular method: set_thingy(new
thing) and that will create the proper class that will manage the
deletion properly. The get method requires the template parameter. Of
course this substitutes one allocation with two, so it's not really
appropriate if it has to be called continuously, but then the void*
thing isn't, either.


On Tue, 2004-04-13 at 07:20, Paul Selormey wrote:
> According to the JTS docs, this is to be used for coordinate reference
> system,
> and it most systems this could be implemented as geometry filter but the
> geomety
> may not be applicable to some existing systems.
> I think an abstract class (interface) will do with at least a "delete"
> method to let
> the object handle its own deletion. On both Java and .NET which supports GC,
> there was no need to consider this.
> BTW, I have realized work on the JTS 1.4 updates is progressing, when is the
> release date?
> Best regards,
> Paul.
> ----- Original Message ----- 
> From: "strk" <strk at keybit.net>
> To: "Yury A. Bychkov" <me at yury.ca>
> Cc: <geos-devel at geos.refractions.net>
> Sent: Tuesday, April 13, 2004 5:23 PM
> Subject: [geos-devel] userData for Geometry
> > The Geometry class has a void *userData that is
> > deleted at Geometry detetion time.
> > Wouldn't it be appropriate to define a class from
> > which userData should derive ?
> > The compiler warns that void * cannot be safely deleted.
> >
> > --strk;
> > _______________________________________________
> > geos-devel mailing list
> > geos-devel at geos.refractions.net
> > http://geos.refractions.net/mailman/listinfo/geos-devel
> >
> >
> _______________________________________________
> geos-devel mailing list
> geos-devel at geos.refractions.net
> http://geos.refractions.net/mailman/listinfo/geos-devel

More information about the geos-devel mailing list