Composer Class Referenceabstract

#include <Pt/Composer.h>

Composes types during serialization. More...

Inherited by BasicComposer< Pt::SerializationInfo >, BasicComposer< R >, BasicComposer< Pt::XmlRpc::Fault >, BasicComposer< T >, and BasicComposer< Pt::SerializationInfo >.

Public Member Functions

virtual ~Composer ()
 Destructor.
void setParent (Composer *parent)
 Sets the parent composer.
Composerparent () const
 Returns the parent composer.
void setTypeName (const std::string &type)
 Sets the type name of the type to compose.
void setTypeName (const char *type, std::size_t len)
 Sets the type name of the type to compose.
void setId (const std::string &id)
 Sets the reference id of the type to compose.
void setId (const char *id, std::size_t len)
 Sets the reference id of the type to compose.
void setString (const Pt::String &value)
 Composes a string value.
void setString (const Pt::Char *value, std::size_t len)
 Composes a string value.
void setBinary (const char *data, std::size_t length)
 Composes a binary value.
void setChar (const Pt::Char &ch)
 Composes a char value.
void setBool (bool value)
 Composes a boolean value.
void setInt (Pt::int64_t value)
 Composes a signed integer type.
void setUInt (Pt::int64_t value)
 Composes an unsigned integer type.
void setFloat (long double value)
 Composes a float value.
void setReference (const std::string &id)
 Composes a reference.
void setReference (const char *id, std::size_t len)
 Composes a reference.
ComposerbeginMember (const std::string &name)
 Begins composition of a struct member.
ComposerbeginMember (const char *name, std::size_t len)
 Begins composition of a struct member.
ComposerbeginElement ()
 Begins composition of a sequence member.
ComposerbeginDictElement ()
 Begins composition of a dict key.
ComposerbeginDictKey ()
 Begins composition of a dict key.
ComposerbeginDictValue ()
 Begins composition of a dict value.
Composerfinish ()
 Finishes composition of a struct or sequence member.

Protected Member Functions

 Composer ()
 Constructor.
virtual void onSetTypeName (const char *, std::size_t)
 Set type name.
virtual void onSetId (const char *id, std::size_t len)=0
 Set reference ID.
virtual void onSetString (const Pt::Char *, std::size_t)
 Compose a string value.
virtual void onSetBinary (const char *, std::size_t)
 Compose a binary value.
virtual void onSetChar (const Pt::Char &)
 Compose a character value.
virtual void onSetBool (bool)
 Compose a bool value.
virtual void onSetInt (Pt::int64_t)
 Compose a integer value.
virtual void onSetUInt (Pt::uint64_t)
 Compose a unsigned integer value.
virtual void onSetFloat (long double)
 Compose a floating point value.
virtual void onSetReference (const char *, std::size_t)
 Compose a reference.
virtual ComposeronBeginMember (const char *, std::size_t)
 Begin composition os a struct member.
virtual ComposeronBeginElement ()
 Begins composition of a sequence member.
virtual ComposeronBeginDictElement ()
 Begins composition of a dict key.
virtual ComposeronBeginDictKey ()
 Begins composition of a dict key.
virtual ComposeronBeginDictValue ()
 Begins composition of a dict value.
virtual ComposeronFinish ()
 Finishes composition of a struct or sequence member.

Detailed Description

Implementing the serialization operators is not the only way to make a type serializable. An alternative is to specialize Pt::BasicComposer. The default implementation of BasicComposer uses the serialization operators and a tree of SerializationInfo objects, which is fine for small and medium sized objects. The parsing layer interacts with this class to compose the actual type, and this layer can be completely customized. This is normally done for container types, where building the complete tree of SerializationInfo objects in memory is undesirable. The following example shows a specialization for std::vector:

template <typename T>
class BasicComposer< std::vector<T> > : public Composer
{
public:
: _type(0)
{
_elemComposer.setParent(this);
}
void begin(std::vector<T>& type)
{
_type = &type;
_type->clear();
}
protected:
virtual void onSetId(const char* id, std::size_t len)
{ }
virtual Composer* onBeginElement()
{
_type->push_back( T() );
_elemComposer.begin( _type->back() );
return &_elemComposer;
}
private:
std::vector<T>* _type;
BasicComposer<T> _elemComposer;
};
Composes serializable types during serialization.
Definition Composer.h:327
Composer()
Constructor.
Definition Composer.h:245
Context for the serialization of types.
Definition Api-SerializationContext.h:26

The specialized BasicComposer inherits Composer, which is the interface used by the deserializer to report parse events. The virtual functions onBeginElement() and onSetId() are overridden to build the vector from the parse events. onBeginElement() is called when the beginning of a vector element has been parsed; it sets up and returns a composer for the new element in the vector. A constructor is required that optionally takes a reference to a SerializationContext; the context can be used in onSetId() to map a reference id to the vector being composed, if another object has a weak reference to it. The begin() function is also required, to set up the composer to start composing a type. A composer may delegate to other composers, which might or might not be specialized; here, the composer for the element type is used, which works for a vector of vectors right away.

Member Function Documentation

◆ setTypeName() [1/2]

void setTypeName ( const std::string & type)

This is only supported by formats that save typename information.

◆ setTypeName() [2/2]

void setTypeName ( const char * type,
std::size_t len )

This is only supported by formats that save typename information.

◆ setId() [1/2]

void setId ( const std::string & id)

This is only supported by formats that support references.

◆ setId() [2/2]

void setId ( const char * id,
std::size_t len )

This is only supported by formats that support references.

◆ setInt()

void setInt ( Pt::int64_t value)

There is only one method for all sizes of signed integer types, because that type information is not required for composition.

◆ setUInt()

void setUInt ( Pt::int64_t value)

There is only one method for all sizes of unsigned integer types, because that type information is not required for composition.

◆ beginDictElement()

Composer * beginDictElement ( )

Returns a composer for the key of the dict element. A subsequent call of beginDictValue returns a composer to the value of the dict element. For both finish() has to be called, after the value was completely composed.