Xcos MVC: add a test and use unnamed functions parameters
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / ParamsAdapter.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014-2014 - Scilab Enterprises - Clement DAVID
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.1-en.txt
10  *
11  */
12
13 #include <string>
14 #include <vector>
15 #include <algorithm>
16 #include <memory>
17
18 #include "double.hxx"
19 #include "string.hxx"
20 #include "list.hxx"
21 #include "tlist.hxx"
22
23 #include "Controller.hxx"
24 #include "ParamsAdapter.hxx"
25
26 extern "C" {
27 #include "sci_malloc.h"
28 #include "charEncoding.h"
29 }
30
31 namespace org_scilab_modules_scicos
32 {
33 namespace view_scilab
34 {
35 namespace
36 {
37
38 const std::wstring scsopt(L"scsopt");
39
40 struct dummy_property
41 {
42
43     static types::InternalType* get(const ParamsAdapter& /*adaptor*/, const Controller& /*controller*/)
44     {
45         // Return a default empty matrix.
46         return types::Double::Empty();
47     }
48
49     static bool set(ParamsAdapter& /*adaptor*/, types::InternalType* /*v*/, Controller& /*controller*/)
50     {
51         // everything should be right as the properties mapped using this adapter do not perform anything
52         return true;
53     }
54 };
55
56 struct title
57 {
58
59     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
60     {
61         ScicosID adaptee = adaptor.getAdaptee()->id();
62
63         std::string title;
64         controller.getObjectProperty(adaptee, DIAGRAM, TITLE, title);
65         std::string path;
66         controller.getObjectProperty(adaptee, DIAGRAM, PATH, path);
67
68         types::String* o = new types::String(2, 1);
69         o->set(0, title.data());
70         o->set(1, path.data());
71
72         return o;
73     }
74
75     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
76     {
77         if (v->getType() != types::InternalType::ScilabString)
78         {
79             return false;
80         }
81
82         ScicosID adaptee = adaptor.getAdaptee()->id();
83
84         std::string path;
85         std::string title;
86         types::String* current = v->getAs<types::String>();
87         if (current->getSize() == 1)
88         {
89             // Pass an empty path
90         }
91         else if (current->getSize() == 2)
92         {
93             char* Path = wide_string_to_UTF8(current->get(1));
94             title = std::string(Path);
95             FREE(Path);
96         }
97         else
98         {
99             return false;
100         }
101
102         char* Title = wide_string_to_UTF8(current->get(0));
103         title = std::string(Title);
104         FREE(Title);
105
106         controller.setObjectProperty(adaptee, DIAGRAM, TITLE, title);
107         controller.setObjectProperty(adaptee, DIAGRAM, PATH, path);
108         return true;
109     }
110 };
111
112 struct tol
113 {
114
115     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
116     {
117         ScicosID adaptee = adaptor.getAdaptee()->id();
118
119         double* data;
120         types::Double* o = new types::Double(1, 7, &data);
121
122         std::vector<double> tol;
123         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
124 #ifdef _MSC_VER
125         std::copy(tol.begin() + 1, tol.end(), stdext::checked_array_iterator<double*>( data, 7 ));
126 #else
127         std::copy(tol.begin() + 1, tol.end(), data);
128 #endif
129
130         return o;
131     }
132
133     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
134     {
135
136         if (v->getType() != types::InternalType::ScilabDouble)
137         {
138             return false;
139         }
140
141         types::Double* current = v->getAs<types::Double>();
142         if (current->getSize() != 7)
143         {
144             return false;
145         }
146
147         ScicosID adaptee = adaptor.getAdaptee()->id();
148
149         std::vector<double> tol;
150         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
151
152         std::copy(current->getReal(), current->getReal() + current->getSize(), tol.begin() + 1);
153
154         controller.setObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
155         return true;
156     }
157 };
158
159 struct tf
160 {
161
162     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
163     {
164         ScicosID adaptee = adaptor.getAdaptee()->id();
165
166         std::vector<double> tf;
167         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tf);
168
169         return new types::Double(tf[0]);
170     }
171
172     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
173     {
174
175         if (v->getType() != types::InternalType::ScilabDouble)
176         {
177             return false;
178         }
179
180         types::Double* current = v->getAs<types::Double>();
181         if (current->getSize() != 1)
182         {
183             return false;
184         }
185
186         ScicosID adaptee = adaptor.getAdaptee()->id();
187
188         std::vector<double> tol;
189         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
190
191         tol[0] = current->get(0);
192
193         controller.setObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
194         return true;
195     }
196 };
197
198 struct context
199 {
200
201     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
202     {
203         ScicosID adaptee = adaptor.getAdaptee()->id();
204
205         std::vector<std::string> context;
206         controller.getObjectProperty(adaptee, DIAGRAM, DIAGRAM_CONTEXT, context);
207
208         if (context.size() == 0)
209         {
210             // An empty context returns an empty matrix
211             return types::Double::Empty();
212         }
213
214         types::String* o = new types::String((int)context.size(), 1);
215         for (int i = 0; i < (int)context.size(); ++i)
216         {
217             o->set(i, context[i].data());
218         }
219
220         return o;
221     }
222
223     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
224     {
225         if (v->getType() == types::InternalType::ScilabString)
226         {
227             types::String* current = v->getAs<types::String>();
228             if (current->getCols() != 0 && current->getCols() != 1)
229             {
230                 return false;
231             }
232
233             ScicosID adaptee = adaptor.getAdaptee()->id();
234
235             std::vector<std::string> context (current->getSize());
236             for (int i = 0; i < (int)context.size(); ++i)
237             {
238                 char* c_str = wide_string_to_UTF8(current->get(i));
239                 context[i] = std::string(c_str);
240                 FREE(c_str);
241             }
242
243             controller.setObjectProperty(adaptee, DIAGRAM, DIAGRAM_CONTEXT, context);
244             return true;
245         }
246         else if (v->getType() == types::InternalType::ScilabDouble)
247         {
248             types::Double* current = v->getAs<types::Double>();
249             if (current->getRows() != 0 || current->getCols() != 0)
250             {
251                 return false;
252             }
253
254             ScicosID adaptee = adaptor.getAdaptee()->id();
255
256             std::vector<std::string> context;
257             controller.setObjectProperty(adaptee, DIAGRAM, DIAGRAM_CONTEXT, context);
258             return true;
259         }
260         return false;
261     }
262 };
263
264 struct options
265 {
266
267     static types::InternalType* get(const ParamsAdapter& /*adaptor*/, const Controller& /*controller*/)
268     {
269         // Return a dummy 'scsopt'-typed tlist.
270         types::String* header = new types::String(scsopt.c_str());
271
272         types::TList* Scsopt = new types::TList();
273         Scsopt->set(0, header);
274         return Scsopt;
275     }
276
277     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
278     {
279         // The model does not store 'options'.
280         return dummy_property::set(adaptor, v, controller);
281     }
282 };
283
284 struct doc
285 {
286     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& /*controller*/)
287     {
288         return adaptor.getDocContent();
289     }
290
291     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& /*controller*/)
292     {
293         adaptor.setDocContent(v->clone());
294         return true;
295     }
296 };
297
298 } /* namespace */
299
300 template<> property<ParamsAdapter>::props_t property<ParamsAdapter>::fields = property<ParamsAdapter>::props_t();
301
302 ParamsAdapter::ParamsAdapter(std::shared_ptr<org_scilab_modules_scicos::model::Diagram> adaptee) :
303     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(adaptee),
304     doc_content(new types::List())
305 {
306     if (property<ParamsAdapter>::properties_have_not_been_set())
307     {
308         property<ParamsAdapter>::fields.reserve(10);
309         property<ParamsAdapter>::add_property(L"wpar", &dummy_property::get, &dummy_property::set);
310         property<ParamsAdapter>::add_property(L"title", &title::get, &title::set);
311         property<ParamsAdapter>::add_property(L"tol", &tol::get, &tol::set);
312         property<ParamsAdapter>::add_property(L"tf", &tf::get, &tf::set);
313         property<ParamsAdapter>::add_property(L"context", &context::get, &context::set);
314         property<ParamsAdapter>::add_property(L"void1", &dummy_property::get, &dummy_property::set);
315         property<ParamsAdapter>::add_property(L"options", &options::get, &options::set);
316         property<ParamsAdapter>::add_property(L"void2", &dummy_property::get, &dummy_property::set);
317         property<ParamsAdapter>::add_property(L"void3", &dummy_property::get, &dummy_property::set);
318         property<ParamsAdapter>::add_property(L"doc", &doc::get, &doc::set);
319     }
320 }
321
322 ParamsAdapter::ParamsAdapter(const ParamsAdapter& adapter) :
323     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(adapter),
324     doc_content(adapter.getDocContent())
325 {
326 }
327
328 ParamsAdapter::~ParamsAdapter()
329 {
330     doc_content->DecreaseRef();
331     doc_content->killMe();
332 }
333
334 std::wstring ParamsAdapter::getTypeStr()
335 {
336     return getSharedTypeStr();
337 }
338 std::wstring ParamsAdapter::getShortTypeStr()
339 {
340     return getSharedTypeStr();
341 }
342
343 types::InternalType* ParamsAdapter::getDocContent() const
344 {
345     doc_content->IncreaseRef();
346     return doc_content;
347 }
348
349 void ParamsAdapter::setDocContent(types::InternalType* v)
350 {
351     doc_content->DecreaseRef();
352     doc_content->killMe();
353
354     v->IncreaseRef();
355     doc_content = v;
356 }
357
358 } /* namespace view_scilab */
359 } /* namespace org_scilab_modules_scicos */