module.h

Go to the documentation of this file.
00001 //    sampalib.org ESL library and tools
00002 //    Copyright (C) 2007  Thierry Grellier
00003 //
00004 //    This program is free software; you can redistribute it and/or modify
00005 //    it under the terms of the GNU General Public License version 2 as
00006 //    published by the Free Software Foundation.
00007 //
00008 //    This program is distributed in the hope that it will be useful,
00009 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //    GNU General Public License for more details.
00012 //
00013 //    You should have received a copy of the GNU General Public License along
00014 //    with this program; if not, write to the Free Software Foundation, Inc.,
00015 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 //
00017 //    contact: www.sampalib.org
00018 
00019 #ifndef SAMPA_MODULE_H
00020 #define SAMPA_MODULE_H
00021 
00022 #include <string>
00023 #include "sampa/core/triggerable.h"
00024 #include "sampa/core/object.h"
00025 
00026 namespace Sampa {
00027 
00028 class EventListenerSingle;
00029 class Simulation;
00030     
00031 class ProcessBase : public Object, public Triggerable {
00032 // would have used protected Triggerable, but serialize doesn't work then...
00033 public:
00034   ProcessBase(const Name& nm);   
00035   ProcessBase& set_next_trigger(EventListenerSingle& event);
00036   ProcessBase& dont_initialize();
00037 protected:
00038   virtual EventListenerSingle* execute() = 0;  
00039   EventListenerSingle* m_trigger;
00040 private:
00041   friend class Simulation;
00042   void trigger();
00043   // disabled
00044   ProcessBase(const ProcessBase&);
00045   ProcessBase& operator=(const ProcessBase&);
00046   // state
00047   bool           m_dont_initialize;
00048   SAMPA_PERSISTENT(ProcessBase);
00049 };
00050 
00051 class Module;
00052 
00053 template<class UserModule>
00054 class Process : public ProcessBase {
00055 public:
00056   typedef void (UserModule::*Method)();
00057   Process(const Name& name, UserModule* module, Method method);
00058   void restart(Method method) { m_method = method; }
00059 private:
00060   EventListenerSingle* execute();
00061   // disabled
00062   Process(const Process&);
00063   Process& operator=(const Process&);
00064   // state  
00065   UserModule* m_module;
00066   Method      m_method; // need to be restored at restart
00067 public:
00068   SAMPA_PERSISTENT(Process); 
00069 };
00070 
00071 class Module : public ContainerObject {
00072 public:  
00073   Module(const ContainerName& nm);
00074 protected:
00075   void set_next_trigger(EventListenerSingle&);
00076 private:
00077   // disabled
00078   Module(const Module&);
00079   Module& operator=(const Module&);
00080   typedef Sampa::Process<Module> Process;
00081   Process& create_process(const std::string& name, Process::Method method);
00082   void restart_process(const std::string& name, Process::Method method);
00083 protected:
00084   ProcessBase* __get_process_by_name(const std::string& name);
00085 public:
00086   SAMPA_PERSISTENT(Module);
00087 };
00088 
00089 # define SAMPA_MODULE(Class)                                                 \
00090   SAMPA_PERSISTENT(Class);                                                    \
00091   public:                                                                     \
00092     typedef Sampa::Process<Class> Process;                                    \
00093   private:                                                                    \
00094     Process& create_process(const std::string& name, Process::Method method); \
00095     void restart_process(const std::string& name, Process::Method method);    \
00096     Class(const Class&);                                                      \
00097     Class& operator=(const Class&);
00098 
00099 # define SAMPA_TEMPLATE_MODULE(Class)                                         \
00100   SAMPA_PERSISTENT(Class);                                                    \
00101   public:                                                                     \
00102     typedef Sampa::Process<Class> Process;                                    \
00103   private:                                                                    \
00104     Process& create_process(const std::string& name, typename Process::Method method); \
00105     void restart_process(const std::string& name, typename Process::Method method);    \
00106     Class(const Class&);                                                      \
00107     Class& operator=(const Class&);
00108 
00109 inline ProcessBase& ProcessBase::set_next_trigger(EventListenerSingle& event)
00110 { 
00111   m_trigger = &event; 
00112   return *this;
00113 }
00114 
00115 } // namespace Sampa
00116 
00117 
00118 #endif

Generated on Sat Feb 16 16:23:15 2008 for Sampa by  doxygen 1.5.3