| | 1 | = HowTo: XML Loading = |
| | 2 | [[TracNav(TracNav/TOC_Development)]] |
| | 3 | |
| | 4 | To make a new class XML-loadable, you have to do three things: |
| | 5 | |
| | 6 | == 1. Derive from !BaseObject == |
| | 7 | First the new class has to derive directly or indirectly from [wiki:BaseObject] (indirectly: deriving from another class following the same rule): |
| | 8 | |
| | 9 | MyClass.h file: |
| | 10 | {{{ |
| | 11 | #include "core/BaseObject.h" |
| | 12 | |
| | 13 | class MyClass : public BaseObject |
| | 14 | { |
| | 15 | public: |
| | 16 | MyClass(); |
| | 17 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); |
| | 18 | |
| | 19 | ... |
| | 20 | }; |
| | 21 | |
| | 22 | or |
| | 23 | |
| | 24 | #include "OtherClass.h" |
| | 25 | |
| | 26 | class MyClass : public OtherClass // If OtherClass is a BaseObject too |
| | 27 | { |
| | 28 | ... |
| | 29 | }; |
| | 30 | }}} |
| | 31 | |
| | 32 | == 2. Create a Factory == |
| | 33 | Additionally you have to create a [wiki:Factory] for the new class. Use the [wiki:CoreIncludes CreateFactory] macro outside a function in a source file: |
| | 34 | |
| | 35 | MyClass.cc file: |
| | 36 | {{{ |
| | 37 | #include "MyClass.h" |
| | 38 | |
| | 39 | CreateFactory(MyClass); |
| | 40 | |
| | 41 | // Constructor: |
| | 42 | MyClass::MyClass() |
| | 43 | { |
| | 44 | RegisterObject(MyClass); |
| | 45 | } |
| | 46 | |
| | 47 | ... |
| | 48 | }}} |
| | 49 | |
| | 50 | == 3. XMLPort == |
| | 51 | At last you have to implement the [wiki:XMLPort] function for the new clss: |
| | 52 | {{{ |
| | 53 | void MyClass::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
| | 54 | { |
| | 55 | SUPER(MyClass, XMLPort, xmlelement, mode); |
| | 56 | |
| | 57 | XMLPortParam(SomeClass, ...); |
| | 58 | XMLPortParam(SomeClass, ...); |
| | 59 | XMLPortParam(SomeClass, ...); |
| | 60 | ... |
| | 61 | |
| | 62 | XMLPortObject(SomeClass, ...); |
| | 63 | ... |
| | 64 | } |
| | 65 | }}} |
| | 66 | Read the related page to learn more about XMLPort and how to load parameters and subobjects: |
| | 67 | * [wiki:XMLPort] |