Boost logo

Boost :

From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2007-05-17 03:33:27

On 5/16/07, Phil Endecott <spam_from_boost_dev_at_[hidden]> wrote:
> Jake wrote:
> Here's a completely fabricated example. To generate:
> <rootnode name="r">
> <subthing>Hello</subthing>
> <subthing>World</subthing>
> </rootnode>
> how about:
> cout << node("rootnode")(
> attribute("name","r"),
> node("subthing")(text("Hello")),
> node("subthing")(text("World"))
> );

I've used this syntax in the past:

rootnode(name = "r")
   subthing(text = "Hello"),
   subthing(text = "World")

Not for XML output, but for general static info that is in a
hierarchy, and modeled somewhat after XML. You could also do:

   rootnode(name = "r", foo = 17)

where the attributes were not strings (could be anything that rootnode
'accepts') etc.

it requires pre 'building' the magic elements and attributes:

struct RootNode
   RootNode(string s);
   void Set(Foo foo);
   void AddChild(...);

// rootnode is an element corresponding to RootNode
// when attributes are passed to rootnode, it will construct a
RootNode with those attributes
// that correspond to the constructor, and call Set for the rest
element<RootNode> rootnode;
attribute<Foo> foo;

the rootnode(...) call doesn't have to create and return a RootNode,
but that's the default. The sub elements get added to the RootNode
via AddChild. If the attribute type doesn't match the Set(s) type, it
doesn't compile. If the child element type doesn't match the AddChild
function(s), it doesn't compile. Creating elements and adding
children, however, were just the default Policies, you could do
whatever. So maybe it could be made to output XML.

Anyhow, I just want to mention that interesting syntax like that IS
possible. (although it wasn't simple...)


Boost list run by bdawes at, gregod at, cpdaniel at, john at