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