Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
arcs2:including_new_data_types [2013/08/24 19:02]
didier
arcs2:including_new_data_types [2013/08/24 19:42] (current)
didier [Subclassing ARCSTypeFactoryTemplate<X>]
Line 1: Line 1:
 +====== Including new data types ======
 +In component libraries, you can introduce new data types in order to extend the ARCS engine.
 +The idea is to explain how to serialize these data types as strings in order to be able to 
 +initialize components using these new data types.
 +
 +The process to introduce these new data types is quite straightforward:​ you must create a subclass
 +of **''​[[http://​arcs.ibisc.univ-evry.fr/​doc/​classARCSTypeFactoryTemplate.html|ARCSTypeFactoryTemplate<​X>​]]''​** where ''​X''​ is your new data type.
 +Then, you will modify the XML library description in order to explain what are the new proposed data types.
 +
 +===== Subclassing ARCSTypeFactoryTemplate<​X>​ =====
 +''​ARCSTypeFactoryTemplate<​X>''​ is declared as follows:
 +<code cpp>
 +template<​class X> class ARCSTypeFactoryTemplate : public ARCSTypeFactory ​
 +
 +  public: ​
 +    virtual QString getTypeName() const = 0; 
 +    virtual QString toString(QVariant v);
 +    virtual QVariant parseString(QString s);
 +  protected:
 +    virtual X parse(QString s) = 0;
 +    virtual QString serialize(X obj) = 0;
 +};
 +</​code>​
 +
 +So, basically, when it is subclassed, only three methods need to be implemented: ​
 +  * first one is ''​getTypeName()''​ where the type name should be given (this type name is not necessarily the same as the programming type name e.g. ''​int''​ can be introduced as ''​integer''​);​
 +  * ''​parse()''​ that will take a string and convert it into an object of the X data type;
 +  * ''​serialize()''​ that takes an object of the X data type and convert it into a string.
 +
 +You should also notice that you may have to use a special naming convention for your class name if you want to shorten the new data type introduction into the component library. In such situation, the name of the class should be: ''​ARCSTypeFactoryTemplate_X''​ where X is the literate data type. For example, if you would like to introduce a new type describing the size of rectangular area using two integers for width and height, you may declare a class like this:
 +<file cpp typesize.h>​
 +#include <​arcs/​arcslibtoolkit.h>​
 +#include <​QSize>​
 +
 +class ARCSTypeFactory_QSize : public ARCSTypeFactoryTemplate<​QSize>​
 +{
 +  public:
 +    virtual QString getTypeName() const { return "​size";​ }
 +  protected:
 +    virtual QSize parse(QString s) ;
 +    virtual QString serialize(QSize s);
 +};
 +</​file>​
 +The idea is to write the size as a string like this "​wxh"​ so the implementation would be like this:
 +<file cpp typesize.cpp>​
 +#include <​typesize.h>​
 +#include <​QStringList>​
 +
 +QSize ARCSTypeFactory_QSize::​parse(QString s)
 +{
 +     ​QStringList sl = s.split("​x"​);​
 +     if (sl.count() != 2) return QSize();
 +     ​return QSize(sl[0].toInt(),​sl[1].toInt());​
 +}
 +
 +QString ARCSTypeFactory_QSize::​serialize(QSize s) {
 +     ​return QString::​number(s.width()) + "​x"​ + QString::​number(s.height());​
 +}
 +</​file>​
 +
 +===== Modifying the component library description =====
 +The XML description of the component library will be as follows:
 +<code xml>
 +<​library>​
 +  <​headers>​
 +    <header name="​typesize.h"/>​
 +  </​headers>​
 +  <​types>​
 +    <type name="​QSize"/>​
 +  </​types>​
 +</​library>​
 +</​code> ​