nih-gov/www.ncbi.nlm.nih.gov/mailman/pipermail/cpp-announce/2001/000048.html

161 lines
5.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE> [C++ Toolkit ANNOUNCE] Assignment and comparison of serializable classes (user-extendable)
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:cpp-announce%40ncbi.nlm.nih.gov?Subject=%5BC%2B%2B%20Toolkit%20ANNOUNCE%5D%20Assignment%20and%20comparison%20of%20serializable%20classes%20%28user-extendable%29&In-Reply-To=">
<META NAME="robots" CONTENT="index,nofollow">
<META http-equiv="Content-Type" content="text/html; charset=us-ascii">
<LINK REL="Previous" HREF="000047.html">
<LINK REL="Next" HREF="000049.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[C++ Toolkit ANNOUNCE] Assignment and comparison of serializable classes (user-extendable)</H1>
<!--htdig_noindex-->
<B>Denis Vakatov</B>
<A HREF="mailto:cpp-announce%40ncbi.nlm.nih.gov?Subject=%5BC%2B%2B%20Toolkit%20ANNOUNCE%5D%20Assignment%20and%20comparison%20of%20serializable%20classes%20%28user-extendable%29&In-Reply-To="
TITLE="[C++ Toolkit ANNOUNCE] Assignment and comparison of serializable classes (user-extendable)">vakatov at ncbi.nlm.nih.gov
</A><BR>
<I>Wed Jul 18 18:05:02 EDT 2001</I>
<P><UL>
<LI>Previous message: <A HREF="000047.html">[C++ Toolkit ANNOUNCE] NCBI C++ Toolkit SNAPSHOT
</A></li>
<LI>Next message: <A HREF="000049.html">[C++ Toolkit ANNOUNCE] C++ ID1_FETCH
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#48">[ date ]</a>
<a href="thread.html#48">[ thread ]</a>
<a href="subject.html#48">[ subject ]</a>
<a href="author.html#48">[ author ]</a>
</LI>
</UL>
<HR>
<!--/htdig_noindex-->
<!--beginarticle-->
<PRE>FYI,
Added:
+ template&lt;class C&gt; C&amp; SerialAssign(C&amp; dest, const C&amp; src)
+ template&lt;class C&gt; bool SerialEquals(const C&amp; object1, const C&amp; object2);
+ class CSerialUserOp
[ASSIGNMENT]
Some time ago, we took steps to prohibit dangerous compiler-generated
bit-wise copying and assignment for all of our generated serializable
classes.
However, as object copying still can be a necessity, so Aleksey
Grichenko has implemented a special generic template function
to copy one serializable object to another:
template&lt;class C&gt; C&amp; SerialAssign(C&amp; dest, const C&amp; src)
(<A HREF="http://www.ncbi.nlm.nih.gov/IEB/ToolBox/CPP_DOC/lxr/ident?i=SerialAssign">http://www.ncbi.nlm.nih.gov/IEB/ToolBox/CPP_DOC/lxr/ident?i=SerialAssign</A>)
For example:
// For the particular class:
CBioseq* BioseqClone(const CBioseq&amp; bs)
{
CBioseq* new_bs = new CBioseq;
SerialAssign&lt;CBioseq&gt;(*new_bs, bs);
return new_bs;
}
// For any eligible class
template&lt;class C&gt;
C* SerialClone(const C&amp; c)
{
C* new_c = new C;
SerialAssign&lt;C&gt;(*new_c, c);
return c;
}
ATTENTION! The destination object must be of exactly(!) the same type as
the source one. Otherwise, an exception will be thrown in runtime.
By default, only the serializable data of the object will be copied
over by SerialAssign(). E.g., in case of &quot;CBioseq&quot;, only members
of its base class, &quot;CBioseq_&quot; would have been copied.
If you want to provide for the copying of some (or all)
non-serializable members as well, then you should inherit
&quot;CBioseq&quot; from the special base class &quot;CSerialUserOp&quot; (in addition
to &quot;CBioseq_&quot;) and implement its virtual function:
virtual void Assign(const CSerialUserOp&amp; source);
This function will be called right after all serializable members
of &quot;CBioseq&quot; are copied over, and in CBioseq::Assign() you can
copy non-serializable &quot;CBioseq&quot; data members manually:
void CBioseq::Assign(const CSerialUserOp&amp; source)
{
const CBioseq&amp; src = dynamic_cast&lt;const CBioseq&amp;&gt;(source);
m_ParentEntry = src.m_ParentEntry;
}
[COMPARISON]
The same technique should be used to provide for the serializable
class comparison.
The generic comparison function (template) is:
template&lt;class C&gt; bool SerialEquals(const C&amp; object1, const C&amp; object2);
(<A HREF="http://www.ncbi.nlm.nih.gov/IEB/ToolBox/CPP_DOC/lxr/ident?i=SerialEquals">http://www.ncbi.nlm.nih.gov/IEB/ToolBox/CPP_DOC/lxr/ident?i=SerialEquals</A>)
and by default it compares only serializable data members.
For example:
// For the particular class:
bool BioseqEquals(const CBioseq&amp; bs1, const CBioseq&amp; bs2)
{
return SerialEquals&lt;CBioseq&gt;(bs1, bs2);
}
To continue comparison in the case if all serializable data members
are equal, you should inherit from &quot;CSerialUserOp&quot; and implement
your own comparison method:
virtual bool Equals(const CSerialUserOp&amp; object) const;
E.g. for &quot;CBioseq&quot;:
bool CBioseq::Equals(const CSerialUserOp&amp; object) const
{
const CBioseq&amp; obj = dynamic_cast&lt;const CBioseq&amp;&gt;(object);
return m_ParentEntry == obj.m_ParentEntry;
}
</PRE>
<!--endarticle-->
<!--htdig_noindex-->
<HR>
<P><UL>
<!--threads-->
<LI>Previous message: <A HREF="000047.html">[C++ Toolkit ANNOUNCE] NCBI C++ Toolkit SNAPSHOT
</A></li>
<LI>Next message: <A HREF="000049.html">[C++ Toolkit ANNOUNCE] C++ ID1_FETCH
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#48">[ date ]</a>
<a href="thread.html#48">[ thread ]</a>
<a href="subject.html#48">[ subject ]</a>
<a href="author.html#48">[ author ]</a>
</LI>
</UL>
<hr>
<a href="http://www.ncbi.nlm.nih.gov/mailman/listinfo/cpp-announce">More information about the cpp-announce
mailing list</a><br>
<!--/htdig_noindex-->
</body></html>