Change return type automatically

View: New views
5 Messages — Rating Filter:   Alert me  

Change return type automatically

by Surya Kiran Gullapalli-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

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.

Thanks,
Surya

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: Change return type automatically

by Steven Watanabe-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: Change return type automatically

by Surya Kiran Gullapalli-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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;
  }
}

 
I've already thought of this. 
 
If you can use compile time constants instead of runtime values,
you can use indices into a fusion::vector.

The SignalId I'm talking about is a compile time constant (an enum)
I'm trying this, but could not get anywhere for the past few days.  Any pointers on this ?

Thanks,
Surya

Surya

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: Change return type automatically

by Steven Watanabe-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

AMDG

Surya Kiran Gullapalli wrote:
>> If you can use compile time constants instead of runtime values,
>> you can use indices into a fusion::vector.
>>    
>
>
> The SignalId I'm talking about is a compile time constant (an enum)
> I'm trying this, but could not get anywhere for the past few days.  Any
> pointers on this ?
>  

#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/at.hpp>

class A {
private:
    typedef boost::fusion::vector<
        boost::signal<void()>,
        boost::signal<void (const string&)>
    > signals_type;
    signals_type m_signals;
public:
    static const int VoidSignal = 0;
    static const int StringSignal = 1;
    template<int ID>
    typename boost::fusion::result_of::at_c<signals_type, ID>::type
    getSignal() { return boost::fusion::at_c<ID>(m_signals); }
};

void f() {
    A a;
    a.getSignal<A::VoidSignal>()
}

In Christ,
Steven Watanabe

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: Change return type automatically

by Surya Kiran Gullapalli-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/at.hpp>

class A {
private:
  typedef boost::fusion::vector<
      boost::signal<void()>,

      boost::signal<void (const string&)>
  > signals_type;
  signals_type m_signals;
public:
  static const int VoidSignal = 0;
  static const int StringSignal = 1;
  template<int ID>
  typename boost::fusion::result_of::at_
c<signals_type, ID>::type
  getSignal() { return boost::fusion::at_c<ID>(m_
signals); }
};

void f() {
  A a;
  a.getSignal<A::VoidSignal>()

}


It worked,
Thanks,

Surya


_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users