arcscontext.cpp
1 /*
2  name: lib/arcscontext.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include <arcs/arcscontext.h>
30 //#include <arcs/arcsfactory.h>
31 #include <arcs/arcslog.h>
32 #include <QtDebug>
33 
34 
36 {
37  factory = ARCSFactory::getInstance();
38  instanciated = false;
39 }
40 
41 
43 {
44  this->factory = ctx.factory;
45 
46  QHashIterator<QString, ARCSAbstractComponent*> i(ctx.componentPool);
47  while(i.hasNext())
48  {
49  i.next();
50  if (i.value())
51  componentPool[i.key()] = i.value()->clone();
52  }
53 
54  constantPool = ctx.constantPool;
55 }
56 
57 
58 
59 ARCSContext::~ARCSContext()
60 {
61 
62 
63 }
64 
66 {
67  int i;
68 
69  QStringList components = componentPool.keys();
70  QStringList constants = constantPool.keys();
71  QStringList result;
72  QString lib;
73 
74  for (i=0; i < components.count(); i++)
75  {
76  lib = factory->getComponentLibrary(componentPool[components[i]]->getType());
77  if (! lib.isEmpty())
78  if (!result.contains(lib))
79  result << lib;
80  }
81 
82  for (i=0; i < constants.count(); i++)
83  {
85  lib = factory->getTypeLibrary(constants[i]);
86  if (! lib.isEmpty())
87  if (!result.contains(lib))
88  result << lib;
89  }
90 
91  return result;
92 }
93 
94 
96 {
97  return constantPool.keys();
98 }
99 
100 
101 
103 {
104  return componentPool.keys();
105 }
106 
107 
109 {
110  bool res = true;
111 
112  QHashIterator<QString, ARCSAbstractComponent*> i(componentPool);
113  while (i.hasNext())
114  {
115  i.next();
116  ARCSAbstractComponent* cmp = i.value();
117  QVariant v = cmp->getProperty("persistent");
118  if (v.isValid() && i.key() != "this" )
119  {
120  if (v == QVariant(true))
121  if (!cmp->isInstanciated())
122  res = res || cmp->instanciate();
123  }
124  else
125  if (!cmp->isInstanciated())
126  res = res && cmp->instanciate();
127  }
128  instanciated = res;
129  return res;
130 }
131 
132 
133 
135 {
136  constantPool.clear();
137 
138  QHashIterator<QString, ARCSAbstractComponent*> i(componentPool);
139 
140  while(i.hasNext())
141  {
142  i.next();
143  factory->destroyComponent(i.value());
144  }
145  componentPool.clear();
146 }
147 
148 
149 ARCSAbstractComponent* ARCSContext::createComponent(QString name, QString className)
150 {
151  ARCSAbstractComponent* candidate= factory->createComponent(className);
152  if (candidate == 0)
153  {
154  ARCSLog::logCritical(ARCS_SOURCE,"cannot create component "+name+" of type "+className);
155  return 0;
156  }
157 
158  candidate->setProperty("id", name);
159 
160  if ( componentPool.contains(name))
161  {
162  qCritical() << "[Context] Component pool already contains a component named" << name;
163  return 0;
164  }
165 
166  componentPool.insert(name, candidate);
167  return candidate;
168 }
169 
170 
171 bool ARCSContext::addComponent(QString name, QString className, QString contents)
172 {
173  ARCSAbstractComponent* candidate = factory->createComponent(className);
174 
175  if (candidate == 0)
176  return false;
177 
178  candidate->setProperty("id", name);
179  if ( ! candidate->parseString(contents) )
180  qCritical() << "[Context] Unable to parse contents:" << contents << "for object" << name << "of type" << className ;
181 
182  if ( componentPool.contains(name))
183  {
184  qCritical() << "[Context] Component pool already contains a component named" << name;
185  return false;
186  }
187 
188  componentPool.insert(name, candidate);
189  return true;
190 }
191 
192 bool ARCSContext::renameComponent(QString oldName, QString newName)
193 {
194  if (componentPool.contains(newName))
195  return false;
196 
197  if (! componentPool.contains(oldName))
198  return false;
199 
200  ARCSAbstractComponent* componentToRename = componentPool.take(oldName);
201  componentToRename->setProperty("id",newName);
202  componentPool.insert(newName,componentToRename);
203  return true;
204 }
205 
206 
208 {
209  if (componentPool.contains(name))
210  {
211  ARCSAbstractComponent* removal = componentPool.take(name);
212  factory->destroyComponent(removal);
213  }
214 }
215 
216 
218 {
219  if (componentPool.contains(name))
220  return componentPool[name];
221  return 0;
222 }
223 
224 
225 bool ARCSContext::addConstant(QString name, QString type, QString representation)
226 {
227  if (constantPool.contains(name))
228  {
229  qCritical() << "[Context] Constant pool already contains a constant name" << name;
230  return false;
231  }
232 
233  QVariant var = factory->dataDeserialize(type, representation);
234 
235  if (!var.isValid())
236  return false;
237 
238  constantPool[name] = var;
239  return true;
240 }
241 
242 
243 void ARCSContext::removeConstant(QString name)
244 {
245  constantPool.remove(name);
246 }
247 
248 
249 QVariant ARCSContext::getConstant(QString name)
250 {
251  if (constantPool.contains(name))
252  return constantPool[name];
253  return QVariant();
254 }
255 
256 
257 QVariant ARCSContext::createValue(QString type, QString representation)
258 {
259  return factory->dataDeserialize(type, representation);
260 }
261 
262 
263 bool ARCSContext::serializeConstant(QString name, QString & type, QString & representation)
264 {
265  QVariant var = getConstant(name);
266  if (! var.isValid())
267  return false;
268 
269  type = factory->getVariantType(var);
270  representation = factory->dataSerialize(var);
271  return true;
272 }
273 
274 
275 bool ARCSContext::modifyConstant(QString name, QString representation)
276 {
277  QString type;
278  QVariant var = getConstant(name);
279  if (! var.isValid())
280  return false;
281 
282  type = factory->getVariantType(var);
283  removeConstant(name);
284  return addConstant(name, type, representation);
285 }
bool instanciate()
Instanciates the real component.
void removeConstant(QString name)
Removes a constant from the constant pool.
bool serializeConstant(QString name, QString &type, QString &representation)
Serializes constants.
void destroyComponent(ARCSAbstractComponent *aac)
Destroys a component.
QString getVariantType(QVariant var)
Returns the type of a QVariant.
QStringList getComponentList()
bool modifyConstant(QString name, QString representation)
Modifies a constant of the list.
QString getTypeLibrary(QString s)
Returns the path of the library associated to a certain kind of type.
bool addComponent(QString name, QString className, QString contents=QString::null)
Adds a component to the list.
static ARCSFactory * getInstance()
Returns the instance of the singleton ARCSFactory.
QVariant getProperty(QString name)
Gets a meta-property from this component.
Class handling the generic description of a component.
void removeComponent(QString name)
Removes a component from the component pool.
ARCSAbstractComponent * createComponent(QString componentType)
Creates a component given its class name.
ARCSContext()
Constructor.
Definition: arcscontext.cpp:35
ARCSAbstractComponent * getComponent(QString name)
Retrieves a component by its id.
bool addConstant(QString name, QString type, QString representation)
Adds a constant to the list.
QString getComponentLibrary(QString s)
Returns the path of the library associated to a certain kind of component.
QStringList computeLibraryList()
Definition: arcscontext.cpp:65
QVariant getConstant(QString name)
Retrieves a constant by its id.
This class manages components and constants in a given context.
Definition: arcscontext.h:45
QStringList getConstantList()
Definition: arcscontext.cpp:95
void reset()
Resets the component and constant lists.
bool instanciatePool()
Instanciates the whole pool of components inside the context.
QString dataSerialize(QVariant var)
Serializes data from their QVariant counterpart.
QVariant dataDeserialize(QString type, QString representation)
Deserialize data from their string representation.
void setProperty(QString name, QVariant value)
Sets a meta-property on this component.
bool isInstanciated()
Determines wether the component is instanciated or not.
virtual bool parseString(QString s)=0
Defines a basic seralization mechanism.