Boost logo

Boost Users :

From: Malte Starostik (home_at_[hidden])
Date: 2005-03-27 20:29:59


Hi,

I'm using a wrapper class to automate postconstruction and
predestruction. A preliminarily boostified version (added licence
header, changed namespace to boost) is available at
http://malte.homeip.net/~malte/prepost/

prepost.hpp[.html] contains the library
The file example.cpp[.html] shows an example usage:

The class thread_base starts a new thread in it's post_construction()
member function which runs the virtual run() member function in the new
thread.
Since run() is to be overridden by a derived class, this cannot be done
by the ctor. Similarly, the thread is joined in the pre_destruction()
member function, as the dtor would be too late since the derived class's
 instance must not have been destructed before the thread finished.

The basic usage is to derive a class X from post_constructed and/or
pre_destructed according to which facilities are needed. Then, instead
of instantiating X directly, user code instantiates prepost_wrapper< X
>. Direct instantiation of X is prevented by the presence of pure
virtuals in the classes post_constructed and pre_destructed. As in most
if not all cases, X will be polymorphic anyway, I considered the benefit
of prevented circumvention of the mechanism worth the two additional
entries in the vtable.

Is there any interest in having this facility? BTW, the name
prepost_wrapper is definately the result of my lacking imagination. Any
suggestions appreciated ;-)

Outstanding issues:
* maybe prepost_wrapper should prevent being used as an intermediate class
* the forwarding ctors of prepost_wrapper are problematic if one or more
of the base class's ctors are explicit
* the hardcoded maximum number of ctor args should be configurable
* anything else I didn't think of?

BTW, I tried to have the default ctor generated by the BOOST_PP_REPEAT
macro as well, but failed miserably to find a way to omit template<> if
n is 0. Some help in this area is appreciated as well.

Cheers,
Malte


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net