1 /**
2  * @file SedListOfRanges.cpp
3  * @brief Implementation of the SedListOfRanges class.
4  * @author DEVISER
5  *
6  * <!--------------------------------------------------------------------------
7  * This file is part of libSEDML. Please visit http://sed-ml.org for more
8  * information about SED-ML. The latest version of libSEDML can be found on
9  * github: https://github.com/fbergmann/libSEDML/
10  *
11 
12  * Copyright (c) 2013-2019, Frank T. Bergmann
13  * All rights reserved.
14  *
15 
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions are met:
18  *
19 
20  * 1. Redistributions of source code must retain the above copyright notice,
21  * this
22  * list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  *
27  * This library is free software; you can redistribute it and/or modify it
28  * under the terms of the GNU Lesser General Public License as published by the
29  * Free Software Foundation. A copy of the license agreement is provided in the
30  * file named "LICENSE.txt" included with this software distribution and also
31  * available online as http://sbml.org/software/libsbml/license.html
32  * ------------------------------------------------------------------------ -->
33  */
34 #include <sedml/SedListOfRanges.h>
35 #include <sbml/xml/XMLInputStream.h>
36 
37 #include <sedml/SedUniformRange.h>
38 #include <sedml/SedVectorRange.h>
39 #include <sedml/SedFunctionalRange.h>
40 #include <sedml/SedDataRange.h>
41 
42 
43 using namespace std;
44 
45 
46 
47 LIBSEDML_CPP_NAMESPACE_BEGIN
48 
49 
50 
51 
52 #ifdef __cplusplus
53 
54 
55 /*
56  * Creates a new SedListOfRanges using the given SED-ML Level and @ p version
57  * values.
58  */
SedListOfRanges(unsigned int level,unsigned int version)59 SedListOfRanges::SedListOfRanges(unsigned int level, unsigned int version)
60   : SedListOf(level, version)
61   , mElementName("listOfRanges")
62 {
63   setSedNamespacesAndOwn(new SedNamespaces(level, version));
64 }
65 
66 
67 /*
68  * Creates a new SedListOfRanges using the given SedNamespaces object @p
69  * sedmlns.
70  */
SedListOfRanges(SedNamespaces * sedmlns)71 SedListOfRanges::SedListOfRanges(SedNamespaces *sedmlns)
72   : SedListOf(sedmlns)
73   , mElementName("listOfRanges")
74 {
75   setElementNamespace(sedmlns->getURI());
76 }
77 
78 
79 /*
80  * Copy constructor for SedListOfRanges.
81  */
SedListOfRanges(const SedListOfRanges & orig)82 SedListOfRanges::SedListOfRanges(const SedListOfRanges& orig)
83   : SedListOf( orig )
84   , mElementName ( orig.mElementName )
85 {
86 }
87 
88 
89 /*
90  * Assignment operator for SedListOfRanges.
91  */
92 SedListOfRanges&
operator =(const SedListOfRanges & rhs)93 SedListOfRanges::operator=(const SedListOfRanges& rhs)
94 {
95   if (&rhs != this)
96   {
97     SedListOf::operator=(rhs);
98     mElementName = rhs.mElementName;
99   }
100 
101   return *this;
102 }
103 
104 
105 /*
106  * Creates and returns a deep copy of this SedListOfRanges object.
107  */
108 SedListOfRanges*
clone() const109 SedListOfRanges::clone() const
110 {
111   return new SedListOfRanges(*this);
112 }
113 
114 
115 /*
116  * Destructor for SedListOfRanges.
117  */
~SedListOfRanges()118 SedListOfRanges::~SedListOfRanges()
119 {
120 }
121 
122 
123 /*
124  * Get a SedRange from the SedListOfRanges.
125  */
126 SedRange*
get(unsigned int n)127 SedListOfRanges::get(unsigned int n)
128 {
129   return static_cast<SedRange*>(SedListOf::get(n));
130 }
131 
132 
133 /*
134  * Get a SedRange from the SedListOfRanges.
135  */
136 const SedRange*
get(unsigned int n) const137 SedListOfRanges::get(unsigned int n) const
138 {
139   return static_cast<const SedRange*>(SedListOf::get(n));
140 }
141 
142 
143 /*
144  * Get a SedRange from the SedListOfRanges based on its identifier.
145  */
146 SedRange*
get(const std::string & sid)147 SedListOfRanges::get(const std::string& sid)
148 {
149   return const_cast<SedRange*>(static_cast<const
150     SedListOfRanges&>(*this).get(sid));
151 }
152 
153 
154 /*
155  * Get a SedRange from the SedListOfRanges based on its identifier.
156  */
157 const SedRange*
get(const std::string & sid) const158 SedListOfRanges::get(const std::string& sid) const
159 {
160   vector<SedBase*>::const_iterator result;
161   result = find_if(mItems.begin(), mItems.end(), SedIdEq<SedRange>(sid));
162   return (result == mItems.end()) ? 0 : static_cast <const SedRange*>
163     (*result);
164 }
165 
166 
167 /*
168  * Removes the nth SedRange from this SedListOfRanges and returns a pointer to
169  * it.
170  */
171 SedRange*
remove(unsigned int n)172 SedListOfRanges::remove(unsigned int n)
173 {
174   return static_cast<SedRange*>(SedListOf::remove(n));
175 }
176 
177 
178 /*
179  * Removes the SedRange from this SedListOfRanges based on its identifier and
180  * returns a pointer to it.
181  */
182 SedRange*
remove(const std::string & sid)183 SedListOfRanges::remove(const std::string& sid)
184 {
185   SedBase* item = NULL;
186   vector<SedBase*>::iterator result;
187 
188   result = find_if(mItems.begin(), mItems.end(), SedIdEq<SedRange>(sid));
189 
190   if (result != mItems.end())
191   {
192     item = *result;
193     mItems.erase(result);
194   }
195 
196   return static_cast <SedRange*> (item);
197 }
198 
199 
200 /*
201  * Adds a copy of the given SedRange to this SedListOfRanges.
202  */
203 int
addRange(const SedRange * sr)204 SedListOfRanges::addRange(const SedRange* sr)
205 {
206   if (sr == NULL)
207   {
208     return LIBSEDML_OPERATION_FAILED;
209   }
210   else if (sr->hasRequiredAttributes() == false)
211   {
212     return LIBSEDML_INVALID_OBJECT;
213   }
214   else if (getLevel() != sr->getLevel())
215   {
216     return LIBSEDML_LEVEL_MISMATCH;
217   }
218   else if (getVersion() != sr->getVersion())
219   {
220     return LIBSEDML_VERSION_MISMATCH;
221   }
222   else if (matchesRequiredSedNamespacesForAddition(static_cast<const
223     SedBase*>(sr)) == false)
224   {
225     return LIBSEDML_NAMESPACES_MISMATCH;
226   }
227   else
228   {
229     return append(sr);
230   }
231 }
232 
233 
234 /*
235  * Get the number of SedRange objects in this SedListOfRanges.
236  */
237 unsigned int
getNumRanges() const238 SedListOfRanges::getNumRanges() const
239 {
240   return size();
241 }
242 
243 
244 /*
245  * Creates a new SedUniformRange object, adds it to this SedListOfRanges object
246  * and returns the SedUniformRange object created.
247  */
248 SedUniformRange*
createUniformRange()249 SedListOfRanges::createUniformRange()
250 {
251   SedUniformRange* sur = NULL;
252 
253   try
254   {
255     sur = new SedUniformRange(getSedNamespaces());
256   }
257   catch (...)
258   {
259   }
260 
261   if (sur != NULL)
262   {
263     appendAndOwn(sur);
264   }
265 
266   return sur;
267 }
268 
269 
270 /*
271  * Creates a new SedVectorRange object, adds it to this SedListOfRanges object
272  * and returns the SedVectorRange object created.
273  */
274 SedVectorRange*
createVectorRange()275 SedListOfRanges::createVectorRange()
276 {
277   SedVectorRange* svr = NULL;
278 
279   try
280   {
281     svr = new SedVectorRange(getSedNamespaces());
282   }
283   catch (...)
284   {
285   }
286 
287   if (svr != NULL)
288   {
289     appendAndOwn(svr);
290   }
291 
292   return svr;
293 }
294 
295 
296 /*
297  * Creates a new SedFunctionalRange object, adds it to this SedListOfRanges
298  * object and returns the SedFunctionalRange object created.
299  */
300 SedFunctionalRange*
createFunctionalRange()301 SedListOfRanges::createFunctionalRange()
302 {
303   SedFunctionalRange* sfr = NULL;
304 
305   try
306   {
307     sfr = new SedFunctionalRange(getSedNamespaces());
308   }
309   catch (...)
310   {
311   }
312 
313   if (sfr != NULL)
314   {
315     appendAndOwn(sfr);
316   }
317 
318   return sfr;
319 }
320 
321 
322 /*
323  * Creates a new SedDataRange object, adds it to this SedListOfRanges object
324  * and returns the SedDataRange object created.
325  */
326 SedDataRange*
createDataRange()327 SedListOfRanges::createDataRange()
328 {
329   SedDataRange* sdr = NULL;
330 
331   try
332   {
333     sdr = new SedDataRange(getSedNamespaces());
334   }
335   catch (...)
336   {
337   }
338 
339   if (sdr != NULL)
340   {
341     appendAndOwn(sdr);
342   }
343 
344   return sdr;
345 }
346 
347 
348 /*
349  * Returns the XML element name of this SedListOfRanges object.
350  */
351 const std::string&
getElementName() const352 SedListOfRanges::getElementName() const
353 {
354   return mElementName;
355 }
356 
357 
358 
359 /** @cond doxygenLibSEDMLInternal */
360 
361 /*
362  * Sets the XML name of this SedListOfRanges object.
363  */
364 void
setElementName(const std::string & name)365 SedListOfRanges::setElementName(const std::string& name)
366 {
367   mElementName = name;
368 }
369 
370 /** @endcond */
371 
372 
373 /*
374  * Returns the libSEDML type code for this SedListOfRanges object.
375  */
376 int
getTypeCode() const377 SedListOfRanges::getTypeCode() const
378 {
379   return SEDML_LIST_OF;
380 }
381 
382 
383 /*
384  * Returns the libSEDML type code for the SED-ML objects contained in this
385  * SedListOfRanges object.
386  */
387 int
getItemTypeCode() const388 SedListOfRanges::getItemTypeCode() const
389 {
390   return SEDML_RANGE;
391 }
392 
393 
394 
395 /** @cond doxygenLibSEDMLInternal */
396 
397 /*
398  * Creates a new SedRange in this SedListOfRanges
399  */
400 SedBase*
createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream & stream)401 SedListOfRanges::createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream&
402   stream)
403 {
404   const std::string& name = stream.peek().getName();
405   SedBase* object = NULL;
406 
407   if (name == "range")
408   {
409     object = new SedRange(getSedNamespaces());
410     appendAndOwn(object);
411   }
412 
413   if (name == "uniformRange")
414   {
415     object = new SedUniformRange(getSedNamespaces());
416     appendAndOwn(object);
417   }
418 
419   if (name == "vectorRange")
420   {
421     object = new SedVectorRange(getSedNamespaces());
422     appendAndOwn(object);
423   }
424 
425   if (name == "functionalRange")
426   {
427     object = new SedFunctionalRange(getSedNamespaces());
428     appendAndOwn(object);
429   }
430 
431   if (name == "dataRange")
432   {
433     object = new SedDataRange(getSedNamespaces());
434     appendAndOwn(object);
435   }
436 
437   return object;
438 }
439 
440 /** @endcond */
441 
442 
443 
444 /** @cond doxygenLibSEDMLInternal */
445 
446 /*
447  * checks concrete types
448  */
449 bool
isValidTypeForList(SedBase * item)450 SedListOfRanges::isValidTypeForList(SedBase* item)
451 {
452   unsigned int tc = item->getTypeCode();
453 
454   return ((tc == SEDML_RANGE_UNIFORMRANGE) || (tc == SEDML_RANGE_VECTORRANGE)
455     || (tc == SEDML_RANGE_FUNCTIONALRANGE) || (tc == SEDML_DATA_RANGE));
456 }
457 
458 /** @endcond */
459 
460 
461 
462 
463 #endif /* __cplusplus */
464 
465 
466 /*
467  * Get a SedRange_t from the SedListOf_t.
468  */
469 LIBSEDML_EXTERN
470 SedRange_t*
SedListOfRanges_getRange(SedListOf_t * slo,unsigned int n)471 SedListOfRanges_getRange(SedListOf_t* slo, unsigned int n)
472 {
473   if (slo == NULL)
474   {
475     return NULL;
476   }
477 
478   return static_cast <SedListOfRanges*>(slo)->get(n);
479 }
480 
481 
482 /*
483  * Get a SedRange_t from the SedListOf_t based on its identifier.
484  */
485 LIBSEDML_EXTERN
486 SedRange_t*
SedListOfRanges_getById(SedListOf_t * slo,const char * sid)487 SedListOfRanges_getById(SedListOf_t* slo, const char *sid)
488 {
489   if (slo == NULL)
490   {
491     return NULL;
492   }
493 
494   return (sid != NULL) ? static_cast <SedListOfRanges*>(slo)->get(sid) : NULL;
495 }
496 
497 
498 /*
499  * Removes the nth SedRange_t from this SedListOf_t and returns a pointer to
500  * it.
501  */
502 LIBSEDML_EXTERN
503 SedRange_t*
SedListOfRanges_remove(SedListOf_t * slo,unsigned int n)504 SedListOfRanges_remove(SedListOf_t* slo, unsigned int n)
505 {
506   if (slo == NULL)
507   {
508     return NULL;
509   }
510 
511   return static_cast <SedListOfRanges*>(slo)->remove(n);
512 }
513 
514 
515 /*
516  * Removes the SedRange_t from this SedListOf_t based on its identifier and
517  * returns a pointer to it.
518  */
519 LIBSEDML_EXTERN
520 SedRange_t*
SedListOfRanges_removeById(SedListOf_t * slo,const char * sid)521 SedListOfRanges_removeById(SedListOf_t* slo, const char* sid)
522 {
523   if (slo == NULL)
524   {
525     return NULL;
526   }
527 
528   return (sid != NULL) ? static_cast <SedListOfRanges*>(slo)->remove(sid) :
529     NULL;
530 }
531 
532 
533 
534 
535 LIBSEDML_CPP_NAMESPACE_END
536 
537 
538