1 /**
2  * @file ListOfSampledVolumes.cpp
3  * @brief Implementation of the ListOfSampledVolumes class.
4  * @author SBMLTeam
5  *
6  * <!--------------------------------------------------------------------------
7  * This file is part of libSBML. Please visit http://sbml.org for more
8  * information about SBML, and the latest version of libSBML.
9  *
10  * Copyright (C) 2020 jointly by the following organizations:
11  *     1. California Institute of Technology, Pasadena, CA, USA
12  *     2. University of Heidelberg, Heidelberg, Germany
13  *     3. University College London, London, UK
14  *
15  * Copyright (C) 2019 jointly by the following organizations:
16  * 1. California Institute of Technology, Pasadena, CA, USA
17  * 2. University of Heidelberg, Heidelberg, Germany
18  *
19  * Copyright (C) 2013-2018 jointly by the following organizations:
20  * 1. California Institute of Technology, Pasadena, CA, USA
21  * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
22  * 3. University of Heidelberg, Heidelberg, Germany
23  *
24  * Copyright (C) 2009-2013 jointly by the following organizations:
25  * 1. California Institute of Technology, Pasadena, CA, USA
26  * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
27  *
28  * Copyright (C) 2006-2008 by the California Institute of Technology,
29  * Pasadena, CA, USA
30  *
31  * Copyright (C) 2002-2005 jointly by the following organizations:
32  * 1. California Institute of Technology, Pasadena, CA, USA
33  * 2. Japan Science and Technology Agency, Japan
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the GNU Lesser General Public License as published by the
37  * Free Software Foundation. A copy of the license agreement is provided in the
38  * file named "LICENSE.txt" included with this software distribution and also
39  * available online as http://sbml.org/software/libsbml/license.html
40  * ------------------------------------------------------------------------ -->
41  */
42 #include <sbml/packages/spatial/sbml/ListOfSampledVolumes.h>
43 #include <sbml/packages/spatial/validator/SpatialSBMLError.h>
44 
45 
46 using namespace std;
47 
48 
49 
50 LIBSBML_CPP_NAMESPACE_BEGIN
51 
52 
53 
54 
55 #ifdef __cplusplus
56 
57 
58 /*
59  * Creates a new ListOfSampledVolumes using the given SBML Level, Version and
60  * &ldquo;spatial&rdquo; package version.
61  */
ListOfSampledVolumes(unsigned int level,unsigned int version,unsigned int pkgVersion)62 ListOfSampledVolumes::ListOfSampledVolumes(unsigned int level,
63                                            unsigned int version,
64                                            unsigned int pkgVersion)
65   : ListOf(level, version)
66 {
67   setSBMLNamespacesAndOwn(new SpatialPkgNamespaces(level, version,
68     pkgVersion));
69 }
70 
71 
72 /*
73  * Creates a new ListOfSampledVolumes using the given SpatialPkgNamespaces
74  * object.
75  */
ListOfSampledVolumes(SpatialPkgNamespaces * spatialns)76 ListOfSampledVolumes::ListOfSampledVolumes(SpatialPkgNamespaces *spatialns)
77   : ListOf(spatialns)
78 {
79   setElementNamespace(spatialns->getURI());
80 }
81 
82 
83 /*
84  * Copy constructor for ListOfSampledVolumes.
85  */
ListOfSampledVolumes(const ListOfSampledVolumes & orig)86 ListOfSampledVolumes::ListOfSampledVolumes(const ListOfSampledVolumes& orig)
87   : ListOf( orig )
88 {
89 }
90 
91 
92 /*
93  * Assignment operator for ListOfSampledVolumes.
94  */
95 ListOfSampledVolumes&
operator =(const ListOfSampledVolumes & rhs)96 ListOfSampledVolumes::operator=(const ListOfSampledVolumes& rhs)
97 {
98   if (&rhs != this)
99   {
100     ListOf::operator=(rhs);
101   }
102 
103   return *this;
104 }
105 
106 
107 /*
108  * Creates and returns a deep copy of this ListOfSampledVolumes object.
109  */
110 ListOfSampledVolumes*
clone() const111 ListOfSampledVolumes::clone() const
112 {
113   return new ListOfSampledVolumes(*this);
114 }
115 
116 
117 /*
118  * Destructor for ListOfSampledVolumes.
119  */
~ListOfSampledVolumes()120 ListOfSampledVolumes::~ListOfSampledVolumes()
121 {
122 }
123 
124 
125 /*
126  * Get a SampledVolume from the ListOfSampledVolumes.
127  */
128 SampledVolume*
get(unsigned int n)129 ListOfSampledVolumes::get(unsigned int n)
130 {
131   return static_cast<SampledVolume*>(ListOf::get(n));
132 }
133 
134 
135 /*
136  * Get a SampledVolume from the ListOfSampledVolumes.
137  */
138 const SampledVolume*
get(unsigned int n) const139 ListOfSampledVolumes::get(unsigned int n) const
140 {
141   return static_cast<const SampledVolume*>(ListOf::get(n));
142 }
143 
144 
145 /*
146  * Get a SampledVolume from the ListOfSampledVolumes based on its identifier.
147  */
148 SampledVolume*
get(const std::string & sid)149 ListOfSampledVolumes::get(const std::string& sid)
150 {
151   return const_cast<SampledVolume*>(static_cast<const
152     ListOfSampledVolumes&>(*this).get(sid));
153 }
154 
155 
156 /*
157  * Get a SampledVolume from the ListOfSampledVolumes based on its identifier.
158  */
159 const SampledVolume*
get(const std::string & sid) const160 ListOfSampledVolumes::get(const std::string& sid) const
161 {
162   vector<SBase*>::const_iterator result;
163   result = find_if(mItems.begin(), mItems.end(), IdEq<SampledVolume>(sid));
164   return (result == mItems.end()) ? 0 : static_cast <const SampledVolume*>
165     (*result);
166 }
167 
168 
169 /*
170  * Removes the nth SampledVolume from this ListOfSampledVolumes and returns a
171  * pointer to it.
172  */
173 SampledVolume*
remove(unsigned int n)174 ListOfSampledVolumes::remove(unsigned int n)
175 {
176   return static_cast<SampledVolume*>(ListOf::remove(n));
177 }
178 
179 
180 /*
181  * Removes the SampledVolume from this ListOfSampledVolumes based on its
182  * identifier and returns a pointer to it.
183  */
184 SampledVolume*
remove(const std::string & sid)185 ListOfSampledVolumes::remove(const std::string& sid)
186 {
187   SBase* item = NULL;
188   vector<SBase*>::iterator result;
189 
190   result = find_if(mItems.begin(), mItems.end(), IdEq<SampledVolume>(sid));
191 
192   if (result != mItems.end())
193   {
194     item = *result;
195     mItems.erase(result);
196   }
197 
198   return static_cast <SampledVolume*> (item);
199 }
200 
201 
202 /*
203  * Adds a copy of the given SampledVolume to this ListOfSampledVolumes.
204  */
205 int
addSampledVolume(const SampledVolume * sv)206 ListOfSampledVolumes::addSampledVolume(const SampledVolume* sv)
207 {
208   if (sv == NULL)
209   {
210     return LIBSBML_OPERATION_FAILED;
211   }
212   else if (sv->hasRequiredAttributes() == false)
213   {
214     return LIBSBML_INVALID_OBJECT;
215   }
216   else if (getLevel() != sv->getLevel())
217   {
218     return LIBSBML_LEVEL_MISMATCH;
219   }
220   else if (getVersion() != sv->getVersion())
221   {
222     return LIBSBML_VERSION_MISMATCH;
223   }
224   else if (matchesRequiredSBMLNamespacesForAddition(static_cast<const
225     SBase*>(sv)) == false)
226   {
227     return LIBSBML_NAMESPACES_MISMATCH;
228   }
229   else
230   {
231     return append(sv);
232   }
233 }
234 
235 
236 /*
237  * Get the number of SampledVolume objects in this ListOfSampledVolumes.
238  */
239 unsigned int
getNumSampledVolumes() const240 ListOfSampledVolumes::getNumSampledVolumes() const
241 {
242   return size();
243 }
244 
245 
246 /*
247  * Creates a new SampledVolume object, adds it to this ListOfSampledVolumes
248  * object and returns the SampledVolume object created.
249  */
250 SampledVolume*
createSampledVolume()251 ListOfSampledVolumes::createSampledVolume()
252 {
253   SampledVolume* sv = NULL;
254 
255   try
256   {
257     SPATIAL_CREATE_NS(spatialns, getSBMLNamespaces());
258     sv = new SampledVolume(spatialns);
259     delete spatialns;
260   }
261   catch (...)
262   {
263   }
264 
265   if (sv != NULL)
266   {
267     appendAndOwn(sv);
268   }
269 
270   return sv;
271 }
272 
273 
274 /*
275  * Used by ListOfSampledVolumes::get() to lookup a SampledVolume based on its
276  * DomainType.
277  */
278 struct IdEqDT5 : public std::unary_function<SBase*, bool>
279 {
280   const string& id;
281 
IdEqDT5IdEqDT5282   IdEqDT5 (const string& id) : id(id) { }
operator ()IdEqDT5283   bool operator() (SBase* sb)
284   {
285   return (static_cast<SampledVolume*>(sb)->getDomainType() == id);
286   }
287 };
288 
289 
290 /*
291  * Get a SampledVolume from the ListOfSampledVolumes based on the DomainType to
292  * which it refers.
293  */
294 const SampledVolume*
getByDomainType(const std::string & sid) const295 ListOfSampledVolumes::getByDomainType(const std::string& sid) const
296 {
297   vector<SBase*>::const_iterator result;
298   result = find_if(mItems.begin(), mItems.end(), IdEqDT5(sid));
299   return (result == mItems.end()) ? 0 : static_cast <const SampledVolume*>
300     (*result);
301 }
302 
303 
304 /*
305  * Get a SampledVolume from the ListOfSampledVolumes based on the DomainType to
306  * which it refers.
307  */
308 SampledVolume*
getByDomainType(const std::string & sid)309 ListOfSampledVolumes::getByDomainType(const std::string& sid)
310 {
311   return const_cast<SampledVolume*>(static_cast<const
312     ListOfSampledVolumes&>(*this).getByDomainType(sid));
313 }
314 
315 
316 /*
317  * Returns the XML element name of this ListOfSampledVolumes object.
318  */
319 const std::string&
getElementName() const320 ListOfSampledVolumes::getElementName() const
321 {
322   static const string name = "listOfSampledVolumes";
323   return name;
324 }
325 
326 
327 /*
328  * Returns the libSBML type code for this ListOfSampledVolumes object.
329  */
330 int
getTypeCode() const331 ListOfSampledVolumes::getTypeCode() const
332 {
333   return SBML_LIST_OF;
334 }
335 
336 
337 /*
338  * Returns the libSBML type code for the SBML objects contained in this
339  * ListOfSampledVolumes object.
340  */
341 int
getItemTypeCode() const342 ListOfSampledVolumes::getItemTypeCode() const
343 {
344   return SBML_SPATIAL_SAMPLEDVOLUME;
345 }
346 
347 
348 
349 /** @cond doxygenLibsbmlInternal */
350 
351 /*
352  * Creates a new SampledVolume in this ListOfSampledVolumes
353  */
354 SBase*
createObject(XMLInputStream & stream)355 ListOfSampledVolumes::createObject(XMLInputStream& stream)
356 {
357   const std::string& name = stream.peek().getName();
358   SBase* object = NULL;
359   SPATIAL_CREATE_NS(spatialns, getSBMLNamespaces());
360 
361   if (name == "sampledVolume")
362   {
363     object = new SampledVolume(spatialns);
364     appendAndOwn(object);
365   }
366 
367   delete spatialns;
368   return object;
369 }
370 
371 /** @endcond */
372 
373 
374 
375 /** @cond doxygenLibsbmlInternal */
376 
377 /*
378  * Writes the namespace for the Spatial package
379  */
380 void
writeXMLNS(XMLOutputStream & stream) const381 ListOfSampledVolumes::writeXMLNS(XMLOutputStream& stream) const
382 {
383   XMLNamespaces xmlns;
384   std::string prefix = getPrefix();
385 
386   if (prefix.empty())
387   {
388     const XMLNamespaces* thisxmlns = getNamespaces();
389     if (thisxmlns && thisxmlns->hasURI(SpatialExtension::getXmlnsL3V1V1()))
390     {
391       xmlns.add(SpatialExtension::getXmlnsL3V1V1(), prefix);
392     }
393   }
394 
395   stream << xmlns;
396 }
397 
398 /** @endcond */
399 
400 
401 
402 
403 #endif /* __cplusplus */
404 
405 
406 /*
407  * Get a SampledVolume_t from the ListOf_t.
408  */
409 LIBSBML_EXTERN
410 SampledVolume_t*
ListOfSampledVolumes_getSampledVolume(ListOf_t * lo,unsigned int n)411 ListOfSampledVolumes_getSampledVolume(ListOf_t* lo, unsigned int n)
412 {
413   if (lo == NULL)
414   {
415     return NULL;
416   }
417 
418   return static_cast <ListOfSampledVolumes*>(lo)->get(n);
419 }
420 
421 
422 /*
423  * Get a SampledVolume_t from the ListOf_t based on its identifier.
424  */
425 LIBSBML_EXTERN
426 SampledVolume_t*
ListOfSampledVolumes_getById(ListOf_t * lo,const char * sid)427 ListOfSampledVolumes_getById(ListOf_t* lo, const char *sid)
428 {
429   if (lo == NULL)
430   {
431     return NULL;
432   }
433 
434   return (sid != NULL) ? static_cast <ListOfSampledVolumes*>(lo)->get(sid) :
435     NULL;
436 }
437 
438 
439 /*
440  * Removes the nth SampledVolume_t from this ListOf_t and returns a pointer to
441  * it.
442  */
443 LIBSBML_EXTERN
444 SampledVolume_t*
ListOfSampledVolumes_remove(ListOf_t * lo,unsigned int n)445 ListOfSampledVolumes_remove(ListOf_t* lo, unsigned int n)
446 {
447   if (lo == NULL)
448   {
449     return NULL;
450   }
451 
452   return static_cast <ListOfSampledVolumes*>(lo)->remove(n);
453 }
454 
455 
456 /*
457  * Removes the SampledVolume_t from this ListOf_t based on its identifier and
458  * returns a pointer to it.
459  */
460 LIBSBML_EXTERN
461 SampledVolume_t*
ListOfSampledVolumes_removeById(ListOf_t * lo,const char * sid)462 ListOfSampledVolumes_removeById(ListOf_t* lo, const char* sid)
463 {
464   if (lo == NULL)
465   {
466     return NULL;
467   }
468 
469   return (sid != NULL) ? static_cast <ListOfSampledVolumes*>(lo)->remove(sid) :
470     NULL;
471 }
472 
473 
474 
475 
476 LIBSBML_CPP_NAMESPACE_END
477 
478 
479