kernel modules Wiedervereinigung
[scilab.git] / scilab / modules / ast / includes / symbol / libraries.hxx
1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
4 *
5 *  This file must be used under the terms of the CeCILL.
6 *  This source file is licensed as described in the file COPYING, which
7 *  you should have received as part of this distribution.  The terms
8 *  are also available at
9 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12
13 #ifndef __LIBRARIES_HXX__
14 #define __LIBRARIES_HXX__
15
16 #include <stack>
17 #include "symbol.hxx"
18 #include "types.hxx"
19 #include "library.hxx"
20
21 namespace symbol
22 {
23 struct ScopedLibrary
24 {
25     ScopedLibrary(int _iLevel, types::Library* _pLib) : m_iLevel(_iLevel), m_pLib(_pLib) {};
26
27     types::MacroFile* getMacroFile(const Symbol& _key)
28     {
29         return m_pLib->get(_key.name_get());
30     }
31
32     int m_iLevel;
33     types::Library* m_pLib;
34 };
35
36 struct Library
37 {
38     Library(const Symbol& _name) : name(_name) {};
39
40     void put(types::Library* _pLib, int _iLevel)
41     {
42         if (empty() || top()->m_iLevel < _iLevel)
43         {
44             //create a new level
45             stack.push(new ScopedLibrary(_iLevel, _pLib));
46             _pLib->IncreaseRef();
47         }
48         else
49         {
50             //update current level
51             types::Library* pLib = top()->m_pLib;
52             if (pLib != _pLib)
53             {
54                 pLib->DecreaseRef();
55                 pLib->killMe();
56                 top()->m_pLib = _pLib;
57                 _pLib->IncreaseRef();
58             }
59         }
60     }
61
62     types::MacroFile* get(const Symbol& _keyMacro) const
63     {
64         if (empty() == false)
65         {
66             return top()->getMacroFile(_keyMacro);
67         }
68
69         return NULL;
70     }
71
72     std::list<std::wstring>* getMacrosName()
73     {
74         if (empty() == false)
75         {
76             return top()->m_pLib->getMacrosName();
77         }
78
79         return new std::list<std::wstring>();
80     }
81
82     bool empty() const
83     {
84         return stack.empty();
85     }
86
87     ScopedLibrary* top() const
88     {
89         return stack.top();
90     }
91
92     void pop()
93     {
94         stack.pop();
95     }
96
97 private :
98     typedef std::stack<ScopedLibrary*> StackLib;
99     StackLib stack;
100     Symbol name;
101     bool m_global;
102 };
103
104 struct Libraries
105 {
106     Libraries() {};
107
108     Library* getOrCreate(const Symbol& _key)
109     {
110         MapLibs::const_iterator it = libs.find(_key);
111         if (it == libs.end())
112         {
113             //create an empty StackedValues
114             Library* lib = new Library(_key);
115             libs[_key] = lib;
116             return lib;
117         }
118
119         return it->second;
120     }
121
122     void put(const Symbol& _keyLib, types::Library* _pLib, int _iLevel)
123     {
124         Library* lib = getOrCreate(_keyLib);
125         lib->put(_pLib, _iLevel);
126     }
127
128     types::InternalType* get(const Symbol& _key, int _iLevel)
129     {
130         MapLibs::reverse_iterator it = libs.rbegin();
131         for (; it != libs.rend() ; ++it)
132         {
133             if (it->second->empty() == false)
134             {
135                 if (_iLevel == -1 || it->second->top()->m_iLevel == _iLevel)
136                 {
137                     types::MacroFile* pMF = it->second->get(_key);
138                     if (pMF)
139                     {
140                         return (types::InternalType*)pMF;
141                     }
142                 }
143             }
144         }
145
146         return NULL;
147     }
148
149     bool remove(const Symbol& _key, int _iLevel)
150     {
151         MapLibs::iterator it = libs.find(_key);
152         if (it != libs.end())
153         {
154             if (it->second->empty() == false)
155             {
156                 if (it->second->top()->m_iLevel == _iLevel)
157                 {
158                     ScopedLibrary * pSL = it->second->top();
159                     types::Library* pIT = pSL->m_pLib;
160                     pIT->DecreaseRef();
161                     pIT->killMe();
162                     it->second->pop();
163                     delete pSL;
164                     return true;
165                 }
166             }
167         }
168
169         return false;
170     }
171
172     std::list<std::wstring>* getMacrosName()
173     {
174         std::list<std::wstring>* names = new std::list<std::wstring>();
175         MapLibs::iterator it = libs.begin();
176         MapLibs::iterator itEnd = libs.end();
177         for (; it != itEnd ; ++it)
178         {
179             std::list<std::wstring>* temp = it->second->getMacrosName();
180             names->insert(names->end(), temp->begin(), temp->end());
181             delete temp;
182         }
183
184         return names;
185     }
186
187     void clearAll()
188     {
189         for (MapLibs::iterator it = libs.begin(); it != libs.end() ; ++it)
190         {
191             while (!it->second->empty())
192             {
193                 ScopedLibrary * pSL = it->second->top();
194                 types::InternalType * pIT = pSL->m_pLib;
195                 pIT->DecreaseRef();
196                 pIT->killMe();
197                 it->second->pop();
198                 delete pSL;
199             }
200
201             delete it->second;
202         }
203     }
204
205 private:
206     typedef std::map<Symbol, Library*> MapLibs;
207     MapLibs libs;
208 };
209 }
210 #endif /* !__LIBRARIES_HXX__ */