summaryrefslogtreecommitdiff
path: root/modules/xml/src/cpp/XMLObject.hxx
blob: 3b699fc04fd54086b98e4dfbb9061a117f126328 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * 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 __XMLOBJECTS_HXX__
#define __XMLOBJECTS_HXX__

#include <iostream>
#include <string>
#include <sstream>
#include <typeinfo>
#include <set>

extern "C"
{
#include "xml_mlist.h"
#include "dynlib_xml_scilab.h"
}

//#define SCILAB_DEBUG_XML

namespace org_modules_xml
{
class VariableScope;

/**
 * @file
 * @author Calixte DENIZET <calixte.denizet@scilab.org>
 *
 * Base class for the XML objects.
 */
class XML_SCILAB_IMPEXP XMLObject
{

public:

#ifdef SCILAB_DEBUG_XML
    static std::set<XMLObject *> pointers;
#endif

    /**
     * Default constructor
     */
    XMLObject();

    /**
     * Destructor
     */
    virtual ~ XMLObject()
    {
#ifdef SCILAB_DEBUG_XML
        //std::cout << "Delete = " << (void*)this << std::endl;
        pointers.erase(this);
#endif
    }

    /**
     * Get the libxml2 pointer
     * @return the pointer
     */
    virtual void *getRealXMLPointer() const
    {
        return 0;
    }

    /**
     * Gets a XML parent object. A set of dependencies is created between the objects
     * to be sure that all the XML objects will be freed when a document will be destroyed.
     * @return the parent XMLObject
     */
    virtual const XMLObject *getXMLObjectParent() const
    {
        return 0;
    }

    /**
     * Sets the attribute value.
     * @param name the attribute names
     * @param value the attribute values
     * @param size the number of names
     */
    virtual void setAttributeValue(const char **name, const char **value, int size) const
    {
        return;
    }

    /**
     * Sets the attribute value with a prefix namespace.
     * @param prefix the namespace prefix or the namespace itself
     * @param name the attribute names
     * @param value the attribute values
     * @param size the number of names
     */
    virtual void setAttributeValue(const char **prefix, const char **name, const char **value, int size) const
    {
        return;
    }

    /**
     * @return the string representation of this object
     */
    virtual const std::string toString() const
    {
        return std::string("");
    }

    /**
     * @return a dump of this object
     */
    virtual const std::string dump(bool indent) const
    {
        return std::string("");
    }

    /**
     * @return the object id
     */
    inline int getId() const
    {
        return id;
    }

    inline bool isValid() const
    {
        return valid;
    }

    inline void invalid()
    {
        valid = false;
    }

    /**
     * Creates the Scilab's variable corresponding to this object
     * @param pos the stack position
     * @return 1 if all is ok, else 0
     */
    int createOnStack(int pos, void *pvApiCtx) const;

    /**
     * @param id the object id
     * @return the object which has the corresponding id or 0 if not found
     */
    template < class T > static T *getFromId(int id)
    {
        return static_cast < T * >(getVariableFromId(id));
    }

protected:
    int id;
    int scilabType;
    bool valid;

    static VariableScope *scope;

    /**
     * Reset the scope
     */
    static void resetScope();

private:

    /**
     * @param id the id
     * @return XMLObject corresponding to the id or 0 if not found
     */
    static XMLObject *getVariableFromId(int id);
};
}

#endif