ssim.h
Go to the documentation of this file.
// -*-C++-*-
//
// This file is part of SSim, a simple discrete-event simulator.
// See http://www.inf.usi.ch/carzaniga/ssim/
//
// Copyright (C) 1998-2004 University of Colorado
// Copyright (C) 2012 Antonio Carzaniga
//
// Authors: Antonio Carzaniga <firstname.lastname@usi.ch>
// See AUTHORS for full details.
//
// SSim is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// SSim 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SSim. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef _ssim_h
#define _ssim_h
#include <functional>
#include <string>
namespace ssim {
typedef int ProcessId;
const ProcessId NULL_PROCESSID = -1;
typedef double Time;
const Time INIT_TIME = 0;
class Event {
public:
Event(): refcount(0) {};
virtual ~Event() {};
virtual std::string str() const { return "(event)"; };
private:
mutable unsigned refcount;
friend class SimImpl; // this is an opaque implementation class
friend class Sim; // these need to be friends to manage refcount
};
typedef std::function<bool (const Event *)> EventPredicate;
class Process {
public:
virtual ~Process() {};
virtual void initialize(void) {};
virtual void process_event(const Event * msg) {};
virtual void stop(void) {};
};
class ProcessWithPId : public Process {
public:
ProcessId activate() throw();
ProcessId pid() const throw();
ProcessWithPId() throw();
private:
ProcessId process_id;
};
class SimErrorHandler {
public:
virtual ~SimErrorHandler() {}
virtual void clear() throw() {}
virtual void handle_busy(ProcessId p, const Event * e) throw() {}
virtual void handle_terminated(ProcessId p, const Event * e) throw() {}
};
class Sim {
public:
static ProcessId create_process(Process *) throw();
static int stop_process(ProcessId) throw();
static void stop_process() throw();
static void clear() throw();
static void self_signal_event(const Event * e) throw();
static void self_signal_event(const Event * e, Time delay) throw();
static void signal_event(ProcessId p, const Event * e) throw();
static void signal_event(ProcessId p, const Event * e, Time d) throw();
static void advance_delay(Time) throw();
static ProcessId this_process() throw();
static Time clock() throw();
static void run_simulation();
static void stop_simulation() throw();
static void set_stop_time(Time t = INIT_TIME) throw();
static void set_error_handler(SimErrorHandler *) throw();
static void ignore_event(EventPredicate pred) throw();
};
void Rprint_actions();
} // end namespace ssim
#endif /* _ssim_h */
ssim::INIT_TIME
const Time INIT_TIME
beginning of time
Definition: ssim.h:79
ssim::Process::initialize
virtual void initialize(void)
action executed when the process is initialized.
Definition: ssim.h:138
ssim::Sim::this_process
static ProcessId this_process()
returns the current process
Definition: ssim.cc:260
ssim::Sim::signal_event
static void signal_event(ProcessId p, const Event *e)
signal an event to the given process immediately
Definition: ssim.cc:276
ssim::EventPredicate
std::function< bool(const Event *)> EventPredicate
Definition: ssim.h:118
ssim::Sim::create_process
static ProcessId create_process(Process *)
creates a new process
Definition: ssim.cc:108
ssim::Sim::set_error_handler
static void set_error_handler(SimErrorHandler *)
registers a handler for simulation errors.
Definition: ssim.cc:284
ssim::ProcessWithPId::pid
ProcessId pid() const
process id of this process.
Definition: ssim.cc:298
ssim::Sim::stop_simulation
static void stop_simulation()
stops execution of the simulation
Definition: ssim.cc:251
ssim::Process
Virtual class (interface) representing processes running within the simulator.
Definition: ssim.h:125
ssim::SimImpl
Definition: ssim.cc:90
ssim::SimErrorHandler::~SimErrorHandler
virtual ~SimErrorHandler()
Definition: ssim.h:253
ssim::ProcessId
int ProcessId
process identifier type
Definition: ssim.h:55
ssim::Process::stop
virtual void stop(void)
executed when the process is explicitly stopped.
Definition: ssim.h:203
ssim::SimErrorHandler
an error handler for simulation errors.
Definition: ssim.h:251
ssim::Sim::advance_delay
static void advance_delay(Time)
advance the execution time of the current process.
Definition: ssim.cc:255
ssim::Sim::ignore_event
static void ignore_event(EventPredicate pred)
Definition: ssim.cc:131
ssim::Event
basic event in the simulation.
Definition: ssim.h:106
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:237
ssim::SimErrorHandler::clear
virtual void clear()
handles a clear operation.
Definition: ssim.h:263
ssim::Event::Event
Event()
Definition: ssim.h:108
ssim::ProcessWithPId
utility Process class providing a utility interface with the simulator.
Definition: ssim.h:212
ssim::Event::~Event
virtual ~Event()
Definition: ssim.h:109
ssim::ProcessWithPId::activate
ProcessId activate()
activates this process within the simulator.
Definition: ssim.cc:288
ssim::Event::refcount
unsigned refcount
Definition: ssim.h:110
ssim::SimErrorHandler::handle_terminated
virtual void handle_terminated(ProcessId p, const Event *e)
handles terminated-process conditions.
Definition: ssim.h:301
ssim::Sim::clear
static void clear()
clears out internal data structures
Definition: ssim.cc:115
ssim::ProcessWithPId::ProcessWithPId
ProcessWithPId()
Definition: ssim.cc:296
ssim::Sim::self_signal_event
static void self_signal_event(const Event *e)
signal an event to the current process immediately
Definition: ssim.cc:268
ssim::Sim::run_simulation
static void run_simulation()
starts execution of the simulation
Definition: ssim.cc:148
ssim::Sim::clock
static Time clock()
returns the current virtual time for the current process
Definition: ssim.cc:264
ssim::SimErrorHandler::handle_busy
virtual void handle_busy(ProcessId p, const Event *e)
handles busy-process conditions.
Definition: ssim.h:282
ssim::Event::str
virtual std::string str() const
Definition: ssim.h:110
ssim::Time
double Time
virtual time type
Definition: ssim.h:75
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:194
ssim::Sim::stop_process
static void stop_process()
stops the execution of the current process
Definition: ssim.cc:241
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:3
ssim::Rprint_actions
void Rprint_actions()
Definition: ssim.cc:81
ssim::Sim
a generic discrete-event sequential simulator
Definition: ssim.h:326
ssim::NULL_PROCESSID
const ProcessId NULL_PROCESSID
no process will be identified by NULL_PROCESSID
Definition: ssim.h:59
ssim::Process::~Process
virtual ~Process()
Definition: ssim.h:127
ssim::ProcessWithPId::process_id
ProcessId process_id
Definition: ssim.h:238