Created
January 15, 2018 02:25
-
-
Save maritaria/3b2bfa9cbc7972538bee8cc4919f88a1 to your computer and use it in GitHub Desktop.
AspectC++ Mixing functional and interaction code
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
#ifndef __CONSUMER_H__ | |
#define __CONSUMER_H__ | |
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
typedef int TimeSlot; | |
class Consumer { | |
public: | |
std::vector<TimeSlot> mSlots; | |
TimeSlot reserveSlot() { | |
for(TimeSlot slot = 0; slot<1000;slot++) { | |
if (isTimeSlotAvailable(slot)) { | |
mSlots.push_back(slot); | |
return slot; | |
} | |
} | |
return 0; | |
} | |
bool isTimeSlotAvailable(TimeSlot slot) { | |
for(std::vector<TimeSlot>::size_type i = 0; i != mSlots.size(); i++) { | |
TimeSlot personalSlot = mSlots[i]; | |
if (slot == personalSlot) return false; | |
} | |
return true; | |
} | |
}; | |
#endif |
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
#include "consumer.h" | |
using namespace std; | |
int main() | |
{ | |
Consumer c1, c2, c3; | |
cout << "Reserving c1 slot #" << c1.reserveSlot() << endl; | |
cout << "Reserving c1 slot #" << c1.reserveSlot() << endl; | |
cout << "Reserving c2 slot #" << c2.reserveSlot() << endl; | |
cout << "Reserving c2 slot #" << c2.reserveSlot() << endl; | |
cout << "Reserving c3 slot #" << c3.reserveSlot() << endl; | |
cout << "Reserving c3 slot #" << c3.reserveSlot() << endl; | |
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
#ifndef __MEDIATOR_AH__ | |
#define __MEDIATOR_AH__ | |
#include "consumer.h" | |
aspect ConsumerMediator { | |
std::vector<Consumer*> mInstances; | |
advice construction("Consumer") : after() { | |
mInstances.push_back(tjp->that()); | |
} | |
advice destruction("Consumer") : before() { | |
mInstances.erase(std::remove(mInstances.begin(), mInstances.end(), tjp->that()), mInstances.end()); | |
} | |
//Intercept the isTimeSlotAvailable to instead loop over all existing instances | |
advice call("% Consumer::isTimeSlotAvailable(...)") && within("% Consumer::reserveSlot(...)") : around() { | |
TimeSlot* slot = (TimeSlot*)tjp->arg(0); | |
bool result = true; | |
for(std::vector<Consumer*>::size_type i = 0; i != mInstances.size(); i++) { | |
Consumer* c = mInstances[i]; | |
if (!c->isTimeSlotAvailable(*slot)) { | |
result = false; | |
break; | |
} | |
} | |
tjp->result()[0] = result; | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment