adevs
/home/rotten/adevs-2.6/include/adevs_abstract_simulator.h
00001 /***************
00002 Copyright (C) 2008 by James Nutaro
00003 
00004 This library is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU Lesser General Public
00006 License as published by the Free Software Foundation; either
00007 version 2 of the License, or (at your option) any later version.
00008 
00009 This library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public
00015 License along with this library; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 Bugs, comments, and questions can be sent to nutaro@gmail.com
00019 ***************/
00020 #ifndef __adevs_abstract_simulator_h_
00021 #define __adevs_abstract_simulator_h_
00022 #include "adevs_models.h"
00023 #include "adevs_event_listener.h"
00024 #include "adevs_bag.h"
00025 
00026 namespace adevs
00027 {
00028 
00034 template <class X, class T = double> class AbstractSimulator 
00035 {
00036         public:
00037                 AbstractSimulator(){}
00042                 void addEventListener(EventListener<X,T>* l)
00043                 {
00044                         listeners.insert(l);
00045                 }
00047                 void removeEventListener(EventListener<X,T>* l)
00048                 {
00049                         listeners.erase(l);
00050                 }
00052                 virtual T nextEventTime() = 0;
00054                 virtual void execUntil(T tend) = 0;
00056                 virtual ~AbstractSimulator(){}
00058                 void notify_output_listeners(Devs<X,T>* model, const X& value, T t);
00060                 void notify_state_listeners(Atomic<X,T>* model, T t);
00061         private:
00063                 Bag<EventListener<X,T>*> listeners;
00064 
00065 };
00066 
00067 template <class X, class T>
00068 void AbstractSimulator<X,T>::notify_output_listeners(Devs<X,T>* model, const X& value, T t)
00069 {
00070         Event<X,T> event(model,value);
00071         typename Bag<EventListener<X,T>*>::iterator iter;
00072         for (iter = listeners.begin(); iter != listeners.end(); iter++)
00073         {
00074                 (*iter)->outputEvent(event,t);
00075         }
00076 }
00077 
00078 template <class X, class T>
00079 void AbstractSimulator<X,T>::notify_state_listeners(Atomic<X,T>* model, T t)
00080 {
00081         typename Bag<EventListener<X,T>*>::iterator iter;
00082         for (iter = listeners.begin(); iter != listeners.end(); iter++)
00083         {
00084                 (*iter)->stateChange(model,t);
00085         }
00086 }
00087 
00088 } // end of namespace
00089 
00090 #endif