1 /**
2  * @file SedListOfModels.cpp
3  * @brief Implementation of the SedListOfModels 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/SedListOfModels.h>
35 #include <sbml/xml/XMLInputStream.h>
36 
37 
38 using namespace std;
39 
40 
41 
42 LIBSEDML_CPP_NAMESPACE_BEGIN
43 
44 
45 
46 
47 #ifdef __cplusplus
48 
49 
50 /*
51  * Creates a new SedListOfModels using the given SED-ML Level and @ p version
52  * values.
53  */
SedListOfModels(unsigned int level,unsigned int version)54 SedListOfModels::SedListOfModels(unsigned int level, unsigned int version)
55   : SedListOf(level, version)
56 {
57   setSedNamespacesAndOwn(new SedNamespaces(level, version));
58 }
59 
60 
61 /*
62  * Creates a new SedListOfModels using the given SedNamespaces object @p
63  * sedmlns.
64  */
SedListOfModels(SedNamespaces * sedmlns)65 SedListOfModels::SedListOfModels(SedNamespaces *sedmlns)
66   : SedListOf(sedmlns)
67 {
68   setElementNamespace(sedmlns->getURI());
69 }
70 
71 
72 /*
73  * Copy constructor for SedListOfModels.
74  */
SedListOfModels(const SedListOfModels & orig)75 SedListOfModels::SedListOfModels(const SedListOfModels& orig)
76   : SedListOf( orig )
77 {
78 }
79 
80 
81 /*
82  * Assignment operator for SedListOfModels.
83  */
84 SedListOfModels&
operator =(const SedListOfModels & rhs)85 SedListOfModels::operator=(const SedListOfModels& rhs)
86 {
87   if (&rhs != this)
88   {
89     SedListOf::operator=(rhs);
90   }
91 
92   return *this;
93 }
94 
95 
96 /*
97  * Creates and returns a deep copy of this SedListOfModels object.
98  */
99 SedListOfModels*
clone() const100 SedListOfModels::clone() const
101 {
102   return new SedListOfModels(*this);
103 }
104 
105 
106 /*
107  * Destructor for SedListOfModels.
108  */
~SedListOfModels()109 SedListOfModels::~SedListOfModels()
110 {
111 }
112 
113 
114 /*
115  * Get a SedModel from the SedListOfModels.
116  */
117 SedModel*
get(unsigned int n)118 SedListOfModels::get(unsigned int n)
119 {
120   return static_cast<SedModel*>(SedListOf::get(n));
121 }
122 
123 
124 /*
125  * Get a SedModel from the SedListOfModels.
126  */
127 const SedModel*
get(unsigned int n) const128 SedListOfModels::get(unsigned int n) const
129 {
130   return static_cast<const SedModel*>(SedListOf::get(n));
131 }
132 
133 
134 /*
135  * Get a SedModel from the SedListOfModels based on its identifier.
136  */
137 SedModel*
get(const std::string & sid)138 SedListOfModels::get(const std::string& sid)
139 {
140   return const_cast<SedModel*>(static_cast<const
141     SedListOfModels&>(*this).get(sid));
142 }
143 
144 
145 /*
146  * Get a SedModel from the SedListOfModels based on its identifier.
147  */
148 const SedModel*
get(const std::string & sid) const149 SedListOfModels::get(const std::string& sid) const
150 {
151   vector<SedBase*>::const_iterator result;
152   result = find_if(mItems.begin(), mItems.end(), SedIdEq<SedModel>(sid));
153   return (result == mItems.end()) ? 0 : static_cast <const SedModel*>
154     (*result);
155 }
156 
157 
158 /*
159  * Removes the nth SedModel from this SedListOfModels and returns a pointer to
160  * it.
161  */
162 SedModel*
remove(unsigned int n)163 SedListOfModels::remove(unsigned int n)
164 {
165   return static_cast<SedModel*>(SedListOf::remove(n));
166 }
167 
168 
169 /*
170  * Removes the SedModel from this SedListOfModels based on its identifier and
171  * returns a pointer to it.
172  */
173 SedModel*
remove(const std::string & sid)174 SedListOfModels::remove(const std::string& sid)
175 {
176   SedBase* item = NULL;
177   vector<SedBase*>::iterator result;
178 
179   result = find_if(mItems.begin(), mItems.end(), SedIdEq<SedModel>(sid));
180 
181   if (result != mItems.end())
182   {
183     item = *result;
184     mItems.erase(result);
185   }
186 
187   return static_cast <SedModel*> (item);
188 }
189 
190 
191 /*
192  * Adds a copy of the given SedModel to this SedListOfModels.
193  */
194 int
addModel(const SedModel * sm)195 SedListOfModels::addModel(const SedModel* sm)
196 {
197   if (sm == NULL)
198   {
199     return LIBSEDML_OPERATION_FAILED;
200   }
201   else if (sm->hasRequiredAttributes() == false)
202   {
203     return LIBSEDML_INVALID_OBJECT;
204   }
205   else if (getLevel() != sm->getLevel())
206   {
207     return LIBSEDML_LEVEL_MISMATCH;
208   }
209   else if (getVersion() != sm->getVersion())
210   {
211     return LIBSEDML_VERSION_MISMATCH;
212   }
213   else if (matchesRequiredSedNamespacesForAddition(static_cast<const
214     SedBase*>(sm)) == false)
215   {
216     return LIBSEDML_NAMESPACES_MISMATCH;
217   }
218   else
219   {
220     return append(sm);
221   }
222 }
223 
224 
225 /*
226  * Get the number of SedModel objects in this SedListOfModels.
227  */
228 unsigned int
getNumModels() const229 SedListOfModels::getNumModels() const
230 {
231   return size();
232 }
233 
234 
235 /*
236  * Creates a new SedModel object, adds it to this SedListOfModels object and
237  * returns the SedModel object created.
238  */
239 SedModel*
createModel()240 SedListOfModels::createModel()
241 {
242   SedModel* sm = NULL;
243 
244   try
245   {
246     sm = new SedModel(getSedNamespaces());
247   }
248   catch (...)
249   {
250   }
251 
252   if (sm != NULL)
253   {
254     appendAndOwn(sm);
255   }
256 
257   return sm;
258 }
259 
260 
261 /*
262  * Returns the XML element name of this SedListOfModels object.
263  */
264 const std::string&
getElementName() const265 SedListOfModels::getElementName() const
266 {
267   static const string name = "listOfModels";
268   return name;
269 }
270 
271 
272 /*
273  * Returns the libSEDML type code for this SedListOfModels object.
274  */
275 int
getTypeCode() const276 SedListOfModels::getTypeCode() const
277 {
278   return SEDML_LIST_OF;
279 }
280 
281 
282 /*
283  * Returns the libSEDML type code for the SED-ML objects contained in this
284  * SedListOfModels object.
285  */
286 int
getItemTypeCode() const287 SedListOfModels::getItemTypeCode() const
288 {
289   return SEDML_MODEL;
290 }
291 
292 
293 
294 /** @cond doxygenLibSEDMLInternal */
295 
296 /*
297  * Creates a new SedModel in this SedListOfModels
298  */
299 SedBase*
createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream & stream)300 SedListOfModels::createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream&
301   stream)
302 {
303   const std::string& name = stream.peek().getName();
304   SedBase* object = NULL;
305 
306   if (name == "model")
307   {
308     object = new SedModel(getSedNamespaces());
309     appendAndOwn(object);
310   }
311 
312   return object;
313 }
314 
315 /** @endcond */
316 
317 
318 
319 
320 #endif /* __cplusplus */
321 
322 
323 /*
324  * Get a SedModel_t from the SedListOf_t.
325  */
326 LIBSEDML_EXTERN
327 SedModel_t*
SedListOfModels_getModel(SedListOf_t * slo,unsigned int n)328 SedListOfModels_getModel(SedListOf_t* slo, unsigned int n)
329 {
330   if (slo == NULL)
331   {
332     return NULL;
333   }
334 
335   return static_cast <SedListOfModels*>(slo)->get(n);
336 }
337 
338 
339 /*
340  * Get a SedModel_t from the SedListOf_t based on its identifier.
341  */
342 LIBSEDML_EXTERN
343 SedModel_t*
SedListOfModels_getById(SedListOf_t * slo,const char * sid)344 SedListOfModels_getById(SedListOf_t* slo, const char *sid)
345 {
346   if (slo == NULL)
347   {
348     return NULL;
349   }
350 
351   return (sid != NULL) ? static_cast <SedListOfModels*>(slo)->get(sid) : NULL;
352 }
353 
354 
355 /*
356  * Removes the nth SedModel_t from this SedListOf_t and returns a pointer to
357  * it.
358  */
359 LIBSEDML_EXTERN
360 SedModel_t*
SedListOfModels_remove(SedListOf_t * slo,unsigned int n)361 SedListOfModels_remove(SedListOf_t* slo, unsigned int n)
362 {
363   if (slo == NULL)
364   {
365     return NULL;
366   }
367 
368   return static_cast <SedListOfModels*>(slo)->remove(n);
369 }
370 
371 
372 /*
373  * Removes the SedModel_t from this SedListOf_t based on its identifier and
374  * returns a pointer to it.
375  */
376 LIBSEDML_EXTERN
377 SedModel_t*
SedListOfModels_removeById(SedListOf_t * slo,const char * sid)378 SedListOfModels_removeById(SedListOf_t* slo, const char* sid)
379 {
380   if (slo == NULL)
381   {
382     return NULL;
383   }
384 
385   return (sid != NULL) ? static_cast <SedListOfModels*>(slo)->remove(sid) :
386     NULL;
387 }
388 
389 
390 
391 
392 LIBSEDML_CPP_NAMESPACE_END
393 
394 
395