Salome HOME
Copyright update: 2016
[modules/kernel.git] / src / DSC / DSC_User / Datastream / Calcium / CalciumGenericProvidesPort.hxx
1 // Copyright (C) 2007-2016  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, 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       throw (Ports::NotDefined, Ports::BadType, Ports::BadValue);       \
144                                                                         \
145     virtual CORBA::Any* get_property(const char* name)                  \
146       throw (Ports::NotDefined);                                        \
147                                                                         \
148     virtual void provides_port_changed(int connection_nbr,              \
149                                        const Engines::DSC::Message message) { \
150       if ( message == Engines::DSC::AddingConnection)                   \
151         {                                                               \
152           _disconnect_mutex.lock();                                     \
153           _mustnotdisconnect++;                                         \
154           _disconnect_mutex.unlock();                                   \
155         }                                                               \
156       else if ( message == Engines::DSC::RemovingConnection )           \
157         {                                                               \
158           disconnect(false);                                            \
159         }                                                               \
160     }                                                                   \
161                                                                         \
162     inline void calcium_erase(float t,long i, bool before)              \
163     {                                                                   \
164       erase(t,i,before);                                                \
165     }                                                                   \
166   };                                                                    \
167
168
169
170 #define CALCIUM_GENERIC_PROVIDES_PORT_CXX(specificPortName)             \
171                                                                         \
172   specificPortName::~specificPortName(void) {};                         \
173                                                                         \
174   void specificPortName::set_property(const char * name,  const CORBA::Any& value) \
175     throw (Ports::NotDefined, Ports::BadType, Ports::BadValue) {        \
176                                                                         \
177     const std::string key(name);                                        \
178     CORBA::Long sl;                                                     \
179     CORBA::Double alpha,delta;                                          \
180     Ports::Calcium_Ports::DependencyType dt;                            \
181     Ports::Calcium_Ports::DateCalSchem  dcs;                            \
182     Ports::Calcium_Ports::InterpolationSchem is;                        \
183     Ports::Calcium_Ports::ExtrapolationSchem es;                        \
184     bool ok=false;                                                      \
185                                                                         \
186     try {                                                               \
187                                                                         \
188       if (key == "StorageLevel" )                                       \
189         {if ( ( ok=(value >>= sl) )   ) Port::setStorageLevel(sl);}     \
190       else if (key == "Alpha"      )                                    \
191         {if ( ( ok=(value >>= alpha) ) ) Port::setAlpha(alpha);}                \
192       else if (key == "DeltaT"     )                                    \
193         {if ( ( ok=(value >>= delta) ) ) Port::setDeltaT(delta);}               \
194       else if (key == "DependencyType" )                                \
195         {if ( ( ok=( value >>= dt)  )  ) Port::setDependencyType(dependencyType[dt]);} \
196       else if (key == "DateCalSchem" )                                  \
197         {if ( ( ok=(value >>= dcs) ) )                                  \
198             Port::setDateCalSchem(dateCalSchem[dcs]);}                  \
199       else if (key == "InterpolationSchem")                             \
200         {if ( ( ok=(value >>= is) ) )                                   \
201             Port::setInterpolationSchem(interpolationSchem[is]);}       \
202       else if (key == "ExtrapolationSchem")                             \
203         {if ( ( ok=(value >>= es) ) )                                   \
204             Port::setExtrapolationSchem(extrapolationSchem[es]);}       \
205       else                                                              \
206         throw Ports::NotDefined();                                      \
207       if (!ok) throw Ports::BadType();                                  \
208                                                                         \
209     } catch ( const DSC_Exception & ex ) {                              \
210                                                                         \
211       std::cerr << ex.what() << std::endl;                              \
212       throw Ports::BadValue();                                          \
213     }                                                                   \
214   }                                                                     \
215                                                                         \
216                                                                         \
217   CORBA::Any* specificPortName::get_property(const char* name)          \
218     throw (Ports::NotDefined) {                                         \
219     const std::string key(name);                                        \
220     CORBA::Any* value=new CORBA::Any;                                   \
221     if (key == "StorageLevel"    )                                      \
222       { (*value) <<= static_cast<CORBA::Long>(Port::getStorageLevel()); } \
223     else if (key == "Alpha"      )                                      \
224       { *value <<= (CORBA::Double) Port::getAlpha();}                   \
225     else if (key == "DeltaT"     )                                      \
226       { *value <<= (CORBA::Double) Port::getDeltaT();}                  \
227     else if (key == "DependencyType" )                                  \
228       {  *value <<= corbaDependencyType[Port::getDependencyType()];}    \
229     else if (key == "DateCalSchem" )                                    \
230       { *value <<= corbaDateCalSchem[Port::getDateCalSchem()];}         \
231     else if (key == "InterpolationSchem")                               \
232       { *value <<= corbaInterpolationSchem[Port::getInterpolationSchem()];} \
233     else if (key == "ExtrapolationSchem")                               \
234       { *value <<= corbaExtrapolationSchem[Port::getExtrapolationSchem()];} \
235     else {                                                              \
236       delete value;                                                     \
237       throw Ports::NotDefined();                                        \
238     }                                                                   \
239     return value;                                                       \
240   };                                            
241                                                                         
242
243 #endif