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 
63  SetDescription_t m_SetDescription;
64 
66 
67 
68  public:
69 
71  {
72 
73  }
74 
75 
76  // =====================================================================
77  // =====================================================================
78 
79 
81  {
82 
83  }
84 
85 
86  // =====================================================================
87  // =====================================================================
88 
89 
90  inline SetDescription_t& items()
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(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  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Index out of bounds");
291 
292  typename SetDescription_t::const_iterator itFrom = m_SetDescription.begin();
293  std::advance(itFrom, indexFrom);
294 
295  T* Item = *itFrom;
296 
297  removeItem(indexFrom);
298 
299  if (indexTo == Last)
300  {
301  appendItem(Item);
302  }
303  else
304  {
305  insertItem(Item, indexTo);
306  }
307  }
308 
309 
310  // =====================================================================
311  // =====================================================================
312 
313 
314  /**
315  Finds the index of the first ware descriptor with the given ID
316  @param[in] ID the ID of the searched descriptor
317  @return the index of the searched descriptor, -1 otherwise
318  */
320  {
321  for (typename SetDescription_t::const_iterator it = m_SetDescription.begin(); it != m_SetDescription.end(); ++it)
322  {
323  if (getID(*it) == ID)
324  {
325  return std::distance(m_SetDescription.begin(), it);
326  }
327  }
328 
329  return -1;
330  }
331 
332 
333  // =====================================================================
334  // =====================================================================
335 
336 
337  /**
338  Finds the index of the first ware descriptor corresponding to the given descriptor
339  @param[in] Item the searched descriptor
340  @return the index of the searched descriptor, -1 otherwise
341  */
342  int findFirstItem(const T* Item) const
343  {
344  typename std::list<T*>::iterator it = std::find(m_SetDescription.begin(), m_SetDescription.end(), Item);
345 
346  if (it != m_SetDescription.end())
347  {
348  return std::distance(m_SetDescription.begin(), it);
349  }
350 
351  return -1;
352  }
353 
354 
355  // =====================================================================
356  // =====================================================================
357 
358 
359  /**
360  Returns an ordered vector of IDs of wares contained in the wares set descriptor
361  @return the vector of IDs
362  */
363  std::vector<openfluid::ware::WareID_t> getOrderedIDs() const
364  {
365  std::vector<std::string> IDs;
366 
367  for (typename SetDescription_t::const_iterator it = m_SetDescription.begin(); it != m_SetDescription.end(); ++it)
368  {
369  IDs.push_back(getID(*it));
370  }
371 
372  return IDs;
373  }
374 
375 
376  // =====================================================================
377  // =====================================================================
378 
379 
380  /**
381  Returns the number of ware descriptors in the wares set descriptor
382  @return the number of ware descriptors
383  */
385  {
386  return m_SetDescription.size();
387  }
388 
389 };
390 
391 
392 } } // namespaces
393 
394 
395 #endif /* __OPENFLUID_FLUIDX_MODELDESCRIPTOR_HPP__ */
Definition: WareSetDescriptor.hpp:54
Definition: StringValue.hpp:88
std::list< T * > SetDescription_t
Definition: WareSetDescriptor.hpp:58
openfluid::ware::WareParams_t m_Params
Definition: WareSetDescriptor.hpp:65
openfluid::ware::WareParams_t getGlobalParameters() const
Definition: WareSetDescriptor.hpp:136
void moveItem(unsigned int indexFrom, unsigned int indexTo)
Definition: WareSetDescriptor.hpp:280
int findFirstItem(const openfluid::ware::WareID_t &ID) const
Definition: WareSetDescriptor.hpp:319
void insertItem(T *Item, unsigned int Index)
Definition: WareSetDescriptor.hpp:225
T & itemAt(unsigned int Index) const
Definition: WareSetDescriptor.hpp:198
int findFirstItem(const T *Item) const
Definition: WareSetDescriptor.hpp:342
std::string WareID_t
Definition: TypeDefs.hpp:49
const SetDescription_t & items() const
Definition: WareSetDescriptor.hpp:100
void setGlobalParameters(const openfluid::ware::WareParams_t &Params)
Definition: WareSetDescriptor.hpp:121
int getItemsCount()
Definition: WareSetDescriptor.hpp:384
SetDescription_t m_SetDescription
Definition: WareSetDescriptor.hpp:63
Definition: FrameworkException.hpp:50
Definition: ApplicationException.hpp:47
void setItems(const SetDescription_t &Items)
Definition: WareSetDescriptor.hpp:182
SetDescription_t & items()
Definition: WareSetDescriptor.hpp:90
std::string WareParamKey_t
Definition: TypeDefs.hpp:124
void removeItem(unsigned int Index)
Definition: WareSetDescriptor.hpp:254
#define OPENFLUID_API
Definition: dllexport.hpp:86
void setGlobalParameter(const openfluid::ware::WareParamKey_t &Key, const openfluid::ware::WareParamValue_t &Value)
Definition: WareSetDescriptor.hpp:110
void eraseGlobalParameter(const openfluid::ware::WareParamKey_t &Key)
Definition: WareSetDescriptor.hpp:146
std::vector< openfluid::ware::WareID_t > getOrderedIDs() const
Definition: WareSetDescriptor.hpp:363
std::map< WareParamKey_t, WareParamValue_t > WareParams_t
Definition: TypeDefs.hpp:128
void appendItem(T *Item)
Definition: WareSetDescriptor.hpp:156