2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2014 - Scilab Enterprises - Anais AUBERT
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
12 /*--------------------------------------------------------------------------*/
14 #include "execvisitor.hxx"
16 #include "signalprocessingfunctions.hxx"
20 #include "elem_common.h"
22 #include "localization.h"
25 /*--------------------------------------------------------------------------*/
27 std::map<__threadId, Signalprocessingfunctions*> Signalprocessing::m_mapSignalprocessingfunctions;
29 void Signalprocessing::addSignalprocessingfunctions(Signalprocessingfunctions* _spFunction)
31 types::ThreadId* pThread = ConfigVariable::getLastRunningThread();
32 m_mapSignalprocessingfunctions[pThread->getThreadId()] = _spFunction;
35 void Signalprocessing::removeSignalprocessingfunctions()
37 types::ThreadId* pThread = ConfigVariable::getLastRunningThread();
38 m_mapSignalprocessingfunctions.erase(pThread->getThreadId());
41 Signalprocessingfunctions* Signalprocessing::getSignalprocessingfunctions()
43 types::ThreadId* pThread = ConfigVariable::getLastRunningThread();
44 return m_mapSignalprocessingfunctions[pThread->getThreadId()];
47 Signalprocessingfunctions::Signalprocessingfunctions(std::wstring callerName)
50 m_wstrCaller = callerName;
55 m_pStringDgetxDyn = NULL;
56 m_pStringDgetyDyn = NULL;
58 m_pStringDgetxStatic = NULL;
59 m_pStringDgetyStatic = NULL;
61 // init static functions
62 if (callerName == L"corr")
64 m_staticFunctionMap[L"corexx"] = (void*) C2F(corexx);
65 m_staticFunctionMap[L"corexy"] = (void*) C2F(corexy);
70 void Signalprocessingfunctions::execFunctionDgetx(double* x, int* siz, int* iss)
75 callDgetx(x, siz, iss);
77 else if (m_pStringDgetxDyn)
79 ConfigVariable::EntryPointStr* func = ConfigVariable::getEntryPoint(m_pStringDgetxDyn->get(0));
82 sprintf(errorMsg, _("Undefined fonction '%ls'.\n"), m_pStringDgetxDyn->get(0));
83 throw ast::ScilabError(errorMsg);
85 ((dgetx_f_t)(func->functionPtr))(x, siz, iss);
87 else if (m_pStringDgetxStatic)// function static
89 ((dgetx_f_t)m_staticFunctionMap[m_pStringDgetxStatic->get(0)])(x, siz, iss);
93 sprintf(errorMsg, _("User function '%s' have not been setted.\n"), "g");
94 throw ast::ScilabError(errorMsg);
98 void Signalprocessingfunctions::setDgetx(types::Callable* _dgetx)
100 m_pCallDgetx = _dgetx;
103 void Signalprocessingfunctions::setDgetx(types::String* _dgetx)
105 m_pStringDgetxStatic = _dgetx;
107 void Signalprocessingfunctions::setDgety(types::Callable* _dgety)
109 m_pCallDgety = _dgety;
111 void Signalprocessingfunctions::setDgety(types::String* _dgety)
113 m_pStringDgetyStatic = _dgety;
116 //param for fortran call
117 void dgetx_f(double* x, int* siz, int* iss)
119 Signalprocessingfunctions* spFunction = NULL;
120 spFunction = Signalprocessing::getSignalprocessingfunctions();
122 if (spFunction == NULL)
124 throw ast::ScilabError(_("An error occurred while getting Signalprocessingfunctions object.\n"));
127 spFunction->execFunctionDgetx(x, siz, iss);
130 void Signalprocessingfunctions::execFunctionDgety(double* y, int* siz, int* iss)
135 callDgety(y, siz, iss);
138 else if (m_pStringDgetxDyn)
140 ConfigVariable::EntryPointStr* func = ConfigVariable::getEntryPoint(m_pStringDgetyDyn->get(0));
143 sprintf(errorMsg, _("Undefined fonction '%ls'.\n"), m_pStringDgetyDyn->get(0));
144 throw ast::ScilabError(errorMsg);
146 ((dgety_f_t)(func->functionPtr))(y, siz, iss);
148 else if (m_pStringDgetyStatic)// function static
150 ((dgety_f_t)m_staticFunctionMap[m_pStringDgetyStatic->get(0)])(y, siz, iss);
154 sprintf(errorMsg, _("User function '%s' have not been setted.\n"), "g");
155 throw ast::ScilabError(errorMsg);
159 //param for fortran call
160 void dgety_f(double* y, int* siz, int* iss)
162 Signalprocessingfunctions* spFunction = NULL;
163 spFunction = Signalprocessing::getSignalprocessingfunctions();
165 if (spFunction == NULL)
167 throw ast::ScilabError(_("An error occurred while getting Signalprocessingfunctions object.\n"));
170 spFunction->execFunctionDgety(y, siz, iss);
173 void Signalprocessingfunctions::callDgety(double* y, int* siz, int* iss)
181 types::optional_list opt;
182 ast::ExecVisitor execFunc;
184 types::Double* pDblY = new types::Double(*siz);
187 types::Double* pDblT = new types::Double(*iss);
188 pDblT->IncreaseRef();
189 pDblY->IncreaseRef();
197 for (int i = 0; i < (int)m_FArgs.size(); i++)
199 m_FArgs[i]->IncreaseRef();
200 in.push_back(m_FArgs[i]);
203 bool bOk = m_pCallDgety->call(in, opt, iRetCount, out, &execFunc) == types::Function::OK;
205 for (int i = 0; i < (int)m_FArgs.size(); i++)
207 m_FArgs[i]->DecreaseRef();
212 sprintf(errorMsg, _("%ls: error while calling user function.\n"), m_pCallDgety->getName().c_str());
213 throw ast::ScilabError(errorMsg);
216 if (out.size() != iRetCount)
218 char* pstrName = wide_string_to_UTF8(m_pCallDgety->getName().c_str());
219 sprintf(errorMsg, _("%s: Wrong number of input argument(s): %d expected.\n"), pstrName, iRetCount);
221 throw ast::ScilabError(errorMsg);
224 out[0]->IncreaseRef();
226 pDblT->DecreaseRef();
227 if (pDblT->isDeletable())
233 pDblY->DecreaseRef();
234 if (pDblY->isDeletable())
240 out[0]->DecreaseRef();
242 if (out[0]->isDouble() == false)
244 char* pstrName = wide_string_to_UTF8(m_pCallDgety->getName().c_str());
245 sprintf(errorMsg, _("%s: Wrong type for output argument #%d: Real matrix expected.\n"), pstrName, 1);
247 throw ast::ScilabError(errorMsg);
249 types::Double* pDblOut = out[0]->getAs<types::Double>();
250 if (pDblOut->isComplex())
252 char* pstrName = wide_string_to_UTF8(m_pCallDgety->getName().c_str());
253 sprintf(errorMsg, _("%s: Wrong type for output argument #%d: Real matrix expected.\n"), pstrName, 1);
255 throw ast::ScilabError(errorMsg);
258 C2F(dcopy)(siz, pDblOut->get(), &one, y, &one);
261 if (out[0]->isDeletable())
267 void Signalprocessingfunctions::callDgetx(double* x, int* siz, int* iss)
275 types::optional_list opt;
276 ast::ExecVisitor execFunc;
278 types::Double* pDblX = new types::Double(*siz);
281 types::Double* pDblT = new types::Double(*iss);
282 pDblT->IncreaseRef();
283 pDblX->IncreaseRef();
291 for (int i = 0; i < (int)m_FArgs.size(); i++)
293 m_FArgs[i]->IncreaseRef();
294 in.push_back(m_FArgs[i]);
297 bool bOk = m_pCallDgetx->call(in, opt, iRetCount, out, &execFunc) == types::Function::OK;
299 for (int i = 0; i < (int)m_FArgs.size(); i++)
301 m_FArgs[i]->DecreaseRef();
306 sprintf(errorMsg, _("%ls: error while calling user function.\n"), m_pCallDgetx->getName().c_str());
307 throw ast::ScilabError(errorMsg);
310 if (out.size() != iRetCount)
312 char* pstrName = wide_string_to_UTF8(m_pCallDgetx->getName().c_str());
313 sprintf(errorMsg, _("%s: Wrong number of input argument(s): %d expected.\n"), pstrName, iRetCount);
315 throw ast::ScilabError(errorMsg);
318 out[0]->IncreaseRef();
320 pDblT->DecreaseRef();
321 if (pDblT->isDeletable())
327 pDblX->DecreaseRef();
328 if (pDblX->isDeletable())
334 out[0]->DecreaseRef();
336 if (out[0]->isDouble() == false)
338 char* pstrName = wide_string_to_UTF8(m_pCallDgetx->getName().c_str());
339 sprintf(errorMsg, _("%s: Wrong type for output argument #%d: Real matrix expected.\n"), pstrName, 1);
341 throw ast::ScilabError(errorMsg);
343 types::Double* pDblOut = out[0]->getAs<types::Double>();
344 if (pDblOut->isComplex())
346 char* pstrName = wide_string_to_UTF8(m_pCallDgetx->getName().c_str());
347 sprintf(errorMsg, _("%s: Wrong type for output argument #%d: Real matrix expected.\n"), pstrName, 1);
349 throw ast::ScilabError(errorMsg);
352 C2F(dcopy)(siz, pDblOut->get(), &one, x, &one);
355 if (out[0]->isDeletable())