SALOME documentation central

src/DSC/DSC_User/Datastream/GenericUsesPort.hxx

00001 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 //  This library is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU Lesser General Public
00008 //  License as published by the Free Software Foundation; either
00009 //  version 2.1 of the License.
00010 //
00011 //  This library is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 //  Lesser General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU Lesser General Public
00017 //  License along with this library; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : GenericUsesPort.hxx
00023 //  Author : Eric Fayolle (EDF)
00024 //  Module : KERNEL
00025 // Modified by : $LastChangedBy$
00026 // Date        : $LastChangedDate: 2007-02-28 15:26:32 +0100 (mer, 28 fév 2007) $
00027 // Id          : $Id$
00028 //
00029 #ifndef _GENERIC_USES_PORT_HXX_
00030 #define _GENERIC_USES_PORT_HXX_
00031 
00032 #include "CorbaTypeManipulator.hxx"
00033 
00034 #include "uses_port.hxx"
00035 #include "SALOME_Ports.hh"
00036 
00037 #include "DSC_Exception.hxx"
00038 
00039 // #define GENERATE_USES_PORT(dataManip,portType,portName)            \
00040 //   const char * _repository_##portType##_name_ = "IDL:Ports/##portType##:1.0"; \
00041 //   GenericUsesPort< dataManip, portType, _repository_##portType##_name_ > portName;
00042 
00043 //ex : GENERATE_USES_PORT(Ports::Data_Short_Port,data_short_port);
00044 
00045 template <typename DataManipulator, typename CorbaPortType, char * repositoryName, 
00046        typename UsesPort=uses_port > 
00047 class GenericUsesPort : public UsesPort
00048 {
00049 public :
00050   // Type de données manipulés 
00051   typedef typename DataManipulator::Type         DataType;
00052   typedef typename DataManipulator::CorbaInType  CorbaInDataType;
00053 
00054   GenericUsesPort();
00055   virtual ~GenericUsesPort();
00056 
00057   virtual const char * get_repository_id();
00058   template <typename TimeType,typename TagType>
00059   void  put(CorbaInDataType data,  TimeType time, TagType tag); 
00060 
00061   virtual void uses_port_changed(Engines::DSC::uses_port * new_uses_port,
00062                      const Engines::DSC::Message message);
00063 
00064 protected :
00065   Engines::DSC::uses_port * _my_ports;
00066 };
00067 
00068 
00069 template <typename DataManipulator,typename CorbaPortType, char * repositoryName, typename UsesPort > 
00070 GenericUsesPort< DataManipulator,CorbaPortType, repositoryName, UsesPort  >::GenericUsesPort() {
00071   _my_ports = NULL;
00072 }
00073 
00074 template <typename DataManipulator,typename CorbaPortType, char * repositoryName, typename UsesPort > 
00075 GenericUsesPort< DataManipulator,CorbaPortType, repositoryName, UsesPort  >::~GenericUsesPort() 
00076 {
00077   delete _my_ports;
00078 }
00079 
00080 template <typename DataManipulator,typename CorbaPortType, char * repositoryName, typename UsesPort > 
00081 const char *
00082 GenericUsesPort< DataManipulator,CorbaPortType, repositoryName, UsesPort  >::get_repository_id() {
00083   return repositoryName;
00084 }
00085 
00086 
00087 template <typename DataManipulator,typename CorbaPortType, char * repositoryName, typename UsesPort > 
00088 template <typename TimeType,typename TagType>
00089 void
00090 GenericUsesPort< DataManipulator,CorbaPortType, repositoryName, UsesPort  >::put( CorbaInDataType data, 
00091                                                     TimeType time, 
00092                                                     TagType tag) {
00093   typedef typename CorbaPortType::_var_type CorbaPortTypeVar;
00094   if (!_my_ports)
00095     throw DSC_Exception(LOC("There is no connected provides port to communicate with."));
00096 
00097   // OLD : PB1 : Cf remarque dans CalciumInterface, si on n'effectue pas de copie
00098   // OLD :       du buffer ! 
00099   // OLD : PB2 : Si les ports provides auquels on envoie data sont collocalisés
00100   // OLD : ils vont partagés le même buffer (à cause de notre optim ds get_data)
00101   // OLD : il faut alors effectuer une copie ici.
00102   // OLD : Pour l'instant on résoud PB2 en créant une copie de la donnée en cas
00103   // OLD : de connexions multiples. Il faudra tester la collocalisation.
00104   // OLD :  DataType copyOfData; // = data; PB1
00105   for(int i = 0; i < _my_ports->length(); i++) {
00106 
00107     CorbaPortTypeVar port = CorbaPortType::_narrow((*_my_ports)[i]);
00108     //if (i) { PB1
00109     //OLD :   copyOfData = DataManipulator::clone(data);
00110 #ifdef MYDEBUG
00111     std::cerr << "-------- GenericUsesPort::put -------- " << std::endl;
00112 #endif
00113     //} PB1
00114     try {
00115       port->put(data,time,tag);
00116       // OLD : port->put(*copyOfData,time,tag);
00117     } catch(const CORBA::SystemException& ex) {
00118       //OLD : DataManipulator::delete_data(copyOfData);
00119       throw DSC_Exception(LOC(OSS() << "Can't invoke put method on port number "
00120                      << i << "( i>=  0)"));
00121 
00122     }
00123     //if (i) PB1 
00124     // La séquence est détruite avec le buffer si on n'est pas collocalisé
00125     // La séquence est détruite sans son buffer sinon (cf comportement de get_data
00126     // appelée dans put (port provides)
00127     //OLD : DataManipulator::delete_data(copyOfData);
00128   }
00129 }
00130 
00131 
00132 template <typename DataManipulator, typename CorbaPortType, char * repositoryName, typename UsesPort>
00133 void 
00134 GenericUsesPort< DataManipulator, CorbaPortType, repositoryName, UsesPort  
00135            >::uses_port_changed(Engines::DSC::uses_port * new_uses_port,
00136                           const Engines::DSC::Message message)
00137 {
00138   if (_my_ports) delete _my_ports;
00139 
00140 #ifdef MYDEBUG
00141   std::cerr << "GenericUsesPort::uses_port_changed" << std::endl;
00142 #endif
00143   _my_ports = new_uses_port;
00144 }
00145 
00146 #endif