Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Version 4 (modified by landauf, 3 years ago) (diff)

fixed links

HowTo: XML Loading

To make a new class XML-loadable, you have to do three things:

1. Derive from BaseObject

First the new class has to derive directly or indirectly from doc/BaseObject (indirectly: deriving from another class following the same rule):

MyClass.h file:

#include "core/BaseObject.h"

class MyClass : public BaseObject
{
    public:
        MyClass(Context* context);
        virtual ~MyClass();

        virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);

        ...
};

or

#include "OtherClass.h"

class MyClass : public OtherClass // If OtherClass is a BaseObject too
{
    ...
};

2. Register Class and Object

Additionally you have to register the new class in the framework. Use the RegisterClass macro outside a function in a source file:

MyClass.cc file:

#include "MyClass.h"

RegisterClass(MyClass);

// Constructor:
MyClass::MyClass(Context* context) : BaseObject(context) // or "OtherClass(context)"
{
    RegisterObject(MyClass);
}

...

3. XMLPort

At last you have to implement the doc/XMLPort function for the new class:

void MyClass::XMLPort(Element& xmlelement, XMLPort::Mode mode)
{
    SUPER(MyClass, XMLPort, xmlelement, mode);

    XMLPortParam(SomeClass, ...);
    XMLPortParam(SomeClass, ...);
    XMLPortParam(SomeClass, ...);
    ...

    XMLPortObject(SomeClass, ...);
    ...
}

Read the related page to learn more about XMLPort and how to load parameters and subobjects: