Xcos MVC: remove the "Deleted" adapter
[scilab.git] / scilab / modules / scicos / sci_gateway / cpp / sci_scicos_new.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
15 #include "gw_scicos.hxx"
16
17 #include "types.hxx"
18 #include "string.hxx"
19 #include "tlist.hxx"
20 #include "mlist.hxx"
21 #include "list.hxx"
22 #include "function.hxx"
23
24 #include "view_scilab/Adapters.hxx"
25 #include "view_scilab/BaseAdapter.hxx"
26 #include "view_scilab/BlockAdapter.hxx"
27 #include "view_scilab/CprAdapter.hxx"
28 #include "view_scilab/DiagramAdapter.hxx"
29 #include "view_scilab/GraphicsAdapter.hxx"
30 #include "view_scilab/LinkAdapter.hxx"
31 #include "view_scilab/ModelAdapter.hxx"
32 #include "view_scilab/ParamsAdapter.hxx"
33 #include "view_scilab/ScsAdapter.hxx"
34 #include "view_scilab/StateAdapter.hxx"
35 #include "view_scilab/TextAdapter.hxx"
36
37 #include "utilities.hxx"
38 #include "Controller.hxx"
39 #include "model/Annotation.hxx"
40 #include "model/Block.hxx"
41 #include "model/Diagram.hxx"
42 #include "model/Link.hxx"
43
44 extern "C"
45 {
46 #include "Scierror.h"
47 #include "localization.h"
48 }
49
50 static const std::string funame = "scicos_new";
51
52 using namespace org_scilab_modules_scicos;
53
54 template<class Adaptor, class Adaptee>
55 types::InternalType * alloc_and_set(kind_t k, types::String* type_name, types::typed_list &in)
56 {
57     Controller controller;
58
59     // create the associated object
60     ScicosID o = controller.createObject(k);
61     Adaptor* adaptor = new Adaptor(controller, controller.getObject<Adaptee>(o));
62
63     // the first header entry is the type
64     for (int i = 1; i < (int)in.size(); i++)
65     {
66         std::wstring name (type_name->get(i));
67         if (!adaptor->setProperty(name, in[i], controller))
68         {
69             Scierror(999, _("%s: Wrong value for input argument #%d: unable to set \"%ls\".\n"), funame.data(), i, name.data());
70             delete adaptor;
71             return 0;
72         }
73     }
74
75     return adaptor;
76 }
77
78 template<class Adaptor, class Adaptee>
79 types::InternalType * alloc_and_set_as_tlist(types::String* type_name, types::typed_list &in)
80 {
81     // check header
82     // The default constructor should be implemented for this Adapter
83     Adaptor adaptor;
84     for (int i = 1; i < (int)in.size(); i++)
85     {
86         std::wstring name(type_name->get(i));
87         if (!adaptor.hasProperty(name))
88         {
89             Scierror(999, _("%s: Wrong value for input argument #%d: unable to set \"%ls\".\n"), funame.data(), i, name.data());
90             return 0;
91         }
92     }
93
94     // copy the data
95     types::TList* tlist = new types::TList();
96     tlist->set(0, type_name->clone());
97     for (int i = 1; i < (int)in.size(); i++)
98     {
99         tlist->set(i, in[i]);
100     }
101
102     return tlist;
103 }
104
105 template<class Adaptor, class Adaptee>
106 types::InternalType * alloc_and_set_as_mlist(types::String* type_name, types::typed_list &in)
107 {
108     // check header
109     // The default constructor should be implemented for this Adapter
110     Adaptor adaptor;
111     for (int i = 1; i < (int)in.size(); i++)
112     {
113         std::wstring name(type_name->get(i));
114         if (!adaptor.hasProperty(name))
115         {
116             Scierror(999, _("%s: Wrong value for input argument #%d: unable to set \"%ls\".\n"), funame.data(), i, name.data());
117             return 0;
118         }
119     }
120
121     // copy the data
122     types::MList* mlist = new types::MList();
123     mlist->set(0, type_name->clone());
124     for (int i = 1; i < (int)in.size(); i++)
125     {
126         mlist->set(i, in[i]);
127     }
128
129     return mlist;
130 }
131
132 types::Function::ReturnValue sci_scicos_new(types::typed_list &in, int _iRetCount, types::typed_list &out)
133 {
134     if (in.size() < 1)
135     {
136         Scierror(999, _("%s: Wrong number of input arguments: At least %d expected.\n"), funame.data(), 1);
137         return types::Function::Error;
138     }
139     if (_iRetCount > 1)
140     {
141         Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), funame.data(), 1);
142         return types::Function::Error;
143     }
144
145     types::InternalType* type = in[0];
146     if (type->getType() != types::InternalType::ScilabString)
147     {
148         Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), funame.data(), 1);
149         return types::Function::Error;
150     }
151
152     types::String* type_name = type->getAs<types::String>();
153     if (type_name->getRows() > 1)
154     {
155         Scierror(999, _("%s: Wrong size for input argument #%d: Row vector expected.\n"), funame.data(), 1);
156         return types::Function::Error;
157     }
158
159     if (type_name->getCols() != (int) in.size())
160     {
161         Scierror(999, _("%s: Wrong number of input argument: %d expected.\n"), funame.data(), type_name->getCols());
162         return types::Function::Error;
163     }
164
165     /*
166      * allocate the right adapter then try to set fields values
167      */
168
169     const view_scilab::Adapters::adapters_index_t adapter_index = view_scilab::Adapters::instance().lookup_by_typename(type_name->get(0));
170     types::InternalType* returnType;
171     switch (adapter_index)
172     {
173         case view_scilab::Adapters::BLOCK_ADAPTER:
174             returnType = alloc_and_set<view_scilab::BlockAdapter, model::Block>(BLOCK, type_name, in);
175             if (returnType == 0)
176             {
177                 return types::Function::Error;
178             }
179             out.push_back(returnType);
180             break;
181         case view_scilab::Adapters::CPR_ADAPTER:
182             returnType = alloc_and_set_as_tlist<view_scilab::CprAdapter, model::Diagram>(type_name, in);
183             if (returnType == 0)
184             {
185                 return types::Function::Error;
186             }
187             out.push_back(returnType);
188             break;
189         case view_scilab::Adapters::DIAGRAM_ADAPTER:
190             returnType = alloc_and_set<view_scilab::DiagramAdapter, model::Diagram>(DIAGRAM, type_name, in);
191             if (returnType == 0)
192             {
193                 return types::Function::Error;
194             }
195             out.push_back(returnType);
196             break;
197         case view_scilab::Adapters::GRAPHIC_ADAPTER:
198             returnType = alloc_and_set_as_mlist<view_scilab::GraphicsAdapter, model::Block>(type_name, in);
199             if (returnType == 0)
200             {
201                 return types::Function::Error;
202             }
203             out.push_back(returnType);
204             break;
205         case view_scilab::Adapters::LINK_ADAPTER:
206             returnType = alloc_and_set<view_scilab::LinkAdapter, model::Link>(LINK, type_name, in);
207             if (returnType == 0)
208             {
209                 return types::Function::Error;
210             }
211             out.push_back(returnType);
212             break;
213         case view_scilab::Adapters::MODEL_ADAPTER:
214             returnType = alloc_and_set_as_mlist<view_scilab::ModelAdapter, model::Block>(type_name, in);
215             if (returnType == 0)
216             {
217                 return types::Function::Error;
218             }
219             out.push_back(returnType);
220             break;
221         case view_scilab::Adapters::PARAMS_ADAPTER:
222             returnType = alloc_and_set_as_tlist<view_scilab::ParamsAdapter, model::Diagram>(type_name, in);
223             if (returnType == 0)
224             {
225                 return types::Function::Error;
226             }
227             out.push_back(returnType);
228             break;
229         case view_scilab::Adapters::SCS_ADAPTER:
230             returnType = alloc_and_set<view_scilab::ScsAdapter, model::Diagram>(DIAGRAM, type_name, in);
231             if (returnType == 0)
232             {
233                 return types::Function::Error;
234             }
235             out.push_back(returnType);
236             break;
237         case view_scilab::Adapters::STATE_ADAPTER:
238             returnType = alloc_and_set_as_tlist<view_scilab::StateAdapter, model::Diagram>(type_name, in);
239             if (returnType == 0)
240             {
241                 return types::Function::Error;
242             }
243             out.push_back(returnType);
244             break;
245         case view_scilab::Adapters::TEXT_ADAPTER:
246             returnType = alloc_and_set<view_scilab::TextAdapter, model::Annotation>(ANNOTATION, type_name, in);
247             if (returnType == 0)
248             {
249                 return types::Function::Error;
250             }
251             out.push_back(returnType);
252             break;
253         default:
254             Scierror(999, _("%s: Wrong value for input argument #%d:  \"%ls\" type is not managed.\n"), funame.data(), 1, type_name->get(0));
255             return types::Function::Error;
256             break;
257     }
258
259     return types::Function::OK;
260 }