Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members

glsignal.h

00001 /***************************************************************************
00002                           glsignal.h  -  description
00003                              -------------------
00004     begin                : Thu Aug 14 2003
00005     copyright            : (C) 2003 by Jacques Gasselin de Richebourg
00006     email                : jacquesgasselin@hotmail.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU Lesser General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef GLSIGNAL_H
00019 #define GLSIGNAL_H
00020 
00021 #include <vector>
00022 #include "glassert.h"
00023 
00037 template <class ArgType, class ReturnType = void>
00038 class Slot
00039 {
00040 public:
00041    virtual ReturnType operator ()(ArgType arg) = 0;
00042 };
00043 
00044 
00045 template <class ArgType, class ReturnType = void>
00046 class Signal
00047 {
00048    std::vector< Slot<ArgType,ReturnType>* > slotList;
00049 public:
00050    Signal(){ }
00051    
00052    std::vector<ReturnType> send(ArgType arg)
00053    {
00054       std::vector<ReturnType> returns;
00055       for(unsigned i = 0; i < slotList.size(); ++i )
00056          returns.push_back((*slotList[i])(arg));
00057       return returns;
00058    }
00059 
00060    void sendWithVector(ArgType arg, std::vector<ReturnType>& vec)
00061    {
00062       for(unsigned i = 0; i < slotList.size(); ++i )
00063          vec.push_back((*slotList[i])(arg));
00064    }
00065    
00066    void connect(Slot<ArgType,ReturnType>* s)
00067    {   slotList.push_back(s);   }
00068 
00069    void disconnect(Slot<ArgType,ReturnType>* s)
00070    {   slotList.erase(s);   }
00071 };
00072 
00073 template <class ArgType>
00074 class Signal<ArgType, void>
00075 {
00076    std::vector< Slot<ArgType,void>* > slotList;
00077 public:
00078    Signal(){ }
00079 
00080    void send(ArgType arg)
00081    {
00082       for(unsigned i = 0; i < slotList.size(); ++i )
00083          (*slotList[i])(arg);
00084    }
00085 
00086    void connect(Slot<ArgType,void>* s)
00087    {   slotList.push_back(s);   }
00088 
00089    void disconnect(Slot<ArgType,void>* s)
00090    {   slotList.erase(s);   }
00091 };
00092 
00093 #endif

Generated on Wed Feb 4 23:11:33 2004 by doxygen 1.3.3