GPL + CeCILL Header change
[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-2014 - Scilab Enterprises - Clement DAVID
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  * === LICENSE_END ===
14  *
15  */
16
17 #include "../../../includes/view_scilab/Adapters.hxx"
18
19 #include <string>
20 #include <algorithm>
21
22 #include "BlockAdapter.hxx"
23 #include "CprAdapter.hxx"
24 #include "DiagramAdapter.hxx"
25 #include "GraphicsAdapter.hxx"
26 #include "LinkAdapter.hxx"
27 #include "ModelAdapter.hxx"
28 #include "ParamsAdapter.hxx"
29 #include "ScsAdapter.hxx"
30 #include "StateAdapter.hxx"
31 #include "TextAdapter.hxx"
32
33 namespace org_scilab_modules_scicos
34 {
35 namespace view_scilab
36 {
37
38 Adapters Adapters::m_instance = Adapters();
39
40 Adapters& Adapters::instance()
41 {
42     return m_instance;
43 }
44
45 Adapters::Adapters()
46 {
47     /*
48      * Create the supported data types list
49      */
50     adapters = adapters_t();
51     // we assume that the INVALID_ADAPTER is set at end of the enum
52     adapters.reserve(INVALID_ADAPTER);
53
54     adapters.push_back(adapter_t(view_scilab::BlockAdapter::getSharedTypeStr(), BLOCK_ADAPTER));
55     adapters.push_back(adapter_t(view_scilab::CprAdapter::getSharedTypeStr(), CPR_ADAPTER));
56     adapters.push_back(adapter_t(view_scilab::DiagramAdapter::getSharedTypeStr(), DIAGRAM_ADAPTER));
57     adapters.push_back(adapter_t(view_scilab::GraphicsAdapter::getSharedTypeStr(), GRAPHIC_ADAPTER));
58     adapters.push_back(adapter_t(view_scilab::LinkAdapter::getSharedTypeStr(), LINK_ADAPTER));
59     adapters.push_back(adapter_t(view_scilab::ModelAdapter::getSharedTypeStr(), MODEL_ADAPTER));
60     adapters.push_back(adapter_t(view_scilab::ParamsAdapter::getSharedTypeStr(), PARAMS_ADAPTER));
61     adapters.push_back(adapter_t(view_scilab::ScsAdapter::getSharedTypeStr(), SCS_ADAPTER));
62     adapters.push_back(adapter_t(view_scilab::StateAdapter::getSharedTypeStr(), STATE_ADAPTER));
63     adapters.push_back(adapter_t(view_scilab::TextAdapter::getSharedTypeStr(), TEXT_ADAPTER));
64
65     std::sort(adapters.begin(), adapters.end());
66 }
67
68 Adapters::~Adapters()
69 {
70     adapters.clear();
71 }
72
73 Adapters::adapters_index_t Adapters::lookup_by_typename(const std::wstring& name)
74 {
75     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
76     if (it != adapters.end() && !(name < it->name))
77     {
78         return it->kind;
79     }
80     return INVALID_ADAPTER;
81 }
82
83
84 std::wstring Adapters::get_typename(Adapters::adapters_index_t kind)
85 {
86     for (auto it : adapters)
87     {
88         if (it.kind == kind)
89         {
90             return it.name;
91         }
92     }
93
94     return L"";
95 }
96
97
98 const model::BaseObject* Adapters::descriptor(types::InternalType* v)
99 {
100     const std::wstring& name = v->getShortTypeStr();
101     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
102     if (v->isUserType() && it != adapters.end() && !(name < it->name))
103     {
104         switch (it->kind)
105         {
106             case BLOCK_ADAPTER:
107                 return v->getAs<view_scilab::BlockAdapter>()->getAdaptee();
108             case CPR_ADAPTER:
109                 return v->getAs<view_scilab::CprAdapter>()->getAdaptee();
110             case DIAGRAM_ADAPTER:
111                 return v->getAs<view_scilab::DiagramAdapter>()->getAdaptee();
112             case GRAPHIC_ADAPTER:
113                 return v->getAs<view_scilab::GraphicsAdapter>()->getAdaptee();
114             case LINK_ADAPTER:
115                 return v->getAs<view_scilab::LinkAdapter>()->getAdaptee();
116             case MODEL_ADAPTER:
117                 return v->getAs<view_scilab::ModelAdapter>()->getAdaptee();
118             case PARAMS_ADAPTER:
119                 return v->getAs<view_scilab::ParamsAdapter>()->getAdaptee();
120             case SCS_ADAPTER:
121                 return v->getAs<view_scilab::ScsAdapter>()->getAdaptee();
122             case STATE_ADAPTER:
123                 return v->getAs<view_scilab::StateAdapter>()->getAdaptee();
124             case TEXT_ADAPTER:
125                 return v->getAs<view_scilab::TextAdapter>()->getAdaptee();
126             default:
127                 return nullptr;
128         }
129     }
130
131     return nullptr;
132 }
133
134 types::InternalType* Adapters::allocate_view(ScicosID id, kind_t kind)
135 {
136     Controller controller;
137
138     switch (kind)
139     {
140         case BLOCK:
141             return new view_scilab::BlockAdapter(controller, static_cast<model::Block*>(controller.getObject(id)));
142         case LINK:
143             return new view_scilab::LinkAdapter(controller, static_cast<model::Link*>(controller.getObject(id)));
144         case DIAGRAM:
145             return new view_scilab::DiagramAdapter(controller, static_cast<model::Diagram*>(controller.getObject(id)));
146         default:
147             return nullptr;
148     }
149 }
150
151 } /* namespace view_scilab */
152 } /* namespace org_scilab_modules_scicos */