Friday, October 2, 2009

Two way serialization function

A trick to avoid having to keep the serialization code for input and output in sync is to use the same code for both input and output:

struct Object {
template <>
STREAM & serialize(STREAM & stream) {
return stream & a & b & c;
}
int a, b, c;
};

Here we have used & as our serialization operator. We could use any operator we like.

We then just implement the operator to do the right thing for our input and output streams:

template < > InputArchive & operator &(InputArchive &a, int &v) {
a.read(&v, sizeof(v));
return a;
}

template < > OutputArchive & operator & (OutputArchive &a, int &v) {
a.write(&v, sizeof(v));
return a;
}

These are both template specializations of a generic streaming template.

template <>
STREAM & operator &(STREAM & stream, T & t) {
t.serialize(stream);
}

Now we can stream all kinds of types either by implementing serialize in the type or by defining a template specialization of operator & for that type.

4 comments:

  1. You can post assignments online to get some smart thoughts about your own task. Additionally, you can take some expert assistance from any great and drawing in task composing administrations on the web. do my math homework for me They are very great and furthermore have an extremely sensible value extend that understudies can manage.

    ReplyDelete
  2. Hey Dance Enthusiasts!

    Just stumbled upon this absolute gem – fuego dance shoes discount code As someone who believes in the power of dance to uplift the spirit, I can't help but get excited about adding a dash of savings to our dance wardrobe.

    Fuego Dance Shoes has been my go-to for quality and style on the dance floor, and now with the magic words "Fuego15," you can waltz, salsa, or tango your way into exclusive savings.

    ReplyDelete
  3. When considering the creation of a mobile app logo, partnering with a reputable graphic design company can be a game-changer. Professional graphic design services brings a wealth of experience and creative expertise to the table, ensuring that your logo not only meets but exceeds industry standards. Whether you're a startup or an established business, entrusting your logo design to a graphic design company can lead to a distinctive and impactful visual representation for your mobile app, setting you apart from the competition and leaving a lasting impression on your target audience.

    ReplyDelete