-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from aMarcireau/event
Event
- Loading branch information
Showing
11 changed files
with
228 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "actor.h" | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
Actor::Actor(): | ||
events(vector<shared_ptr<Event> >()) | ||
{ | ||
} | ||
|
||
/** | ||
* Attach an event | ||
*/ | ||
void Actor::attach(shared_ptr<Event> event) | ||
{ | ||
events.push_back(event); | ||
} | ||
|
||
/** | ||
* Detach an event | ||
*/ | ||
void Actor::detach(Event & event) | ||
{ | ||
for ( | ||
vector<shared_ptr<Event> >::iterator eventIterator = events.begin(); | ||
eventIterator != events.end(); | ||
) { | ||
if ((*eventIterator).get() == &event) { | ||
events.erase(eventIterator); | ||
} else { | ||
++eventIterator; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Notification called by a Stepper object | ||
*/ | ||
void Actor::notify(unsigned int step) | ||
{ | ||
for ( | ||
vector<shared_ptr<Event> >::iterator eventIterator = events.begin(); | ||
eventIterator != events.end(); | ||
) { | ||
(*eventIterator)->action(*this, step); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef __ALOBE__ACTOR__ | ||
#define __ALOBE__ACTOR__ | ||
|
||
#include <vector> | ||
|
||
#include "event.h" | ||
|
||
using namespace std; | ||
|
||
/** | ||
* <<Abstract>> Actor | ||
* | ||
* Actor in the simulation | ||
*/ | ||
class Actor | ||
{ | ||
public: | ||
Actor(); | ||
void attach(shared_ptr<Event> event); // Attach an event | ||
void detach(Event & event); // Detach an event | ||
void notify(unsigned int step); // Notification called by a Stepper object | ||
|
||
private: | ||
vector<shared_ptr<Event> > events; | ||
}; | ||
|
||
#endif // __ALOBE__ACTOR__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
#ifndef __alobe__event__ | ||
#define __alobe__event__ | ||
#ifndef __ALOBE__EVENT__ | ||
#define __ALOBE__EVENT__ | ||
|
||
class Actor; | ||
|
||
//using namespace std; | ||
|
||
/** | ||
* <<Abstract>> Event | ||
* | ||
* An event is trigger by the Stepper it is attached to, through its notify method. | ||
* The action() method will be called if the passed step value is equal to triggerStep. | ||
* An event is trigger by an actor, which passes itself and the current step to the event | ||
*/ | ||
class Event | ||
{ | ||
public: | ||
Event(unsigned int step = 0); // Constructor | ||
void notify(unsigned int step); // Notification called by the Stepper | ||
unsigned int getTriggerStep(); // Getter for triggerStep | ||
Event(unsigned int step = 0); | ||
void action(Actor & actor, unsigned int step); // Trigger event action on an actor with a given step | ||
|
||
protected: | ||
virtual void action() = 0; // Action called by notify() | ||
|
||
private: | ||
unsigned int triggerStep; // Store the trigger step value | ||
virtual void filteredAction(Actor & actor) = 0; // Filtered action is called only if step == triggerStep | ||
// This method is protected so that it can be called | ||
// from abstract inherited classes | ||
protected: | ||
unsigned int triggerStep; // This property is protected so that it can be called | ||
// from abstract inherited classes | ||
}; | ||
|
||
#endif /* defined(__alobe__event__) */ | ||
#endif // __ALOBE__EVENT__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
#include "simulation.h" | ||
#include "stepper.h" | ||
#include "actor.h" | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
unique_ptr<Simulation> simulation = make_unique<Simulation>(make_unique<Stepper>()); | ||
|
||
simulation->toStep(10); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
#ifndef __alobe__periodicEvent__ | ||
#define __alobe__periodicEvent__ | ||
#ifndef __ALOBE__PERIODIC_EVENT__ | ||
#define __ALOBE__PERIODIC_EVENT__ | ||
|
||
#include "event.h" | ||
|
||
//using namespace std; | ||
|
||
/** | ||
* <<Abstract>> PeriodicEvent | ||
* Inherits Event | ||
* | ||
* Starting at triggerStep, PeriodicEvent objects call their action method every triggerPeriod | ||
* Starting at triggerStep, PeriodicEvent objects call their filteredAction method every triggerPeriod | ||
*/ | ||
class PeriodicEvent: public Event | ||
{ | ||
public: | ||
PeriodicEvent(unsigned int step = 0, unsigned int period = 1); // Constructor | ||
void notify(unsigned int step); // Notification called by the Stepper | ||
unsigned int getTriggerPeriod(); // Getter for triggerPeriod | ||
PeriodicEvent(unsigned int step = 0, unsigned int period = 1); | ||
void action(Actor & actor, unsigned int step); // Change event filter rules (add periodicity) | ||
|
||
private: | ||
unsigned int triggerPeriod; // Store the trigger step value | ||
unsigned int triggerPeriod; | ||
}; | ||
|
||
#endif /* defined(__alobe__periodicEvent__) */ | ||
#endif // __ALOBE__PERIODIC_EVENT__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "simulation.h" | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
Simulation::Simulation(unique_ptr<Stepper> stepper): | ||
_stepper(move(stepper)), | ||
_actors(vector<shared_ptr<Actor> >()) | ||
{ | ||
} | ||
|
||
/** | ||
* Add an actor | ||
*/ | ||
void Simulation::add(shared_ptr<Actor> actor) | ||
{ | ||
_actors.push_back(actor); | ||
} | ||
|
||
/** | ||
* Calculate simulation steps until reaching 'step' | ||
*/ | ||
void Simulation::toStep(unsigned int step) | ||
{ | ||
while (_stepper->getStep() < step) { | ||
nextStep(); | ||
} | ||
} | ||
|
||
/** | ||
* Calculate the simulation next step | ||
*/ | ||
void Simulation::nextStep() | ||
{ | ||
_stepper->increment(); | ||
nextStepCallback(); | ||
} | ||
|
||
/** | ||
* Called at the end of the nextStep method | ||
*/ | ||
void Simulation::nextStepCallback() | ||
{ | ||
std::cout << "Step: "<< _stepper->getStep() << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef __ALOBE__SIMULATION__ | ||
#define __ALOBE__SIMULATION__ | ||
|
||
#include <iostream> | ||
|
||
#include "stepper.h" | ||
#include "actor.h" | ||
|
||
using namespace std; | ||
|
||
/** | ||
* Simulation | ||
* | ||
* Controller for the simulation | ||
*/ | ||
class Simulation | ||
{ | ||
public: | ||
Simulation(unique_ptr<Stepper> stepper); | ||
void add(shared_ptr<Actor> actor); // Add an actor | ||
void toStep(unsigned int step); // Calculate simulation steps until reaching 'step' | ||
void nextStep(); // Calculate the simulation next step | ||
|
||
private: | ||
void nextStepCallback(); // Called at the end of the nextStep method | ||
|
||
private: | ||
unique_ptr<Stepper> _stepper; | ||
vector<shared_ptr<Actor> > _actors; | ||
}; | ||
|
||
#endif // __ALOBE__SIMULATION__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.