summaryrefslogtreecommitdiff
path: root/modules/xml/src/cpp/VariableScope.hxx
blob: 9d7c633fbc677b4a1d607c19805b6df324e7debc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
 *
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

#ifndef __VARIABLESCOPE_HXX__
#define __VARIABLESCOPE_HXX__

#include <map>
#include <stack>
#include <vector>
#include <libxml/xmlmemory.h>

namespace org_modules_xml
{
class XMLObject;
class XMLNodeList;

/**
 * @file
 * @author Calixte DENIZET <calixte.denizet@scilab.org>
 *
 * Class to handle the mapping between XMLObjects and their id
 */
class VariableScope
{

    std::vector < XMLObject * >*scope;
    int position;
    int initialSize;
    std::stack < int >*freePlaces;

    static std::map < const XMLObject *, std::map < const XMLObject *, bool>*>*parentToChildren;
    static std::map < void *, XMLObject * >*mapLibXMLToXMLObject;
    static std::map < void *, XMLNodeList * >*mapLibXMLToXMLNodeList;
    static xmlFreeFunc XMLFreeFunc;

public:
    /**
     * Registers a pointer and its associated object.
     * The aim of this mapping is to delete an existing object when a pointer
     * in the xml tree is freed.
     * @param libxml a pointer in the xml tree
     * @param obj the corresponding object
     */
    static void registerPointers(void *libxml, XMLObject * obj);

    /**
     * Unregisters a pointer. It can be used when a pointer in the tree is freed or
     * locally to avoid cyclic dependencies on removal.
     * @param libxml a pointer in the xml tree
     */
    static void unregisterPointer(void *libxml);

    /**
     * Unregisters a pointer. It can be used when a pointer in the tree is freed or
     * locally to avoid cyclic dependencies on removal.
     * @param libxml a pointer in the xml tree
     */
    static void unregisterNodeListPointer(void *libxml);

    /**
     * Registers a pointer and its associated object.
     * The aim of this mapping is to delete an existing object when a pointer
     * in the xml tree is freed.
     * @param libxml a pointer in the xml tree
     * @param nodeList the corresponding nodeList
     */
    static void registerPointers(void *libxml, XMLNodeList * nodeList);

    /**
     * Default constructor
     * @param initialSize the default size of the scope
     */
    VariableScope(int initialSize);

    /**
     * Destructor
     */
    ~VariableScope();

    /**
     * Gets the variable id from the object
     * @param obj the object
     * @return the corresponding id
     */
    int getVariableId(const XMLObject & obj);

    /**
     * Gets the object from the id
     * @param id the object id
     * @return the object pointer or 0 if not found
     */
    XMLObject *getVariableFromId(int id);

    /**
     * Removes an id from the scope
     * @param id the id
     */
    void removeId(int id);

    /**
     * Gets the XMLObject associated with a libxml pointer
     * @param libxml the libxml pointer
     * @return the XMLObject pointer
     */
    XMLObject *getXMLObjectFromLibXMLPtr(void *libxml) const;

    /**
     * Gets the XMLNodeList associated with a libxml pointer
     * @param libxml the libxml pointer
     * @return the XMLNodeList pointer
     */
    XMLNodeList *getXMLNodeListFromLibXMLPtr(void *libxml) const;

private:
    static void _xmlFreeFunc(void *mem);
    static void initXMLMemory();
    static xmlFreeFunc getFreeFunc(xmlFreeFunc freeFunc);

    static void removeChildFromParent(const XMLObject * child);

    /**
     * Removes the object dependencies if they exist
     * @param obj the object
     */
    void removeDependencies(XMLObject * obj);
};
}

#endif