Boost logo

Boost :

From: Peder Holt (peder.holt_at_[hidden])
Date: 2007-07-25 16:03:39


<snip>

> I would like to see a more declarative syntax for much of this stuff.
>
> element_ptr info = root->insert_element(root->begin_children(), "articleinfo");
> if (title)
> {
> info->insert(info->begin_children(), title);
> info->insert_comment(info->begin_children(), "This title was moved");
> }
> element_ptr author = info->append_element("author");
> element_ptr firstname = author->append_element("firstname");
> firstname->set_content("Joe");
> element_ptr surname = author->append_element("surname");
> surname->set_content("Random");
>
> could be something like:
>
> root.push_front(
> tag("articleinfo")[
> title ? (comment("This title was moved"), title) : NULL
> , tag("author")[
> tag("firstname")["Joe"],
> tag("surname")["Random"]
> ]
> ]
> )
>

I have implemented something similar for my company.
We already have an xml parser that we use, but until now we have had
no formalized schema (except documents describing them.)
In stead of creating a tool that generates c++ code based on an xml
schema, we write the xml-schema directly in c++. The import/export
code is independent of this schema.
The xml-schema is implemented using expression templates.
Here is how it looks:

//Class definition
class xml_position : public attributes {
public:
   xml_position();
   position import() const;
   bool export(const position& pos);
private:
   double m_x,m_y,m_z;
};

//Implementation
xml_position::xml_position()
{
   required(attribute<double>("x",&xml_position::m_x);
   required(attribute<double>("y",&xml_position::m_y);
   required(attribute<double>("z",&xml_position::m_z);
}

//import/export code is trivial.

Now, another class, indexedPoints, has the following member
typedef std::map<int,position> position_map;
position_map m_positions;

This can be exposed as follows:

required(
   sequence(
      "positions",
      element<xml_position>("position",&position_map::second)
      .required(attribute<int>("index",&position_map::first)),
     &indexedPoints::m_positions
   )
);

The nice thing about this, is that we get scema validation for free,
and we are able to auto generate a valid w3c xml schema.

Regards,
Peder

>
> Cheers,
>
> --
> Dave Abrahams
> Boost Consulting
> http://www.boost-consulting.com
>
> The Astoria Seminar ==> http://www.astoriaseminar.com
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>


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