Documentation for OpenFLUID 2.2.0
WareSetDescriptor.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file WareSetDescriptor.hpp
35 
36  @author Jean-Christophe Fabre <jean-christophe.fabre@inra.fr>
37 */
38 
39 
40 #ifndef __OPENFLUID_FLUIDX_WARESETDESCRIPTOR_HPP__
41 #define __OPENFLUID_FLUIDX_WARESETDESCRIPTOR_HPP__
42 
43 
44 #include <list>
45 
46 #include <openfluid/dllexport.hpp>
47 #include <openfluid/ware/TypeDefs.hpp>
48 
49 
50 namespace openfluid { namespace fluidx {
51 
52 
53 template<class T>
55 {
56  public:
57 
58  typedef std::list<T*> SetDescription_t;
59 
60 
61  protected:
62 
64 
66 
67 
68  public:
69 
71  {
72 
73  }
74 
75 
76  // =====================================================================
77  // =====================================================================
78 
79 
80  virtual ~WareSetDescriptor<T>()
81  {
82 
83  }
84 
85 
86  // =====================================================================
87  // =====================================================================
88 
89 
91  {
92  return m_SetDescription;
93  }
94 
95 
96  // =====================================================================
97  // =====================================================================
98 
99 
100  inline const SetDescription_t& items() const
101  {
102  return m_SetDescription;
103  }
104 
105 
106  // =====================================================================
107  // =====================================================================
108 
109 
112  {
113  m_Params[Key] = Value;
114  }
115 
116 
117  // =====================================================================
118  // =====================================================================
119 
120 
122  {
123  openfluid::ware::WareParams_t::const_iterator it;
124 
125  for (it=Params.begin();it!=Params.end();++it)
126  {
127  m_Params[it->first] = it->second;
128  }
129  }
130 
131 
132  // =====================================================================
133  // =====================================================================
134 
135 
137  {
138  return m_Params;
139  }
140 
141 
142  // =====================================================================
143  // =====================================================================
144 
145 
147  {
148  m_Params.erase(Key);
149  }
150 
151 
152  // =====================================================================
153  // =====================================================================
154 
155 
156  void appendItem(T* Item)
157  {
158  m_SetDescription.push_back(Item);
159  }
160 
161 
162  // =====================================================================
163  // =====================================================================
164 
165 
166  /**
167  Returns the ID of a ware given by a pointer on its descriptor
168  @param[in] Item the pointer to the descriptor
169  @return the ID of the ware
170  */
171  virtual openfluid::ware::WareID_t getID(const T* Item) const = 0;
172 
173 
174  // =====================================================================
175  // =====================================================================
176 
177 
178  /**
179  Sets the list of pointers to ware descriptors
180  @param[in] Items the list of pointers to descriptors
181  */
182  void setItems(const SetDescription_t& Items)
183  {
184  m_SetDescription = Items;
185  }
186 
187 
188  // =====================================================================
189  // =====================================================================
190 
191 
192  /**
193  Returns a reference to the ware descriptor at a given index
194  @param[in] Index the given index
195  @return the descriptor
196  @throws openfluid::base::FrameworkException Index out of bounds
197  */
198  T& itemAt(unsigned int Index) const
199  {
200 
201  if (Index < m_SetDescription.size())
202  {
203  typename SetDescription_t::const_iterator it = m_SetDescription.begin();
204  std::advance(it, Index);
205 
206  return **it;
207  }
208  else
209  {
210  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Index out of bounds");
211  }
212  }
213 
214 
215  // =====================================================================
216  // =====================================================================
217 
218 
219  /**
220  Inserts a pointer to ware descriptor at a given index in the wares set descriptor
221  @param[in] Item the descriptor to insert
222  @param[in] Index the index where the descriptor is inserted
223  @throws openfluid::base::FrameworkException Index out of bounds
224  */
225  void insertItem(T* Item, unsigned int Index)
226  {
227  if (Index == 0)
228  {
229  m_SetDescription.insert(m_SetDescription.begin(), Item);
230  }
231  else if (Index < m_SetDescription.size())
232  {
233  typename SetDescription_t::iterator it = m_SetDescription.begin();
234  std::advance(it, Index);
235 
236  m_SetDescription.insert(it, Item);
237  }
238  else
239  {
240  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Index out of bounds");
241  }
242  }
243 
244 
245  // =====================================================================
246  // =====================================================================
247 
248 
249  /**
250  Removes the ware descriptor at a given index from the wares set descriptor
251  @param[in] Index the index of the descriptor to remove
252  @throws openfluid::base::FrameworkException Index out of bounds
253  */
254  void removeItem(unsigned int Index)
255  {
256  if (Index < m_SetDescription.size())
257  {
258  typename SetDescription_t::iterator it = m_SetDescription.begin();
259  std::advance(it, Index);
260 
261  m_SetDescription.erase(it);
262  }
263  else
264  {
265  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Index out of bounds");
266  }
267  }
268 
269 
270  // =====================================================================
271  // =====================================================================
272 
273 
274  /**
275  Moves a ware descriptor at a given index to another index in the wares set descriptor
276  @param[in] indexFrom initial index of the descriptor
277  @param[in] indexTo destination index of the descriptor
278  @throws openfluid::base::FrameworkException Index out of bounds
279  */
280  void moveItem(unsigned int indexFrom, unsigned int indexTo)
281  {
282  if (indexFrom == indexTo)
283  {
284  return;
285  }
286 
287  unsigned int Last = m_SetDescription.size() - 1;
288 
289  if (indexFrom > Last || indexTo > Last)
290  {
291  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Index out of bounds");
292  }
293 
294  typename SetDescription_t::const_iterator itFrom = m_SetDescription.begin();
295  std::advance(itFrom, indexFrom);
296 
297  T* Item = *itFrom;
298 
299  removeItem(indexFrom);
300 
301  if (indexTo == Last)
302  {
303  appendItem(Item);
304  }
305  else
306  {
307  insertItem(Item, indexTo);
308  }
309  }
310 
311 
312  // =====================================================================
313  // =====================================================================
314 
315 
316  /**
317  Finds the index of the first ware descriptor with the given ID
318  @param[in] ID the ID of the searched descriptor
319  @return the index of the searched descriptor, -1 otherwise
320  */
322  {
323  for (typename SetDescription_t::const_iterator it = m_SetDescription.begin(); it != m_SetDescription.end(); ++it)
324  {
325  if (getID(*it) == ID)
326  {
327  return std::distance(m_SetDescription.begin(), it);
328  }
329  }
330 
331  return -1;
332  }
333 
334 
335  // =====================================================================
336  // =====================================================================
337 
338 
339  /**
340  Finds the index of the first ware descriptor corresponding to the given descriptor
341  @param[in] Item the searched descriptor
342  @return the index of the searched descriptor, -1 otherwise
343  */
344  int findFirstItem(const T* Item) const
345  {
346  typename std::list<T*>::iterator it = std::find(m_SetDescription.begin(), m_SetDescription.end(), Item);
347 
348  if (it != m_SetDescription.end())
349  {
350  return std::distance(m_SetDescription.begin(), it);
351  }
352 
353  return -1;
354  }
355 
356 
357  // =====================================================================
358  // =====================================================================
359 
360 
361  /**
362  Returns an ordered vector of IDs of wares contained in the wares set descriptor
363  @return the vector of IDs
364  */
365  std::vector<openfluid::ware::WareID_t> getOrderedIDs() const
366  {
367  std::vector<std::string> IDs;
368 
369  for (typename SetDescription_t::const_iterator it = m_SetDescription.begin(); it != m_SetDescription.end(); ++it)
370  {
371  IDs.push_back(getID(*it));
372  }
373 
374  return IDs;
375  }
376 
377 
378  // =====================================================================
379  // =====================================================================
380 
381 
382  /**
383  Returns the number of ware descriptors in the wares set descriptor
384  @return the number of ware descriptors
385  */
387  {
388  return m_SetDescription.size();
389  }
390 
391 };
392 
393 
394 } } // namespaces
395 
396 
397 #endif /* __OPENFLUID_FLUIDX_WARESETDESCRIPTOR_HPP__ */
Definition: FrameworkException.hpp:51
Definition: StringValue.hpp:83
Definition: WareSetDescriptor.hpp:55
void insertItem(T *Item, unsigned int Index)
Definition: WareSetDescriptor.hpp:225
int findFirstItem(const openfluid::ware::WareID_t &ID) const
Definition: WareSetDescriptor.hpp:321
T & itemAt(unsigned int Index) const
Definition: WareSetDescriptor.hpp:198
std::vector< openfluid::ware::WareID_t > getOrderedIDs() const
Definition: WareSetDescriptor.hpp:365
int getItemsCount()
Definition: WareSetDescriptor.hpp:386
void removeItem(unsigned int Index)
Definition: WareSetDescriptor.hpp:254
void setGlobalParameters(const openfluid::ware::WareParams_t &Params)
Definition: WareSetDescriptor.hpp:121
virtual openfluid::ware::WareID_t getID(const T *Item) const =0
openfluid::ware::WareParams_t m_Params
Definition: WareSetDescriptor.hpp:65
std::list< T * > SetDescription_t
Definition: WareSetDescriptor.hpp:58
openfluid::ware::WareParams_t getGlobalParameters() const
Definition: WareSetDescriptor.hpp:136
const SetDescription_t & items() const
Definition: WareSetDescriptor.hpp:100
void eraseGlobalParameter(const openfluid::ware::WareParamKey_t &Key)
Definition: WareSetDescriptor.hpp:146
void moveItem(unsigned int indexFrom, unsigned int indexTo)
Definition: WareSetDescriptor.hpp:280
void setItems(const SetDescription_t &Items)
Definition: WareSetDescriptor.hpp:182
void setGlobalParameter(const openfluid::ware::WareParamKey_t &Key, const openfluid::ware::WareParamValue_t &Value)
Definition: WareSetDescriptor.hpp:110
void appendItem(T *Item)
Definition: WareSetDescriptor.hpp:156
SetDescription_t m_SetDescription
Definition: WareSetDescriptor.hpp:63
int findFirstItem(const T *Item) const
Definition: WareSetDescriptor.hpp:344
SetDescription_t & items()
Definition: WareSetDescriptor.hpp:90
#define OPENFLUID_API
Definition: dllexport.hpp:86
std::string WareID_t
Definition: TypeDefs.hpp:49
std::string WareParamKey_t
Definition: TypeDefs.hpp:142
std::map< WareParamKey_t, WareParamValue_t > WareParams_t
Definition: TypeDefs.hpp:146
Definition: ApplicationException.hpp:47