This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Post constructor/Pre destructor hooks
- From: David Orchard <david at errol dot org dot uk>
- To: gcc at gnu dot org
- Date: Mon, 13 Aug 2007 22:16:22 +0100
- Subject: Post constructor/Pre destructor hooks
Hi,
Some base classes (specifically thread base classes) would benefit from
being able to execute a function after the object is constructed and before
it is destructed. For a thread class the thread can't start until after the
object is constructed and must stop before the object is destructed.
Does such a (probably non-standard) feature already exist or am I missing
something.
My idea was something like :-
#pragma hooks
class thread
{
private:
static void start_hook(thread*);
static void stop_hook(thread*);
};
a) There can be only one class in any class hierarchy with the 'hooks'
property.
// The following class definition is illegal
#pragma hooks
class my_thread : public thread
{
};
This removes any ordering issues for multiple hooks since there can only be
one set of hooks. All though the base class could implement a mechanism for
derived classes to have hooks. But this is an application issue.
b) Any object with a 'hooks' class in its class hierarchy has '<hooks
class>::start_hook(ptr);' inserted after its constructor is complete (stack
or new) and '<hooks class>::stop_hook(ptr)' before its destructor (stack or
delete). For example :
class runit : public thread
{
};
void fred()
{
runit a;
//thread::start_hook(&a); <== Inserted by compiler
runit *b = new runit;
//thread::start_hook(b); <== Inserted by compiler
runit c[2];
//thread::start_hook(c+0); <== Inserted by compiler
//thread::start_hook(c+1); <== Inserted by compiler
// Do the work
//if (NULL != b) thread::stop_hook(b); <== Inserted by compiler
delete b;
//thread::stop_hook(c+0); <== Inserted by compiler
//thread::stop_hook(c+1); <== Inserted by compiler
// Implicit destruction of c
//thread::stop_hook(&a); <== Inserted by compiler
// Implicit destruction of a
}
c) It is not manditory but advisable to disable the copy constructor for all
'hook' classes.
--
================================================================
David Orchard david@errol.org.uk
================================================================