AdvancedWareSetDescriptor.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  @file AdvancedWareSetDescriptor.hpp
34 
35  @author Jean-Christophe FABRE <jean-christophe.fabre@supagro.inra.fr>
36  @author Aline LIBRES <aline.libres@gmail.com>
37 */
38 
39 
40 #ifndef __OPENFLUID_FLUIDX_ADVANCEDWARESETDESCRIPTOR_HPP__
41 #define __OPENFLUID_FLUIDX_ADVANCEDWARESETDESCRIPTOR_HPP__
42 
43 #include <list>
44 
45 #include <openfluid/dllexport.hpp>
48 
49 
50 namespace openfluid { namespace fluidx {
51 
52 
53 template<class M, class I>
55 {
56  protected:
57 
59 
60 
61  public:
62 
63 
65  mp_Descriptor(&Desc)
66  {
67 
68  }
69 
70 
71  // =====================================================================
72  // =====================================================================
73 
74 
76  { }
77 
78 
79  // =====================================================================
80  // =====================================================================
81 
82  /**
83  Runs checking of descriptors
84  */
85  virtual void check() = 0;
86 
87 
88  // =====================================================================
89  // =====================================================================
90 
91 
92  /**
93  Returns the ID of a ware given by a pointer on its descriptor
94  @param[in] Item the pointer to the descriptor
95  @return the ID of the ware
96  */
97  virtual openfluid::ware::WareID_t getID(I* Item) const = 0;
98 
99 
100  // =====================================================================
101  // =====================================================================
102 
103 
104  /**
105  Returns a const reference to the list of pointers to ware descriptors
106  @return the list of pointers to descriptors
107  */
108  const std::list<I*>& items() const
109  {
110  return mp_Descriptor->items();
111  }
112 
113 
114  // =====================================================================
115  // =====================================================================
116 
117 
118  /**
119  Sets the list of pointers to ware descriptors
120  @param[in] Items the list of pointers to descriptors
121  */
122  void setItems(const std::list<I*>& Items)
123  {
124  mp_Descriptor->items() = Items;
125  }
126 
127 
128  // =====================================================================
129  // =====================================================================
130 
131 
132  /**
133  Returns a reference to the ware descriptor at a given index
134  @param[in] Index the given index
135  @return the descriptor
136  @throws openfluid::base::FrameworkException Index out of bounds
137  */
138  I& itemAt(unsigned int Index) const
139  {
140  std::list<I*>& Items = mp_Descriptor->items();
141 
142  if (Index < Items.size())
143  {
144  typename std::list<I*>::iterator it = Items.begin();
145  std::advance(it, Index);
146 
147  return **it;
148  }
149  else
150  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
151  "Index out of bounds");
152  }
153 
154 
155  // =====================================================================
156  // =====================================================================
157 
158 
159  /**
160  Appends a pointer to ware descriptor at the end of the wares set descriptor
161  @param[in] Item the descriptor to add
162  */
163  void appendItem(I* Item)
164  {
165  if (Item)
166  mp_Descriptor->appendItem(Item);
167  }
168 
169 
170  // =====================================================================
171  // =====================================================================
172 
173  /**
174  Inserts a pointer to ware descriptor at a given index in the wares set descriptor
175  @param[in] Item the descriptor to insert
176  @param[in] Index the index where the descriptor is inserted
177  @throws openfluid::base::FrameworkException Index out of bounds
178  */
179  void insertItem(I* Item, unsigned int Index)
180  {
181  std::list<I*>& Items = mp_Descriptor->items();
182 
183  if (Index == 0)
184  Items.insert(Items.begin(), Item);
185  else if (Index < Items.size())
186  {
187  typename std::list<I*>::iterator it = Items.begin();
188  std::advance(it, Index);
189 
190  Items.insert(it, Item);
191  }
192  else
193  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
194  "Index out of bounds");
195  }
196 
197 
198  // =====================================================================
199  // =====================================================================
200 
201 
202  /**
203  Removes the ware descriptor at a given index from the wares set descriptor
204  @param[in] Index the index of the descriptor to remove
205  @throws openfluid::base::FrameworkException Index out of bounds
206  */
207  void removeItem(unsigned int Index)
208  {
209  std::list<I*>& Items = mp_Descriptor->items();
210 
211  if (Index < Items.size())
212  {
213  typename std::list<I*>::iterator it = Items.begin();
214  std::advance(it, Index);
215 
216  Items.erase(it);
217  }
218  else
219  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
220  "Index out of bounds");
221  }
222 
223 
224  // =====================================================================
225  // =====================================================================
226 
227 
228  /**
229  Moves a ware descriptor at a given index to another index in the wares set descriptor
230  @param[in] indexFrom initial index of the descriptor
231  @param[in] indexTo destination index of the descriptor
232  @throws openfluid::base::FrameworkException Index out of bounds
233  */
234  void moveItem(unsigned int indexFrom, unsigned int indexTo)
235  {
236  if (indexFrom == indexTo)
237  return;
238 
239  std::list<I*>& Items = mp_Descriptor->items();
240 
241  unsigned int Last = Items.size() - 1;
242 
243  if (indexFrom > Last || indexTo > Last)
244  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
245  "Index out of bounds");
246 
247  typename std::list<I*>::const_iterator itFrom = Items.begin();
248  std::advance(itFrom, indexFrom);
249 
250  I* Item = *itFrom;
251 
252  removeItem(indexFrom);
253 
254  if (indexTo == Last)
255  appendItem(Item);
256  else
257  insertItem(Item, indexTo);
258  }
259 
260 
261  // =====================================================================
262  // =====================================================================
263 
264 
265  /**
266  Finds the index of the first ware descriptor with the given ID
267  @param[in] ID the ID of the searched descriptor
268  @return the index of the searched descriptor, -1 otherwise
269  */
271  {
272  std::list<I*>& Items = mp_Descriptor->items();
273 
274  for (typename std::list<I*>::iterator it = Items.begin(); it != Items.end(); ++it)
275  {
276  if (getID(*it) == ID)
277  return std::distance(Items.begin(), it);
278  }
279 
280  return -1;
281  }
282 
283 
284  // =====================================================================
285  // =====================================================================
286 
287  /**
288  Finds the index of the first ware descriptor corresponding to the given descriptor
289  @param[in] Item the searched descriptor
290  @return the index of the searched descriptor, -1 otherwise
291  */
292  int findFirstItem(const I* Item) const
293  {
294  std::list<I*>& Items = mp_Descriptor->items();
295 
296  typename std::list<I*>::iterator it = std::find(Items.begin(), Items.end(), Item);
297 
298  if (it != Items.end())
299  return std::distance(Items.begin(), it);
300 
301  return -1;
302  }
303 
304 
305  // =====================================================================
306  // =====================================================================
307 
308 
309  /**
310  Returns an ordered vector of IDs of wares contained in the wares set descriptor
311  @return the vector of IDs
312  */
313  std::vector<openfluid::ware::WareID_t> getOrderedIDs() const
314  {
315  std::vector<std::string> IDs;
316 
317  std::list<I*>& Items = mp_Descriptor->items();
318 
319  for (typename std::list<I*>::const_iterator it = Items.begin(); it != Items.end(); ++it)
320  IDs.push_back(getID(*it));
321 
322  return IDs;
323  }
324 
325 
326  // =====================================================================
327  // =====================================================================
328 
329  /**
330  Returns the number of ware descriptors in the wares set descriptor
331  @return the number of ware descriptors
332  */
334  {
335  return mp_Descriptor->items().size();
336  }
337 
338 
339  // =====================================================================
340  // =====================================================================
341 
342 
345  {
346  mp_Descriptor->setGlobalParameter(Key, Value);
347  }
348 
349 
350  // =====================================================================
351  // =====================================================================
352 
353 
355  {
356  mp_Descriptor->setGlobalParameters(Params);
357  }
358 
359 
360  // =====================================================================
361  // =====================================================================
362 
363 
365  {
366  return mp_Descriptor->getGlobalParameters();
367  }
368 
369 
370  // =====================================================================
371  // =====================================================================
372 
373 
375  {
376  mp_Descriptor->eraseGlobalParameter(Key);
377  }
378 };
379 
380 
381 } }
382 
383 
384 #endif /* __OPENFLUID_FLUIDX_ADVANCEDWARESETDESCRIPTOR_HPP__ */
int getItemsCount()
Definition: AdvancedWareSetDescriptor.hpp:333
void setGlobalParameter(const openfluid::ware::WareParamKey_t &Key, const openfluid::ware::WareParamValue_t &Value)
Definition: AdvancedWareSetDescriptor.hpp:343
Definition: FrameworkException.hpp:50
Definition: StringValue.hpp:91
int findFirstItem(const I *Item) const
Definition: AdvancedWareSetDescriptor.hpp:292
openfluid::ware::WareParams_t getGlobalParameters() const
Definition: AdvancedWareSetDescriptor.hpp:364
#define OPENFLUID_API
Definition: dllexport.hpp:87
virtual ~AdvancedWareSetDescriptor()
Definition: AdvancedWareSetDescriptor.hpp:75
std::string WareID_t
Definition: TypeDefs.hpp:50
void moveItem(unsigned int indexFrom, unsigned int indexTo)
Definition: AdvancedWareSetDescriptor.hpp:234
const std::list< I * > & items() const
Definition: AdvancedWareSetDescriptor.hpp:108
int findFirstItem(const openfluid::ware::WareID_t &ID) const
Definition: AdvancedWareSetDescriptor.hpp:270
void setItems(const std::list< I *> &Items)
Definition: AdvancedWareSetDescriptor.hpp:122
Definition: ApplicationException.hpp:47
void removeItem(unsigned int Index)
Definition: AdvancedWareSetDescriptor.hpp:207
M * mp_Descriptor
Definition: AdvancedWareSetDescriptor.hpp:58
void eraseGlobalParameter(const openfluid::ware::WareParamKey_t &Key)
Definition: AdvancedWareSetDescriptor.hpp:374
std::string WareParamKey_t
Definition: TypeDefs.hpp:126
AdvancedWareSetDescriptor(M &Desc)
Definition: AdvancedWareSetDescriptor.hpp:64
void insertItem(I *Item, unsigned int Index)
Definition: AdvancedWareSetDescriptor.hpp:179
std::vector< openfluid::ware::WareID_t > getOrderedIDs() const
Definition: AdvancedWareSetDescriptor.hpp:313
void appendItem(I *Item)
Definition: AdvancedWareSetDescriptor.hpp:163
void setGlobalParameters(const openfluid::ware::WareParams_t &Params)
Definition: AdvancedWareSetDescriptor.hpp:354
std::map< WareParamKey_t, WareParamValue_t > WareParams_t
Definition: TypeDefs.hpp:130
I & itemAt(unsigned int Index) const
Definition: AdvancedWareSetDescriptor.hpp:138
Definition: AdvancedWareSetDescriptor.hpp:54