Xcos MVC: implement model.sim and anticipating on future model properties.
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / ModelAdapter.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-en.txt
10  *
11  */
12
13 #include "list.hxx"
14 #include "double.hxx"
15 #include "string.hxx"
16
17 #include "Controller.hxx"
18 #include "ModelAdapter.hxx"
19 #include "ports_management.hxx"
20
21 namespace org_scilab_modules_scicos
22 {
23 namespace view_scilab
24 {
25
26 struct sim
27 {
28
29     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
30     {
31         model::Block* adaptee = adaptor.getAdaptee();
32
33         // First, extact the function Name
34         std::string name;
35         controller.getObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
36         types::String* Name = new types::String(1, 1);
37         Name->set(0, name.data());
38
39         // Then the Api. If it is zero, then just return the Name. Otherwise, return a list containing both.
40         int api;
41         controller.getObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api);
42
43         if (api == 0)
44         {
45             return Name;
46         }
47         else
48         {
49             types::Double* Api = new types::Double(1, 1, (double) api);
50             types::List* o = new types::List();
51             o->set(0, Name);
52             o->set(1, Api);
53             return o;
54         }
55     }
56
57     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
58     {
59         model::Block* adaptee = adaptor.getAdaptee();
60
61         if (v->getType() == types::InternalType::ScilabString)
62         {
63             types::String* current = v->getAs<types::String>();
64             if (current->getSize() != 1)
65             {
66                 return false;
67             }
68
69             char* c_str = wide_string_to_UTF8(current->get(0));
70             std::string name = std::string(c_str);
71             FREE(c_str);
72
73             // If the input is a scalar string, then the functionApi is 0.
74             int api = 0;
75
76             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
77             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api);
78         }
79         else if (v->getType() == types::InternalType::ScilabList)
80         {
81             // If the input is a 2-sized list, then it must be string and positive integer.
82             types::List* current = v->getAs<types::List>();
83             if (current->getSize() != 2)
84             {
85                 return false;
86             }
87             if (current->get(0)->getType() != types::InternalType::ScilabString || current->get(1)->getType() != types::InternalType::ScilabDouble)
88             {
89                 return false;
90             }
91
92             types::String* Name = current->get(0)->getAs<types::String>();
93             if (Name->getSize() != 1)
94             {
95                 return false;
96             }
97             char* c_str = wide_string_to_UTF8(Name->get(0));
98             std::string name = std::string(c_str);
99             FREE(c_str);
100
101             types::Double* Api = current->get(1)->getAs<types::Double>();
102             if (Api->getSize() != 1)
103             {
104                 return false;
105             }
106             double api = Api->get(0);
107             if (floor(api) != api)
108             {
109                 return false;
110             }
111             int api_int = (int) api;
112
113             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
114             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api_int);
115         }
116         else
117         {
118             return false;
119         }
120         return true;
121     }
122 };
123
124 template<> property<ModelAdapter>::props_t property<ModelAdapter>::fields = property<ModelAdapter>::props_t();
125
126 ModelAdapter::ModelAdapter(const ModelAdapter& o) :
127     BaseAdapter<ModelAdapter, org_scilab_modules_scicos::model::Block>(o) { }
128
129 ModelAdapter::ModelAdapter(org_scilab_modules_scicos::model::Block* o) :
130     BaseAdapter<ModelAdapter, org_scilab_modules_scicos::model::Block>(o)
131 {
132     if (property<ModelAdapter>::properties_has_not_been_set())
133     {
134         property<ModelAdapter>::fields.reserve(1);
135         property<ModelAdapter>::add_property(L"sim", &sim::get, &sim::set);
136     }
137 }
138
139 ModelAdapter::~ModelAdapter()
140 {
141 }
142
143 bool ModelAdapter::toString(std::wostringstream& ostr)
144 {
145     ostr << L"ModelAdapter.hxx: Dunno what to display there" << std::endl;
146     return true;
147 }
148
149 std::wstring ModelAdapter::getTypeStr()
150 {
151     return getSharedTypeStr();
152 }
153
154 std::wstring ModelAdapter::getShortTypeStr()
155 {
156     return getSharedTypeStr();
157 }
158
159 } /* view_scilab */
160 } /* namespace org_scilab_modules_scicos */