/
sci_scicos_new.cpp
325 lines (291 loc) · 10.7 KB
/
sci_scicos_new.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2014-2014 - Scilab Enterprises - Clement DAVID
*
* 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
*
*/
#include <string>
#include "../../includes/view_scilab/Adapters.hxx"
#include "gw_scicos.hxx"
#include "types.hxx"
#include "string.hxx"
#include "tlist.hxx"
#include "mlist.hxx"
#include "list.hxx"
#include "function.hxx"
#include "view_scilab/BaseAdapter.hxx"
#include "view_scilab/BlockAdapter.hxx"
#include "view_scilab/CprAdapter.hxx"
#include "view_scilab/DiagramAdapter.hxx"
#include "view_scilab/GraphicsAdapter.hxx"
#include "view_scilab/LinkAdapter.hxx"
#include "view_scilab/ModelAdapter.hxx"
#include "view_scilab/ParamsAdapter.hxx"
#include "view_scilab/ScsAdapter.hxx"
#include "view_scilab/StateAdapter.hxx"
#include "view_scilab/TextAdapter.hxx"
#include "utilities.hxx"
#include "Controller.hxx"
#include "model/Annotation.hxx"
#include "model/Block.hxx"
#include "model/Diagram.hxx"
#include "model/Link.hxx"
extern "C"
{
#include "Scierror.h"
#include "localization.h"
}
static const std::string funame = "scicos_new";
using namespace org_scilab_modules_scicos;
template<class Adaptor, class Adaptee>
types::InternalType * alloc_and_set(kind_t k, types::String* type_name, types::typed_list &in)
{
Controller controller;
// create the associated object
ScicosID o = controller.createObject(k);
Adaptor* adaptor = new Adaptor(controller, controller.getObject<Adaptee>(o));
// the first header entry is the type
for (int i = 1; i < (int)in.size(); i++)
{
std::wstring name (type_name->get(i));
if (!adaptor->setProperty(name, in[i], controller))
{
Scierror(999, _("%s: Wrong value for input argument #%d: unable to set \"%ls\".\n"), funame.data(), i, name.data());
delete adaptor;
return 0;
}
}
return adaptor;
}
template<class Adaptor, class Adaptee>
types::InternalType * alloc_and_set_as_tlist(types::String* type_name, types::typed_list &in)
{
// check header
// The default constructor should be implemented for this Adapter
Adaptor adaptor;
for (int i = 1; i < (int)in.size(); i++)
{
std::wstring name(type_name->get(i));
if (!adaptor.hasProperty(name))
{
Scierror(999, _("%s: Wrong value for input argument #%d: unable to set \"%ls\".\n"), funame.data(), i, name.data());
return 0;
}
}
// copy the data
types::TList* tlist = new types::TList();
tlist->set(0, type_name->clone());
for (int i = 1; i < (int)in.size(); i++)
{
tlist->set(i, in[i]);
}
return tlist;
}
template<class Adaptor, class Adaptee>
types::InternalType * alloc_and_set_as_mlist(types::String* type_name, types::typed_list &in)
{
// check header
// The default constructor should be implemented for this Adapter
Adaptor adaptor;
for (int i = 1; i < (int)in.size(); i++)
{
std::wstring name(type_name->get(i));
if (!adaptor.hasProperty(name))
{
Scierror(999, _("%s: Wrong value for input argument #%d: unable to set \"%ls\".\n"), funame.data(), i, name.data());
return 0;
}
}
// copy the data
types::MList* mlist = new types::MList();
mlist->set(0, type_name->clone());
for (int i = 1; i < (int)in.size(); i++)
{
mlist->set(i, in[i]);
}
return mlist;
}
types::Function::ReturnValue allocate(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
types::InternalType* type = in[0];
types::String* type_name = type->getAs<types::String>();
if (type_name->getRows() > 1)
{
Scierror(999, _("%s: Wrong size for input argument #%d: Row vector expected.\n"), funame.data(), 1);
return types::Function::Error;
}
if (type_name->getCols() != (int) in.size())
{
Scierror(999, _("%s: Wrong number of input argument: %d expected.\n"), funame.data(), type_name->getCols());
return types::Function::Error;
}
/*
* allocate the right adapter then try to set fields values
*/
const view_scilab::Adapters::adapters_index_t adapter_index = view_scilab::Adapters::instance().lookup_by_typename(type_name->get(0));
types::InternalType* returnType;
switch (adapter_index)
{
case view_scilab::Adapters::BLOCK_ADAPTER:
returnType = alloc_and_set<view_scilab::BlockAdapter, model::Block>(BLOCK, type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
case view_scilab::Adapters::CPR_ADAPTER:
returnType = alloc_and_set_as_tlist<view_scilab::CprAdapter, model::Diagram>(type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
case view_scilab::Adapters::DIAGRAM_ADAPTER:
returnType = alloc_and_set<view_scilab::DiagramAdapter, model::Diagram>(DIAGRAM, type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
case view_scilab::Adapters::GRAPHIC_ADAPTER:
returnType = alloc_and_set_as_mlist<view_scilab::GraphicsAdapter, model::Block>(type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
case view_scilab::Adapters::LINK_ADAPTER:
returnType = alloc_and_set<view_scilab::LinkAdapter, model::Link>(LINK, type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
case view_scilab::Adapters::MODEL_ADAPTER:
returnType = alloc_and_set_as_mlist<view_scilab::ModelAdapter, model::Block>(type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
case view_scilab::Adapters::PARAMS_ADAPTER:
returnType = alloc_and_set_as_tlist<view_scilab::ParamsAdapter, model::Diagram>(type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
case view_scilab::Adapters::SCS_ADAPTER:
returnType = alloc_and_set<view_scilab::ScsAdapter, model::Diagram>(DIAGRAM, type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
case view_scilab::Adapters::STATE_ADAPTER:
returnType = alloc_and_set_as_tlist<view_scilab::StateAdapter, model::Diagram>(type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
case view_scilab::Adapters::TEXT_ADAPTER:
returnType = alloc_and_set<view_scilab::TextAdapter, model::Annotation>(ANNOTATION, type_name, in);
if (returnType == 0)
{
return types::Function::Error;
}
out.push_back(returnType);
break;
default:
Scierror(999, _("%s: Wrong value for input argument #%d: \"%ls\" type is not managed.\n"), funame.data(), 1, type_name->get(0));
return types::Function::Error;
break;
}
return types::Function::OK;
}
types::Function::ReturnValue get(types::Double* UIDs, int _iRetCount, types::typed_list &out)
{
if (UIDs->getSize() != _iRetCount)
{
Scierror(999, _("%s: Wrong size for input argument #%d: %dx%d expected.\n"), funame.data(), 1, _iRetCount, 1);
return types::Function::Error;
}
Controller controller;
types::Function::ReturnValue retValue = types::Function::OK;
for (int i = 0; i < _iRetCount; ++i)
{
ScicosID uid = UIDs->get(i);
// create the associated object
model::BaseObject* o = controller.getObject(uid);
if (o == nullptr)
{
Scierror(999, _("%s: Wrong value for input argument #%d: invalid UID.\n"), funame.data(), 1);
retValue = types::Function::Error;
break;
}
switch (o->kind())
{
case DIAGRAM:
out.push_back(new view_scilab::DiagramAdapter(controller, static_cast<model::Diagram*>(controller.referenceObject(o))));
break;
case BLOCK:
out.push_back(new view_scilab::BlockAdapter(controller, static_cast<model::Block*>(controller.referenceObject(o))));
break;
case LINK:
out.push_back(new view_scilab::LinkAdapter(controller, static_cast<model::Link*>(controller.referenceObject(o))));
break;
default:
Scierror(999, _("%s: Wrong value for input argument #%d: not handled kind.\n"), funame.data(), 1);
retValue = types::Function::Error;
break;
}
}
if (retValue != types::Function::OK)
{
// something goes wrong, release the allocated data
for (types::typed_list::iterator it = out.begin(); it != out.end(); ++it)
{
delete *it;
}
}
return retValue;
}
types::Function::ReturnValue sci_scicos_new(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
if (in.size() < 1)
{
Scierror(999, _("%s: Wrong number of input arguments: At least %d expected.\n"), funame.data(), 1);
return types::Function::Error;
}
if (_iRetCount > 1)
{
Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), funame.data(), 1);
return types::Function::Error;
}
types::InternalType* type = in[0];
switch (type->getType())
{
case types::InternalType::ScilabString:
return allocate(in, _iRetCount, out);
case types::InternalType::ScilabDouble:
return get(type->getAs<types::Double>(), _iRetCount, out);
default:
Scierror(999, _("%s: Wrong type for input argument #%d: String or ID expected.\n"), funame.data(), 1);
return types::Function::Error;
}
}