Boost logo

Boost Users :

Subject: Re: [Boost-users] Change return type automatically
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-10-28 00:28:34


AMDG

Surya Kiran Gullapalli wrote:
> I've a class with multiple boost signals like this
>
> class A
> {
> private:
> boost::signal<void()> m_voidSignal ;
> boost::signal<void (const string&)> m_stringSignal ;
> public:
> boost::signal<void()>& voidSignal() { return m_voidSignal ; }
> boost::signal<void (const string&)>& stringSignal { return
> m_stringSignal ; }
> } ;
>
> when i'm connecting signals to slots, I do some thing like this.
>
> A a;
> a.voidSignal().connect (...) ;
>
> This works fine. But I've many signals with different signatures. what I
> want to achieve is a single interface where I want to map every signal to
> enums and access signal from the enums like this.
>
> class A
> {
> public:
> typedef enum {
> VoidSignal, StringSignal ... } ; SignalIds ;
>
> // return type should be deducted automatically from the enum
> ReturnType& getSignal (SignalId) ;
> ...
> } ;
>
> So that I can do some thing like this
>
> A a;
> a.getSignal (A::VoidSignal).connect (...) ;
> a.getSignal (A::StringSignal).connect (...) ;
>
> Basically with a single interface, I want to change the return type
> automatically. Can I achieve something like this with any of the boost
> libraries (mpl, fusion etc) ? Any pointers on this would be of great help.
>

You can't make the return type change based on a runtime condition.

The best that you can do with a runtime id is:

template<class Args...>
void A::connect(SignalIds id, const Args& args...) {
    switch(id) {
        case VoidSignal: m_voidSignal.connect(args...); break;
        case StringSignal: m_stringSignal.connect(args...); break;
    }
}

If you can use compile time constants instead of runtime values,
you can use indices into a fusion::vector.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net