Salome HOME
CCAR: some corrections in :
[modules/kernel.git] / src / DSC / DSC_User / Datastream / Calcium / CalciumGenericProvidesPort.hxx
1 //  Copyright (C) 2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //
21 //
22 //  File   : CalciumGenericProvidesPort.hxx
23 //  Author : Eric Fayolle (EDF)
24 //  Module : KERNEL
25 // Id          : $Id$
26
27 #ifndef _CALCIUM_GENERIC_PROVIDES_PORT_HXX_
28 #define _CALCIUM_GENERIC_PROVIDES_PORT_HXX_
29
30 #include <omnithread.h>
31
32 #include "PortProperties_i.hxx"
33
34 #include "calcium_provides_port.hxx"
35
36 #include "GenericProvidesPort.hxx"
37 #include "CalciumCouplingPolicy.hxx"
38
39 #include "CorbaTypes2CalciumTypes.hxx"
40 #include "CalciumTypes2CorbaTypes.hxx"
41
42 #include "DSC_Exception.hxx"
43 #include <iostream>
44
45 //
46 // Cette macro CPP remplace une déclaration : template <typename CorbaInterface, typename CorbaDataManipulator > 
47 // mais permet surtout de générer différentes classes (dont le nom est spécifié par specificPortName ) 
48 //
49 // Variante: Creer un trait qui à partir de CorbaInterface déduit CorbaDataManipulator
50 // et simplifier la classe
51 //
52 // L'utilisation conjointe du paramètre ... et de __VA_ARGS__ au lieu d'un 'CorbaDataManipulator' permet
53 // d'invoquer la macro avec un troisième token contenant une virgule 
54 // (qui est considéré comme un séparateur d'argument par le PP ) 
55
56 #define CALCIUM_GENERIC_PROVIDES_PORT_HXX(specificPortName,CorbaInterface,...) \
57   class specificPortName :   public virtual CorbaInterface ,            \
58                              public virtual POA_Ports::PortProperties,  \
59                              public GenericProvidesPort< __VA_ARGS__ , CalciumCouplingPolicy, calcium_provides_port > { \
60   private :                                                             \
61     omni_mutex     _disconnect_mutex; \
62     int            _mustnotdisconnect; \
63   public :                                                              \
64     typedef  __VA_ARGS__               DataManipulator;                 \
65     typedef  DataManipulator::Type     CorbaDataType;                   \
66     typedef GenericPort< DataManipulator ,                              \
67       CalciumCouplingPolicy >          Port;                            \
68       specificPortName () : _mustnotdisconnect(0) {}; \
69                                                                         \
70     virtual ~ specificPortName ();                                      \
71                                                                         \
72     inline void disconnect(bool provideLastGivenValue) {                \
73       _disconnect_mutex.lock();                       \
74       if(_mustnotdisconnect > 1)                      \
75       {                                               \
76         _mustnotdisconnect--;                         \
77       }                                               \
78       else if(_mustnotdisconnect == 1)                \
79       {                                               \
80         _mustnotdisconnect--;                         \
81         Port::disconnect(provideLastGivenValue);      \
82       }                                               \
83       _disconnect_mutex.unlock();                     \
84     }                                                                                                 \
85     inline void setDependencyType(CalciumTypes::DependencyType dependencyType) { \
86       Port::setDependencyType(dependencyType);                          \
87     }                                                                   \
88     inline CalciumTypes::DependencyType getDependencyType () const {    \
89       return Port::getDependencyType();                                 \
90     }                                                                   \
91     inline void   setStorageLevel (size_t storageLevel) {               \
92       Port::setStorageLevel(storageLevel);                              \
93     }                                                                   \
94     inline size_t getStorageLevel () const {                            \
95       return Port::getStorageLevel();                                   \
96     }                                                                   \
97     inline void   setDateCalSchem (CalciumTypes::DateCalSchem   dateCalSchem) { \
98       Port::setDateCalSchem (dateCalSchem);                             \
99     }                                                                   \
100     inline CalciumTypes::DateCalSchem getDateCalSchem () const {        \
101       return Port::getDateCalSchem ();                                  \
102     }                                                                   \
103     inline void setAlpha(double alpha) {                                \
104       Port::setAlpha(alpha);                                            \
105     }                                                                   \
106     inline double getAlpha() const  {                                   \
107       return Port::getAlpha();                                          \
108     }                                                                   \
109     inline void   setDeltaT(double deltaT ) {                           \
110       Port::setDeltaT(deltaT);                                          \
111     }                                                                   \
112     inline double getDeltaT() const  {                                  \
113       return Port::getDeltaT();                                         \
114     }                                                                   \
115     inline void setInterpolationSchem (CalciumTypes::InterpolationSchem interpolationSchem) { \
116       Port::setInterpolationSchem(interpolationSchem);                  \
117     }                                                                   \
118     inline void setExtrapolationSchem (CalciumTypes::ExtrapolationSchem extrapolationSchem) { \
119       Port::setExtrapolationSchem(extrapolationSchem);                  \
120     }                                                                   \
121     inline CalciumTypes::InterpolationSchem getInterpolationSchem() const  { \
122       return Port::getInterpolationSchem();                             \
123     }                                                                   \
124     inline CalciumTypes::ExtrapolationSchem getExtrapolationSchem() const  { \
125       return Port::getExtrapolationSchem();                             \
126     }                                                                   \
127                                                                         \
128     inline void put( DataManipulator::CorbaInType data,                 \
129                      CORBA::Double time, CORBA::Long tag) {             \
130       Port::put(data, time, tag);                                       \
131     }                                                                   \
132                                                                         \
133     inline Ports::Port_ptr get_port_ref() {                             \
134       return _this();                                                   \
135     }                                                                   \
136                                                                         \
137     Ports::PortProperties_ptr get_port_properties() {                   \
138       return POA_Ports::PortProperties::_this();                        \
139     }                                                                   \
140                                                                         \
141     virtual void set_property(const char * name, const CORBA::Any& value) \
142       throw (Ports::NotDefined, Ports::BadType, Ports::BadValue);       \
143                                                                         \
144     virtual CORBA::Any* get_property(const char* name)                  \
145       throw (Ports::NotDefined);                                        \
146                                                                         \
147     virtual void provides_port_changed(int connection_nbr,              \
148                                        const Engines::DSC::Message message) { \
149       if ( message == Engines::DSC::AddingConnection)                   \
150         {                                                 \
151           _disconnect_mutex.lock();                \
152           _mustnotdisconnect++;                           \
153           _disconnect_mutex.unlock();              \
154         }                                                 \
155       else if ( message == Engines::DSC::RemovingConnection )           \
156         {                                                 \
157           disconnect(false);                              \
158         }                                                 \
159     }                                                                   \
160   };                                                                    \
161
162
163
164 #define CALCIUM_GENERIC_PROVIDES_PORT_CXX(specificPortName)             \
165                                                                         \
166   specificPortName::~specificPortName(void) {};                         \
167                                                                         \
168   void specificPortName::set_property(const char * name,  const CORBA::Any& value) \
169     throw (Ports::NotDefined, Ports::BadType, Ports::BadValue) {        \
170                                                                         \
171     const std::string key(name);                                        \
172     CORBA::Long sl;                                                     \
173     CORBA::Double alpha,delta;                                          \
174     Ports::Calcium_Ports::DependencyType dt;                            \
175     Ports::Calcium_Ports::DateCalSchem  dcs;                            \
176     Ports::Calcium_Ports::InterpolationSchem is;                        \
177     Ports::Calcium_Ports::ExtrapolationSchem es;                        \
178     bool ok=false;                                                      \
179                                                                         \
180     try {                                                               \
181                                                                         \
182       if (key == "StorageLevel" )                                       \
183         {if ( ok=(value >>= sl)    ) Port::setStorageLevel(sl);}        \
184       else if (key == "Alpha"      )                                    \
185         {if ( ok=(value >>= alpha) ) Port::setAlpha(alpha);}            \
186       else if (key == "DeltaT"     )                                    \
187         {if ( ok=(value >>= delta) ) Port::setDeltaT(delta);}           \
188       else if (key == "DependencyType" )                                \
189         {if ( ok=( value >>= dt)   ) Port::setDependencyType(dependencyType[dt]);} \
190       else if (key == "DateCalSchem" )                                  \
191         {if ( ok=(value >>= dcs) )                                      \
192             Port::setDateCalSchem(dateCalSchem[dcs]);}                  \
193       else if (key == "InterpolationSchem")                             \
194         {if ( ok=(value >>= is) )                                       \
195             Port::setInterpolationSchem(interpolationSchem[is]);}       \
196       else if (key == "ExtrapolationSchem")                             \
197         {if ( ok=(value >>= es) )                                       \
198             Port::setExtrapolationSchem(extrapolationSchem[es]);}       \
199       else                                                              \
200         throw Ports::NotDefined();                                      \
201       if (!ok) throw Ports::BadType();                                  \
202                                                                         \
203     } catch ( const DSC_Exception & ex ) {                              \
204                                                                         \
205       std::cerr << ex.what() << std::endl;                              \
206       throw Ports::BadValue();                                          \
207     }                                                                   \
208   }                                                                     \
209                                                                         \
210                                                                         \
211   CORBA::Any* specificPortName::get_property(const char* name)          \
212     throw (Ports::NotDefined) {                                         \
213     const std::string key(name);                                        \
214     CORBA::Any* value=new CORBA::Any;                                   \
215     if (key == "StorageLevel"    )                                      \
216       { (*value) <<= static_cast<CORBA::Long>(Port::getStorageLevel()); } \
217     else if (key == "Alpha"      )                                      \
218       { *value <<= (CORBA::Double) Port::getAlpha();}                   \
219     else if (key == "DeltaT"     )                                      \
220       { *value <<= (CORBA::Double) Port::getDeltaT();}                  \
221     else if (key == "DependencyType" )                                  \
222       {  *value <<= corbaDependencyType[Port::getDependencyType()];}    \
223     else if (key == "DateCalSchem" )                                    \
224       { *value <<= corbaDateCalSchem[Port::getDateCalSchem()];}         \
225     else if (key == "InterpolationSchem")                               \
226       { *value <<= corbaInterpolationSchem[Port::getInterpolationSchem()];} \
227     else if (key == "ExtrapolationSchem")                               \
228       { *value <<= corbaExtrapolationSchem[Port::getExtrapolationSchem()];} \
229     else {                                                              \
230       delete value;                                                     \
231       throw Ports::NotDefined();                                        \
232     }                                                                   \
233     return value;                                                       \
234   };                                            
235                                                                         
236
237 #endif