97f9a6dce5bd2bd50d7facaa5895bb1c35e6823f
[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  *
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  *
14  */
15
16 #include "../../../includes/view_scilab/Adapters.hxx"
17
18 #include <string>
19 #include <algorithm>
20
21 #include "BlockAdapter.hxx"
22 #include "CprAdapter.hxx"
23 #include "DiagramAdapter.hxx"
24 #include "GraphicsAdapter.hxx"
25 #include "LinkAdapter.hxx"
26 #include "ModelAdapter.hxx"
27 #include "ParamsAdapter.hxx"
28 #include "ScsAdapter.hxx"
29 #include "StateAdapter.hxx"
30 #include "TextAdapter.hxx"
31
32 namespace org_scilab_modules_scicos
33 {
34 namespace view_scilab
35 {
36
37 Adapters Adapters::m_instance = Adapters();
38
39 Adapters& Adapters::instance()
40 {
41     return m_instance;
42 }
43
44 Adapters::Adapters()
45 {
46     /*
47      * Create the supported data types list
48      */
49     adapters = adapters_t();
50     // we assume that the INVALID_ADAPTER is set at end of the enum
51     adapters.reserve(INVALID_ADAPTER);
52
53     adapters.push_back(adapter_t(view_scilab::BlockAdapter::getSharedTypeStr(), BLOCK_ADAPTER));
54     adapters.push_back(adapter_t(view_scilab::CprAdapter::getSharedTypeStr(), CPR_ADAPTER));
55     adapters.push_back(adapter_t(view_scilab::DiagramAdapter::getSharedTypeStr(), DIAGRAM_ADAPTER));
56     adapters.push_back(adapter_t(view_scilab::GraphicsAdapter::getSharedTypeStr(), GRAPHIC_ADAPTER));
57     adapters.push_back(adapter_t(view_scilab::LinkAdapter::getSharedTypeStr(), LINK_ADAPTER));
58     adapters.push_back(adapter_t(view_scilab::ModelAdapter::getSharedTypeStr(), MODEL_ADAPTER));
59     adapters.push_back(adapter_t(view_scilab::ParamsAdapter::getSharedTypeStr(), PARAMS_ADAPTER));
60     adapters.push_back(adapter_t(view_scilab::ScsAdapter::getSharedTypeStr(), SCS_ADAPTER));
61     adapters.push_back(adapter_t(view_scilab::StateAdapter::getSharedTypeStr(), STATE_ADAPTER));
62     adapters.push_back(adapter_t(view_scilab::TextAdapter::getSharedTypeStr(), TEXT_ADAPTER));
63
64     std::sort(adapters.begin(), adapters.end());
65 }
66
67 Adapters::~Adapters()
68 {
69     adapters.clear();
70 }
71
72 Adapters::adapters_index_t Adapters::lookup_by_typename(const std::wstring& name)
73 {
74     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
75     if (it != adapters.end() && !(name < it->name))
76     {
77         return it->kind;
78     }
79     return INVALID_ADAPTER;
80 }
81
82 std::wstring Adapters::get_typename(Adapters::adapters_index_t kind)
83 {
84     for (auto it : adapters)
85     {
86         if (it.kind == kind)
87         {
88             return it.name;
89         }
90     }
91
92     return L"";
93 }
94
95 model::BaseObject* Adapters::descriptor(types::InternalType* v)
96 {
97     const std::wstring& name = v->getShortTypeStr();
98     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
99     if (v->isUserType() && it != adapters.end() && !(name < it->name))
100     {
101         return descriptor(it->kind, v);
102     }
103     return nullptr;
104 }
105
106 model::BaseObject* Adapters::descriptor(adapters_index_t index, types::InternalType* v)
107 {
108     switch (index)
109     {
110         case BLOCK_ADAPTER:
111             return v->getAs<view_scilab::BlockAdapter>()->getAdaptee();
112         case CPR_ADAPTER:
113             return v->getAs<view_scilab::CprAdapter>()->getAdaptee();
114         case DIAGRAM_ADAPTER:
115             return v->getAs<view_scilab::DiagramAdapter>()->getAdaptee();
116         case GRAPHIC_ADAPTER:
117             return v->getAs<view_scilab::GraphicsAdapter>()->getAdaptee();
118         case LINK_ADAPTER:
119             return v->getAs<view_scilab::LinkAdapter>()->getAdaptee();
120         case MODEL_ADAPTER:
121             return v->getAs<view_scilab::ModelAdapter>()->getAdaptee();
122         case PARAMS_ADAPTER:
123             return v->getAs<view_scilab::ParamsAdapter>()->getAdaptee();
124         case SCS_ADAPTER:
125             return v->getAs<view_scilab::ScsAdapter>()->getAdaptee();
126         case STATE_ADAPTER:
127             return v->getAs<view_scilab::StateAdapter>()->getAdaptee();
128         case TEXT_ADAPTER:
129             return v->getAs<view_scilab::TextAdapter>()->getAdaptee();
130         default:
131             return nullptr;
132     }
133 }
134
135 types::InternalType* Adapters::allocate_view(ScicosID id, kind_t kind)
136 {
137     Controller controller;
138
139     switch (kind)
140     {
141         case BLOCK:
142             return new view_scilab::BlockAdapter(controller, controller.getBaseObject<model::Block>(id));
143         case LINK:
144             return new view_scilab::LinkAdapter(controller, controller.getBaseObject<model::Link>(id));
145         case DIAGRAM:
146             return new view_scilab::DiagramAdapter(controller, controller.getBaseObject<model::Diagram>(id));
147         default:
148             return nullptr;
149     }
150 }
151
152 } /* namespace view_scilab */
153 } /* namespace org_scilab_modules_scicos */