The Artima Developer Community
Sponsored Link

Weblogs Forum
Automated Serialization using Templates

6 replies on 1 page. Most recent reply: Sep 14, 2005 5:08 AM by Vesa Karvonen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Automated Serialization using Templates (View in Weblogs)
Posted: Sep 8, 2005 5:55 PM
Reply to this message Reply
Summary
Here is a pattern I call the Bus Pattern. It's like the Visitor Pattern, except it's not.
Advertisement
I am probably reinventing the wheel, but here goes nothing. Given the following class:
  MyClass {
    ClassA a;
    ClassB c
    ClassC c
  } 
Let's say you want to stream the thing to XML. Simply add the following function:
  template<typename Bus> 
  void GetOnTheBus(Bus bus) {
    bus(a);
    bus(b);
    bus(c);
  }
Now you could write an XML streaming mechanism like this:
  class ToXmlBus {
  public:
    template<typename T>
    operator()(T& x) {
      x.GetOnTheBus();
    }
    operator()(int& n) {
      cout << "<int>" << n << "</int>";
    }
    operator()(char& c) {
      cout << "<char>" << n << "</char>";
    }
    ...
  }
  class FromXmlBus {
  public:
    template<typename T>
    operator()(T& x) {
      x.GetOnTheBus();
    }
    operator()(int& n) {
      cin >> "<int>" >> n >> "</int>";
    }
    operator()(char& c) {
      cin >> "<char>" >> n >> "</char>";
    }
    ...
  }
Cool isn't it? Now I can't be the first person to name this, can anyone point me in the right direction?


Volodya Orlenko

Posts: 2
Nickname: volodya
Registered: Aug, 2005

Re: Automated Serialization using Templates Posted: Sep 8, 2005 6:49 PM
Reply to this message Reply
I found that almost every possible template-based idea that ever came to my ming is already implemented in Boost :)

This one is in Boost Serialization.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Automated Serialization using Templates Posted: Sep 8, 2005 6:54 PM
Reply to this message Reply
> I found that almost every possible template-based idea
> that ever came to my ming is already implemented in Boost
> :)
>
> This one is in Boost Serialization.

Thanks for pointing that out to me, I appreciate it.

Harrison Ainsworth

Posts: 57
Nickname: hxa7241
Registered: Apr, 2005

i recognize it; and more automation still? Posted: Sep 9, 2005 12:50 PM
Reply to this message Reply
i have done something like this, except:

* the getOnTheBus equivalent was manually separated into two -- serializing-in sometimes includes validation logic, and they were operator<< and >> to follow the stream convention.
* the Bus argument was polymorphic to give xml/text/binary serialization alternatives.

it soon ran into problems; gradually extending it lead inexerorably to mixing virtuals and member templates and template parameters that are templates and partial specialisations (oh no!). a disheartening experience (i tend to avoid templates now, and am happier for it).

what i also tried to do, and almost did, is to have something even more automated: instead of needing a sequence of calls, just pass an array of pointers (to the member variables) to the serializer. does anyone have an implementation that does that?

Todd Blanchard

Posts: 316
Nickname: tblanchard
Registered: May, 2003

I don't get the value of the template Posted: Sep 10, 2005 8:07 AM
Reply to this message Reply
I did something a lot like this many years ago (about 10) to bind memory locations to SQL fetches using the old open client library in sybase. It was part of an extension to iostreams that provided dbstreams.

Templates are not necessary if you only use it on classes with an abstract base class that includes getOnTheBus in its protocol or provides an appropriate friend function (I piggybacked this on the existence of op<< and >>).

It seems to me that C++ template use is all about getting duck typing, which is the antithesis of the original type-implies-protocol system of C++. In this case the template use just clutters the basic intent I think.

Adi Shavit

Posts: 6
Nickname: adish
Registered: Apr, 2005

Re: Automated Serialization using Templates Posted: Sep 10, 2005 11:06 PM
Reply to this message Reply
This is essentially like: <http://www.codeproject.com/soap/paramio.asp> an extended version of which appeared in CUJ May '04 only there the xml mechanism is external and the serialization is done via the "normal" <</>>.
Adi

Vesa Karvonen

Posts: 116
Nickname: vkarvone
Registered: Jun, 2004

Re: Automated Serialization using Templates Posted: Sep 14, 2005 5:08 AM
Reply to this message Reply
This isn't immediately related to your technique, but I thought it might still be interesting to know about.

Standard ML (SML) does not include language support for serialization. Furthermore, although the SML Basis Library does contain a few overloaded operators, SML does not provide language support for overloading. Nevertheless, you can implement a fairly convenient combinator library for serialization as shown in the technical report "Type-Specialized Serialization with Sharing" by Martin Elsman: http://www1.itu.dk/sw5450.asp.

You'll need to be able to read SML to make most out of the TR, but Elsman's combinator library essentially allows you to implement serialization functions (pickling and unpickling functions) by specifying the type of the data using combinators. The TR gives an example implementation of serialization functions for a list of integer pairs:


let open Pickle in list (pair (int, int)) end


The identifiers "list", "pair", and "int" in the above snippet of code are not bound to types, but to combinators that produce serialization functions. The combinators are defined by the "Pickle" module. The idiom "let open DSL in ... end" is a common idiom in SML for using a Domain Specific combinator Library (or Language).

Flat View: This topic has 6 replies on 1 page
Topic: For Loops Previous Topic   Next Topic Topic: Heron Needs a Killer App

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use