001fc131798e402cac8e6308b73e6ded45319760
[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
17 #include "double.hxx"
18 #include "string.hxx"
19 #include "list.hxx"
20 #include "tlist.hxx"
21
22 #include "utilities.hxx"
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             path = 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() != 6 && 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         // In case the last parameter is missing
155         if (current->getSize() == 6)
156         {
157             tol[7] = 0;
158         }
159
160         controller.setObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
161         return true;
162     }
163 };
164
165 struct tf
166 {
167
168     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
169     {
170         ScicosID adaptee = adaptor.getAdaptee()->id();
171
172         std::vector<double> tf;
173         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tf);
174
175         return new types::Double(tf[0]);
176     }
177
178     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
179     {
180
181         if (v->getType() != types::InternalType::ScilabDouble)
182         {
183             return false;
184         }
185
186         types::Double* current = v->getAs<types::Double>();
187         if (current->getSize() != 1)
188         {
189             return false;
190         }
191
192         ScicosID adaptee = adaptor.getAdaptee()->id();
193
194         std::vector<double> tol;
195         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
196
197         tol[0] = current->get(0);
198
199         controller.setObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
200         return true;
201     }
202 };
203
204 struct context
205 {
206
207     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
208     {
209         ScicosID adaptee = adaptor.getAdaptee()->id();
210
211         std::vector<std::string> context;
212         controller.getObjectProperty(adaptee, DIAGRAM, DIAGRAM_CONTEXT, context);
213
214         if (context.size() == 0)
215         {
216             // An empty context returns an empty matrix
217             return types::Double::Empty();
218         }
219
220         types::String* o = new types::String((int)context.size(), 1);
221         for (int i = 0; i < (int)context.size(); ++i)
222         {
223             o->set(i, context[i].data());
224         }
225
226         return o;
227     }
228
229     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
230     {
231         if (v->getType() == types::InternalType::ScilabString)
232         {
233             types::String* current = v->getAs<types::String>();
234             if (current->getCols() != 0 && current->getCols() != 1)
235             {
236                 return false;
237             }
238
239             ScicosID adaptee = adaptor.getAdaptee()->id();
240
241             std::vector<std::string> context (current->getSize());
242             for (int i = 0; i < (int)context.size(); ++i)
243             {
244                 char* c_str = wide_string_to_UTF8(current->get(i));
245                 context[i] = std::string(c_str);
246                 FREE(c_str);
247             }
248
249             controller.setObjectProperty(adaptee, DIAGRAM, DIAGRAM_CONTEXT, context);
250             return true;
251         }
252         else if (v->getType() == types::InternalType::ScilabDouble)
253         {
254             types::Double* current = v->getAs<types::Double>();
255             if (!current->isEmpty())
256             {
257                 return false;
258             }
259
260             ScicosID adaptee = adaptor.getAdaptee()->id();
261
262             std::vector<std::string> context;
263             controller.setObjectProperty(adaptee, DIAGRAM, DIAGRAM_CONTEXT, context);
264             return true;
265         }
266         return false;
267     }
268 };
269
270 struct options
271 {
272
273     static types::InternalType* get(const ParamsAdapter& /*adaptor*/, const Controller& /*controller*/)
274     {
275         // Return a dummy 'scsopt'-typed tlist.
276         types::String* header = new types::String(scsopt.c_str());
277
278         types::TList* Scsopt = new types::TList();
279         Scsopt->append(header);
280         return Scsopt;
281     }
282
283     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
284     {
285         // The model does not store 'options'.
286         return dummy_property::set(adaptor, v, controller);
287     }
288 };
289
290 struct doc
291 {
292     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& /*controller*/)
293     {
294         return adaptor.getDocContent();
295     }
296
297     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& /*controller*/)
298     {
299         adaptor.setDocContent(v->clone());
300         return true;
301     }
302 };
303
304 } /* namespace */
305
306 template<> property<ParamsAdapter>::props_t property<ParamsAdapter>::fields = property<ParamsAdapter>::props_t();
307 static void initialize_fields()
308 {
309     if (property<ParamsAdapter>::properties_have_not_been_set())
310     {
311         property<ParamsAdapter>::fields.reserve(10);
312         property<ParamsAdapter>::add_property(L"wpar", &dummy_property::get, &dummy_property::set);
313         property<ParamsAdapter>::add_property(L"title", &title::get, &title::set);
314         property<ParamsAdapter>::add_property(L"tol", &tol::get, &tol::set);
315         property<ParamsAdapter>::add_property(L"tf", &tf::get, &tf::set);
316         property<ParamsAdapter>::add_property(L"context", &context::get, &context::set);
317         property<ParamsAdapter>::add_property(L"void1", &dummy_property::get, &dummy_property::set);
318         property<ParamsAdapter>::add_property(L"options", &options::get, &options::set);
319         property<ParamsAdapter>::add_property(L"void2", &dummy_property::get, &dummy_property::set);
320         property<ParamsAdapter>::add_property(L"void3", &dummy_property::get, &dummy_property::set);
321         property<ParamsAdapter>::add_property(L"doc", &doc::get, &doc::set);
322     }
323 }
324
325 ParamsAdapter::ParamsAdapter() :
326     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(),
327     doc_content(new types::List())
328 {
329     initialize_fields();
330 }
331
332 ParamsAdapter::ParamsAdapter(const Controller& c, org_scilab_modules_scicos::model::Diagram* adaptee) :
333     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(c, adaptee),
334     doc_content(new types::List())
335 {
336     initialize_fields();
337 }
338
339 ParamsAdapter::~ParamsAdapter()
340 {
341     doc_content->DecreaseRef();
342     doc_content->killMe();
343 }
344
345 std::wstring ParamsAdapter::getTypeStr()
346 {
347     return getSharedTypeStr();
348 }
349 std::wstring ParamsAdapter::getShortTypeStr()
350 {
351     return getSharedTypeStr();
352 }
353
354 types::InternalType* ParamsAdapter::getDocContent() const
355 {
356     doc_content->IncreaseRef();
357     return doc_content;
358 }
359
360 void ParamsAdapter::setDocContent(types::InternalType* v)
361 {
362     doc_content->DecreaseRef();
363     doc_content->killMe();
364
365     v->IncreaseRef();
366     doc_content = v;
367 }
368
369 } /* namespace view_scilab */
370 } /* namespace org_scilab_modules_scicos */