1 /**
2  * @file SedListOfSlices.cpp
3  * @brief Implementation of the SedListOfSlices 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/SedListOfSlices.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 SedListOfSlices using the given SED-ML Level and @ p version
52  * values.
53  */
SedListOfSlices(unsigned int level,unsigned int version)54 SedListOfSlices::SedListOfSlices(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 SedListOfSlices using the given SedNamespaces object @p
63  * sedmlns.
64  */
SedListOfSlices(SedNamespaces * sedmlns)65 SedListOfSlices::SedListOfSlices(SedNamespaces *sedmlns)
66   : SedListOf(sedmlns)
67 {
68   setElementNamespace(sedmlns->getURI());
69 }
70 
71 
72 /*
73  * Copy constructor for SedListOfSlices.
74  */
SedListOfSlices(const SedListOfSlices & orig)75 SedListOfSlices::SedListOfSlices(const SedListOfSlices& orig)
76   : SedListOf( orig )
77 {
78 }
79 
80 
81 /*
82  * Assignment operator for SedListOfSlices.
83  */
84 SedListOfSlices&
operator =(const SedListOfSlices & rhs)85 SedListOfSlices::operator=(const SedListOfSlices& 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 SedListOfSlices object.
98  */
99 SedListOfSlices*
clone() const100 SedListOfSlices::clone() const
101 {
102   return new SedListOfSlices(*this);
103 }
104 
105 
106 /*
107  * Destructor for SedListOfSlices.
108  */
~SedListOfSlices()109 SedListOfSlices::~SedListOfSlices()
110 {
111 }
112 
113 
114 /*
115  * Get a SedSlice from the SedListOfSlices.
116  */
117 SedSlice*
get(unsigned int n)118 SedListOfSlices::get(unsigned int n)
119 {
120   return static_cast<SedSlice*>(SedListOf::get(n));
121 }
122 
123 
124 /*
125  * Get a SedSlice from the SedListOfSlices.
126  */
127 const SedSlice*
get(unsigned int n) const128 SedListOfSlices::get(unsigned int n) const
129 {
130   return static_cast<const SedSlice*>(SedListOf::get(n));
131 }
132 
133 
134 /*
135  * Get a SedSlice from the SedListOfSlices based on its identifier.
136  */
137 SedSlice*
get(const std::string & sid)138 SedListOfSlices::get(const std::string& sid)
139 {
140   return const_cast<SedSlice*>(static_cast<const
141     SedListOfSlices&>(*this).get(sid));
142 }
143 
144 
145 /*
146  * Get a SedSlice from the SedListOfSlices based on its identifier.
147  */
148 const SedSlice*
get(const std::string & sid) const149 SedListOfSlices::get(const std::string& sid) const
150 {
151   vector<SedBase*>::const_iterator result;
152   result = find_if(mItems.begin(), mItems.end(), SedIdEq<SedSlice>(sid));
153   return (result == mItems.end()) ? 0 : static_cast <const SedSlice*>
154     (*result);
155 }
156 
157 
158 /*
159  * Removes the nth SedSlice from this SedListOfSlices and returns a pointer to
160  * it.
161  */
162 SedSlice*
remove(unsigned int n)163 SedListOfSlices::remove(unsigned int n)
164 {
165   return static_cast<SedSlice*>(SedListOf::remove(n));
166 }
167 
168 
169 /*
170  * Removes the SedSlice from this SedListOfSlices based on its identifier and
171  * returns a pointer to it.
172  */
173 SedSlice*
remove(const std::string & sid)174 SedListOfSlices::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<SedSlice>(sid));
180 
181   if (result != mItems.end())
182   {
183     item = *result;
184     mItems.erase(result);
185   }
186 
187   return static_cast <SedSlice*> (item);
188 }
189 
190 
191 /*
192  * Adds a copy of the given SedSlice to this SedListOfSlices.
193  */
194 int
addSlice(const SedSlice * ss)195 SedListOfSlices::addSlice(const SedSlice* ss)
196 {
197   if (ss == NULL)
198   {
199     return LIBSEDML_OPERATION_FAILED;
200   }
201   else if (ss->hasRequiredAttributes() == false)
202   {
203     return LIBSEDML_INVALID_OBJECT;
204   }
205   else if (getLevel() != ss->getLevel())
206   {
207     return LIBSEDML_LEVEL_MISMATCH;
208   }
209   else if (getVersion() != ss->getVersion())
210   {
211     return LIBSEDML_VERSION_MISMATCH;
212   }
213   else if (matchesRequiredSedNamespacesForAddition(static_cast<const
214     SedBase*>(ss)) == false)
215   {
216     return LIBSEDML_NAMESPACES_MISMATCH;
217   }
218   else
219   {
220     return append(ss);
221   }
222 }
223 
224 
225 /*
226  * Get the number of SedSlice objects in this SedListOfSlices.
227  */
228 unsigned int
getNumSlices() const229 SedListOfSlices::getNumSlices() const
230 {
231   return size();
232 }
233 
234 
235 /*
236  * Creates a new SedSlice object, adds it to this SedListOfSlices object and
237  * returns the SedSlice object created.
238  */
239 SedSlice*
createSlice()240 SedListOfSlices::createSlice()
241 {
242   SedSlice* ss = NULL;
243 
244   try
245   {
246     ss = new SedSlice(getSedNamespaces());
247   }
248   catch (...)
249   {
250   }
251 
252   if (ss != NULL)
253   {
254     appendAndOwn(ss);
255   }
256 
257   return ss;
258 }
259 
260 
261 /*
262  * Used by SedListOfSlices::get() to lookup a SedSlice based on its Reference.
263  */
264 struct SedIdEqR
265 {
266   const string& id;
267 
SedIdEqRSedIdEqR268   SedIdEqR (const string& id) : id(id) { }
operator ()SedIdEqR269   bool operator() (SedBase* sb)
270   {
271   return (static_cast<SedSlice*>(sb)->getReference() == id);
272   }
273 };
274 
275 
276 /*
277  * Get a SedSlice from the SedListOfSlices based on the Reference to which it
278  * refers.
279  */
280 const SedSlice*
getByReference(const std::string & sid) const281 SedListOfSlices::getByReference(const std::string& sid) const
282 {
283   vector<SedBase*>::const_iterator result;
284   result = find_if(mItems.begin(), mItems.end(), SedIdEqR(sid));
285   return (result == mItems.end()) ? 0 : static_cast <const SedSlice*>
286     (*result);
287 }
288 
289 
290 /*
291  * Get a SedSlice from the SedListOfSlices based on the Reference to which it
292  * refers.
293  */
294 SedSlice*
getByReference(const std::string & sid)295 SedListOfSlices::getByReference(const std::string& sid)
296 {
297   return const_cast<SedSlice*>(static_cast<const
298     SedListOfSlices&>(*this).getByReference(sid));
299 }
300 
301 
302 /*
303  * Used by SedListOfSlices::get() to lookup a SedSlice based on its Index.
304  */
305 struct SedIdEqI
306 {
307   const string& id;
308 
SedIdEqISedIdEqI309   SedIdEqI (const string& id) : id(id) { }
operator ()SedIdEqI310   bool operator() (SedBase* sb)
311   {
312   return (static_cast<SedSlice*>(sb)->getIndex() == id);
313   }
314 };
315 
316 
317 /*
318  * Get a SedSlice from the SedListOfSlices based on the Index to which it
319  * refers.
320  */
321 const SedSlice*
getByIndex(const std::string & sid) const322 SedListOfSlices::getByIndex(const std::string& sid) const
323 {
324   vector<SedBase*>::const_iterator result;
325   result = find_if(mItems.begin(), mItems.end(), SedIdEqI(sid));
326   return (result == mItems.end()) ? 0 : static_cast <const SedSlice*>
327     (*result);
328 }
329 
330 
331 /*
332  * Get a SedSlice from the SedListOfSlices based on the Index to which it
333  * refers.
334  */
335 SedSlice*
getByIndex(const std::string & sid)336 SedListOfSlices::getByIndex(const std::string& sid)
337 {
338   return const_cast<SedSlice*>(static_cast<const
339     SedListOfSlices&>(*this).getByIndex(sid));
340 }
341 
342 
343 /*
344  * Returns the XML element name of this SedListOfSlices object.
345  */
346 const std::string&
getElementName() const347 SedListOfSlices::getElementName() const
348 {
349   static const string name = "listOfSlices";
350   return name;
351 }
352 
353 
354 /*
355  * Returns the libSEDML type code for this SedListOfSlices object.
356  */
357 int
getTypeCode() const358 SedListOfSlices::getTypeCode() const
359 {
360   return SEDML_LIST_OF;
361 }
362 
363 
364 /*
365  * Returns the libSEDML type code for the SED-ML objects contained in this
366  * SedListOfSlices object.
367  */
368 int
getItemTypeCode() const369 SedListOfSlices::getItemTypeCode() const
370 {
371   return SEDML_DATA_SLICE;
372 }
373 
374 
375 
376 /** @cond doxygenLibSEDMLInternal */
377 
378 /*
379  * Creates a new SedSlice in this SedListOfSlices
380  */
381 SedBase*
createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream & stream)382 SedListOfSlices::createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream&
383   stream)
384 {
385   const std::string& name = stream.peek().getName();
386   SedBase* object = NULL;
387 
388   if (name == "slice")
389   {
390     object = new SedSlice(getSedNamespaces());
391     appendAndOwn(object);
392   }
393 
394   return object;
395 }
396 
397 /** @endcond */
398 
399 
400 
401 
402 #endif /* __cplusplus */
403 
404 
405 /*
406  * Get a SedSlice_t from the SedListOf_t.
407  */
408 LIBSEDML_EXTERN
409 SedSlice_t*
SedListOfSlices_getSlice(SedListOf_t * slo,unsigned int n)410 SedListOfSlices_getSlice(SedListOf_t* slo, unsigned int n)
411 {
412   if (slo == NULL)
413   {
414     return NULL;
415   }
416 
417   return static_cast <SedListOfSlices*>(slo)->get(n);
418 }
419 
420 
421 /*
422  * Get a SedSlice_t from the SedListOf_t based on its identifier.
423  */
424 LIBSEDML_EXTERN
425 SedSlice_t*
SedListOfSlices_getById(SedListOf_t * slo,const char * sid)426 SedListOfSlices_getById(SedListOf_t* slo, const char *sid)
427 {
428   if (slo == NULL)
429   {
430     return NULL;
431   }
432 
433   return (sid != NULL) ? static_cast <SedListOfSlices*>(slo)->get(sid) : NULL;
434 }
435 
436 
437 /*
438  * Removes the nth SedSlice_t from this SedListOf_t and returns a pointer to
439  * it.
440  */
441 LIBSEDML_EXTERN
442 SedSlice_t*
SedListOfSlices_remove(SedListOf_t * slo,unsigned int n)443 SedListOfSlices_remove(SedListOf_t* slo, unsigned int n)
444 {
445   if (slo == NULL)
446   {
447     return NULL;
448   }
449 
450   return static_cast <SedListOfSlices*>(slo)->remove(n);
451 }
452 
453 
454 /*
455  * Removes the SedSlice_t from this SedListOf_t based on its identifier and
456  * returns a pointer to it.
457  */
458 LIBSEDML_EXTERN
459 SedSlice_t*
SedListOfSlices_removeById(SedListOf_t * slo,const char * sid)460 SedListOfSlices_removeById(SedListOf_t* slo, const char* sid)
461 {
462   if (slo == NULL)
463   {
464     return NULL;
465   }
466 
467   return (sid != NULL) ? static_cast <SedListOfSlices*>(slo)->remove(sid) :
468     NULL;
469 }
470 
471 
472 
473 
474 LIBSEDML_CPP_NAMESPACE_END
475 
476 
477