SingleThreadAnalogMsgDispatcher.cpp

Go to the documentation of this file.
00001 #include "SingleThreadAnalogMsgDispatcher.h"
00002 
00003 #include <iostream>
00004 using std::cerr;
00005 using std::endl;
00006 
00007 using std::make_pair;
00008 
00009 
00010 
00011 SingleThreadAnalogMsgDispatcher::SingleThreadAnalogMsgDispatcher()
00012 {
00013     initialized = false;
00014     port2port_msg_sources_positions = new port_msg_sources_positions_type;
00015     field2port_msg_sources_positions = new field_msg_sources_positions_type;
00016     port2field_msg_sources_positions = new port_msg_sources_positions_type;
00017     field2field_msg_sources_positions = new field_msg_sources_positions_type;
00018 }
00019 
00020 SingleThreadAnalogMsgDispatcher::~SingleThreadAnalogMsgDispatcher()
00021 {
00022     if (!initialized) {
00023         delete port2port_msg_sources_positions;
00024         delete port2field_msg_sources_positions;
00025         delete field2port_msg_sources_positions;
00026         delete field2field_msg_sources_positions;
00027     }
00028 }
00029 
00030 void SingleThreadAnalogMsgDispatcher::dispatchAnalogMsgs()
00031 {
00032     //cerr << "Invoked dispatch analog msgs" << endl;
00033     // Dispatch the port -> port messages
00034     port_msg_sources_type::const_iterator p2p_src_it;
00035     port_msg_destinations_type::const_iterator p2p_dest_it = port2port_msg_destinations.begin();
00036     for (p2p_src_it = port2port_msg_sources.begin(); p2p_src_it != port2port_msg_sources.end();
00037             ++p2p_src_it, ++p2p_dest_it) {
00038         vector< pair< SimObject *,analog_port_id_t> >::const_iterator p2p_dest_it_2;
00039         for (p2p_dest_it_2 = p2p_dest_it->begin(); p2p_dest_it_2 != p2p_dest_it->end(); ++p2p_dest_it_2) {
00040             double v = p2p_src_it->first->getAnalogOutput(p2p_src_it->second);
00041             p2p_dest_it_2->first->setAnalogInput(v, p2p_dest_it_2->second);
00042         }
00043     }
00044 
00045     // Dispatch field -> port messages
00046 
00047     //  cerr << "dispatch field -> port messages" << endl;
00048     field_msg_sources_type::const_iterator f2p_src_it;
00049     port_msg_destinations_type::const_iterator f2p_dest_it = field2port_msg_destinations.begin();
00050     for (f2p_src_it = field2port_msg_sources.begin(); f2p_src_it != field2port_msg_sources.end();
00051             ++f2p_src_it, ++f2p_dest_it) {
00052         vector< pair<SimObject *,analog_port_id_t> >::const_iterator f2p_dest_it_2;
00053         for (f2p_dest_it_2 = f2p_dest_it->begin(); f2p_dest_it_2 != f2p_dest_it->end(); ++f2p_dest_it_2) {              
00054             f2p_dest_it_2->first->setAnalogInput(Field::getValue(f2p_src_it->first, f2p_src_it->second), f2p_dest_it_2->second);
00055         }
00056     }
00057 
00058     //  cerr << "dispatch port->field messages" << endl;
00059     // Dispatch port -> field messages
00060     port_msg_sources_type::const_iterator p2f_src_it;
00061     field_msg_destinations_type::const_iterator p2f_dest_it = port2field_msg_destinations.begin();
00062     for (p2f_src_it = port2field_msg_sources.begin(); p2f_src_it != port2field_msg_sources.end();
00063             ++p2f_src_it, ++p2f_dest_it) {
00064         vector< pair<void *, Field::ValueType > >::const_iterator p2f_dest_it_2;
00065         for (p2f_dest_it_2 = p2f_dest_it->begin(); p2f_dest_it_2 != p2f_dest_it->end(); ++p2f_dest_it_2)
00066             Field::setValue(p2f_dest_it_2->first, p2f_dest_it_2->second, p2f_src_it->first->getAnalogOutput(p2f_src_it->second));
00067     }
00068 
00069     //  cerr << "dispatch field->field messages" << endl;
00070     // Dispatch field -> field messages
00071     field_msg_sources_type::const_iterator f2f_src_it;
00072     field_msg_destinations_type::const_iterator f2f_dest_it = field2field_msg_destinations.begin();
00073     for (f2f_src_it = field2field_msg_sources.begin(); f2f_src_it != field2field_msg_sources.end();
00074             ++f2f_src_it, ++f2f_dest_it) {
00075         vector< pair<void *, Field::ValueType> >::const_iterator f2f_dest_it_2;
00076         for (f2f_dest_it_2 = f2f_dest_it->begin(); f2f_dest_it_2 != f2f_dest_it->end(); ++f2f_dest_it_2)
00077             Field::setValue(f2f_dest_it_2->first, f2f_dest_it_2->second, Field::getValue(f2f_src_it->first, f2f_src_it->second));
00078     }
00079 }
00080 
00081 void SingleThreadAnalogMsgDispatcher::addAnalogMessage(SimObject *src, analog_port_id_t src_port, SimObject *dest, analog_port_id_t dest_port)
00082 {
00083     port_msg_sources_positions_type::const_iterator src_pos_it;
00084     pair< SimObject *, analog_port_id_t> src_pair(src, src_port);
00085     int position;
00086     if ( (src_pos_it = port2port_msg_sources_positions->find(src_pair))
00087             != port2port_msg_sources_positions->end()) {
00088         position = src_pos_it->second;
00089     } else {
00090         position = port2port_msg_sources.size() ;
00091         port2port_msg_destinations.push_back( vector< pair< SimObject *, analog_port_id_t> >());
00092         port2port_msg_sources.push_back(src_pair);
00093         port2port_msg_sources_positions->insert(make_pair(src_pair, position));
00094     }    
00095     port2port_msg_destinations[position].push_back(make_pair(dest,dest_port));
00096 }
00097 
00098 void SingleThreadAnalogMsgDispatcher::addAnalogMessage(SimObject *src, analog_port_id_t src_port, SimObject *dest, string destfield)
00099 {
00100         const Field * dest_field = dest->findField(destfield);
00101     port_msg_sources_positions_type::const_iterator src_pos_it;
00102     pair< SimObject *,analog_port_id_t> src_pair(src, src_port);
00103     int position;
00104     if ( (src_pos_it = port2field_msg_sources_positions->find(src_pair))
00105             != port2field_msg_sources_positions->end()) {
00106         position = src_pos_it->second;
00107     } else {
00108         position = port2field_msg_sources.size();
00109         port2field_msg_destinations.push_back( vector< pair< void *, Field::ValueType > >());        
00110         port2field_msg_sources.push_back(src_pair);
00111         port2field_msg_sources_positions->insert(make_pair(src_pair, position));
00112     }
00113     port2field_msg_destinations[position].push_back( make_pair(dest_field->getPtr(dest), dest_field->getType()));
00114     
00115 }
00116 
00117 void SingleThreadAnalogMsgDispatcher::addAnalogMessage(SimObject *src, string srcfield, SimObject *dest, analog_port_id_t dest_port)
00118 {
00119         const Field * src_field = src->findField(srcfield);
00120     field_msg_sources_positions_type::const_iterator src_pos_it;    
00121     void *src_ptr = src_field->getPtr(src);
00122     int position;
00123     if ( (src_pos_it = field2port_msg_sources_positions->find(src_ptr))
00124             != field2port_msg_sources_positions->end()) {
00125         position = src_pos_it->second;
00126     } else {
00127         position = field2port_msg_sources.size();
00128         field2port_msg_sources.push_back(make_pair(src_ptr, src_field->getType()));        
00129         field2port_msg_destinations.push_back( vector< pair<SimObject *,analog_port_id_t> >());
00130         field2port_msg_sources_positions->insert(make_pair(src_ptr,position));
00131     }
00132     field2port_msg_destinations[position].push_back(make_pair(dest,dest_port));
00133 }
00134 
00135 void SingleThreadAnalogMsgDispatcher::addAnalogMessage(SimObject *src, string srcfield, SimObject *dest, string destfield)
00136 {
00137         const Field * src_field = src->findField(srcfield);
00138         const Field * dest_field = dest->findField(destfield);
00139     field_msg_sources_positions_type::const_iterator src_pos_it;
00140     void *src_ptr =  src_field->getPtr(src);
00141     int position;
00142     if ( (src_pos_it = field2field_msg_sources_positions->find(src_ptr))
00143             != field2field_msg_sources_positions->end()) {
00144         position = src_pos_it->second;
00145     } else {
00146         position = field2field_msg_sources.size();
00147         field2field_msg_sources_positions->insert(make_pair(src_ptr, position));
00148         field2field_msg_sources.push_back(make_pair(src_ptr, src_field->getType()));
00149         field2field_msg_destinations.push_back( vector< pair< void *, Field::ValueType > >());
00150     }
00151     field2field_msg_destinations[position].push_back(make_pair(dest_field->getPtr(dest),dest_field->getType()));
00152 }
00153 
00154 void SingleThreadAnalogMsgDispatcher::initialize()
00155 {
00156     if (!initialized) {
00157         initialized = true;
00158         delete port2port_msg_sources_positions;
00159         delete port2field_msg_sources_positions;
00160         delete field2port_msg_sources_positions;
00161         delete field2field_msg_sources_positions;
00162     }
00163 }

Generated on Wed Jul 9 16:34:38 2008 for PCSIM by  doxygen 1.5.5