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 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:

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;
};

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:

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);
};

The idea is to write the size as a string like this “wxh” so the implementation would be like this:

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());
}

Modifying the component library description

The XML description of the component library will be as follows:

<library>
  <headers>
    <header name="typesize.h"/>
  </headers>
  <types>
    <type name="QSize"/>
  </types>
</library>