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