Salome HOME
[EDF30382] : Synchro mecanism to ease test of extreme situations
[modules/kernel.git] / src / DSC / DSC_User / Datastream / Calcium / CalciumGenericProvidesPort.hxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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
23 //  File   : CalciumGenericProvidesPort.hxx
24 //  Author : Eric Fayolle (EDF)
25 //  Module : KERNEL
26 // Id          : $Id$
27 //
28 #ifndef _CALCIUM_GENERIC_PROVIDES_PORT_HXX_
29 #define _CALCIUM_GENERIC_PROVIDES_PORT_HXX_
30
31 #include <omnithread.h>
32
33 #include "PortProperties_i.hxx"
34
35 #include "calcium_provides_port.hxx"
36
37 #include "GenericProvidesPort.hxx"
38 #include "CalciumCouplingPolicy.hxx"
39
40 #include "CorbaTypes2CalciumTypes.hxx"
41 #include "CalciumTypes2CorbaTypes.hxx"
42
43 #include "DSC_Exception.hxx"
44 #include <iostream>
45
46 //
47 // Cette macro CPP remplace une déclaration : template <typename CorbaInterface, typename CorbaDataManipulator > 
48 // mais permet surtout de générer différentes classes (dont le nom est spécifié par specificPortName ) 
49 //
50 // Variante: Creer un trait qui à partir de CorbaInterface déduit CorbaDataManipulator
51 // et simplifier la classe
52 //
53 // L'utilisation conjointe du paramètre ... et de __VA_ARGS__ au lieu d'un 'CorbaDataManipulator' permet
54 // d'invoquer la macro avec un troisième token contenant une virgule 
55 // (qui est considéré comme un séparateur d'argument par le PP ) 
56
57 #define CALCIUM_GENERIC_PROVIDES_PORT_HXX(specificPortName,CorbaInterface,...) \
58   class specificPortName :   public virtual CorbaInterface ,            \
59                              public virtual POA_Ports::PortProperties,  \
60                              public GenericProvidesPort< __VA_ARGS__ , CalciumCouplingPolicy, calcium_provides_port > { \
61   private :                                                             \
62     omni_mutex     _disconnect_mutex; \
63     int            _mustnotdisconnect; \
64   public :                                                              \
65     typedef  __VA_ARGS__               DataManipulator;                 \
66     typedef  DataManipulator::Type     CorbaDataType;                   \
67     typedef GenericPort< DataManipulator ,                              \
68       CalciumCouplingPolicy >          Port;                            \
69       specificPortName () : _mustnotdisconnect(0) {}; \
70                                                                         \
71     virtual ~ specificPortName ();                                      \
72                                                                         \
73     inline void disconnect(bool provideLastGivenValue) {                \
74       _disconnect_mutex.lock();                       \
75       if(_mustnotdisconnect > 1)                      \
76       {                                               \
77         _mustnotdisconnect--;                         \
78       }                                               \
79       else if(_mustnotdisconnect == 1)                \
80       {                                               \
81         _mustnotdisconnect--;                         \
82         Port::disconnect(provideLastGivenValue);      \
83       }                                               \
84       _disconnect_mutex.unlock();                     \
85     }                                                 \
86     inline void setDependencyType(CalciumTypes::DependencyType dependencyType) { \
87       Port::setDependencyType(dependencyType);                          \
88     }                                                                   \
89     inline CalciumTypes::DependencyType getDependencyType () const {    \
90       return Port::getDependencyType();                                 \
91     }                                                                   \
92     inline void   setStorageLevel (size_t storageLevel) {               \
93       Port::setStorageLevel(storageLevel);                              \
94     }                                                                   \
95     inline size_t getStorageLevel () const {                            \
96       return Port::getStorageLevel();                                   \
97     }                                                                   \
98     inline void   setDateCalSchem (CalciumTypes::DateCalSchem   dateCalSchem) { \
99       Port::setDateCalSchem (dateCalSchem);                             \
100     }                                                                   \
101     inline CalciumTypes::DateCalSchem getDateCalSchem () const {        \
102       return Port::getDateCalSchem ();                                  \
103     }                                                                   \
104     inline void setAlpha(double alpha) {                                \
105       Port::setAlpha(alpha);                                            \
106     }                                                                   \
107     inline double getAlpha() const  {                                   \
108       return Port::getAlpha();                                          \
109     }                                                                   \
110     inline void   setDeltaT(double deltaT ) {                           \
111       Port::setDeltaT(deltaT);                                          \
112     }                                                                   \
113     inline double getDeltaT() const  {                                  \
114       return Port::getDeltaT();                                         \
115     }                                                                   \
116     inline void setInterpolationSchem (CalciumTypes::InterpolationSchem interpolationSchem) { \
117       Port::setInterpolationSchem(interpolationSchem);                  \
118     }                                                                   \
119     inline void setExtrapolationSchem (CalciumTypes::ExtrapolationSchem extrapolationSchem) { \
120       Port::setExtrapolationSchem(extrapolationSchem);                  \
121     }                                                                   \
122     inline CalciumTypes::InterpolationSchem getInterpolationSchem() const  { \
123       return Port::getInterpolationSchem();                             \
124     }                                                                   \
125     inline CalciumTypes::ExtrapolationSchem getExtrapolationSchem() const  { \
126       return Port::getExtrapolationSchem();                             \
127     }                                                                   \
128                                                                         \
129     inline void put( DataManipulator::CorbaInType data,                 \
130                      CORBA::Double time, CORBA::Long tag) {             \
131       Port::put(data, time, tag);                                       \
132     }                                                                   \
133                                                                         \
134     inline Ports::Port_ptr get_port_ref() {                             \
135       return _this();                                                   \
136     }                                                                   \
137                                                                         \
138     Ports::PortProperties_ptr get_port_properties() {                   \
139       return POA_Ports::PortProperties::_this();                        \
140     }                                                                   \
141                                                                         \
142     virtual void set_property(const char * name, const CORBA::Any& value); \
143                                                                         \
144     virtual CORBA::Any* get_property(const char* name);                 \
145                                                                         \
146     virtual void provides_port_changed(int /*connection_nbr*/,          \
147                                        const Engines::DSC::Message message) { \
148       if ( message == Engines::DSC::AddingConnection)                   \
149         {                                                               \
150           _disconnect_mutex.lock();                                     \
151           _mustnotdisconnect++;                                         \
152           _disconnect_mutex.unlock();                                   \
153         }                                                               \
154       else if ( message == Engines::DSC::RemovingConnection )           \
155         {                                                               \
156           disconnect(false);                                            \
157         }                                                               \
158     }                                                                   \
159                                                                         \
160     inline void calcium_erase(float t,long i, bool before)              \
161     {                                                                   \
162       erase(t,i,before);                                                \
163     }                                                                   \
164   };                                                                    \
165
166
167
168 #define CALCIUM_GENERIC_PROVIDES_PORT_CXX(specificPortName)             \
169                                                                         \
170   specificPortName::~specificPortName(void) {}                         \
171                                                                         \
172   void specificPortName::set_property(const char * name,  const CORBA::Any& value) \
173   {                                                                     \
174                                                                         \
175     const std::string key(name);                                        \
176     CORBA::Long sl;                                                     \
177     CORBA::Double alpha,delta;                                          \
178     Ports::Calcium_Ports::DependencyType dt;                            \
179     Ports::Calcium_Ports::DateCalSchem  dcs;                            \
180     Ports::Calcium_Ports::InterpolationSchem is;                        \
181     Ports::Calcium_Ports::ExtrapolationSchem es;                        \
182     bool ok=false;                                                      \
183                                                                         \
184     try {                                                               \
185                                                                         \
186       if (key == "StorageLevel" )                                       \
187         {if ( ( ok=(value >>= sl) )   ) Port::setStorageLevel(sl);}     \
188       else if (key == "Alpha"      )                                    \
189         {if ( ( ok=(value >>= alpha) ) ) Port::setAlpha(alpha);}                \
190       else if (key == "DeltaT"     )                                    \
191         {if ( ( ok=(value >>= delta) ) ) Port::setDeltaT(delta);}               \
192       else if (key == "DependencyType" )                                \
193         {if ( ( ok=( value >>= dt)  )  ) Port::setDependencyType(dependencyType[dt]);} \
194       else if (key == "DateCalSchem" )                                  \
195         {if ( ( ok=(value >>= dcs) ) )                                  \
196             Port::setDateCalSchem(dateCalSchem[dcs]);}                  \
197       else if (key == "InterpolationSchem")                             \
198         {if ( ( ok=(value >>= is) ) )                                   \
199             Port::setInterpolationSchem(interpolationSchem[is]);}       \
200       else if (key == "ExtrapolationSchem")                             \
201         {if ( ( ok=(value >>= es) ) )                                   \
202             Port::setExtrapolationSchem(extrapolationSchem[es]);}       \
203       else                                                              \
204         throw Ports::NotDefined();                                      \
205       if (!ok) throw Ports::BadType();                                  \
206                                                                         \
207     } catch ( const DSC_Exception & ex ) {                              \
208                                                                         \
209       std::cerr << ex.what() << std::endl;                              \
210       throw Ports::BadValue();                                          \
211     }                                                                   \
212   }                                                                     \
213                                                                         \
214                                                                         \
215   CORBA::Any* specificPortName::get_property(const char* name)          \
216   {                                                                     \
217     const std::string key(name);                                        \
218     CORBA::Any* value=new CORBA::Any;                                   \
219     if (key == "StorageLevel"    )                                      \
220       { (*value) <<= static_cast<CORBA::Long>(Port::getStorageLevel()); } \
221     else if (key == "Alpha"      )                                      \
222       { *value <<= (CORBA::Double) Port::getAlpha();}                   \
223     else if (key == "DeltaT"     )                                      \
224       { *value <<= (CORBA::Double) Port::getDeltaT();}                  \
225     else if (key == "DependencyType" )                                  \
226       {  *value <<= corbaDependencyType[Port::getDependencyType()];}    \
227     else if (key == "DateCalSchem" )                                    \
228       { *value <<= corbaDateCalSchem[Port::getDateCalSchem()];}         \
229     else if (key == "InterpolationSchem")                               \
230       { *value <<= corbaInterpolationSchem[Port::getInterpolationSchem()];} \
231     else if (key == "ExtrapolationSchem")                               \
232       { *value <<= corbaExtrapolationSchem[Port::getExtrapolationSchem()];} \
233     else {                                                              \
234       delete value;                                                     \
235       throw Ports::NotDefined();                                        \
236     }                                                                   \
237     return value;                                                       \
238   }                                            
239                                                                         
240
241 #endif