Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions protothread.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ class Protothread
public:
// Construct a new protothread that will start from the beginning
// of its Run() function.
Protothread() : _ptLine(0) { }
Protothread() = default;

virtual ~Protothread() = default;

// Restart protothread.
void Restart() { _ptLine = 0; }
Expand All @@ -100,7 +102,7 @@ class Protothread

// Return true if the protothread is running or waiting, false if it has
// ended or exited.
bool IsRunning() { return _ptLine != LineNumberInvalid; }
bool IsRunning() const { return _ptLine != LineNumberInvalid; }

// Run next part of protothread or return immediately if it's still
// waiting. Return true if protothread is still running, false if it
Expand All @@ -110,14 +112,14 @@ class Protothread
protected:
// Used to store a protothread's position (what Dunkels calls a
// "local continuation").
typedef unsigned short LineNumber;
using LineNumber = unsigned short;

// An invalid line number, used to mark the protothread has ended.
static const LineNumber LineNumberInvalid = (LineNumber)(-1);

// Stores the protothread's position (by storing the line number of
// the last PT_WAIT, which is then switched on at the next Run).
LineNumber _ptLine;
LineNumber _ptLine = 0;
};

// Declare start of protothread (use at start of Run() implementation).
Expand Down
43 changes: 21 additions & 22 deletions simcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace simcpp {

SimulationPtr Simulation::create() { return std::make_shared<Simulation>(); }

void Simulation::run_process(ProcessPtr process, simtime delay /* = 0.0 */) {
void Simulation::run_process(const ProcessPtr& process, simtime delay /* = 0.0 */) {
auto event = this->event();
event->add_handler(process);
event->trigger(delay);
Expand All @@ -23,7 +23,7 @@ EventPtr Simulation::timeout(simtime delay) {

EventPtr Simulation::any_of(std::initializer_list<EventPtr> events) {
int n = 1;
for (auto &event : events) {
for (const auto &event : events) {
if (event->is_triggered()) {
n = 0;
break;
Expand All @@ -39,7 +39,7 @@ EventPtr Simulation::any_of(std::initializer_list<EventPtr> events) {

EventPtr Simulation::all_of(std::initializer_list<EventPtr> events) {
int n = 0;
for (auto &event : events) {
for (const auto &event : events) {
if (!event->is_triggered()) {
++n;
}
Expand All @@ -52,7 +52,7 @@ EventPtr Simulation::all_of(std::initializer_list<EventPtr> events) {
return process;
}

void Simulation::schedule(EventPtr event, simtime delay /* = 0.0 */) {
void Simulation::schedule(const EventPtr& event, simtime delay /* = 0.0 */) {
queued_events.emplace(now + delay, next_id, event);
++next_id;
}
Expand All @@ -78,7 +78,7 @@ void Simulation::advance_by(simtime duration) {
now = target;
}

bool Simulation::advance_to(EventPtr event) {
bool Simulation::advance_to(const EventPtr& event) {
while (event->is_pending() && has_next()) {
step();
}
Expand All @@ -87,19 +87,18 @@ bool Simulation::advance_to(EventPtr event) {
}

void Simulation::run() {
while (step()) {
}
while (step());
}

simtime Simulation::get_now() { return now; }
simtime Simulation::get_now() const { return now; }

bool Simulation::has_next() { return !queued_events.empty(); }
bool Simulation::has_next() const { return !queued_events.empty(); }

simtime Simulation::peek_next_time() { return queued_events.top().time; }
simtime Simulation::peek_next_time() const { return queued_events.top().time; }

/* Simulation::QueuedEvent */

Simulation::QueuedEvent::QueuedEvent(simtime time, size_t id, EventPtr event)
Simulation::QueuedEvent::QueuedEvent(simtime time, size_t id, const EventPtr& event)
: time(time), id(id), event(event) {}

bool Simulation::QueuedEvent::operator<(const QueuedEvent &other) const {
Expand All @@ -112,15 +111,15 @@ bool Simulation::QueuedEvent::operator<(const QueuedEvent &other) const {

/* Event */

Event::Event(SimulationPtr sim) : sim(sim) {}
Event::Event(const SimulationPtr& sim) : sim(sim) {}

bool Event::add_handler(ProcessPtr process) {
bool Event::add_handler(const ProcessPtr& process) {
// Handler takes an additional EventPtr arg, but this is ignored by the
// bound function.
return add_handler(std::bind(&Process::resume, process));
}

bool Event::add_handler(Handler handler) {
bool Event::add_handler(const Handler& handler) {
if (is_triggered()) {
return false;
}
Expand Down Expand Up @@ -167,30 +166,30 @@ void Event::process() {

state = State::Processed;

for (auto &handler : handlers) {
for (const auto& handler : handlers) {
handler(shared_from_this());
}

handlers.clear();
}

bool Event::is_pending() { return state == State::Pending; }
bool Event::is_pending() const { return state == State::Pending; }

bool Event::is_triggered() {
bool Event::is_triggered() const {
return state == State::Triggered || state == State::Processed;
}

bool Event::is_processed() { return state == State::Processed; }
bool Event::is_processed() const { return state == State::Processed; }

bool Event::is_aborted() { return state == State::Aborted; }
bool Event::is_aborted() const { return state == State::Aborted; }

Event::State Event::get_state() { return state; }
Event::State Event::get_state() const { return state; }

void Event::Aborted() {}

/* Process */

Process::Process(SimulationPtr sim) : Event(sim), Protothread() {}
Process::Process(const SimulationPtr& sim) : Event(sim), Protothread() {}

void Process::resume() {
// Is the process already finished?
Expand All @@ -213,7 +212,7 @@ ProcessPtr Process::shared_from_this() {

/* Condition */

Condition::Condition(SimulationPtr sim, int n) : Process(sim), n(n) {}
Condition::Condition(const SimulationPtr& sim, int n) : Process(sim), n(n) {}

bool Condition::Run() {
PT_BEGIN();
Expand Down
34 changes: 17 additions & 17 deletions simcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Simulation : public std::enable_shared_from_this<Simulation> {
* @param process Process to be run.
* @param delay Delay after which to run the process.
*/
void run_process(ProcessPtr process, simtime delay = 0.0);
void run_process(const ProcessPtr& process, simtime delay = 0.0);

/**
* Construct an event.
Expand Down Expand Up @@ -142,7 +142,7 @@ class Simulation : public std::enable_shared_from_this<Simulation> {
* @param event Event instance.
* @param delay Delay after which the event is processed.
*/
void schedule(EventPtr event, simtime delay = 0.0);
void schedule(const EventPtr& event, simtime delay = 0.0);

/**
* Process the next scheduled event.
Expand All @@ -165,19 +165,19 @@ class Simulation : public std::enable_shared_from_this<Simulation> {
* @return Whether the event was triggered. If no scheduled events are left or
* the event is aborted, this is not the case.
*/
bool advance_to(EventPtr event);
bool advance_to(const EventPtr& event);

/// Run the simulation until no scheduled events are left.
void run();

/// @return Current simulation time.
simtime get_now();
simtime get_now() const;

/// @return Whether a scheduled event is left.
bool has_next();
bool has_next() const;

/// @return Time at which the next event is scheduled.
simtime peek_next_time();
simtime peek_next_time() const;

private:
class QueuedEvent {
Expand All @@ -186,7 +186,7 @@ class Simulation : public std::enable_shared_from_this<Simulation> {
size_t id;
EventPtr event;

QueuedEvent(simtime time, size_t id, EventPtr event);
QueuedEvent(simtime time, size_t id, const EventPtr& event);

bool operator<(const QueuedEvent &other) const;
};
Expand Down Expand Up @@ -221,7 +221,7 @@ class Event : public std::enable_shared_from_this<Event> {
*
* @param sim Simulation instance.
*/
explicit Event(SimulationPtr sim);
explicit Event(const SimulationPtr& sim);

/**
* Add the resume method of a process as an handler of the event.
Expand All @@ -233,7 +233,7 @@ class Event : public std::enable_shared_from_this<Event> {
* @param process The process to resume when the event is processed.
* @return Whether the event was not already triggered.
*/
bool add_handler(ProcessPtr process);
bool add_handler(const ProcessPtr& process);

/**
* Add the callback as an handler of the event.
Expand All @@ -244,7 +244,7 @@ class Event : public std::enable_shared_from_this<Event> {
* receives the event instance as an argument.
* @return Whether the event was not already triggered.
*/
bool add_handler(Handler handler);
bool add_handler(const Handler& handler);

/**
* Trigger the event with a delay.
Expand Down Expand Up @@ -274,22 +274,22 @@ class Event : public std::enable_shared_from_this<Event> {
void process();

/// @return Whether the event is pending.
bool is_pending();
bool is_pending() const;

/**
* @return Whether the event is triggered. Also true if the event is
* processed.
*/
bool is_triggered();
bool is_triggered() const;

/// @return Whether the event is aborted.
bool is_aborted();
bool is_aborted() const;

/// @return Whether the event is processed.
bool is_processed();
bool is_processed() const;

/// @return Whether the event is pending.
State get_state();
State get_state() const;

/// Called when the event is aborted.
virtual void Aborted();
Expand All @@ -316,7 +316,7 @@ class Process : public Event, public Protothread {
*
* @param sim Simulation instance.
*/
explicit Process(SimulationPtr sim);
explicit Process(const SimulationPtr& sim);

/**
* Resumes the process.
Expand All @@ -339,7 +339,7 @@ class Condition : public Process {
* @param sim Simulation instance.
* @param n Number of times the process must be resumed before it finishes.
*/
Condition(SimulationPtr sim, int n);
Condition(const SimulationPtr& sim, int n);

/**
* Wait for the given number of resumes and then finish.
Expand Down
22 changes: 17 additions & 5 deletions simobj.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
#ifndef SIMOBJ_H_
#define SIMOBJ_H_

#include "simcpp.h"

#define OBSERVABLE_PROPERTY(TYP, NAM, VAL) \
private: \
TYP NAM = VAL; \
\
public: \
shared_ptr<Event> NAM##_event = std::make_shared<Event>(env); \
TYP get_##NAM() { return this->NAM; } \
simcpp::EventPtr NAM##_event{std::make_shared<simcpp::Event>(env)}; \
TYP get_##NAM() const { return this->NAM; } \
void set_##NAM(TYP v) { \
this->NAM = v; \
this->env->schedule(this->NAM##_event); \
this->NAM##_event = std::make_shared<Event>(env); \
this->NAM##_event = std::make_shared<simcpp::Event>(env); \
}


namespace simcpp {

class EnvObj {
protected:
shared_ptr<Simulation> env;
SimulationPtr env;

public:
EnvObj(shared_ptr<Simulation> s) : env(s) {}
explicit EnvObj(const SimulationPtr& s) : env(s) {}
virtual ~EnvObj() = default;
};

}


#endif