[xsd-users] XDR

Boris Kolpackov boris at codesynthesis.com
Wed Sep 26 13:53:56 EDT 2007


Hi Michael,

Caruso, Michael - AES <Michael.Caruso at itt.com> writes:

> Is there XDR support in xsd?

Yes, XDR is supported from XSD 3.0.0. The usage is very similar to
the ACE CDR. Here is an example (taken from the XDR example for the
next release of XSD):

#include <cstring> // std::memcpy

#include <rpc/xdr.h>

#include <xsd/cxx/tree/xdr-stream-insertion.hxx>
#include <xsd/cxx/tree/xdr-stream-extraction.hxx>

extern "C" int
overflow (char* p, char* buf, int n)
{
  xml_schema::buffer* dst (reinterpret_cast<xml_schema::buffer*> (p));

  std::size_t size (dst->size ());
  std::size_t capacity (dst->capacity ());

  // Implement exponential growth.
  //
  if (size + n > capacity && size + n < capacity * 2)
    dst->capacity (capacity * 2);

  dst->size (size + n);
  std::memcpy (dst->data () + size, buf, n);

  return n;
}

struct underflow_info
{
  xml_schema::buffer* buf;
  std::size_t pos;
};

extern "C" int
underflow (char* p, char* buf, int n)
{
  underflow_info* ui (reinterpret_cast<underflow_info*> (p));

  std::size_t size (ui->buf->size () - ui->pos);
  n = size > n ? n : size;

  std::memcpy (buf, ui->buf->data () + ui->pos, n);
  ui->pos += n;

  return n;
}

int
main ()
{
  xml_schema::buffer buf;

  // Write
  //
  {
    type& obj = ... // object model

    XDR xdr;
    xdrrec_create (&xdr, 0, 0, reinterpret_cast<char*> (&buf), 0, &overflow);
    xdr.x_op = XDR_ENCODE;

    xml_schema::ostream<XDR> oxdr (xdr);

    oxdr << obj;

    xdrrec_endofrecord (&xdr, true); // flush the data.
    xdr_destroy (&xdr);
  }

  // Read
  //
  {
    underflow_info ui;
    ui.buf = &buf;
    ui.pos = 0;

    XDR xdr;
    xdrrec_create (&xdr, 0, 0,
                   reinterpret_cast<char*> (&ui), &underflow, 0);
    xdr.x_op = XDR_DECODE;
    xdrrec_skiprecord (&xdr);

    xml_schema::istream<XDR> ixdr (xdr);

    type* obj = new type (ixdr);

    xdr_destroy (&xdr);
  }
}


Boris




More information about the xsd-users mailing list