M
macracan
It has been discussed before, but I still can't find a solution and I
have a need. So here goes:
The problem:
I'm writing a something to handle messages from XWindows. The idea is
to have virtual handlers for different
kind of messages. But XWindows has a feature thereby you have to
subscribe to messages you're interested in.
I was thinking of providing empty handlers by default, and overwrite
them in derived classes. And I would need
to automatically detect which virtual fcts are overwritten, so I can
tell XWindows to which message I wish to subscribe.
Somewhat like so:
struct WindowSink
{
// ...
// default empty handlers
virtual void Move(int x, int y){}
virtual void Draw(){}
};
//...
struct MyWindowSink : public WindowSink
{
// overwrite Draw only
void Draw(){ /*some different behavior*/}
};
void Subscribe(WindowSink &sink)
{
static WindowSink sinkref; // some reference to compare with
long mask = 0;
// figure out which messages to subscribe to
if (&sinkref.Move != &sink.Move) mask |= 1;
if (&sinkref.Draw != &sink.Draw) mask |= 2;
// subscribe to messages based on mask
XSelectInput(/* some other params */, mask);
}
int main()
{
MyWindodwSink sink;
//...
Enable(sink);
// ...
return 0;
}
Now as the group already knows, the lines
if (&sinkref.Move != &sink.Move) mask |= 1;
if (&sinkref.Draw != &sink.Draw) mask |= 2;
are not valid C++ code anymore.
Is there any way I can write it?
BTW, I've looked into rtti (didn't find solution), and low level non-
portable magic (got lost and gave up).
I haven't thought it through completely, but am quite sure I can solve
the problem if I use something other then
virtual functions. I could use functionoids (hope I got the right word
here) or could emulate the virtual fct mechanism completely under my
control. The first alternative is not as elegant as using virt
functions (and lacks from data members being inaccessible inside
functionoids), while the second is downright ugly.
Thanx,
Adrian
have a need. So here goes:
The problem:
I'm writing a something to handle messages from XWindows. The idea is
to have virtual handlers for different
kind of messages. But XWindows has a feature thereby you have to
subscribe to messages you're interested in.
I was thinking of providing empty handlers by default, and overwrite
them in derived classes. And I would need
to automatically detect which virtual fcts are overwritten, so I can
tell XWindows to which message I wish to subscribe.
Somewhat like so:
struct WindowSink
{
// ...
// default empty handlers
virtual void Move(int x, int y){}
virtual void Draw(){}
};
//...
struct MyWindowSink : public WindowSink
{
// overwrite Draw only
void Draw(){ /*some different behavior*/}
};
void Subscribe(WindowSink &sink)
{
static WindowSink sinkref; // some reference to compare with
long mask = 0;
// figure out which messages to subscribe to
if (&sinkref.Move != &sink.Move) mask |= 1;
if (&sinkref.Draw != &sink.Draw) mask |= 2;
// subscribe to messages based on mask
XSelectInput(/* some other params */, mask);
}
int main()
{
MyWindodwSink sink;
//...
Enable(sink);
// ...
return 0;
}
Now as the group already knows, the lines
if (&sinkref.Move != &sink.Move) mask |= 1;
if (&sinkref.Draw != &sink.Draw) mask |= 2;
are not valid C++ code anymore.
Is there any way I can write it?
BTW, I've looked into rtti (didn't find solution), and low level non-
portable magic (got lost and gave up).
I haven't thought it through completely, but am quite sure I can solve
the problem if I use something other then
virtual functions. I could use functionoids (hope I got the right word
here) or could emulate the virtual fct mechanism completely under my
control. The first alternative is not as elegant as using virt
functions (and lacks from data members being inaccessible inside
functionoids), while the second is downright ugly.
Thanx,
Adrian