SALOME documentation central

src/DSC/DSC_User/Datastream/Calcium/CalciumGenericProvidesPort.hxx

00001 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 //  This library is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU Lesser General Public
00008 //  License as published by the Free Software Foundation; either
00009 //  version 2.1 of the License.
00010 //
00011 //  This library is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 //  Lesser General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU Lesser General Public
00017 //  License along with this library; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : CalciumGenericProvidesPort.hxx
00023 //  Author : Eric Fayolle (EDF)
00024 //  Module : KERNEL
00025 // Id          : $Id$
00026 //
00027 #ifndef _CALCIUM_GENERIC_PROVIDES_PORT_HXX_
00028 #define _CALCIUM_GENERIC_PROVIDES_PORT_HXX_
00029 
00030 #include <omnithread.h>
00031 
00032 #include "PortProperties_i.hxx"
00033 
00034 #include "calcium_provides_port.hxx"
00035 
00036 #include "GenericProvidesPort.hxx"
00037 #include "CalciumCouplingPolicy.hxx"
00038 
00039 #include "CorbaTypes2CalciumTypes.hxx"
00040 #include "CalciumTypes2CorbaTypes.hxx"
00041 
00042 #include "DSC_Exception.hxx"
00043 #include <iostream>
00044 
00045 //
00046 // Cette macro CPP remplace une déclaration : template <typename CorbaInterface, typename CorbaDataManipulator > 
00047 // mais permet surtout de générer différentes classes (dont le nom est spécifié par specificPortName ) 
00048 //
00049 // Variante: Creer un trait qui à partir de CorbaInterface déduit CorbaDataManipulator
00050 // et simplifier la classe
00051 //
00052 // L'utilisation conjointe du paramètre ... et de __VA_ARGS__ au lieu d'un 'CorbaDataManipulator' permet
00053 // d'invoquer la macro avec un troisième token contenant une virgule 
00054 // (qui est considéré comme un séparateur d'argument par le PP ) 
00055 
00056 #define CALCIUM_GENERIC_PROVIDES_PORT_HXX(specificPortName,CorbaInterface,...) \
00057   class specificPortName :   public virtual CorbaInterface ,          \
00058                     public virtual POA_Ports::PortProperties,    \
00059                     public GenericProvidesPort< __VA_ARGS__ , CalciumCouplingPolicy, calcium_provides_port > { \
00060   private :                                       \
00061     omni_mutex     _disconnect_mutex; \
00062     int            _mustnotdisconnect; \
00063   public :                                        \
00064     typedef  __VA_ARGS__               DataManipulator;               \
00065     typedef  DataManipulator::Type     CorbaDataType;            \
00066     typedef GenericPort< DataManipulator ,                  \
00067       CalciumCouplingPolicy >          Port;                \
00068       specificPortName () : _mustnotdisconnect(0) {}; \
00069                                              \
00070     virtual ~ specificPortName ();                     \
00071                                              \
00072     inline void disconnect(bool provideLastGivenValue) {         \
00073       _disconnect_mutex.lock();                       \
00074       if(_mustnotdisconnect > 1)                      \
00075       {                                               \
00076         _mustnotdisconnect--;                         \
00077       }                                               \
00078       else if(_mustnotdisconnect == 1)                \
00079       {                                               \
00080         _mustnotdisconnect--;                         \
00081         Port::disconnect(provideLastGivenValue);      \
00082       }                                               \
00083       _disconnect_mutex.unlock();                     \
00084     }                                             \
00085     inline void setDependencyType(CalciumTypes::DependencyType dependencyType) { \
00086       Port::setDependencyType(dependencyType);                   \
00087     }                                             \
00088     inline CalciumTypes::DependencyType getDependencyType () const {  \
00089       return Port::getDependencyType();                     \
00090     }                                             \
00091     inline void   setStorageLevel (size_t storageLevel) {        \
00092       Port::setStorageLevel(storageLevel);                  \
00093     }                                             \
00094     inline size_t getStorageLevel () const {                \
00095       return Port::getStorageLevel();                       \
00096     }                                             \
00097     inline void   setDateCalSchem (CalciumTypes::DateCalSchem   dateCalSchem) { \
00098       Port::setDateCalSchem (dateCalSchem);                 \
00099     }                                             \
00100     inline CalciumTypes::DateCalSchem getDateCalSchem () const { \
00101       return Port::getDateCalSchem ();                      \
00102     }                                             \
00103     inline void setAlpha(double alpha) {                    \
00104       Port::setAlpha(alpha);                           \
00105     }                                             \
00106     inline double getAlpha() const  {                       \
00107       return Port::getAlpha();                              \
00108     }                                             \
00109     inline void   setDeltaT(double deltaT ) {                    \
00110       Port::setDeltaT(deltaT);                              \
00111     }                                             \
00112     inline double getDeltaT() const  {                      \
00113       return Port::getDeltaT();                             \
00114     }                                             \
00115     inline void setInterpolationSchem (CalciumTypes::InterpolationSchem interpolationSchem) { \
00116       Port::setInterpolationSchem(interpolationSchem);           \
00117     }                                             \
00118     inline void setExtrapolationSchem (CalciumTypes::ExtrapolationSchem extrapolationSchem) { \
00119       Port::setExtrapolationSchem(extrapolationSchem);           \
00120     }                                             \
00121     inline CalciumTypes::InterpolationSchem getInterpolationSchem() const  { \
00122       return Port::getInterpolationSchem();                 \
00123     }                                             \
00124     inline CalciumTypes::ExtrapolationSchem getExtrapolationSchem() const  { \
00125       return Port::getExtrapolationSchem();                 \
00126     }                                             \
00127                                              \
00128     inline void put( DataManipulator::CorbaInType data,               \
00129                CORBA::Double time, CORBA::Long tag) {       \
00130       Port::put(data, time, tag);                      \
00131     }                                             \
00132                                              \
00133     inline Ports::Port_ptr get_port_ref() {                 \
00134       return _this();                                  \
00135     }                                             \
00136                                              \
00137     Ports::PortProperties_ptr get_port_properties() {            \
00138       return POA_Ports::PortProperties::_this();            \
00139     }                                             \
00140                                              \
00141     virtual void set_property(const char * name, const CORBA::Any& value) \
00142       throw (Ports::NotDefined, Ports::BadType, Ports::BadValue);     \
00143                                              \
00144     virtual CORBA::Any* get_property(const char* name)           \
00145       throw (Ports::NotDefined);                       \
00146                                              \
00147     virtual void provides_port_changed(int connection_nbr,       \
00148                            const Engines::DSC::Message message) { \
00149       if ( message == Engines::DSC::AddingConnection)            \
00150         {                                                 \
00151           _disconnect_mutex.lock();                \
00152           _mustnotdisconnect++;                           \
00153           _disconnect_mutex.unlock();              \
00154         }                                                 \
00155       else if ( message == Engines::DSC::RemovingConnection )         \
00156         {                                                 \
00157           disconnect(false);                              \
00158         }                                                 \
00159     }                                             \
00160   };                                         \
00161 
00162 
00163 
00164 #define CALCIUM_GENERIC_PROVIDES_PORT_CXX(specificPortName)      \
00165                                              \
00166   specificPortName::~specificPortName(void) {};                  \
00167                                              \
00168   void specificPortName::set_property(const char * name,  const CORBA::Any& value) \
00169     throw (Ports::NotDefined, Ports::BadType, Ports::BadValue) { \
00170                                              \
00171     const std::string key(name);                       \
00172     CORBA::Long sl;                               \
00173     CORBA::Double alpha,delta;                              \
00174     Ports::Calcium_Ports::DependencyType dt;                \
00175     Ports::Calcium_Ports::DateCalSchem  dcs;                \
00176     Ports::Calcium_Ports::InterpolationSchem is;            \
00177     Ports::Calcium_Ports::ExtrapolationSchem es;            \
00178     bool ok=false;                                \
00179                                              \
00180     try {                                    \
00181                                              \
00182       if (key == "StorageLevel" )                      \
00183      {if ( ( ok=(value >>= sl) )   ) Port::setStorageLevel(sl);} \
00184       else if (key == "Alpha"      )                        \
00185      {if ( ( ok=(value >>= alpha) ) ) Port::setAlpha(alpha);}         \
00186       else if (key == "DeltaT"     )                        \
00187      {if ( ( ok=(value >>= delta) ) ) Port::setDeltaT(delta);}        \
00188       else if (key == "DependencyType" )                    \
00189      {if ( ( ok=( value >>= dt)  )  ) Port::setDependencyType(dependencyType[dt]);} \
00190       else if (key == "DateCalSchem" )                      \
00191      {if ( ( ok=(value >>= dcs) ) )                         \
00192          Port::setDateCalSchem(dateCalSchem[dcs]);}              \
00193       else if (key == "InterpolationSchem")                 \
00194      {if ( ( ok=(value >>= is) ) )                     \
00195          Port::setInterpolationSchem(interpolationSchem[is]);}   \
00196       else if (key == "ExtrapolationSchem")                 \
00197      {if ( ( ok=(value >>= es) ) )                     \
00198          Port::setExtrapolationSchem(extrapolationSchem[es]);}   \
00199       else                                        \
00200      throw Ports::NotDefined();                        \
00201       if (!ok) throw Ports::BadType();                      \
00202                                              \
00203     } catch ( const DSC_Exception & ex ) {                  \
00204                                                   \
00205       std::cerr << ex.what() << std::endl;                  \
00206       throw Ports::BadValue();                              \
00207     }                                             \
00208   }                                          \
00209                                              \
00210                                              \
00211   CORBA::Any* specificPortName::get_property(const char* name)        \
00212     throw (Ports::NotDefined) {                             \
00213     const std::string key(name);                       \
00214     CORBA::Any* value=new CORBA::Any;                       \
00215     if (key == "StorageLevel"    )                     \
00216       { (*value) <<= static_cast<CORBA::Long>(Port::getStorageLevel()); } \
00217     else if (key == "Alpha"      )                     \
00218       { *value <<= (CORBA::Double) Port::getAlpha();}            \
00219     else if (key == "DeltaT"     )                     \
00220       { *value <<= (CORBA::Double) Port::getDeltaT();}           \
00221     else if (key == "DependencyType" )                      \
00222       {  *value <<= corbaDependencyType[Port::getDependencyType()];}  \
00223     else if (key == "DateCalSchem" )                        \
00224       { *value <<= corbaDateCalSchem[Port::getDateCalSchem()];}       \
00225     else if (key == "InterpolationSchem")                   \
00226       { *value <<= corbaInterpolationSchem[Port::getInterpolationSchem()];} \
00227     else if (key == "ExtrapolationSchem")                   \
00228       { *value <<= corbaExtrapolationSchem[Port::getExtrapolationSchem()];} \
00229     else {                                        \
00230       delete value;                               \
00231       throw Ports::NotDefined();                       \
00232     }                                             \
00233     return value;                                 \
00234   };                          
00235                                              
00236 
00237 #endif