1 /**
2  * @file SedListOf.h
3  * @brief Definition of the SedListOf 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  * @class SedListOf
35  * @sbmlbrief{} TODO:Definition of the SedListOf class.
36  */
37 
38 
39 #ifndef SedListOf_h
40 #define SedListOf_h
41 
42 
43 #include <sedml/common/extern.h>
44 #include <sedml/common/sedmlfwd.h>
45 #include <sedml/SedTypeCodes.h>
46 
47 
48 #ifdef __cplusplus
49 
50 
51 #include <vector>
52 #include <algorithm>
53 #include <functional>
54 
55 #include <sedml/SedBase.h>
56 
57 LIBSEDML_CPP_NAMESPACE_BEGIN
58 
59 class SedVisitor;
60 
61 
62 /** @cond doxygenLibsedmlInternal */
63 /**
64  * Used by SedListOf::get() to lookup an SedBase based by its id.
65  */
66 #ifndef SWIG
67 template<class CNAME>
68 struct SedIdEq
69 {
70   const std::string& id;
71 
SedIdEqSedIdEq72   SedIdEq (const std::string& id) : id(id) { }
operatorSedIdEq73   bool operator() (SedBase* sb)
74        { return static_cast <CNAME*> (sb)->getId() == id; }
75 };
76 #endif /* SWIG */
77 /** @endcond */
78 
79 
80 class LIBSEDML_EXTERN SedListOf : public SedBase
81 {
82 public:
83 
84   /**
85    * Creates a new SedListOf object.
86    *
87    * @param level the SED-ML Level; if not assigned, defaults to the
88    * value of SEDML_DEFAULT_LEVEL.
89    *
90    * @param version the Version within the SED-ML Level; if not assigned,
91    * defaults to the value of SEDML_DEFAULT_VERSION.
92    */
93   SedListOf (unsigned int level   = SEDML_DEFAULT_LEVEL,
94           unsigned int version = SEDML_DEFAULT_VERSION);
95 
96 
97   /**
98    * Creates a new SedListOf with a given SedNamespaces object.
99    *
100    * @param sedmlns the set of SED-ML namespaces that this SedListOf should
101    * contain.
102    */
103   SedListOf (SedNamespaces* sedmlns);
104 
105 
106   /**
107    * Destroys this SedListOf and the items inside it.
108    */
109   virtual ~SedListOf ();
110 
111 
112   /**
113    * Copy constructor; creates a copy of this SedListOf.
114    *
115    * @param orig the SedListOf instance to copy.
116    */
117   SedListOf (const SedListOf& orig);
118 
119 
120   /**
121    * Assignment operator for SedListOf.
122    */
123   SedListOf& operator=(const SedListOf& rhs);
124 
125 
126 
127   /** @cond doxygenLibsedmlInternal */
128   /**
129    * Accepts the given SedVisitor.
130    *
131    * @param v the SedVisitor instance to be used.
132    *
133    * @return the result of calling <code>v.visit()</code>, which indicates
134    * whether the Visitor would like to visit the next item in the
135    * list.
136    */
137   virtual bool accept (SedVisitor& v) const;
138   /** @endcond */
139 
140 
141   /**
142    * Creates and returns a deep copy of this SedListOf object.
143    *
144    * @return the (deep) copy of this SedListOf object.
145    */
146   virtual SedListOf* clone () const;
147 
148 
149   /**
150    * Adds an item to the end of this SedListOf's list of items.
151    *
152    * This method makes a clone of the @p item handed to it.  This means that
153    * when the SedListOf object is destroyed, the original items will not be
154    * destroyed.  For a method with an alternative ownership behavior, see the
155    * SedListOf::appendAndOwn(@if java SedBase@endif) method.
156    *
157    * @param item the item to be added to the list.
158    *
159    * @copydetails doc_returns_success_code
160    * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
161    * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
162    *
163    * @see appendAndOwn(SedBase* disownedItem)
164    * @see appendFrom(const SedListOf* list)
165    */
166   int append (const SedBase* item);
167 
168 
169   /**
170    * Adds an item to the end of this SedListOf's list of items.
171    *
172    * This method does not clone the @p disownedItem handed to it; instead, it assumes
173    * ownership of it.  This means that when the SedListOf is destroyed, the item
174    * will be destroyed along with it.  For a method with an alternative
175    * ownership behavior, see the SedListOf::append(SedBase* item) method.
176    *
177    * @param disownedItem the item to be added to the list.
178    *
179    * @copydetails doc_returns_success_code
180    * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
181    * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
182    *
183    * @see append(const SedBase* item)
184    * @see appendFrom(const SedListOf* list)
185    */
186   int appendAndOwn (SedBase* disownedItem);
187 
188 
189   /**
190    * Adds a clone of a list of items to this SedListOf's list.
191    *
192    * Note that because this clones the objects handed to it, the original
193    * items will not be destroyed when this SedListOf object is destroyed.
194    *
195    * @param list a list of items to be added.
196    *
197    * @copydetails doc_returns_success_code
198    * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
199    * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
200    *
201    * @see append(const SedBase* item)
202    * @see appendAndOwn(SedBase* disownedItem)
203    */
204   virtual int appendFrom (const SedListOf* list);
205 
206 
207   /**
208    * Inserts an item at a given position in this SedListOf's list of items.
209    *
210    * This variant of the method makes a clone of the @p item handed to it.
211    * This means that when the SedListOf is destroyed, the original @p item will
212    * <em>not</em> be destroyed.
213    *
214    * @param location the location in the list where to insert the item.
215    * @param item the item to be inserted to the list.
216    *
217    * @copydetails doc_returns_success_code
218    * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
219    * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
220    *
221    * @see insertAndOwn(int location, SedBase* item)
222    */
223   int insert (int location, const SedBase* item);
224 
225 
226   /**
227    * Inserts an item at a given position in this SedListOf's list of items.
228    *
229    * This variant of the method does not make a clone of the @p disownedItem handed to it.
230    * This means that when the SedListOf is destroyed, the original @p item
231    * <em>will</em> be destroyed.
232    *
233    * @param location the location where to insert the item
234    * @param disownedItem the item to be inserted to the list
235    *
236    * @copydetails doc_returns_success_code
237    * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
238    * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
239    *
240    * @see insert(int location, const SedBase* item)
241    */
242   int insertAndOwn(int location, SedBase* disownedItem);
243 
244 
245   /**
246    * Get an item from the list.
247    *
248    * @param n the index number of the item to get.
249    *
250    * @return the <em>n</em>th item in this SedListOf items, or a null pointer if
251    * the index number @p n refers to a nonexistent position in this list.
252    *
253    * @see size()
254    */
255   virtual const SedBase* get (unsigned int n) const;
256 
257 
258   /**
259    * Get an item from the list.
260    *
261    * @param n the index number of the item to get.
262    *
263    * @return the <em>n</em>th item in this SedListOf items, or a null pointer if
264    * the index number @p n refers to a nonexistent position in this list.
265    *
266    * @see size()
267    */
268   virtual SedBase* get (unsigned int n);
269 
270 
271   /**
272    * Returns the first child element it can find with a specific "id"
273    * attribute value, or @c NULL if no such object is found.
274    *
275    * @param id string representing the "id" attribute value of the
276    * object to find.
277    *
278    * @return pointer to the first element found with the given identifier.
279    */
280   virtual SedBase* getElementBySId(const std::string& id);
281 
282 
283   /**
284    * Returns the first child element found with the given meta-identifier.
285    *
286    * @param metaid string representing the "metaid" attribute of the object
287    * to find.
288    *
289    * @return the first element found with the given @p metaid, or @c NULL if
290    * no such object is found.
291    */
292   virtual SedBase* getElementByMetaId(const std::string& metaid);
293 
294 
295   /**
296    * Returns a List of all child SedBase objects.
297    *
298    * The values returned include all children of the objects in this SedListOf
299    * list, nested to an arbitrary depth.
300    *
301    * @return a List of pointers to all child objects.
302    */
303   virtual List* getAllElements(SedElementFilter* filter = NULL);
304 
305 
306   /**
307    * Removes all items in this SedListOf object.
308    *
309    * If parameter @p doDelete is @c true (default), all items in this SedListOf
310    * object are deleted and cleared, and thus the caller doesn't have to
311    * delete those items.  Otherwise, all items are cleared only from this
312    * SedListOf object; the caller is still responsible for deleting the actual
313    * items.  (In the latter case, callers are advised to store pointers to
314    * all items elsewhere before calling this function.)
315    *
316    * @param doDelete if @c true (default), all items are deleted and cleared.
317    * Otherwise, all items are just cleared and not deleted.
318    *
319    * @ifnot hasDefaultArgs @htmlinclude warn-default-args-in-docs.html @endif@~
320    */
321   void clear (bool doDelete = true);
322 
323 
324   /**
325    * Removes all items in this SedListOf object and deletes its properties too.
326    *
327    * This performs a call to clear() with an argument of @c true (thus removing
328    * all the child objects in the list), followed by calls to various libSEDML
329    * <code>unset<em>Foo</em></code> methods to delete everything else: CVTerm
330    * objects, model history objects, etc.
331    *
332    * @if cpp Implementations of subclasses of SedListOf may need to override
333    * this method if different handling of child objects is needed.@endif@~
334    *
335    * @copydetails doc_returns_success_code
336    * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
337    */
338   virtual int removeFromParentAndDelete();
339 
340 
341   /**
342    * Removes the <em>n</em>th item from this SedListOf list of items and returns
343    * it.
344    *
345    * The caller owns the returned item and is responsible for deleting it.
346    *
347    * @param n the index of the item to remove
348    *
349    * @see size()
350    */
351   virtual SedBase* remove (unsigned int n);
352 
353 
354   /**
355    * Returns number of items in this SedListOf list.
356    *
357    * @return the number of items in this SedListOf items.
358    */
359   unsigned int size () const;
360 
361 
362   /** @cond doxygenLibsedmlInternal */
363   /**
364    * Sets the parent SedDocument of this SED-ML object.
365    *
366    * @param d the SedDocument that should become the parent of this
367    * SedListOf.
368    */
369   virtual void setSedDocument (SedDocument* d);
370   /** @endcond */
371 
372 
373   /** @cond doxygenLibsedmlInternal */
374   /**
375    * Sets this SED-ML object to child SED-ML objects (if any).
376    * (Creates a child-parent relationship by the parent)
377    *
378    * Subclasses must override this function if they define
379    * one ore more child elements.
380    * Basically, this function needs to be called in
381    * constructor, copy constructor and assignment operator.
382    *
383    * @if cpp
384    * @see setSedDocument()
385    * @see enablePackageInternal()
386    * @endif
387    */
388   virtual void connectToChild ();
389   /** @endcond */
390 
391 
392   /**
393    * Returns the libSEDML type code for this object, namely,
394    * @sedmlconstant{SEDML_LIST_OF, SedTypeCode_t}.
395    *
396    * @copydetails doc_what_are_typecodes
397    *
398    * @return the SED-ML type code for this object:
399    * @sedmlconstant{SEDML_LIST_OF, SedTypeCode_t} (default).
400    *
401    * @note The various SedListOf classes mostly differ from each other in what they
402    * contain.  Hence, one must call getItemTypeCode() to fully determine the
403    * class of this SED-ML object.
404    *
405    * @see getItemTypeCode()
406    * @see getElementName()
407    */
408   virtual int getTypeCode () const;
409 
410 
411   /**
412    * Get the type code of the objects contained in this SedListOf.
413    *
414    * Classes that inherit from the SedListOf class should override this method
415    * to return the SED-ML type code for the objects contained in this SedListOf.
416    * If they do not, this method will return
417    * @sedmlconstant{SEDML_UNKNOWN, SedTypeCode_t}
418    *
419    * @return The SedListOf base class contains no SED-ML objects, and therefore
420    * this method returns @sedmlconstant{SEDML_UNKNOWN, SedTypeCode_t}.
421    *
422    * @see getElementName()
423    * @see getPackageName()
424    */
425   virtual int getItemTypeCode () const;
426 
427 
428   /**
429    * Returns the XML element name of this object, which for SedListOf, is
430    * always @c "listOf".
431    *
432    * @return the XML name of this element.
433    */
434   virtual const std::string& getElementName () const;
435 
436 
437   /** @cond doxygenLibsedmlInternal */
438   /**
439    * Subclasses should override this method to write out their contained
440    * SED-ML objects as XML elements.  Be sure to call your parents
441    * implementation of this method as well.
442    */
443   virtual void writeElements (XMLOutputStream& stream) const;
444   /** @endcond */
445 
446 
447 protected:
448   /** @cond doxygenLibsedmlInternal */
449   typedef std::vector<SedBase*>           ListItem;
450   typedef std::vector<SedBase*>::iterator ListItemIter;
451 
452   /**
453    * Subclasses should override this method to get the list of
454    * expected attributes.
455    * This function is invoked from corresponding readAttributes()
456    * function.
457    */
458   virtual void addExpectedAttributes(ExpectedAttributes& attributes);
459 
460 
461   /**
462    * Subclasses should override this method to read values from the given
463    * XMLAttributes set into their specific fields.  Be sure to call your
464    * parents implementation of this method as well.
465    */
466   virtual void readAttributes (const XMLAttributes& attributes,
467                                const ExpectedAttributes& expectedAttributes);
468 
469   /**
470    * Subclasses should override this method to write their XML attributes
471    * to the XMLOutputStream.  Be sure to call your parents implementation
472    * of this method as well.  For example:
473    *
474    *   SedBase::writeAttributes(stream);
475    *   stream.writeAttribute( "id"  , mId   );
476    *   stream.writeAttribute( "name", mName );
477    *   ...
478    */
479   virtual void writeAttributes (XMLOutputStream& stream) const;
480 
481   virtual bool isValidTypeForList(SedBase * item);
482 
483   ListItem mItems;
484 
485   /** @endcond */
486 };
487 
488 LIBSEDML_CPP_NAMESPACE_END
489 
490 #endif  /* __cplusplus */
491 
492 
493 #ifndef SWIG
494 
495 LIBSEDML_CPP_NAMESPACE_BEGIN
496 BEGIN_C_DECLS
497 
498 
499 /**
500  * Creates a new instance of a SedListOf_t structure.
501  *
502  * @param level an unsigned int, the SED-ML Level to assign to this
503  * SedListOf_t structure.
504  *
505  * @param version an unsigned int, the SED-ML Version to assign to this
506  * SedListOf_t structure.
507  *
508  * @return a pointer to the newly-created SedListOf_t structure.
509  *
510  * @memberof SedListOf_t
511  */
512 LIBSEDML_EXTERN
513 SedListOf_t *
514 SedListOf_create (unsigned int level, unsigned int version);
515 
516 
517 /**
518  * Frees the given SedListOf_t structure.
519  *
520  * This function assumes each item in the list is derived from SedBase_t.
521  *
522  * @param lo the SedListOf_t structure to be freed.
523  *
524  * @memberof SedListOf_t
525  */
526 LIBSEDML_EXTERN
527 void
528 SedListOf_free (SedListOf_t *lo);
529 
530 
531 /**
532  * Creates a deep copy of the given SedListOf_t structure.
533  *
534  * @param lo the SedListOf_t structure to be copied.
535  *
536  * @return a (deep) copy of the given SedListOf_t structure, or a null
537  * pointer if a failure occurred.
538  *
539  * @memberof SedListOf_t
540  */
541 LIBSEDML_EXTERN
542 SedListOf_t *
543 SedListOf_clone (const SedListOf_t *lo);
544 
545 
546 /**
547  * Adds a copy of a given item to the end of a SedListOf_t list.
548  *
549  * @param lo the SedListOf_t structure to which the @p item should be appended.
550  * @param item the item to append to the list.
551  *
552  * @copydetails doc_returns_success_code
553  * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
554  * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
555  *
556  * @see SedListOf_appendAndOwn()
557  *
558  * @memberof SedListOf_t
559  */
560 LIBSEDML_EXTERN
561 int
562 SedListOf_append (SedListOf_t *lo, const SedBase_t *item);
563 
564 
565 /**
566  * Adds the given item to the end of a SedListOf_t list.
567  *
568  * @param lo the SedListOf_t structure to which the @p disownedItem should be appended.
569  * @param disownedItem the item to append to the list.
570  *
571  * Unlike SedListOf_append(), this function does not copy the @p disownedItem.
572  * The given @p lo list will contain the original item.
573  *
574  * @copydetails doc_returns_success_code
575  * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
576  * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
577  *
578  * @see SedListOf_append()
579  *
580  * @memberof SedListOf_t
581  */
582 LIBSEDML_EXTERN
583 int
584 SedListOf_appendAndOwn (SedListOf_t *lo, SedBase_t *disownedItem);
585 
586 
587 /**
588  * Adds clones a list of items from one list to another.
589  *
590  * @param lo the SedListOf_t list to which @p list will be appended.
591  * @param list the list of items to append to @p lo.
592  *
593  * @copydetails doc_returns_success_code
594  * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
595  * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
596  *
597  * @memberof SedListOf_t
598  */
599 LIBSEDML_EXTERN
600 int
601 SedListOf_appendFrom (SedListOf_t *lo, SedListOf_t *list);
602 
603 
604 /**
605  * Inserts a copy of an item into a SedListOf_t list at a given position.
606  *
607  * @param lo the list into which @p item will be inserted.
608  * @param location the starting index for the @p item in the @p lo list.
609  * @param item the item to append to insert into @p lo.
610  *
611  * @copydetails doc_returns_success_code
612  * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
613  * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
614  *
615  * @memberof SedListOf_t
616  */
617 LIBSEDML_EXTERN
618 int
619 SedListOf_insert (SedListOf_t *lo, int location, const SedBase_t *item);
620 
621 
622 /**
623  * Inserts an item into a SedListOf_t list at a given position.
624  *
625  * Unlike SedListOf_insert(), this function does not clone @p disownedItem before
626  * inserting it into @p lo, which means that @p lo becomes the owner.
627  *
628  * @param lo the list into which @p disownedItem will be inserted.
629  * @param location the starting index for the @p disownedItem in the @p lo list.
630  * @param disownedItem the item to append to insert into @p lo.
631  *
632  * @copydetails doc_returns_success_code
633  * @li @sedmlconstant{LIBSEDML_OPERATION_SUCCESS, OperationReturnValues_t}
634  * @li @sedmlconstant{LIBSEDML_INVALID_OBJECT, OperationReturnValues_t}
635  *
636  * @memberof SedListOf_t
637  */
638 LIBSEDML_EXTERN
639 int
640 SedListOf_insertAndOwn (SedListOf_t *lo, int location, SedBase_t *disownedItem);
641 
642 
643 /**
644  * Returns the <em>n</em>th item of a given list.
645  *
646  * @param lo the list from which to retrieve the item.
647  * @param n the index of the item to retrieve.
648  *
649  * @return the <em>n</em>th item in this SedListOf items, or a null pointer if
650  * the index number @p n refers to a nonexistent position in @p lo.
651  *
652  * @see SedListOf_size()
653  *
654  * @memberof SedListOf_t
655  */
656 LIBSEDML_EXTERN
657 SedBase_t *
658 SedListOf_get (SedListOf_t *lo, unsigned int n);
659 
660 
661 /**
662  * Removes all items in this SedListOf_t structure.
663  *
664  * If @p doDelete is true (non-zero), all items in this SedListOf_t structure
665  * are both deleted and cleared, and thus the caller doesn't have to delete
666  * those items.  Otherwise, if @p doDelete is false (zero), all items are
667  * only cleared from this SedListOf_t structure and the caller is responsible
668  * for deleting all items.  (In the latter case, callers are advised to store
669  * pointers to all items elsewhere before calling this function.)
670  *
671  * @param lo the SedListOf_t structure to clear
672  * @param doDelete whether to delete the items.
673  *
674  * @memberof SedListOf_t
675  */
676 LIBSEDML_EXTERN
677 void
678 SedListOf_clear (SedListOf_t *lo, int doDelete);
679 
680 
681 /**
682  * Removes the <em>n</em>th item from this SedListOf_t list and returns it.
683  *
684  * The caller owns the returned item and is responsible for deleting it.
685  *
686  * @param lo the list from which the item should be removed.
687  * @param n the index number of the item to remove.
688  *
689  * @return the item removed, or a null pointer if no item existed at the
690  * index @p n.
691  *
692  * @memberof SedListOf_t
693  */
694 LIBSEDML_EXTERN
695 SedBase_t *
696 SedListOf_remove (SedListOf_t *lo, unsigned int n);
697 
698 
699 /**
700  * Returns the number of items in this SedListOf_t items.
701  *
702  * @param lo the SedListOf_t structure to count.
703  *
704  * @return the number of items in @p lo.
705  *
706  * @memberof SedListOf_t
707  */
708 LIBSEDML_EXTERN
709 unsigned int
710 SedListOf_size (const SedListOf_t *lo);
711 
712 
713 /**
714  * Get the type code of the objects contained in the given SedListOf_t
715  * structure.
716  *
717  * @copydetails doc_what_are_typecodes
718  *
719  * @param lo the SedListOf_t whose item type codes are sought.
720  *
721  * @return the type code corresponding to the objects in @p lo,
722  * or @sedmlconstant{SEDML_UNKNOWN, SedTypeCode_t}.
723  *
724  * @memberof SedListOf_t
725  */
726 LIBSEDML_EXTERN
727 int
728 SedListOf_getItemTypeCode (const SedListOf_t *lo);
729 
730 
731 END_C_DECLS
732 LIBSEDML_CPP_NAMESPACE_END
733 
734 #endif  /* !SWIG */
735 #endif  /* SedListOf_h */
736 
737