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 
44 #include <openfluid/dllexport.hpp>
47 
48 #include <list>
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) return;
237 
238  std::list<I*>& Items = mp_Descriptor->items();
239 
240  unsigned int Last = Items.size() - 1;
241 
242  if (indexFrom > Last || indexTo > Last)
243  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
244  "Index out of bounds");
245 
246  typename std::list<I*>::const_iterator itFrom = Items.begin();
247  std::advance(itFrom, indexFrom);
248 
249  I* Item = *itFrom;
250 
251  removeItem(indexFrom);
252 
253  if (indexTo == Last)
254  appendItem(Item);
255  else
256  insertItem(Item, indexTo);
257  }
258 
259 
260  // =====================================================================
261  // =====================================================================
262 
263 
264  /**
265  Finds the index of the first ware descriptor with the given ID
266  @param[in] ID the ID of the searched descriptor
267  @return the index of the searched descriptor, -1 otherwise
268  */
270  {
271  std::list<I*>& Items = mp_Descriptor->items();
272 
273  for (typename std::list<I*>::iterator it = Items.begin(); it != Items.end(); ++it)
274  {
275  if (getID(*it) == ID)
276  return std::distance(Items.begin(), it);
277  }
278 
279  return -1;
280  }
281 
282 
283  // =====================================================================
284  // =====================================================================
285 
286  /**
287  Finds the index of the first ware descriptor corresponding to the given descriptor
288  @param[in] Item the searched descriptor
289  @return the index of the searched descriptor, -1 otherwise
290  */
291  int findFirstItem(const I* Item) const
292  {
293  std::list<I*>& Items = mp_Descriptor->items();
294 
295  typename std::list<I*>::iterator it = std::find(Items.begin(), Items.end(), Item);
296 
297  if (it != Items.end())
298  return std::distance(Items.begin(), it);
299 
300  return -1;
301  }
302 
303 
304  // =====================================================================
305  // =====================================================================
306 
307 
308  /**
309  Returns an ordered vector of IDs of wares contained in the wares set descriptor
310  @return the vector of IDs
311  */
312  std::vector<openfluid::ware::WareID_t> getOrderedIDs() const
313  {
314  std::vector<std::string> IDs;
315 
316  std::list<I*>& Items = mp_Descriptor->items();
317 
318  for (typename std::list<I*>::const_iterator it = Items.begin(); it != Items.end(); ++it)
319  IDs.push_back(getID(*it));
320 
321  return IDs;
322  }
323 
324 
325  // =====================================================================
326  // =====================================================================
327 
328  /**
329  Returns the number of ware descriptors in the wares set descriptor
330  @return the number of ware descriptors
331  */
333  {
334  return mp_Descriptor->items().size();
335  }
336 
337 
338  // =====================================================================
339  // =====================================================================
340 
341 
344  {
345  mp_Descriptor->setGlobalParameter(Key, Value);
346  }
347 
348 
349  // =====================================================================
350  // =====================================================================
351 
352 
354  {
355  mp_Descriptor->setGlobalParameters(Params);
356  }
357 
358 
359  // =====================================================================
360  // =====================================================================
361 
362 
364  {
365  return mp_Descriptor->getGlobalParameters();
366  }
367 
368 
369  // =====================================================================
370  // =====================================================================
371 
372 
374  {
375  mp_Descriptor->eraseGlobalParameter(Key);
376  }
377 };
378 
379 
380 } }
381 
382 #endif /* __OPENFLUID_FLUIDX_ADVANCEDWARESETDESCRIPTOR_HPP__ */
int findFirstItem(const openfluid::ware::WareID_t &ID) const
Definition: AdvancedWareSetDescriptor.hpp:269
Definition: AdvancedWareSetDescriptor.hpp:54
openfluid::ware::WareParams_t getGlobalParameters() const
Definition: AdvancedWareSetDescriptor.hpp:363
std::string WareID_t
Definition: TypeDefs.hpp:50
void moveItem(unsigned int indexFrom, unsigned int indexTo)
Definition: AdvancedWareSetDescriptor.hpp:234
void removeItem(unsigned int Index)
Definition: AdvancedWareSetDescriptor.hpp:207
const std::list< I * > & items() const
Definition: AdvancedWareSetDescriptor.hpp:108
void setGlobalParameter(const openfluid::ware::WareParamKey_t &Key, const openfluid::ware::WareParamValue_t &Value)
Definition: AdvancedWareSetDescriptor.hpp:342
void insertItem(I *Item, unsigned int Index)
Definition: AdvancedWareSetDescriptor.hpp:179
AdvancedWareSetDescriptor(M &Desc)
Definition: AdvancedWareSetDescriptor.hpp:64
void eraseGlobalParameter(const openfluid::ware::WareParamKey_t &Key)
Definition: AdvancedWareSetDescriptor.hpp:373
std::string WareParamKey_t
Definition: TypeDefs.hpp:126
void setItems(const std::list< I * > &Items)
Definition: AdvancedWareSetDescriptor.hpp:122
std::vector< openfluid::ware::WareID_t > getOrderedIDs() const
Definition: AdvancedWareSetDescriptor.hpp:312
void setGlobalParameters(const openfluid::ware::WareParams_t &Params)
Definition: AdvancedWareSetDescriptor.hpp:353
Definition: FrameworkException.hpp:50
Definition: ApplicationException.hpp:47
M * mp_Descriptor
Definition: AdvancedWareSetDescriptor.hpp:58
virtual ~AdvancedWareSetDescriptor()
Definition: AdvancedWareSetDescriptor.hpp:75
int getItemsCount()
Definition: AdvancedWareSetDescriptor.hpp:332
int findFirstItem(const I *Item) const
Definition: AdvancedWareSetDescriptor.hpp:291
#define OPENFLUID_API
Definition: dllexport.hpp:87
void appendItem(I *Item)
Definition: AdvancedWareSetDescriptor.hpp:163
Definition: StringValue.hpp:91
I & itemAt(unsigned int Index) const
Definition: AdvancedWareSetDescriptor.hpp:138
std::map< WareParamKey_t, WareParamValue_t > WareParams_t
Definition: TypeDefs.hpp:130