From 636ae51408b6eaafa0f719c3b26192a69477b256 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Thu, 6 Nov 2003 19:59:54 +0000 Subject: [PATCH] Agrego el tanque. Tira unos warnings muy locos al compilar, tengo que esperar a llegar a casa para revisar el libro, ya que la herencia multiple puede causar algun problema. Vere luego si anda como esta. --- Model/include/tank.h | 35 +++++++++++++++++++++++ Model/src/tank.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 Model/include/tank.h create mode 100644 Model/src/tank.cpp diff --git a/Model/include/tank.h b/Model/include/tank.h new file mode 100644 index 0000000..d4173af --- /dev/null +++ b/Model/include/tank.h @@ -0,0 +1,35 @@ + + +#ifndef _TANK_H_ +#define _TANK_H_ + +#include "source.h" +#include "drain.h" + +namespace PlaQui { + +namespace Model { + +class Tank:public Source, public Drain { +public: + Tank(const std::string &_name); + virtual ~Tank(); + + virtual bool get_output(); + + virtual void recieve_msg(int msg, IConector *who, void *data); + virtual void update(); + virtual void simulate(); +protected: + float litros; + float actual_in_flow; + float actual_out_flow; +private: + Tank():Drain("null"),Source("null"),Control("null") {} + Tank &operator = (const Tank &) { return *this; } +}; + +} +} + +#endif // _TANK_H_ diff --git a/Model/src/tank.cpp b/Model/src/tank.cpp new file mode 100644 index 0000000..88cbbe6 --- /dev/null +++ b/Model/src/tank.cpp @@ -0,0 +1,68 @@ + +#include "tank.h" +#include + +using namespace PlaQui::Model; + +Tank::Tank(const std::string &_name):Source(_name),Drain(_name),Control(_name) +{ + litros = 0.0f; +} + +Tank::~Tank() +{ +} + +bool Tank::get_output() +{ + return litros > 0; +} + +void Tank::update() +{ + updated = true; +} + +void Tank::simulate() +{ + if (!updated) return; + + // le resto lo que entrego + litros -= actual_out_flow; + // le sumo lo que recibo + litros += actual_in_flow; + + std::cout << name << "Capacidad: " << capacity; + std::cout << " Litros : " << litros << std::endl; + updated = false; +} + +void Tank::recieve_msg(int msg, IConector *who, void *data) +{ + int pos = OUT; + + // Verifico si esta conectado a mi entrada + std::list::iterator i; + for(i=in_list.begin(); i!=in_list.end(); i++) { + if ((*i) == who) pos = IN; + } + + switch (msg) { + case MSG_QUERY_MAX_FLOW_OUT: + actual_in_flow = capacity - litros; + if (*((float *)data) < actual_in_flow) + actual_in_flow = *((float *)data); + actual_out_flow = litros; + send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &actual_out_flow); + + who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &actual_in_flow); + updated = true; + break; + case MSG_RESPONSE_MAX_FLOW: + if (pos == OUT) { + if (*((float *)data) < actual_out_flow) + actual_out_flow = *((float *)data); + } + } +} + -- 2.43.0