I'm using JUCE as a x-platform framework, and I'm using template listener classes to map button/combobox etc. callbacks to certain handler functions. Since the different widgets have their own callback function name, I use the following structure:
template<typename Type, typename Widget>
class ListenerBase : public Widget::Listener
{
public:
typedef void (Type::*TCallbackType)(void);
protected:
void notifyCallback(Widget* notifier)
{
...
}
void addHandler(Widget* notifier, TCallbackType callback)
{
notifier->addListener(this);
...
}
};
template<typename Type>
class ButtonListenerHandler : public ListenerBase<Type, Button>
{
protected:
void buttonClicked(Button* btn)
{
notifyCallback(btn);
}
};
template<typename Type>
class LabelListenerHandler : public ListenerBase<Type, Label>
{
protected:
void labelTextChanged(Label* lbl)
{
notifyCallback(lbl);
}
};
And it works fine, as long as I use only one of the handler specializations in my class. As soon as I use more than one, VC++ 2008 complains of ambiguity between the addHandler calls as if the compiler cannot distiguish between addHandler(Button*, ...) and addHandler(Label*, ...) !! These functions are of different prototypes due to being templatized, so I have no idea why the compiler is giving me a hard time. Ideas ?
Edit due to requests:
A class with different listeners may look like:
class MyClass : public ButtonListenerHandler<MyClass>
, public LabelListenerHandler<MyClass>
{
...
void buttonHandlerFunction();
void labelHandlerFunction();
Button* m_btn;
Label* m_label;
};
A where the error occurs:
MyClass::MyClass()
{
...
addHandler(m_btn, &MyClass::buttonHandlerFunction); <<< error
addHandler(m_label, &MyClass::labelHandlerFunction); <<< error
}
And the error is:
1>MyClass.cpp(287) : error C2385: ambiguous access of 'addHandler'
1> could be the 'addHandler' in base 'ListenerBase<MyClass,juce::Button>'
1> or could be the 'addHandler' in base 'ListenerBase<MyClass,juce::Label>'