Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2002-09-12 02:07:01


Michel André wrote:
> > > Oh... I thought about variable names too! Did I understood correctly
> > > that you use scheme with const and non-const 'describe'?
> >
> > Here is what I do currently:
[snip]
> > template<class W, class A> void write(W & w, X const & x, A const & a)
> > {
> > begin_struct(w, a);
> > write(w, x.i, "i");
> > end_struct(w, a);
> > }
> >
> > This is quite repetitive and error-prone, but without
> reflection/metadata...
>
> Actually I use a similar aproach to describe/serialize objects. This is a
> minimalist example showing my approach..
>
>
> template<typename DATA>
> struct Description
> {};
>
> struct Person
> {
> Person() {}
> Person(const std::string& firstName, const std::string& lastName, int
> birthYear) : m_firstName(firstName), m_lastName(lastName),
> m_birthYear(birthYear) {}
> std::string m_firstName;
> std::string m_lastName;
> int m_birthYear;
> };
>
> template<>
> struct Description<Person>
> {
> template<typename STRUCT, typename VISITOR>
> static void Describe(STRUCT& data, VISITOR& visitor)
> {
> visitor.Begin(data);
> visitor.Attribute(data.m_firstName, "FirstName");
> visitor.Attribute(data.m_lastName, "LastName");
> visitor.Attribute(data.m_birthYear, "BirthYear");
> visitor.End(data);
> }
> };
[snip]
> int main(int argc, char* argv[])
> {
> Person p("Michel", "Andre", 1971);
> std::stringstream ss;
> Writer w(ss);
> Describe(p, w);
> Person p1;
> Reader r(ss);
> Describe(p1, r);
> Writer w1(std::cout);
> Describe(p1, w1);
> return 0;
> }
>
> I beleive this is const safe since STRUCT& can be bound to const Person& if
> a const safe version is needed.

Let me see... I think you're right! Or do I miss something? I had some
ideas how const_cast without risking underfined behaviour, but your
approach requires no tricks.

> It also solves the problem with needing two or 3 separate description/stream
> functions. The version we use is a bit more sophisticated but this is the
> base (Basically the Description class is used as a streaming policy traits
> class containing more information about a type and also carrying information
> if a class has a specialized description and an unique string and numeric id
> for the type). All error detection is omitted. I have 4 formats today a xml,
> binary, text, db.
>
> Maybe this approach could be modified and used to build a serialization
> library on? At least I've used it successfully in several projects, but
> maybe the a aproach is to simplistic for a complex streaming library.

I'd like something like that --- and don't see why it's too simplictic.

- Volodya


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk