Any interest in postconstruction/predestruction facility?
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
"Malte Starostik" wrote:
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/
How is your solution better than to have - factory function/class for post-constructor and - deleter attached to shared_ptr for pre-destructor? /Pavel
Pavel Vozenilek schrieb:
"Malte Starostik" wrote:
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/
How is your solution better than to have - factory function/class for post-constructor and - deleter attached to shared_ptr for pre-destructor?
I guess only in that the latter is too obvious ;-) Of course I've used that solution before, but when I needed it this time it didn't occur to me and I started with prepost.hpp, of course after that I didn't consider an alternative to the whole approach, only implementation details. Oh well, coding throughout the holidays->lack of sleep->getting caught up. But thanks for reminding me, now I really don't see the need to anymore, not even for myself. Cheers, Malte, slightly embarassed (in the positive way :-) indeed
participants (2)
-
Malte Starostik
-
Pavel Vozenilek