#include <Pt/Decomposer.h>
Manages the decomposition of types during serialization. More...
Inherited by BasicDecomposer< ValueType< R > >, BasicDecomposer< A >, and BasicDecomposer< T >.
Public Member Functions | |
| virtual | ~Decomposer () |
| Destructor. | |
| void | setParent (Decomposer *parent) |
| Sets the parent. | |
| Decomposer * | parent () const |
| Returns the parent. | |
| void | format (Formatter &formatter) |
| Format the type completely. | |
| void | beginFormat (Formatter &formatter) |
| Begin formatting the type. | |
| Decomposer * | advanceFormat (Formatter &formatter) |
| Advance formatting the type. | |
Protected Member Functions | |
| Decomposer () | |
| Default constructor. | |
| virtual void | onFormat (Formatter &formatter) |
| Format the type completely. | |
| virtual void | onBeginFormat (Formatter &formatter)=0 |
| Begin formatting the type. | |
| virtual Decomposer * | onAdvanceFormat (Formatter &formatter)=0 |
| Advance formatting the type. | |
An alternative to the serialization operators is to specialize Pt::BasicDecomposer, normally for container types where driving output directly to a Formatter is preferable to building a complete tree of SerializationInfo objects in memory. The following example shows a specialization for std::vector:
A specialized BasicDecomposer requires a constructor that optionally takes a reference to a SerializationContext and a begin() function to set up the decomposer to start decomposing a value by name. The virtual functions onBeginFormat() and onAdvanceFormat() are overridden to react to formatting events. onBeginFormat() is called when the vector should start being formatted; here Pt::Formatter::beginSequence() formats the beginning of the sequence and an iterator to the begin of the vector is kept. onAdvanceFormat() is called when the next element can be formatted: it finishes the current element with Pt::Formatter::finishElement(), starts the next one with Pt::Formatter::beginElement(), or finishes the sequence with Pt::Formatter::finishSequence() once the end of the vector is reached. It returns a decomposer, either for the current vector element or the parent decomposer if all elements were decomposed. A decomposer may delegate to other decomposers, which might or might not be specialized; here the decomposer for the element type is used, which works for a vector of vectors right away.