SALOME documentation central

src/DSC/DSC_User/Datastream/CouplingPolicy.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   : CouplingPolicy.hxx
00023 //  Author : Eric Fayolle (EDF)
00024 //  Module : KERNEL
00025 // Modified by : $LastChangedBy$
00026 // Date        : $LastChangedDate: 2007-02-28 15:26:32 +0100 (mer, 28 fév 2007) $
00027 // Id          : $Id$
00028 //
00029 #ifndef _COUPLING_POLICY_HXX_
00030 #define _COUPLING_POLICY_HXX_
00031 
00032 #include "IteratorTraits.hxx"
00033 #include "FindKeyPredicate.hxx"
00034 #include <algorithm>
00035 #include <functional>
00036 #include <iterator>
00037 
00038 // La politique de couplage doit définir
00039 // 1) le type DataId qui identifie une instance de données 
00040 // 2) Un container de DataId appelé DataIdContainer et son itérateur
00041 // 3) Un constructeur de DataIdContainer qui  initialise le container à partir d'un DataId
00042 
00044 //   COUPLING_POLICY::DataId 
00045 //   est construit par  DataId(T1 time,T2 tag)
00046 //
00047 //   COUPLING_POLICY::DataIdContainer
00048 //   qui défini  
00049 //   les types    : DataIdContainer::iterator 
00050 //   les méthodes : begin(), end(), empty()
00051 //   COUPLING_POLICY::DataIdContainer::iterator 
00052 //   qui défini 
00053 //   les méthodes : != , == , ++() , ()++, *(), =
00054 
00055 //   COUPLING_POLICY::DataTable
00056 //    std::map< DataId, DataType>      DataTable;
00057 
00058 //   Définir void COUPLING_POLICY::DataIdContainer(const DataId &, CouplingPolicy & )
00059 //   qui initialise le container à partir d'un DataId
00060 
00061 //   Opérateur d'affichage d'un dataId (les types doivent être affichables)
00062 
00063   
00064 class CouplingPolicy {
00065 
00066 public:
00067 
00068   // Renvoie isEqual si le dataId attendu est trouvé dans storedDataIds :
00069   //   - l'itérateur wDataIt1 pointe alors sur ce dataId
00070   // Cette méthode doit être redéfini dans le mode de couplage s'il veut gérer
00071   // le cas d'un  dataId damandé non trouvé mais encadrable 
00072   // par deux autres dataIds. La méthode renvoie alors isBounded== true et : 
00073   //   - l'itérateur wDataIt1 doit  être tel que :
00074   //     wDataIt1->first < wdataId < (wDataIt1+1)->first
00075   template < typename Container >
00076   bool isDataIdConveniant(Container & storedDatas, 
00077                  const typename Container::key_type & expectedDataId,
00078                  bool & isEqual ,  bool & isBounded, 
00079                  typename Container::iterator & wDataIt1 ) const {
00080     typedef typename Container::key_type   key_type;
00081     typedef typename Container::value_type value_type;
00082     typedef typename Container::iterator iterator;
00083     isBounded = false;
00084     FindKeyPredicate<value_type> fkp(expectedDataId);
00085     wDataIt1  = std::find_if(storedDatas.begin(),storedDatas.end(),fkp);
00086     isEqual   = (wDataIt1 != storedDatas.end());
00087     std::cout << "-------- Generic isDataIdConvenient : isEqual : " << isEqual << " , isBounded " << isBounded << std::endl;
00088     return isEqual || isBounded;
00089   }
00090 
00091   
00092   // Méthode Vide déclarée ici pour définir son nom dans GenericPort
00093   // Elle profite à tous les modes de couplages n'implémentant pas
00094   // de comportement particulier dans la méthode get pour 
00095   // le cas d'une demande de dataId inexistant mais encadré par deux autres
00096   template <typename DataManipulator, class EnableIf = void >
00097   struct BoundedDataIdProcessor{
00098     BoundedDataIdProcessor(const CouplingPolicy & couplingPolicy) {};
00099     template < typename Iterator, typename DataId > 
00100     void inline apply(typename iterator_t<Iterator>::value_type & data,
00101                 const DataId & dataId,
00102                 const Iterator  & it1) const {
00103       typedef typename iterator_t<Iterator>::value_type value_type;
00104       std::cout << "-------- Generic BoundedDataIdProcessor.apply() called " << std::endl;
00105 
00106     }
00107   };
00108 
00109   // Supprime un DataId et ses données associées
00110   // du container
00111   // Cette méthode utilisée dans GenericPort::Get 
00112   // peut être surchargée pour par exemple
00113   // conserver un historique.
00114   template <typename DataManipulator>
00115   struct EraseDataIdProcessor {
00116 
00117     EraseDataIdProcessor(CouplingPolicy couplingPolicy) {};
00118 
00119     template < typename Container >
00120     void apply(Container & storedDatas, 
00121             typename Container::iterator & wDataIt1 ) const {
00122       typedef typename Container::key_type   key_type;
00123       typedef typename Container::value_type value_type;
00124       typedef typename Container::iterator iterator;
00125 
00126       std::cout << "-------- Generic eraseDataId called " << std::endl;
00127     }
00128   };
00129 
00130   // Lorsque cette méthode est appelée l'expectedDataId n'a pas été trouvé
00131   // et n'est pas non plus encadrée (en mode temporel)
00132   // Si l'on effectue pas de traitement particulier la méthode renvoie false
00133   // Si le port a reçu une directive STOP une exception est levée
00134   // Si le port a reçu une directive CONTINUE, on localise l'expected
00135   template < typename DataManipulator > 
00136   struct DisconnectProcessor {
00137 
00138     DisconnectProcessor(const CouplingPolicy & couplingPolicy) {};
00139 
00140     template < typename Container, typename DataId >
00141     bool apply(Container & storedDatas,
00142             const DataId & expectedDataId,
00143             typename Container::iterator & wDataIt1 ) const {
00144       typedef typename Container::key_type   key_type;
00145       typedef typename Container::value_type value_type;
00146       typedef typename Container::iterator   iterator;
00147 
00148       std::cout << "-------- Generic DisconnectProcessor called " << std::endl;
00149       return true;
00150     }
00151   };
00152 
00153   // Permet de réveiller les méthodes d'un GenericPort en attente
00154   // depuis une CouplingPolicy
00155   virtual void wakeupWaiting(){};
00156 
00157   virtual ~CouplingPolicy() {}
00158 
00159 };
00160 
00161 #endif