2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
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
13 #ifndef __LIBRARIES_HXX__
14 #define __LIBRARIES_HXX__
19 #include "library.hxx"
25 ScopedLibrary(int _iLevel, types::Library* _pLib) : m_iLevel(_iLevel), m_pLib(_pLib) {};
27 types::MacroFile* getMacroFile(const Symbol& _key)
29 return m_pLib->get(_key.getName());
33 types::Library* m_pLib;
38 typedef std::stack<ScopedLibrary*> StackLib;
40 Library(const Symbol& _name) : name(_name), m_global(false) {};
42 void put(types::Library* _pLib, int _iLevel)
44 if (empty() || top()->m_iLevel < _iLevel)
47 stack.push(new ScopedLibrary(_iLevel, _pLib));
52 //update current level
53 types::Library* pLib = top()->m_pLib;
58 top()->m_pLib = _pLib;
64 void put(ScopedLibrary* pSL)
69 types::MacroFile* get(const Symbol& _keyMacro) const
73 return top()->getMacroFile(_keyMacro);
79 int getMacrosName(std::list<std::wstring>& lst)
83 top()->m_pLib->getMacrosName(lst);
86 return static_cast<int>(lst.size());
94 ScopedLibrary* top() const
104 inline Symbol getSymbol() const
117 typedef std::map<Symbol, Library*> MapLibs;
121 Library* getOrCreate(const Symbol& _key)
123 MapLibs::const_iterator it = libs.find(_key);
124 if (it == libs.end())
126 //create an empty StackedValues
127 Library* lib = new Library(_key);
135 void put(const Symbol& _keyLib, types::Library* _pLib, int _iLevel)
137 Library* lib = getOrCreate(_keyLib);
138 lib->put(_pLib, _iLevel);
141 bool putInPreviousScope(const Symbol& _keyLib, types::Library* _pLib, int _iLevel)
143 Library* lib = getOrCreate(_keyLib);
147 lib->put(_pLib, _iLevel);
149 else if (lib->top()->m_iLevel > _iLevel)
151 ScopedLibrary* pLib = lib->top();
153 putInPreviousScope(_keyLib, _pLib, _iLevel);
154 //decresef ref before, increase it in put
155 //pVar->m_pIT->DecreaseRef();
156 lib->put(pLib->m_pLib, pLib->m_iLevel);
160 lib->put(_pLib, _iLevel);
166 types::InternalType* get(const Symbol& _key, int _iLevel)
168 //does _key is a lib name
169 auto lib = libs.find(_key);
170 if (lib != libs.end())
172 if (lib->second->empty() == false)
174 if (_iLevel == -1 || lib->second->top()->m_iLevel == _iLevel)
176 return lib->second->top()->m_pLib;
181 //does _key is a macro in a lib
182 auto it = libs.rbegin();
183 for (auto it = libs.rbegin(), itEnd = libs.rend(); it != itEnd ; ++it)
185 Library* lib = it->second;
186 if (it->second->empty() == false)
188 if (_iLevel == -1 || it->second->top()->m_iLevel == _iLevel)
190 types::MacroFile* pMF = it->second->get(_key);
193 return (types::InternalType*)pMF;
202 bool remove(const Symbol& _key, int _iLevel)
204 MapLibs::iterator it = libs.find(_key);
205 if (it != libs.end())
207 if (it->second->empty() == false)
209 if (it->second->top()->m_iLevel == _iLevel)
211 ScopedLibrary * pSL = it->second->top();
212 types::Library* pIT = pSL->m_pLib;
225 int getMacrosName(std::list<std::wstring>& lst)
227 MapLibs::iterator it = libs.begin();
228 MapLibs::iterator itEnd = libs.end();
231 it.second->getMacrosName(lst);
234 return static_cast<int>(lst.size());
237 int getVarsName(std::list<std::wstring>& lst)
241 if (it.second->empty() == false)
243 lst.push_back(it.first.getName().c_str());
247 return static_cast<int>(lst.size());
250 int getVarsToVariableBrowser(std::list<Library*>& lst)
252 for (auto lib : libs)
254 if (lib.second->empty() == false)
256 lst.push_back(lib.second);
260 return static_cast<int>(lst.size());
265 for (auto lib : libs)
267 while (!lib.second->empty())
269 ScopedLibrary * pSL = lib.second->top();
270 types::InternalType * pIT = pSL->m_pLib;
281 bool getVarsNameForWho(std::list<std::wstring>* lstVarName, int* iVarLenMax, bool bSorted = false) const
283 for (auto it = libs.begin(), itEnd = libs.end(); it != itEnd; ++it)
285 std::wstring wstrVarName(it->first.getName().c_str());
286 if (lstVarName && it->second->empty() == false)
288 lstVarName->push_back(wstrVarName);
289 *iVarLenMax = std::max(*iVarLenMax, (int)wstrVarName.size());
304 int whereis(std::list<std::wstring>& lst, const Symbol& _key)
306 for (auto lib : libs)
308 if (lib.second->get(_key) != NULL)
310 lst.push_back(lib.first.getName());
313 return static_cast<int>(lst.size());
316 int librarieslist(std::list<std::wstring>& lst)
318 for (auto lib : libs)
320 lst.push_back(lib.first.getName());
323 return static_cast<int>(lst.size());
330 #endif /* !__LIBRARIES_HXX__ */