arcsarray.h
Go to the documentation of this file.
1 /*
2  name: include/arcs/arcsarray.h
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 #ifndef __ARCSARRAY_H__
30 #define __ARCSARRAY_H__
31 
32 #include <arcs/arcsfactory.h>
33 #include <cassert>
34 
45 
50 template<class X> class ARCSArrayTemplate
51 {
52  public:
54  {
55  size = ref.size;
56  data = 0;
57  allocate();
58  for (unsigned int i = 0; i < size; i++)
59  data[i] = ref.data[i];
60  }
61 
62  ARCSArrayTemplate() { size = 0; data = 0; }
63  ARCSArrayTemplate(unsigned int s) { size = s; allocate(); }
64  ~ARCSArrayTemplate() { if (data) delete[] data; }
65 
66  void setSize(unsigned int s)
67  {
68  unsigned int oldSize = size;
69  unsigned int i;
70  X* oldData = data;
71  data = 0;
72  allocate();
73  if (s < oldSize && oldData != 0)
74  {
75  for (i = 0; i < s; i++)
76  data[i] = oldData[i];
77  }
78  else
79  {
80  for (i = 0; i < oldSize; i++)
81  data[i] = oldData[i];
82 
83  }
84  size = s;
85  delete[] oldData;
86  //allocate();
87  }
88  unsigned int getSize() { return size; }
89 
90  X& operator[] (const unsigned int i) { assert(i < size); return data[i]; }
91 
92  ARCSArrayTemplate<X>& operator=(const ARCSArrayTemplate<X>& ref)
93  {
94  size = ref.size;
95  data = 0;
96  allocate();
97  for (unsigned int i = 0; i < size; i++)
98  data[i] = ref.data[i];
99  return *this;
100  }
101 
102 
103  private:
104  bool allocate() { if ( data) delete[] data; data = new X[size]; return data != 0; }
105  unsigned int size;
106  X* data;
107 };
108 
109 
110 
112 
134 template<class X> class ARCSArrayFactoryTemplate : public ARCSTypeFactory
135 {
136  public:
137  virtual QString getTypeName() const { return baseType() + "Array";}
138 
139  virtual QString toString(QVariant v);
140  virtual QVariant parseString(QString s);
141 
142  protected:
143  virtual QString separator() const { return ":" ; }
144  virtual QString baseType() const = 0;
145 };
146 
147 
148 
149 template<class X>
151 {
152  if (! v.canConvert<ARCSArrayTemplate<X> >())
153  return QString::null;
154 
156  if (!factory)
157  return QString::null;
158 
159 
160  QString res;
161  ARCSArrayTemplate<X> array = v.value<ARCSArrayTemplate<X> >();
162  for (unsigned int i = 0; i < array.getSize() ; i++)
163  {
164  QVariant v = QVariant::fromValue(array[i]);
165  if (i !=0 )
166  res+= separator();
167  res += factory->dataSerialize(v);
168  }
169  return res;
170 }
171 
172 template<class X>
174 {
175  ARCSArrayTemplate<X> array;
176  if (s.isEmpty())
177  return QVariant::fromValue(array);
178 
180  if (!factory)
181  return QVariant();
182 
183  QStringList lst = s.split(separator());
184 
185  array.setSize(lst.count());
186 
187  for ( int i = 0; i < lst.count() ; i++)
188  {
189 
190  QVariant var = factory->dataDeserialize(baseType(), lst[i]);
191  if (var.canConvert<X>())
192  array[i] = var.value<X>();
193 
194  }
195  return QVariant::fromValue(array);
196 }
197 
198 #endif // __ARCSARRAY_H__
static ARCSFactory * getInstance()
Returns the instance of the singleton ARCSFactory.
Template class to implement array serialization.
Definition: arcsarray.h:134
virtual QVariant parseString(QString s)
Should create data from their string representation.
Definition: arcsarray.h:173
A singleton handling all needed factories in ARCS runtime.
Definition: arcsfactory.h:58
Template class to implement arrays.
Definition: arcsarray.h:50
virtual QString toString(QVariant v)
Should return a string representation of data.
Definition: arcsarray.h:150
Generic class describing how type factories should be implemented.
virtual QString getTypeName() const
Should return the name of the type factory.
Definition: arcsarray.h:137
QString dataSerialize(QVariant var)
Serializes data from their QVariant counterpart.
QVariant dataDeserialize(QString type, QString representation)
Deserialize data from their string representation.