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