Microsimulation API
ssim.h
Go to the documentation of this file.
1 // -*-C++-*-
2 //
3 // This file is part of SSim, a simple discrete-event simulator.
4 // See http://www.inf.usi.ch/carzaniga/ssim/
5 //
6 // Copyright (C) 1998-2004 University of Colorado
7 // Copyright (C) 2012 Antonio Carzaniga
8 //
9 // Authors: Antonio Carzaniga <firstname.lastname@usi.ch>
10 // See AUTHORS for full details.
11 //
12 // SSim is free software: you can redistribute it and/or modify it under
13 // the terms of the GNU General Public License as published by the Free
14 // Software Foundation, either version 3 of the License, or (at your
15 // option) any later version.
16 //
17 // SSim is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with SSim. If not, see <http://www.gnu.org/licenses/>.
24 //
25 #ifndef _ssim_h
26 #define _ssim_h
27 
28 #include <boost/function.hpp>
29 #include <string>
30 
51 namespace ssim {
52 
55 extern const char * Version;
56 
59 typedef int ProcessId;
60 
64 
79 typedef double Time;
80 
83 const Time INIT_TIME = 0;
84 
111 class Event {
112  public:
113  Event(): refcount(0) {};
114  virtual ~Event() {};
115  virtual std::string str() const { return "(event)"; };
116 
117  private:
118  mutable unsigned refcount;
119  friend class SimImpl; // this is an opaque implementation class
120  friend class Sim; // these need to be friends to manage refcount
121 };
122 
123  typedef boost::function<bool (const Event *)> EventPredicate;
124 
130 class Process {
131  public:
132  virtual ~Process() {};
133 
143  virtual void initialize(void) {};
144 
199  virtual void process_event(const Event * msg) {};
200 
208  virtual void stop(void) {};
209 };
210 
217 class ProcessWithPId : public Process {
218  public:
230  ProcessId activate() throw();
231 
238  ProcessId pid() const throw();
239 
240  ProcessWithPId() throw();
241 
242  private:
244 };
245 
257 public:
258  virtual ~SimErrorHandler() {}
259 
268  virtual void clear() throw() {}
269 
287  virtual void handle_busy(ProcessId p, const Event * e) throw() {}
288 
306  virtual void handle_terminated(ProcessId p, const Event * e) throw() {}
307 };
308 
331 class Sim {
332 public:
345  static ProcessId create_process(Process *) throw();
346 
348  static int stop_process(ProcessId) throw();
350  static void stop_process() throw();
351 
362  static void clear() throw();
363 
378  static void self_signal_event(const Event * e) throw();
379 
396  static void self_signal_event(const Event * e, Time delay) throw();
397 
417  static void signal_event(ProcessId p, const Event * e) throw();
418 
440  static void signal_event(ProcessId p, const Event * e, Time d) throw();
441 
519  static void advance_delay(Time) throw();
520 
542  static ProcessId this_process() throw();
543 
561  static Time clock() throw();
562 
564  static void run_simulation();
566  static void stop_simulation() throw();
567 
580  static void set_stop_time(Time t = INIT_TIME) throw();
581 
589  static void set_error_handler(SimErrorHandler *) throw();
590  static void ignore_event(EventPredicate pred) throw();
591 };
592  void Rprint_actions();
593 
594 } // end namespace ssim
595 
596 #endif /* _ssim_h */
597 
ssim::ProcessWithPId::process_id
ProcessId process_id
Definition: ssim.h:243
ssim::Event::refcount
unsigned refcount
Definition: ssim.h:115
ssim::Sim::set_stop_time
static void set_stop_time(Time t=INIT_TIME)
stops the execution of the simulation at the given time
Definition: ssim.cc:239
ssim::Event::Event
Event()
Definition: ssim.h:113
ssim::Sim::self_signal_event
static void self_signal_event(const Event *e)
signal an event to the current process immediately
Definition: ssim.cc:270
ssim::Sim
a generic discrete-event sequential simulator
Definition: ssim.h:331
ssim::Event::~Event
virtual ~Event()
Definition: ssim.h:114
ssim::Version
const char * Version
version identifier for this ssim library
Definition: ssim.cc:33
ssim::ProcessWithPId::activate
ProcessId activate()
activates this process within the simulator.
Definition: ssim.cc:290
ssim::SimErrorHandler::handle_terminated
virtual void handle_terminated(ProcessId p, const Event *e)
handles terminated-process conditions.
Definition: ssim.h:306
ssim::Process
Virtual class (interface) representing processes running within the simulator.
Definition: ssim.h:130
ssim::Event::str
virtual std::string str() const
Definition: ssim.h:115
ssim::Process::stop
virtual void stop(void)
executed when the process is explicitly stopped.
Definition: ssim.h:208
ssim::Sim::create_process
static ProcessId create_process(Process *)
creates a new process
Definition: ssim.cc:110
ssim::SimErrorHandler::clear
virtual void clear()
handles a clear operation.
Definition: ssim.h:268
ssim::Sim::set_error_handler
static void set_error_handler(SimErrorHandler *)
registers a handler for simulation errors.
Definition: ssim.cc:286
ssim::Process::initialize
virtual void initialize(void)
action executed when the process is initialized.
Definition: ssim.h:143
ssim::Process::~Process
virtual ~Process()
Definition: ssim.h:132
ssim::Sim::clock
static Time clock()
returns the current virtual time for the current process
Definition: ssim.cc:266
ssim::ProcessId
int ProcessId
process identifier type
Definition: ssim.h:59
ssim::ProcessWithPId::pid
ProcessId pid() const
process id of this process.
Definition: ssim.cc:300
ssim::Sim::stop_process
static void stop_process()
stops the execution of the current process
Definition: ssim.cc:243
ssim::SimErrorHandler::~SimErrorHandler
virtual ~SimErrorHandler()
Definition: ssim.h:258
ssim::SimErrorHandler
an error handler for simulation errors.
Definition: ssim.h:256
ssim::Time
double Time
virtual time type
Definition: ssim.h:79
ssim::Event
basic event in the simulation.
Definition: ssim.h:111
ssim::Sim::advance_delay
static void advance_delay(Time)
advance the execution time of the current process.
Definition: ssim.cc:257
ssim::SimImpl
Definition: ssim.cc:92
ssim::Process::process_event
virtual void process_event(const Event *msg)
action executed in response to an event signaled to this process.
Definition: ssim.h:199
ssim::Rprint_actions
void Rprint_actions()
Definition: ssim.cc:83
ssim::Sim::this_process
static ProcessId this_process()
returns the current process
Definition: ssim.cc:262
ssim::ProcessWithPId
utility Process class providing a utility interface with the simulator.
Definition: ssim.h:217
ssim::SimErrorHandler::handle_busy
virtual void handle_busy(ProcessId p, const Event *e)
handles busy-process conditions.
Definition: ssim.h:287
ssim::EventPredicate
boost::function< bool(const Event *)> EventPredicate
Definition: ssim.h:123
ssim::ProcessWithPId::ProcessWithPId
ProcessWithPId()
Definition: ssim.cc:298
ssim::Sim::stop_simulation
static void stop_simulation()
stops execution of the simulation
Definition: ssim.cc:253
ssim::NULL_PROCESSID
const ProcessId NULL_PROCESSID
no process will be identified by NULL_PROCESSID
Definition: ssim.h:63
ssim::Sim::clear
static void clear()
clears out internal data structures
Definition: ssim.cc:117
ssim::INIT_TIME
const Time INIT_TIME
beginning of time
Definition: ssim.h:83
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:8
ssim::Sim::signal_event
static void signal_event(ProcessId p, const Event *e)
signal an event to the given process immediately
Definition: ssim.cc:278
ssim::Sim::run_simulation
static void run_simulation()
starts execution of the simulation
Definition: ssim.cc:150
ssim::Sim::ignore_event
static void ignore_event(EventPredicate pred)
Definition: ssim.cc:133