Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/kernel.git] / src / DSC / DSC_User / Datastream / CouplingPolicy.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   : CouplingPolicy.hxx
23 //  Author : Eric Fayolle (EDF)
24 //  Module : KERNEL
25 // Modified by : $LastChangedBy$
26 // Date        : $LastChangedDate: 2007-02-28 15:26:32 +0100 (mer, 28 fév 2007) $
27 // Id          : $Id$
28
29 #ifndef _COUPLING_POLICY_HXX_
30 #define _COUPLING_POLICY_HXX_
31
32 #include "IteratorTraits.hxx"
33 #include "FindKeyPredicate.hxx"
34 #include <algorithm>
35 #include <functional>
36 #include <iterator>
37
38 // La politique de couplage doit définir
39 // 1) le type DataId qui identifie une instance de données 
40 // 2) Un container de DataId appelé DataIdContainer et son itérateur
41 // 3) Un constructeur de DataIdContainer qui  initialise le container à partir d'un DataId
42
43 //////////  Le DataId //////////////
44 //   COUPLING_POLICY::DataId 
45 //   est construit par  DataId(T1 time,T2 tag)
46 //
47 //   COUPLING_POLICY::DataIdContainer
48 //   qui défini  
49 //   les types    : DataIdContainer::iterator 
50 //   les méthodes : begin(), end(), empty()
51 //   COUPLING_POLICY::DataIdContainer::iterator 
52 //   qui défini 
53 //   les méthodes : != , == , ++() , ()++, *(), =
54
55 //   COUPLING_POLICY::DataTable
56 //    std::map< DataId, DataType>      DataTable;
57
58 //   Définir void COUPLING_POLICY::DataIdContainer(const DataId &, CouplingPolicy & )
59 //   qui initialise le container à partir d'un DataId
60
61 //   Opérateur d'affichage d'un dataId (les types doivent être affichables)
62
63   
64 class CouplingPolicy {
65
66 public:
67
68   // Renvoie isEqual si le dataId attendu est trouvé dans storedDataIds :
69   //   - l'itérateur wDataIt1 pointe alors sur ce dataId
70   // Cette méthode doit être redéfini dans le mode de couplage s'il veut gérer
71   // le cas d'un  dataId damandé non trouvé mais encadrable 
72   // par deux autres dataIds. La méthode renvoie alors isBounded== true et : 
73   //   - l'itérateur wDataIt1 doit  être tel que :
74   //     wDataIt1->first < wdataId < (wDataIt1+1)->first
75   template < typename Container >
76   bool isDataIdConveniant(Container & storedDatas, 
77                           const typename Container::key_type & expectedDataId,
78                           bool & isEqual ,  bool & isBounded, 
79                           typename Container::iterator & wDataIt1 ) const {
80     typedef typename Container::key_type   key_type;
81     typedef typename Container::value_type value_type;
82     typedef typename Container::iterator iterator;
83     isBounded = false;
84     FindKeyPredicate<value_type> fkp(expectedDataId);
85     wDataIt1  = std::find_if(storedDatas.begin(),storedDatas.end(),fkp);
86     isEqual   = (wDataIt1 != storedDatas.end());
87     std::cout << "-------- Generic isDataIdConvenient : isEqual : " << isEqual << " , isBounded " << isBounded << std::endl;
88     return isEqual || isBounded;
89   }
90
91   
92   // Méthode Vide déclarée ici pour définir son nom dans GenericPort
93   // Elle profite à tous les modes de couplages n'implémentant pas
94   // de comportement particulier dans la méthode get pour 
95   // le cas d'une demande de dataId inexistant mais encadré par deux autres
96   template <typename DataManipulator, class EnableIf = void >
97   struct BoundedDataIdProcessor{
98     BoundedDataIdProcessor(const CouplingPolicy & couplingPolicy) {};
99     template < typename Iterator, typename DataId > 
100     void inline apply(typename iterator_t<Iterator>::value_type & data,
101                       const DataId & dataId,
102                       const Iterator  & it1) const {
103       typedef typename iterator_t<Iterator>::value_type value_type;
104       std::cout << "-------- Generic BoundedDataIdProcessor.apply() called " << std::endl;
105
106     }
107   };
108
109   // Supprime un DataId et ses données associées
110   // du container
111   // Cette méthode utilisée dans GenericPort::Get 
112   // peut être surchargée pour par exemple
113   // conserver un historique.
114   template <typename DataManipulator>
115   struct EraseDataIdProcessor {
116
117     EraseDataIdProcessor(CouplingPolicy couplingPolicy) {};
118
119     template < typename Container >
120     void apply(Container & storedDatas, 
121                typename Container::iterator & wDataIt1 ) const {
122       typedef typename Container::key_type   key_type;
123       typedef typename Container::value_type value_type;
124       typedef typename Container::iterator iterator;
125
126       std::cout << "-------- Generic eraseDataId called " << std::endl;
127     }
128   };
129
130   // Lorsque cette méthode est appelée l'expectedDataId n'a pas été trouvé
131   // et n'est pas non plus encadrée (en mode temporel)
132   // Si l'on effectue pas de traitement particulier la méthode renvoie false
133   // Si le port a reçu une directive STOP une exception est levée
134   // Si le port a reçu une directive CONTINUE, on localise l'expected
135   template < typename DataManipulator > 
136   struct DisconnectProcessor {
137
138     DisconnectProcessor(const CouplingPolicy & couplingPolicy) {};
139
140     template < typename Container, typename DataId >
141     bool apply(Container & storedDatas,
142                const DataId & expectedDataId,
143                typename Container::iterator & wDataIt1 ) const {
144       typedef typename Container::key_type   key_type;
145       typedef typename Container::value_type value_type;
146       typedef typename Container::iterator   iterator;
147
148       std::cout << "-------- Generic DisconnectProcessor called " << std::endl;
149       return true;
150     }
151   };
152
153   // Permet de réveiller les méthodes d'un GenericPort en attente
154   // depuis une CouplingPolicy
155   virtual void wakeupWaiting(){};
156
157   virtual ~CouplingPolicy() {}
158
159 };
160
161 #endif