FIFO Example C++

#include
#include
#include
#include
using namespace std;

typedef int pType;

// global list of Agents
class Agent;
set allAgents;

class Pipe{
queue p;
public:
pType read() { pType r = p.front(); p.pop(); return r; }
void write( pType r ) { p.push(r); }

int size() { return p.size(); }
bool empty() { return p.empty(); }
};

class Agent{
protected:
Agent() { allAgents.insert(this);}
~Agent() { allAgents.erase(this); }
public:
virtual void tick() = 0;
string name;
};

// ********************************************

class Generator : public Agent {
static int a;
Pipe *out;
public:
Generator( Pipe &p ) { out = &p; }
void tick(){
out->write ( a++ );
}
};

int Generator::a = 0;

// ********************************************

class Consumer : public Agent {
Pipe *in;
public:
Consumer( Pipe &p ) { in = &p; }
void tick(){
if(!in->empty())
cout <read() <empty())
out->write(2*in->read());
}
};

class Split : public Agent{
Pipe *in, *out1, *out2;
public:
Split( Pipe &p1, Pipe &p2, Pipe &p3){ in = &p1; out1 = &p2; out2 = &p3; }
void tick(){
if(!in->empty()){
pType r = in->read();
out1->write(r);
out2->write(r);
}
}
};

// ********************************************

int main() {

Pipe a,b1,b2,c;
Generator A(a); A.name = "A";
Split S(a,b1,c); S.name = "S";
Modifier M(b1,b2); M.name = "M";
Consumer B1(b2); B1.name = "B1";
Consumer B2(c); B2.name = "B2";

M.tick();
//cout << "Start: " << a.size() << endl;

set::iterator i;
int j;
for(j=0; j<10; j++){
//cout << "a: " << a.size() << endl;
//cout << "b: " << b.size() << endl;

for(i=allAgents.begin(); i != allAgents.end(); i++){
//cout <name <tick();
}
}

};

One thought on “FIFO Example C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s