/// This file is part of the X3DToolKit library
/// Copyright (C) 2002-2004  Yannick Le Goc (legoc@imag.fr)

/// http://artis.imag.fr/Members/Yannick.Legoc/X3D/

/// This library is free software; you can redistribute it and/or
/// modify it under the terms of the GNU Lesser General Public
/// License as published by the Free Software Foundation; either
/// version 2.1 of the License, or (at your option) any later version.

/// This library is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
/// Lesser General Public License for more details.

/// You should have received a copy of the GNU Lesser General Public
/// License along with this library; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////
///                            Signal.h                                       ///
////////////////////////////////////////////////////////////////////////////////

#ifndef SIGNAL_H
#define SIGNAL_H

#include "Platform.h"

#include <map>
#include <list>
#include <iostream>

namespace RC {

template<class A, class B, class T> class TSignal;
template<class A, class T> class SignalSender;

/*! \brief Class declared in <b><X3DTK/kernel.h></b> providing a signal-slot mechanism.
 * 
 * Indeed Qt doesn't allow to write template Q_OBJECT.
 *
 *  \ingroup base
 */

class Signal
{
public:
  virtual ~Signal() {}
  virtual void transmit() = 0;

#ifdef TEMPLATE_SPECIALIZATION_SUPPORTED
  /// Connects two objects.
  template<class A, class SA, class B, class SB, class T>
  inline static void connect(A *a, T (SA:: *getPtr)() const, B *b, void (SB:: *setPtr)(T t))
  {
    //std::cout << "connecting..." << std::endl;
    SignalSender<SA, T>::connect(a, getPtr, static_cast<SB *>(b), setPtr);  
  };
  
  /// Connects two objects which don't exchange datas.
  template<class A, class SA, class B, class SB>
  inline static void connect(A *a, void (SA:: *getPtr)() const, B *b, void (SB:: *setPtr)())
  {
    //std::cout << "connecting..." << std::endl;
    SignalSender<SA, void>::connect(a, getPtr, static_cast<SB *>(b), setPtr);  
  };

  /// Disconnects two connected objects.
  template<class A, class SA, class B, class SB, class T>
  inline static void disconnect(A *a, T (SA:: *getPtr)() const, B *b, void (SB:: *setPtr)(T t))
  {
    //std::cout << "disconnecting...";
    SignalSender<SA, T>::disconnect(a, getPtr, static_cast<SB *>(b), setPtr);  
  };

  /// Disconnects two connected objects which don't exchange datas.
  template<class A, class SA, class B, class SB>
  inline static void disconnect(A *a, void (SA:: *getPtr)() const, B *b, void (SB:: *setPtr)())
  {
    //std::cout << "disconnecting...";
    SignalSender<SA, void>::disconnect(a, getPtr, static_cast<SB *>(b), setPtr);  
  };

  /// Sends the datas from the object a to all the connected objects to a by the 
  /// method of which pointer is getPtr.
  template<class A, class SA, class T>
  inline static void send(A *a, T (SA:: *getPtr)() const)
  {
    SignalSender<SA, T>::send(a, getPtr);  
  };
#endif  
};

#ifdef TEMPLATE_SPECIALIZATION_SUPPORTED

////////////////////////////////////////////////////////////////////////////////
/// SignalSender

template<class A, class T>
class SignalSender
{
private:
  typedef std::list<std::pair<void (A:: *)() const, Signal *> > SignalMap;
  typedef std::map<A *, SignalMap> SignalMapMap;

public:
  template<class B>
  static void connect(A *a, T (A:: *getPtr)() const, B *b, void (B:: *setPtr)(T t));

  template<class B>
  static void disconnect(A *a, T (A:: *getPtr)() const, B *b, void (B:: *setPtr)(T t));

  static void send(A *a, T (A:: *getPtr)() const);
  
private:
  static SignalMapMap _signalMap;
};

template<class A>
class SignalSender<A, void>
{
private:
  typedef std::list<std::pair<void (A:: *)() const, Signal *> > SignalMap;
  typedef std::map<A *, SignalMap> SignalMapMap;

public:
  template<class B>
  static void connect(A *a, void (A:: *getPtr)() const, B *b, void (B:: *setPtr)());

  template<class B>
  static void disconnect(A *a, void (A:: *getPtr)() const, B *b, void (B:: *setPtr)());

  static void send(A *a, void (A:: *getPtr)() const);
  
private:
  static SignalMapMap _signalMap;
};

////////////////////////////////////////////////////////////////////////////////
/// TSignal

template<class A, class B, class T>
class TSignal : public Signal
{
  template<class X, class Y> friend class SignalSender;
public:
  TSignal(A *a, T (A:: *getPtr)() const, B *b, void (B:: *setPtr)(T t));

  void transmit();

private:
  A *_a;
  T (A:: *_getPtr)() const;
  B *_b;
  void (B:: *_setPtr)(T t);
};

template<class A, class B>
class TSignal<A, B, void> : public Signal
{
  template<class X, class Y> friend class SignalSender;
public:
  TSignal(A *a, void (A:: *getPtr)() const, B *b, void (B:: *setPtr)());

  void transmit();

private:
  A *_a;
  void (A:: *_getPtr)() const;
  B *_b;
  void (B:: *_setPtr)();
};


template<class A, class T>  
template<class B>
void SignalSender<A, T>::connect(A *a, T (A:: *getPtr)() const, B *b, void (B:: *setPtr)(T t))
{
  Signal *r = new TSignal<A, B, T>(a, getPtr, b, setPtr);
  _signalMap[a].push_back(std::pair<void (A:: *)() const, Signal *>(reinterpret_cast<void (A:: *)() const>(getPtr), r));
}

template<class A, class T>
template<class B>
void SignalSender<A, T>::disconnect(A *a, T (A:: *getPtr)() const, B *b, void (B:: *setPtr)(T t))
{
  for (typename SignalMap::iterator it = _signalMap[a].begin(); it != _signalMap[a].end(); ++it)
  {
    if ((*it).first == reinterpret_cast<void (A:: *)() const>(getPtr))
    {
      TSignal<A, B, T> *tr = dynamic_cast<TSignal<A, B, T> *>((*it).second);
    
      if ((tr != 0) 
       && (b == tr->_b)
       && (setPtr == tr->_setPtr))
      {
        _signalMap[a].erase(it);
        delete tr;
        if (_signalMap[a].empty())  
          _signalMap.erase(a);
        return;           
      }
    }
  }  
}

template<class A, class T>
void SignalSender<A, T>::send(A *a, T (A:: *getPtr)() const)
{
  //std::cout << "sending..." << std::endl;      
  for (typename SignalMap::iterator it = _signalMap[a].begin(); it != _signalMap[a].end(); ++it)
  {
    if ((*it).first == reinterpret_cast<void (A:: *)() const>(getPtr))
      (*it).second->transmit();
  }  
}

template<class A>
template<class B>
void SignalSender<A, void>::connect(A *a, void (A:: *getPtr)() const, B *b, void (B:: *setPtr)())
{
  Signal *r = new TSignal<A, B, void>(a, getPtr, b, setPtr);
  _signalMap[a].push_back(std::pair<void (A:: *)() const, Signal *>(reinterpret_cast<void (A:: *)() const>(getPtr), r));
}

template<class A>
template<class B>
void SignalSender<A, void>::disconnect(A *a, void (A:: *getPtr)() const, B *b, void (B:: *setPtr)())
{
  for (typename SignalMap::iterator it = _signalMap[a].begin(); it != _signalMap[a].end(); ++it)
  {
    if ((*it).first == reinterpret_cast<void (A:: *)() const>(getPtr))
    {
      TSignal<A, B, void> *tr = dynamic_cast<TSignal<A, B, void> *>((*it).second);
    
      if ((tr != 0) 
       && (b == tr->_b)
       && (setPtr == tr->_setPtr))
      {
        _signalMap[a].erase(it);
        delete tr;
        if (_signalMap[a].empty())  
          _signalMap.erase(a);
        return;
      }
    }
  }
}

template<class A>
void SignalSender<A, void>::send(A *a, void (A:: *getPtr)() const)
{
  //std::cout << "sending void..." << std::endl;    
  for (typename SignalMap::iterator it = _signalMap[a].begin(); it != _signalMap[a].end(); ++it)
  {
    if ((*it).first == reinterpret_cast<void (A:: *)() const>(getPtr))
      (*it).second->transmit();
  } 
}

template<class A, class T>
typename SignalSender<A, T>::SignalMapMap SignalSender<A, T>::_signalMap;

template<class A>
typename SignalSender<A, void>::SignalMapMap SignalSender<A, void>::_signalMap;

////////////////////////////////////////////////////////////////////////////////
/// TSignal

template<class A, class B, class T>
TSignal<A, B, T>::TSignal(A *a, T (A:: *getPtr)() const, B *b, void (B:: *setPtr)(T t))
: _a(a), _getPtr(getPtr), _b(b), _setPtr(setPtr)
{
}

template<class A, class B, class T>
void TSignal<A, B, T>::transmit()
{
  //std::cout << "transmitting..." << std::endl;
  (_b->*_setPtr)((_a->*_getPtr)());
}

template<class A, class B>
TSignal<A, B, void>::TSignal(A *a, void (A:: *getPtr)() const, B *b, void (B:: *setPtr)())
: _a(a), _getPtr(getPtr), _b(b), _setPtr(setPtr)
{
}

template<class A, class B>
void TSignal<A, B, void>::transmit()
{
  //std::cout << "transmitting..." << std::endl;
  (_a->*_getPtr)();
  (_b->*_setPtr)();
}

#endif

}

#endif

