C++ APIs [Re: libevent-like callback in c++]

Leandro Lucarella llucax at gmail.com
Wed Jan 9 16:28:18 CET 2008


Hi, me again! :)

Marc Lehmann, el 14 de diciembre a las 20:10 me escribiste:
> The bigger problem you will face is that there are ten or so watcher types
> as opposed to only one.

I think I underestimated this issue =P

Is not that is a lot of work typing (that, as painful as it is, it's done
only once ;), but the lack of a base "class" to have a container of
watchers.

I thought about a lot of solutions, but all has their drawbacks:

* "Interfaces": a pure virtual base class with the basic operations
  (start(), stop() mainly) and multiple inheritance for each watcher
  (like struct io: ev_io, base { ... };). The obvious problem here
  is the objects are not POD anymore, so you can't pass a io object
  directly to ev_io_xxx() functions. Discarded.
* Wrappers: instead of inheriting directly from ev_xxx watcher, make a
  wrapper, like struct base { void* w; }, but again, start() and stop()
  would have to be virtual, because each watcher type has it own
  ev_xxx_start() and stop() functions. This time at least is doable
  because I have my POD watcher w to pass to the libev functions, but
  implies an extra pointer per watcher and the extra indirection in the
  start()/stop() calls because of the virtualness, but this can't be
  avoided by nature if I want a common base class.
  Another approach to this would be to store a pointer to a start() and
  stop() functions, but that would take some extra space, since I have to
  store all the function pointers, which looks stupid =)
* I probably thought about other "broken" solution that I can't remember
  now =P

So the question is, what is worst? To don't have support to put any kind
of watcher into the same container, or the extra space/complexity? Does
anyone have a better solution?

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Aprendan de la primavera, que antecede al verano, precede al inverno y
no lo anda diciendo por ahí.
	-- Ricardo Vaporeso. Llanos de Luzuriaga, 1914.




More information about the libev mailing list