1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 #ifndef __XMLLIST_HXX__
17 #define __XMLLIST_HXX__
18 
19 #include <string>
20 #include "XMLRemovable.hxx"
21 
22 #include "xml.h"
23 
24 namespace org_modules_xml
25 {
26 class XMLObject;
27 
28 /**
29  * @file
30  * @author Calixte DENIZET <calixte.denizet@scilab.org>
31  *
32  * Virtual class to handle a list of XMLObjects
33  */
34 class XMLList: public XMLObject, public XMLRemovable
35 {
36 
37 public:
38     /**
39      * Gets the element with the given index.
40      * @param index the element index
41      * @return the corresponding object
42      */
43     virtual const XMLObject *getListElement(int index) = 0;
44 
45     /**
46      * Default constructor
47      */
48     XMLList();
49 
50     /**
51      * @return the list size
52      */
getSize() const53     int getSize() const
54     {
55         return size;
56     }
57 
incrementSize()58     void incrementSize()
59     {
60         size++;
61     }
62 
63     /**
64      * Get the content of each node of the list
65      * @return an array of strings
66      */
67     virtual const char **getContentFromList() const = 0;
68 
69     /**
70      * Get the name of each node of the list
71      * @return an array of strings
72      */
73     virtual const char **getNameFromList() const = 0;
74 
75     const std::string toString() const;
76 
77 protected:
78     int size;
79 
80     /**
81      * Gets an element in a linked list with a given index.
82      * The element is reached from a previous element which has an index.
83      * This way to search the element is faster in a for loop where the indexes are
84      * consecutives.
85      * @param index the searched index
86      * @param max the max
87      * @param prev a pointer on the previous index (*prev is modified by this function)
88      * @param prevElem a pointer on the previous element (*prevElem is modified by this function)
89      * @return the found element
90      */
getListElement(int index,int max,int * prev,T ** prevElem)91     template < typename T > static T *getListElement(int index, int max, int *prev, T ** prevElem)
92     {
93         if (index >= 1 && index <= max)
94         {
95             if (index != *prev)
96             {
97                 if (index < *prev)
98                 {
99                     for (int i = *prev; i > index; i--, *prevElem = (*prevElem)->prev)
100                     {
101                         ;
102                     }
103                 }
104                 else
105                 {
106                     for (int i = *prev; i < index; i++, *prevElem = (*prevElem)->next)
107                     {
108                         ;
109                     }
110                 }
111                 *prev = index;
112             }
113 
114             return *prevElem;
115         }
116 
117         return 0;
118     }
119 };
120 }
121 
122 #endif
123