Salome HOME
CCAR: add trace file for DSC/CALCIUM calls
[modules/kernel.git] / src / DSC / DSC_User / Datastream / GenericUsesPort.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : GenericUsesPort.hxx
23 //  Author : Eric Fayolle (EDF)
24 //  Module : KERNEL
25 // Modified by : $LastChangedBy$
26 // Date        : $LastChangedDate: 2007-02-28 15:26:32 +0100 (mer, 28 fév 2007) $
27 // Id          : $Id$
28 //
29 #ifndef _GENERIC_USES_PORT_HXX_
30 #define _GENERIC_USES_PORT_HXX_
31
32 #include "CorbaTypeManipulator.hxx"
33
34 #include "uses_port.hxx"
35 #include "SALOME_Ports.hh"
36
37 #include "DSC_Exception.hxx"
38
39 // #define GENERATE_USES_PORT(dataManip,portType,portName)                      \
40 //   const char * _repository_##portType##_name_ = "IDL:Ports/##portType##:1.0"; \
41 //   GenericUsesPort< dataManip, portType, _repository_##portType##_name_ > portName;
42
43 //ex : GENERATE_USES_PORT(Ports::Data_Short_Port,data_short_port);
44
45 template <typename DataManipulator, typename CorbaPortType, char * repositoryName, 
46           typename UsesPort=uses_port > 
47 class GenericUsesPort : public UsesPort
48 {
49 public :
50   // Type de données manipulés 
51   typedef typename DataManipulator::Type         DataType;
52   typedef typename DataManipulator::CorbaInType  CorbaInDataType;
53
54   GenericUsesPort();
55   virtual ~GenericUsesPort();
56
57   virtual const char * get_repository_id();
58   template <typename TimeType,typename TagType>
59   void  put(CorbaInDataType data,  TimeType time, TagType tag); 
60
61   virtual void uses_port_changed(Engines::DSC::uses_port * new_uses_port,
62                                  const Engines::DSC::Message message);
63
64 protected :
65   Engines::DSC::uses_port * _my_ports;
66 };
67
68
69 template <typename DataManipulator,typename CorbaPortType, char * repositoryName, typename UsesPort > 
70 GenericUsesPort< DataManipulator,CorbaPortType, repositoryName, UsesPort  >::GenericUsesPort() {
71   _my_ports = NULL;
72 }
73
74 template <typename DataManipulator,typename CorbaPortType, char * repositoryName, typename UsesPort > 
75 GenericUsesPort< DataManipulator,CorbaPortType, repositoryName, UsesPort  >::~GenericUsesPort() 
76 {
77   delete _my_ports;
78 }
79
80 template <typename DataManipulator,typename CorbaPortType, char * repositoryName, typename UsesPort > 
81 const char *
82 GenericUsesPort< DataManipulator,CorbaPortType, repositoryName, UsesPort  >::get_repository_id() {
83   return repositoryName;
84 }
85
86
87 template <typename DataManipulator,typename CorbaPortType, char * repositoryName, typename UsesPort > 
88 template <typename TimeType,typename TagType>
89 void
90 GenericUsesPort< DataManipulator,CorbaPortType, repositoryName, UsesPort  >::put( CorbaInDataType data, 
91                                                                                   TimeType time, 
92                                                                                   TagType tag) {
93   typedef typename CorbaPortType::_var_type CorbaPortTypeVar;
94   if (!_my_ports)
95     throw DSC_Exception(LOC("There is no connected provides port to communicate with."));
96
97   // OLD : PB1 : Cf remarque dans CalciumInterface, si on n'effectue pas de copie
98   // OLD :       du buffer ! 
99   // OLD : PB2 : Si les ports provides auquels on envoie data sont collocalisés
100   // OLD : ils vont partagés le même buffer (à cause de notre optim ds get_data)
101   // OLD : il faut alors effectuer une copie ici.
102   // OLD : Pour l'instant on résoud PB2 en créant une copie de la donnée en cas
103   // OLD : de connexions multiples. Il faudra tester la collocalisation.
104   // OLD :  DataType copyOfData; // = data; PB1
105   for(int i = 0; i < _my_ports->length(); i++) {
106
107     CorbaPortTypeVar port = CorbaPortType::_narrow((*_my_ports)[i]);
108     //if (i) { PB1
109     //OLD :   copyOfData = DataManipulator::clone(data);
110 #ifdef MYDEBUG
111     std::cerr << "-------- GenericUsesPort::put -------- " << std::endl;
112 #endif
113     //} PB1
114     try {
115       port->put(data,time,tag);
116       // OLD : port->put(*copyOfData,time,tag);
117     } catch(const CORBA::SystemException& ex) {
118       //OLD : DataManipulator::delete_data(copyOfData);
119       throw DSC_Exception(LOC(OSS() << "Can't invoke put method on port number "
120                               << i << "( i>=  0)"));
121
122     }
123     //if (i) PB1 
124     // La séquence est détruite avec le buffer si on n'est pas collocalisé
125     // La séquence est détruite sans son buffer sinon (cf comportement de get_data
126     // appelée dans put (port provides)
127     //OLD : DataManipulator::delete_data(copyOfData);
128   }
129 }
130
131
132 template <typename DataManipulator, typename CorbaPortType, char * repositoryName, typename UsesPort>
133 void 
134 GenericUsesPort< DataManipulator, CorbaPortType, repositoryName, UsesPort  
135                  >::uses_port_changed(Engines::DSC::uses_port * new_uses_port,
136                                       const Engines::DSC::Message message)
137 {
138   if (_my_ports) delete _my_ports;
139
140 #ifdef MYDEBUG
141   std::cerr << "GenericUsesPort::uses_port_changed" << std::endl;
142 #endif
143   _my_ports = new_uses_port;
144 }
145
146 #endif