[xsde-users] Re: Processing variable length root elements as part
of message parsing / serializing
Boris Kolpackov
boris at codesynthesis.com
Tue Nov 23 10:56:04 EST 2010
Hi Terry,
O'Laughlin, Terry <Terry.O'Laughlin at ipc.com> writes:
> I'm adapting the 'multiroot' example as we are using XML as the basis
> for messaging in a communications protocol. Works great with fixed-
> length messages. What do I need to do to adapt it to using variable
> length root elements?
If the root element type is variable-length, then you can store a
pointer to the object model instead of the value. Here is how one
can adapt the document_pimpl class from the 'multiroot' example
(only changed parts are shown):
class document_pimpl: ...
{
public:
~document_pimpl ()
{
free ();
}
request_type
result_type () const
{
return result_type_;
}
// Return the object model and release ownership.
//
protocol::balance*
balance () const
{
protocol::balance r = balance_;
balance_ = 0;
return r;
}
protocol::withdraw*
withdraw () const
{
protocol::withdraw r = withdraw_;
withdraw_ = 0;
return r;
}
virtual void
reset ()
{
free ();
xml_schema::document_pimpl::reset ();
balance_p_.reset ();
withdraw_p_.reset ();
}
private:
void
free ()
{
switch (result_type_)
{
case balance_type:
{
delete balance_;
break;
}
case withdraw_type:
{
delete withdraw_;
break;
}
unknown_type:
{
break;
}
}
result_type_ = unknown_type;
}
private:
request_type result_type_;
protocol::balance* balance_;
protocol::withdraw* withdraw_;
};
Then, in main(), the relevant changes are as follows:
// Print what we've got.
//
switch (doc_p.result_type ())
{
case balance_type:
{
balance* b = doc_p.balance ();
cerr << "balance request for acc# " << b->account () << endl;
delete b;
break;
}
case withdraw_type:
{
withdraw* w = doc_p.withdraw ();
cerr << "withdrawal request for acc# " << w->account () << ", "
<< "amount: " << w->amount () << endl;
delete w;
break;
}
case unknown_type:
{
cerr << "unknown request" << endl;
break;
}
}
Boris
More information about the xsde-users
mailing list