1 /**
2  * @file SedListOfStyles.cpp
3  * @brief Implementation of the SedListOfStyles 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/SedListOfStyles.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 SedListOfStyles using the given SED-ML Level and @ p version
52  * values.
53  */
SedListOfStyles(unsigned int level,unsigned int version)54 SedListOfStyles::SedListOfStyles(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 SedListOfStyles using the given SedNamespaces object @p
63  * sedMLns.
64  */
SedListOfStyles(SedNamespaces * sedMLns)65 SedListOfStyles::SedListOfStyles(SedNamespaces *sedMLns)
66   : SedListOf(sedMLns)
67 {
68   setElementNamespace(sedMLns->getURI());
69 }
70 
71 
72 /*
73  * Copy constructor for SedListOfStyles.
74  */
SedListOfStyles(const SedListOfStyles & orig)75 SedListOfStyles::SedListOfStyles(const SedListOfStyles& orig)
76   : SedListOf( orig )
77 {
78 }
79 
80 
81 /*
82  * Assignment operator for SedListOfStyles.
83  */
84 SedListOfStyles&
operator =(const SedListOfStyles & rhs)85 SedListOfStyles::operator=(const SedListOfStyles& 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 SedListOfStyles object.
98  */
99 SedListOfStyles*
clone() const100 SedListOfStyles::clone() const
101 {
102   return new SedListOfStyles(*this);
103 }
104 
105 
106 /*
107  * Destructor for SedListOfStyles.
108  */
~SedListOfStyles()109 SedListOfStyles::~SedListOfStyles()
110 {
111 }
112 
113 
114 /*
115  * Get a SedStyle from the SedListOfStyles.
116  */
117 SedStyle*
get(unsigned int n)118 SedListOfStyles::get(unsigned int n)
119 {
120   return static_cast<SedStyle*>(SedListOf::get(n));
121 }
122 
123 
124 /*
125  * Get a SedStyle from the SedListOfStyles.
126  */
127 const SedStyle*
get(unsigned int n) const128 SedListOfStyles::get(unsigned int n) const
129 {
130   return static_cast<const SedStyle*>(SedListOf::get(n));
131 }
132 
133 
134 /*
135  * Get a SedStyle from the SedListOfStyles based on its identifier.
136  */
137 SedStyle*
get(const std::string & sid)138 SedListOfStyles::get(const std::string& sid)
139 {
140   return const_cast<SedStyle*>(static_cast<const
141     SedListOfStyles&>(*this).get(sid));
142 }
143 
144 
145 /*
146  * Get a SedStyle from the SedListOfStyles based on its identifier.
147  */
148 const SedStyle*
get(const std::string & sid) const149 SedListOfStyles::get(const std::string& sid) const
150 {
151   vector<SedBase*>::const_iterator result;
152   result = find_if(mItems.begin(), mItems.end(), SedIdEq<SedStyle>(sid));
153   return (result == mItems.end()) ? 0 : static_cast <const SedStyle*>
154     (*result);
155 }
156 
157 
158 /*
159  * Removes the nth SedStyle from this SedListOfStyles and returns a pointer to
160  * it.
161  */
162 SedStyle*
remove(unsigned int n)163 SedListOfStyles::remove(unsigned int n)
164 {
165   return static_cast<SedStyle*>(SedListOf::remove(n));
166 }
167 
168 
169 /*
170  * Removes the SedStyle from this SedListOfStyles based on its identifier and
171  * returns a pointer to it.
172  */
173 SedStyle*
remove(const std::string & sid)174 SedListOfStyles::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<SedStyle>(sid));
180 
181   if (result != mItems.end())
182   {
183     item = *result;
184     mItems.erase(result);
185   }
186 
187   return static_cast <SedStyle*> (item);
188 }
189 
190 
191 /*
192  * Adds a copy of the given SedStyle to this SedListOfStyles.
193  */
194 int
addStyle(const SedStyle * ss)195 SedListOfStyles::addStyle(const SedStyle* 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 SedStyle objects in this SedListOfStyles.
227  */
228 unsigned int
getNumStyles() const229 SedListOfStyles::getNumStyles() const
230 {
231   return size();
232 }
233 
234 
235 /*
236  * Creates a new SedStyle object, adds it to this SedListOfStyles object and
237  * returns the SedStyle object created.
238  */
239 SedStyle*
createStyle()240 SedListOfStyles::createStyle()
241 {
242   SedStyle* ss = NULL;
243 
244   try
245   {
246     ss = new SedStyle(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 SedListOfStyles::get() to lookup a SedStyle based on its BaseStyle.
263  */
264 struct SedIdEqBS
265 {
266   const string& id;
267 
SedIdEqBSSedIdEqBS268   SedIdEqBS (const string& id) : id(id) { }
operator ()SedIdEqBS269   bool operator() (SedBase* sb)
270   {
271   return (static_cast<SedStyle*>(sb)->getBaseStyle() == id);
272   }
273 };
274 
275 
276 /*
277  * Get a SedStyle from the SedListOfStyles based on the BaseStyle to which it
278  * refers.
279  */
280 const SedStyle*
getByBaseStyle(const std::string & sid) const281 SedListOfStyles::getByBaseStyle(const std::string& sid) const
282 {
283   vector<SedBase*>::const_iterator result;
284   result = find_if(mItems.begin(), mItems.end(), SedIdEqBS(sid));
285   return (result == mItems.end()) ? 0 : static_cast <const SedStyle*>
286     (*result);
287 }
288 
289 
290 /*
291  * Get a SedStyle from the SedListOfStyles based on the BaseStyle to which it
292  * refers.
293  */
294 SedStyle*
getByBaseStyle(const std::string & sid)295 SedListOfStyles::getByBaseStyle(const std::string& sid)
296 {
297   return const_cast<SedStyle*>(static_cast<const
298     SedListOfStyles&>(*this).getByBaseStyle(sid));
299 }
300 
301 
302 /*
303  * Returns the XML element name of this SedListOfStyles object.
304  */
305 const std::string&
getElementName() const306 SedListOfStyles::getElementName() const
307 {
308   static const string name = "listOfStyles";
309   return name;
310 }
311 
312 
313 /*
314  * Returns the libSEDML type code for this SedListOfStyles object.
315  */
316 int
getTypeCode() const317 SedListOfStyles::getTypeCode() const
318 {
319   return SEDML_LIST_OF;
320 }
321 
322 
323 /*
324  * Returns the libSEDML type code for the SED-ML objects contained in this
325  * SedListOfStyles object.
326  */
327 int
getItemTypeCode() const328 SedListOfStyles::getItemTypeCode() const
329 {
330   return SEDML_STYLE;
331 }
332 
333 
334 
335 /** @cond doxygenLibSEDMLInternal */
336 
337 /*
338  * Creates a new SedStyle in this SedListOfStyles
339  */
340 SedBase*
createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream & stream)341 SedListOfStyles::createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream&
342   stream)
343 {
344   const std::string& name = stream.peek().getName();
345   SedBase* object = NULL;
346 
347   if (name == "style")
348   {
349     object = new SedStyle(getSedNamespaces());
350     appendAndOwn(object);
351   }
352 
353   return object;
354 }
355 
356 /** @endcond */
357 
358 
359 
360 
361 #endif /* __cplusplus */
362 
363 
364 /*
365  * Get a SedStyle_t from the SedListOf_t.
366  */
367 LIBSEDML_EXTERN
368 SedStyle_t*
SedListOfStyles_getStyle(SedListOf_t * slo,unsigned int n)369 SedListOfStyles_getStyle(SedListOf_t* slo, unsigned int n)
370 {
371   if (slo == NULL)
372   {
373     return NULL;
374   }
375 
376   return static_cast <SedListOfStyles*>(slo)->get(n);
377 }
378 
379 
380 /*
381  * Get a SedStyle_t from the SedListOf_t based on its identifier.
382  */
383 LIBSEDML_EXTERN
384 SedStyle_t*
SedListOfStyles_getById(SedListOf_t * slo,const char * sid)385 SedListOfStyles_getById(SedListOf_t* slo, const char *sid)
386 {
387   if (slo == NULL)
388   {
389     return NULL;
390   }
391 
392   return (sid != NULL) ? static_cast <SedListOfStyles*>(slo)->get(sid) : NULL;
393 }
394 
395 
396 /*
397  * Removes the nth SedStyle_t from this SedListOf_t and returns a pointer to
398  * it.
399  */
400 LIBSEDML_EXTERN
401 SedStyle_t*
SedListOfStyles_remove(SedListOf_t * slo,unsigned int n)402 SedListOfStyles_remove(SedListOf_t* slo, unsigned int n)
403 {
404   if (slo == NULL)
405   {
406     return NULL;
407   }
408 
409   return static_cast <SedListOfStyles*>(slo)->remove(n);
410 }
411 
412 
413 /*
414  * Removes the SedStyle_t from this SedListOf_t based on its identifier and
415  * returns a pointer to it.
416  */
417 LIBSEDML_EXTERN
418 SedStyle_t*
SedListOfStyles_removeById(SedListOf_t * slo,const char * sid)419 SedListOfStyles_removeById(SedListOf_t* slo, const char* sid)
420 {
421   if (slo == NULL)
422   {
423     return NULL;
424   }
425 
426   return (sid != NULL) ? static_cast <SedListOfStyles*>(slo)->remove(sid) :
427     NULL;
428 }
429 
430 
431 
432 
433 LIBSEDML_CPP_NAMESPACE_END
434 
435 
436