2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2014-2014 - Scilab Enterprises - Clement DAVID
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
18 #include "utilities.hxx"
20 #include "model/BaseObject.hxx"
21 #include "model/Annotation.hxx"
22 #include "model/Diagram.hxx"
23 #include "model/Block.hxx"
24 #include "model/Link.hxx"
25 #include "model/Port.hxx"
27 namespace org_scilab_modules_scicos
31 lastId(0), allObjects()
33 std::vector<int> datatypeDefault (3, 1);
34 datatypeDefault[0] = -1;
35 datatypes.push_back(new model::Datatype(datatypeDefault));
40 while (!datatypes.empty())
42 Model::erase(datatypes[0]);
47 ScicosID Model::createObject(kind_t k)
50 * Allocate the object per kind
56 o = new model::Annotation();
59 o = new model::Diagram();
62 o = new model::Block();
65 o = new model::Link();
68 o = new model::Port();
73 * Found the next unused id
81 // full map, detection
82 bool has_looped = false;
84 objects_map_t::iterator iter = allObjects.lower_bound(lastId);
85 while (iter != allObjects.end() && !(lastId < iter->first)) // while key is found
93 // if the map is full, return 0;
103 iter = allObjects.lower_bound(lastId);
109 allObjects.insert(iter, std::make_pair(lastId, o));
114 void Model::deleteObject(ScicosID uid)
116 objects_map_t::iterator iter = allObjects.lower_bound(uid);
117 if (iter == allObjects.end() || uid < iter->first)
119 throw std::string("key has not been found");
123 allObjects.erase(iter);
126 model::BaseObject* Model::getObject(ScicosID uid) const
128 objects_map_t::const_iterator iter = allObjects.lower_bound(uid);
129 if (iter == allObjects.end() || uid < iter->first)
131 throw std::string("key has not been found");
137 update_status_t Model::setObject(model::BaseObject* o)
139 objects_map_t::iterator iter = allObjects.lower_bound(o->id());
140 if (iter == allObjects.end() || o->id() < iter->first)
142 throw std::string("key has not been found");
145 if (*iter->second == *o)
150 o->id(iter->second->id());
156 // datatypes being a vector of Datatype pointers, we need a dereferencing comparison operator to use std::lower_bound()
157 static bool isInferior(const model::Datatype* d1, const model::Datatype* d2)
162 model::Datatype* Model::flyweight(const model::Datatype& d)
164 datatypes_set_t::iterator iter = std::lower_bound(datatypes.begin(), datatypes.end(), &d, isInferior);
165 if (iter != datatypes.end() && !(d < **iter)) // if d is found
172 return *datatypes.insert(iter, new model::Datatype(d));
176 void Model::erase(model::Datatype* d)
178 datatypes_set_t::iterator iter = std::lower_bound(datatypes.begin(), datatypes.end(), d, isInferior);
179 if (iter != datatypes.end() && !(*d < **iter)) // if d is found
182 if ((*iter)->refCount < 0)
185 datatypes.erase(iter);
190 } /* namespace org_scilab_modules_scicos */