Decomposer Class Referenceabstract

#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.
Decomposerparent () const
 Returns the parent.
void format (Formatter &formatter)
 Format the type completely.
void beginFormat (Formatter &formatter)
 Begin formatting the type.
DecomposeradvanceFormat (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 DecomposeronAdvanceFormat (Formatter &formatter)=0
 Advance formatting the type.

Detailed Description

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:

template <typename T>
class BasicDecomposer< std::vector<T> > : public Decomposer
{
public:
: _type(0)
{
_elemDecomposer.setParent(this);
}
void begin(const std::vector<T>& type, const char* name)
{
_type = &type;
_name = name;
}
protected:
void onBeginFormat(Formatter& formatter)
{
formatter.beginSequence(_name.c_str(), "std::vector", "");
_it = _type->begin();
}
Decomposer* onAdvanceFormat(Formatter& formatter)
{
if( _it != _type->begin() )
{
formatter.finishElement();
}
if( _it == _type->end() )
{
formatter.finishSequence();
return this->parent();
}
formatter.beginElement();
_elemDecomposer.begin(*_it, "");
_elemDecomposer.beginFormat(formatter);
++_it;
return &_elemDecomposer;
}
private:
std::string _name;
const std::vector<T>* _type;
BasicDecomposer<T> _elemDecomposer;
typename std::vector<T>::const_iterator _it;
};
Manages the decomposition of types during serialization.
Definition Decomposer.h:185
Decomposer()
Default constructor.
Definition Decomposer.h:153
Context for the serialization of types.
Definition Api-SerializationContext.h:26

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.