I
Imre
Hi
I'm looking for a way to make sure that whenever a new instance of a
class template A is created, then an instance of class template B is
also created, with the same template parameters. Of course, I could do
it by referencing B<T> from A<T>, but I'd like to do it without
modifying A at all.
I'm afraid it's not possile with separate translation units, but it
might still worth asking.
Anyway, here's an example of where this could be useful.
Let's suppose that, based on a custom reflection system, we have an
object editor. It's a window, with a cell for each registered member
variables of the edited object. When constructing the editor, we create
an EditorCell<T> for each member of type T.
Let's further assume that we have multiple implementations of this
object editor, using differenet GUI libraries. We do this by
subclassing EditorCell. Say, we have EditorCellGui1<T>, and
EditorCellGui2<T>, both derived from EditorCell<T>. (We can also have
specializations of either the base or the derived classes, but that
doesn't really matter now.) We also have some config variable defining
the currently selected implementation to be used. Using this config
var, and our reflection system, we can find the corresponding subclass
of EditorCell<T>, and create a new instance of it.
So when creating a new editor cell for a member of type T, we call
EditorCell<T>::Create() (a static function), which uses the reflection
system to find a subclass (say, EditorCellGui1<T>), and calls the
subclass' Create() function, which returns a new instance.
The problem is that all these classes are templates, so they are only
instantiated if directly referenced. But EditorCellGui1<T> is never
really referenced, so it won't be instantiated, and won't be registered
into the reflection system, won't be found by EditorCell<T>::Create(),
etc.
A nice solution would be to somehow instruct the compiler to
automatically create an EditorCellGui1<T> instance for every
EditorCell<T> instance. Doing this by referencing EditorCellGui1<T>
from EditorCell<T> is quite ugly, since it needs modifying EditorCell
whenever a new gui implementation is added (and it also complicates
dependencies). So it would be better to do it in / at the subclass
(EditorCellGui1).
So, my question is, how do other people do stuff like this? Any
workarounds, alternative designs, suggestions, whatever?
Thanks,
Imre
I'm looking for a way to make sure that whenever a new instance of a
class template A is created, then an instance of class template B is
also created, with the same template parameters. Of course, I could do
it by referencing B<T> from A<T>, but I'd like to do it without
modifying A at all.
I'm afraid it's not possile with separate translation units, but it
might still worth asking.
Anyway, here's an example of where this could be useful.
Let's suppose that, based on a custom reflection system, we have an
object editor. It's a window, with a cell for each registered member
variables of the edited object. When constructing the editor, we create
an EditorCell<T> for each member of type T.
Let's further assume that we have multiple implementations of this
object editor, using differenet GUI libraries. We do this by
subclassing EditorCell. Say, we have EditorCellGui1<T>, and
EditorCellGui2<T>, both derived from EditorCell<T>. (We can also have
specializations of either the base or the derived classes, but that
doesn't really matter now.) We also have some config variable defining
the currently selected implementation to be used. Using this config
var, and our reflection system, we can find the corresponding subclass
of EditorCell<T>, and create a new instance of it.
So when creating a new editor cell for a member of type T, we call
EditorCell<T>::Create() (a static function), which uses the reflection
system to find a subclass (say, EditorCellGui1<T>), and calls the
subclass' Create() function, which returns a new instance.
The problem is that all these classes are templates, so they are only
instantiated if directly referenced. But EditorCellGui1<T> is never
really referenced, so it won't be instantiated, and won't be registered
into the reflection system, won't be found by EditorCell<T>::Create(),
etc.
A nice solution would be to somehow instruct the compiler to
automatically create an EditorCellGui1<T> instance for every
EditorCell<T> instance. Doing this by referencing EditorCellGui1<T>
from EditorCell<T> is quite ugly, since it needs modifying EditorCell
whenever a new gui implementation is added (and it also complicates
dependencies). So it would be better to do it in / at the subclass
(EditorCellGui1).
So, my question is, how do other people do stuff like this? Any
workarounds, alternative designs, suggestions, whatever?
Thanks,
Imre