[xsd-users] comparison question

Rizzuto, Raymond Raymond.Rizzuto at sig.com
Fri Jun 27 09:23:57 EDT 2008


Boris,

In my application I am populating an object, then serializing to XML.
The reason I then parsed it back to an object and compared them is to
see if the destination would receive the same representation as I had
started with.  In the case I was testing, this comparison would have
failed since the two NMTOKENS lists were of different sizes (1 had a
single, invalid, xml token of "state of the union", the other had 4
tokens of each of those words"

If I had done OM->XML1->OM2->XML2 and compared XML1 and XML2, I would
not have seen this.  While this discrepancy is due to a coding bug, it
is valuable to pick these bugs up during development, which is why I was
trying to use the comparison functions.

I actually think the comparison functions are useful for people that are
creating XML documents for exactly this kind of round trip validation.
However, I won't be able to use it as is without a polymorphic
comparison.  Since that would have to be a member function, adding it
would require changing the generated code.  That sounds like a
maintenance nightmare.

I think code like the following would work:

In MessageType:

    virtual bool operator=(const MessageType&x) const = 0;


    bool
    operator== (const MessageType& x, const MessageType& y)
    {
      return x.operator==(y);
    }


In derived NewRequest

    bool operator=(const MessageType&x) const
    {
      if (typeid(x) != typeid(this))
            return false;
      const NewRequest &x2 = dynamic_cast<const NewRequest &>(x);
      // now do member-wise compare of this and x2

Ray

-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com]
Sent: Friday, June 27, 2008 4:13 AM
To: Rizzuto, Raymond
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] comparison question

Hi Ray,

Rizzuto, Raymond <Raymond.Rizzuto at sig.com> writes:

> operator== would have to e a virtual member function - I'm not sure if

> that is allowed or not.

I think this is allowed though you will have to use the same type
for the right-hand-side operand in order for the virtual mechanism
to work. A cleaner approach would be to provide a virtual compare()
function which is called by operator==(). Overall, it will be way
too heavy-weight for the common cases since most comparisons won't
need rtti/virtual function mechanism (polymorphism is normally used
only in a handful of places).

Generally, the generated comparison operators are provided as a
quick-and-dirty member-wise comparison for simple cases (I am still
not sure it was a good idea to provide them at all). In some cases
this semantics can be completely wrong. Applications that need
comparison for any non-trivial cases (e.g., polymorphic objects)
should provide their own comparison operators.

In your particular case it may be easier to compare the XML
representations (e.g., by serializing them to strings) instead
of the object models (e.g., do XML->OM1->XML1->OM2->XML2 and
compare XML1 and XML2 instead of OM1 and OM2).

Boris

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.




More information about the xsd-users mailing list