0a820945cba0e163c6175b5b0803b06755b51bbb
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / Adapters.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014-2016 - Scilab Enterprises - Clement DAVID
4  *  Copyright (C) 2017-2018 - ESI Group - Clement DAVID
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16
17 #include "../../../includes/view_scilab/Adapters.hxx"
18
19 #include <string>
20 #include <algorithm>
21
22 #include "View.hxx"
23 #include "BlockAdapter.hxx"
24 #include "CprAdapter.hxx"
25 #include "DiagramAdapter.hxx"
26 #include "GraphicsAdapter.hxx"
27 #include "LinkAdapter.hxx"
28 #include "ModelAdapter.hxx"
29 #include "ParamsAdapter.hxx"
30 #include "ScsAdapter.hxx"
31 #include "StateAdapter.hxx"
32 #include "TextAdapter.hxx"
33
34 namespace org_scilab_modules_scicos
35 {
36 namespace view_scilab
37 {
38
39 struct AdapterView : public View
40 {
41     void objectCreated(const ScicosID& uid, kind_t kind) override {};
42     void objectReferenced(const ScicosID& uid, kind_t kind, unsigned refCount) override {};
43     void objectUnreferenced(const ScicosID& uid, kind_t kind, unsigned refCount) override {};
44     void objectDeleted(const ScicosID& uid, kind_t kind) override
45     {
46         switch (kind)
47         {
48             case BLOCK:
49                 GraphicsAdapter::remove_partial_links_information(uid);
50                 break;
51             case LINK:
52                 LinkAdapter::remove_partial_links_information(uid);
53                 break;
54             default:
55                 break;
56         }
57     };
58     void objectCloned(const ScicosID& uid, const ScicosID& cloned, kind_t kind) override
59     {
60         Controller controller;
61
62         switch (kind)
63         {
64             case BLOCK:
65                 GraphicsAdapter::add_partial_links_information(controller, uid, cloned);
66                 break;
67             case LINK:
68                 LinkAdapter::add_partial_links_information(controller, uid, cloned);
69                 break;
70             default:
71                 break;
72         }
73     };
74     void propertyUpdated(const ScicosID& uid, kind_t kind, object_properties_t property, update_status_t status) override {};
75 };
76
77 Adapters Adapters::m_instance;
78
79 Adapters& Adapters::instance()
80 {
81     return m_instance;
82 }
83
84 Adapters::Adapters()
85 {
86     /*
87      * Create the supported data types list
88      */
89     adapters = adapters_t();
90     // we assume that the INVALID_ADAPTER is set at end of the enum
91     adapters.reserve(INVALID_ADAPTER);
92
93     adapters.push_back(adapter_t(view_scilab::BlockAdapter::getSharedTypeStr(), BLOCK_ADAPTER));
94     adapters.push_back(adapter_t(view_scilab::CprAdapter::getSharedTypeStr(), CPR_ADAPTER));
95     adapters.push_back(adapter_t(view_scilab::DiagramAdapter::getSharedTypeStr(), DIAGRAM_ADAPTER));
96     adapters.push_back(adapter_t(view_scilab::GraphicsAdapter::getSharedTypeStr(), GRAPHIC_ADAPTER));
97     adapters.push_back(adapter_t(view_scilab::LinkAdapter::getSharedTypeStr(), LINK_ADAPTER));
98     adapters.push_back(adapter_t(view_scilab::ModelAdapter::getSharedTypeStr(), MODEL_ADAPTER));
99     adapters.push_back(adapter_t(view_scilab::ParamsAdapter::getSharedTypeStr(), PARAMS_ADAPTER));
100     adapters.push_back(adapter_t(view_scilab::ScsAdapter::getSharedTypeStr(), SCS_ADAPTER));
101     adapters.push_back(adapter_t(view_scilab::StateAdapter::getSharedTypeStr(), STATE_ADAPTER));
102     adapters.push_back(adapter_t(view_scilab::TextAdapter::getSharedTypeStr(), TEXT_ADAPTER));
103
104     std::sort(adapters.begin(), adapters.end());
105
106     Controller controller;
107     controller.register_view("Adapters", new AdapterView());
108 }
109
110 Adapters::~Adapters()
111 {
112     adapters.clear();
113 }
114
115 Adapters::adapters_index_t Adapters::lookup_by_typename(const std::wstring& name)
116 {
117     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
118     if (it != adapters.end() && !(name < it->name))
119     {
120         return it->kind;
121     }
122     return INVALID_ADAPTER;
123 }
124
125 std::wstring Adapters::get_typename(Adapters::adapters_index_t kind)
126 {
127     for (auto it : adapters)
128     {
129         if (it.kind == kind)
130         {
131             return it.name;
132         }
133     }
134
135     return L"";
136 }
137
138 model::BaseObject* Adapters::descriptor(types::InternalType* v)
139 {
140     const std::wstring& name = v->getShortTypeStr();
141     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
142     if (v->isUserType() && it != adapters.end() && !(name < it->name))
143     {
144         return descriptor(it->kind, v);
145     }
146     return nullptr;
147 }
148
149 model::BaseObject* Adapters::descriptor(adapters_index_t index, types::InternalType* v)
150 {
151     switch (index)
152     {
153         case BLOCK_ADAPTER:
154             return v->getAs<view_scilab::BlockAdapter>()->getAdaptee();
155         case CPR_ADAPTER:
156             return v->getAs<view_scilab::CprAdapter>()->getAdaptee();
157         case DIAGRAM_ADAPTER:
158             return v->getAs<view_scilab::DiagramAdapter>()->getAdaptee();
159         case GRAPHIC_ADAPTER:
160             return v->getAs<view_scilab::GraphicsAdapter>()->getAdaptee();
161         case LINK_ADAPTER:
162             return v->getAs<view_scilab::LinkAdapter>()->getAdaptee();
163         case MODEL_ADAPTER:
164             return v->getAs<view_scilab::ModelAdapter>()->getAdaptee();
165         case PARAMS_ADAPTER:
166             return v->getAs<view_scilab::ParamsAdapter>()->getAdaptee();
167         case SCS_ADAPTER:
168             return v->getAs<view_scilab::ScsAdapter>()->getAdaptee();
169         case STATE_ADAPTER:
170             return v->getAs<view_scilab::StateAdapter>()->getAdaptee();
171         case TEXT_ADAPTER:
172             return v->getAs<view_scilab::TextAdapter>()->getAdaptee();
173         default:
174             return nullptr;
175     }
176 }
177
178 types::InternalType* Adapters::allocate_view(ScicosID id, kind_t kind)
179 {
180     Controller controller;
181
182     return allocate_view(controller, controller.getBaseObject(id));
183 }
184
185 types::InternalType* Adapters::allocate_view(Controller& controller, model::BaseObject* o)
186 {
187     switch (o->kind())
188     {
189         case BLOCK:
190                 return new view_scilab::BlockAdapter(controller, static_cast<model::Block*>(o));
191         case DIAGRAM:
192                 return new view_scilab::DiagramAdapter(controller, static_cast<model::Diagram*>(o));
193         case ANNOTATION:
194                 return new view_scilab::TextAdapter(controller, static_cast<model::Annotation*>(o));
195         case LINK:
196             return new view_scilab::LinkAdapter(controller, static_cast<model::Link*>(o));
197         default:
198             return nullptr;
199     }
200 }
201
202 } /* namespace view_scilab */
203 } /* namespace org_scilab_modules_scicos */