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 __VARIABLESCOPE_HXX__
17 #define __VARIABLESCOPE_HXX__
18 
19 #include <map>
20 #include <stack>
21 #include <vector>
22 #include <libxml/xmlmemory.h>
23 
24 namespace org_modules_xml
25 {
26 class XMLObject;
27 class XMLNodeList;
28 
29 /**
30  * @file
31  * @author Calixte DENIZET <calixte.denizet@scilab.org>
32  *
33  * Class to handle the mapping between XMLObjects and their id
34  */
35 class VariableScope
36 {
37 
38     std::vector < XMLObject * >*scope;
39     int position;
40     int initialSize;
41     std::stack < int >*freePlaces;
42 
43     static std::map < const XMLObject *, std::map < const XMLObject *, bool>*> parentToChildren;
44     static std::map < void *, XMLObject * > mapLibXMLToXMLObject;
45     static std::map < void *, XMLNodeList * > mapLibXMLToXMLNodeList;
46     static xmlFreeFunc XMLFreeFunc;
47 
48 public:
49     /**
50      * Registers a pointer and its associated object.
51      * The aim of this mapping is to delete an existing object when a pointer
52      * in the xml tree is freed.
53      * @param libxml a pointer in the xml tree
54      * @param obj the corresponding object
55      */
56     static void registerPointers(void *libxml, XMLObject * obj);
57 
58     /**
59      * Unregisters a pointer. It can be used when a pointer in the tree is freed or
60      * locally to avoid cyclic dependencies on removal.
61      * @param libxml a pointer in the xml tree
62      */
63     static void unregisterPointer(void *libxml);
64 
65     /**
66      * Unregisters a pointer. It can be used when a pointer in the tree is freed or
67      * locally to avoid cyclic dependencies on removal.
68      * @param libxml a pointer in the xml tree
69      */
70     static void unregisterNodeListPointer(void *libxml);
71 
72     /**
73      * Registers a pointer and its associated object.
74      * The aim of this mapping is to delete an existing object when a pointer
75      * in the xml tree is freed.
76      * @param libxml a pointer in the xml tree
77      * @param nodeList the corresponding nodeList
78      */
79     static void registerPointers(void *libxml, XMLNodeList * nodeList);
80 
81     /**
82      * Default constructor
83      * @param initialSize the default size of the scope
84      */
85     VariableScope(int initialSize);
86 
87     /**
88      * Destructor
89      */
90     ~VariableScope();
91 
92     /**
93      * Gets the variable id from the object
94      * @param obj the object
95      * @return the corresponding id
96      */
97     int getVariableId(const XMLObject & obj);
98 
99     /**
100      * Gets the object from the id
101      * @param id the object id
102      * @return the object pointer or 0 if not found
103      */
104     XMLObject *getVariableFromId(int id);
105 
106     /**
107      * Removes an id from the scope
108      * @param id the id
109      */
110     void removeId(int id);
111 
112     /**
113      * Gets the XMLObject associated with a libxml pointer
114      * @param libxml the libxml pointer
115      * @return the XMLObject pointer
116      */
117     XMLObject *getXMLObjectFromLibXMLPtr(void *libxml) const;
118 
119     /**
120      * Gets the XMLNodeList associated with a libxml pointer
121      * @param libxml the libxml pointer
122      * @return the XMLNodeList pointer
123      */
124     XMLNodeList *getXMLNodeListFromLibXMLPtr(void *libxml) const;
125 
126 private:
127     static void _xmlFreeFunc(void *mem);
128     static void initXMLMemory();
129     static xmlFreeFunc getFreeFunc(xmlFreeFunc freeFunc);
130 
131     static void removeChildFromParent(const XMLObject * child);
132 
133     /**
134      * Removes the object dependencies if they exist
135      * @param obj the object
136      */
137     void removeDependencies(XMLObject * obj);
138 };
139 }
140 
141 #endif
142