Salome HOME
*** empty log message ***
[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_semaphore _mustnotdisconnectyet;                               \
62   public :                                                              \
63     typedef  __VA_ARGS__               DataManipulator;                 \
64     typedef  DataManipulator::Type     CorbaDataType;                   \
65     typedef GenericPort< DataManipulator ,                              \
66       CalciumCouplingPolicy >          Port;                            \
67                                                                         \
68     specificPortName () :_mustnotdisconnectyet(0) {};                   \
69                                                                         \
70     virtual ~ specificPortName ();                                      \
71                                                                         \
72     inline void disconnect(bool provideLastGivenValue) {                \
73       if (! _mustnotdisconnectyet.trywait() ) {                         \
74         Port::disconnect(provideLastGivenValue);                        \
75       }                                                                 \
76     }                                                                   \
77     inline void setDependencyType(CalciumTypes::DependencyType dependencyType) { \
78       Port::setDependencyType(dependencyType);                          \
79     }                                                                   \
80     inline CalciumTypes::DependencyType getDependencyType () const {    \
81       return Port::getDependencyType();                                 \
82     }                                                                   \
83     inline void   setStorageLevel (size_t storageLevel) {               \
84       Port::setStorageLevel(storageLevel);                              \
85     }                                                                   \
86     inline size_t getStorageLevel () const {                            \
87       return Port::getStorageLevel();                                   \
88     }                                                                   \
89     inline void   setDateCalSchem (CalciumTypes::DateCalSchem   dateCalSchem) { \
90       Port::setDateCalSchem (dateCalSchem);                             \
91     }                                                                   \
92     inline CalciumTypes::DateCalSchem getDateCalSchem () const {        \
93       return Port::getDateCalSchem ();                                  \
94     }                                                                   \
95     inline void setAlpha(double alpha) {                                \
96       Port::setAlpha(alpha);                                            \
97     }                                                                   \
98     inline double getAlpha() const  {                                   \
99       return Port::getAlpha();                                          \
100     }                                                                   \
101     inline void   setDeltaT(double deltaT ) {                           \
102       Port::setDeltaT(deltaT);                                          \
103     }                                                                   \
104     inline double getDeltaT() const  {                                  \
105       return Port::getDeltaT();                                         \
106     }                                                                   \
107     inline void setInterpolationSchem (CalciumTypes::InterpolationSchem interpolationSchem) { \
108       Port::setInterpolationSchem(interpolationSchem);                  \
109     }                                                                   \
110     inline void setExtrapolationSchem (CalciumTypes::ExtrapolationSchem extrapolationSchem) { \
111       Port::setExtrapolationSchem(extrapolationSchem);                  \
112     }                                                                   \
113     inline CalciumTypes::InterpolationSchem getInterpolationSchem() const  { \
114       return Port::getInterpolationSchem();                             \
115     }                                                                   \
116     inline CalciumTypes::ExtrapolationSchem getExtrapolationSchem() const  { \
117       return Port::getExtrapolationSchem();                             \
118     }                                                                   \
119                                                                         \
120     inline void put( DataManipulator::CorbaInType data,                 \
121                      CORBA::Double time, CORBA::Long tag) {             \
122       Port::put(data, time, tag);                                       \
123     }                                                                   \
124                                                                         \
125     inline Ports::Port_ptr get_port_ref() {                             \
126       return _this();                                                   \
127     }                                                                   \
128                                                                         \
129     Ports::PortProperties_ptr get_port_properties() {                   \
130       return POA_Ports::PortProperties::_this();                        \
131     }                                                                   \
132                                                                         \
133     virtual void set_property(const char * name, const CORBA::Any& value) \
134       throw (Ports::NotDefined, Ports::BadType, Ports::BadValue);       \
135                                                                         \
136     virtual CORBA::Any* get_property(const char* name)                  \
137       throw (Ports::NotDefined);                                        \
138                                                                         \
139     virtual void provides_port_changed(int connection_nbr,              \
140                                        const Engines::DSC::Message message) { \
141       if ( message == Engines::DSC::AddingConnection)                   \
142         _mustnotdisconnectyet.post();                                   \
143       else if ( message == Engines::DSC::RemovingConnection )           \
144         disconnect(false);                                              \
145     }                                                                   \
146   };                                                                    \
147
148
149
150 #define CALCIUM_GENERIC_PROVIDES_PORT_CXX(specificPortName)             \
151                                                                         \
152   specificPortName::~specificPortName(void) {};                         \
153                                                                         \
154   void specificPortName::set_property(const char * name,  const CORBA::Any& value) \
155     throw (Ports::NotDefined, Ports::BadType, Ports::BadValue) {        \
156                                                                         \
157     const std::string key(name);                                        \
158     CORBA::Long sl;                                                     \
159     CORBA::Double alpha,delta;                                          \
160     Ports::Calcium_Ports::DependencyType dt;                            \
161     Ports::Calcium_Ports::DateCalSchem  dcs;                            \
162     Ports::Calcium_Ports::InterpolationSchem is;                        \
163     Ports::Calcium_Ports::ExtrapolationSchem es;                        \
164     bool ok=false;                                                      \
165                                                                         \
166     try {                                                               \
167                                                                         \
168       if (key == "StorageLevel" )                                       \
169         {if ( ok=(value >>= sl)    ) Port::setStorageLevel(sl);}        \
170       else if (key == "Alpha"      )                                    \
171         {if ( ok=(value >>= alpha) ) Port::setAlpha(alpha);}            \
172       else if (key == "DeltaT"     )                                    \
173         {if ( ok=(value >>= delta) ) Port::setDeltaT(delta);}           \
174       else if (key == "DependencyType" )                                \
175         {if ( ok=( value >>= dt)   ) Port::setDependencyType(dependencyType[dt]);} \
176       else if (key == "DateCalSchem" )                                  \
177         {if ( ok=(value >>= dcs) )                                      \
178             Port::setDateCalSchem(dateCalSchem[dcs]);}                  \
179       else if (key == "InterpolationSchem")                             \
180         {if ( ok=(value >>= is) )                                       \
181             Port::setInterpolationSchem(interpolationSchem[is]);}       \
182       else if (key == "ExtrapolationSchem")                             \
183         {if ( ok=(value >>= es) )                                       \
184             Port::setExtrapolationSchem(extrapolationSchem[es]);}       \
185       else                                                              \
186         throw Ports::NotDefined();                                      \
187       if (!ok) throw Ports::BadType();                                  \
188                                                                         \
189     } catch ( const DSC_Exception & ex ) {                              \
190                                                                         \
191       std::cerr << ex.what() << std::endl;                              \
192       throw Ports::BadValue();                                          \
193     }                                                                   \
194   }                                                                     \
195                                                                         \
196                                                                         \
197   CORBA::Any* specificPortName::get_property(const char* name)          \
198     throw (Ports::NotDefined) {                                         \
199     const std::string key(name);                                        \
200     CORBA::Any* value=new CORBA::Any;                                   \
201     if (key == "StorageLevel"    )                                      \
202       { (*value) <<= static_cast<CORBA::Long>(Port::getStorageLevel()); } \
203     else if (key == "Alpha"      )                                      \
204       { *value <<= (CORBA::Double) Port::getAlpha();}                   \
205     else if (key == "DeltaT"     )                                      \
206       { *value <<= (CORBA::Double) Port::getDeltaT();}                  \
207     else if (key == "DependencyType" )                                  \
208       {  *value <<= corbaDependencyType[Port::getDependencyType()];}    \
209     else if (key == "DateCalSchem" )                                    \
210       { *value <<= corbaDateCalSchem[Port::getDateCalSchem()];}         \
211     else if (key == "InterpolationSchem")                               \
212       { *value <<= corbaInterpolationSchem[Port::getInterpolationSchem()];} \
213     else if (key == "ExtrapolationSchem")                               \
214       { *value <<= corbaExtrapolationSchem[Port::getExtrapolationSchem()];} \
215     else {                                                              \
216       delete value;                                                     \
217       throw Ports::NotDefined();                                        \
218     }                                                                   \
219     return value;                                                       \
220   };                                            
221                                                                         
222
223 #endif