Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2003-10-12 17:19:41


The name of collection items is hard coded as "item"
I'm not sure that every XML data item needs a name attribute but
I'm sure everyone expects to see one - as I do. In any case I wasn't
concerned as the the following example shows:

class A
{
        vector<int> ivector;
        vector<float> ffector
        template<class Archive>
        void serialize(Archive &ar, unsigned int file_version){
                ar << BOOST_SERIALIZATION_NVP(ivector);
                ar << BOOST_SERIALIZATION_NVP(fvector);
        }
}

would be rendered as:

...
<ivector>
        <count>2</count>
        <item>32</item>
        <item>47></item>
</ivector>
<fvector>
        <count>24.8768</count>
        <item>32.23</item>
        <item>478.97></item>
</fvector>

 There's never any possibilty of confusion. In fact serializaiton doesn't use
the names at all.

If you still don't like the word "item" the best is to override the appropriat
aspect of the collection serialization as you've done here:

>FWIW, these are the macros I came up with:
>#define CONTAINER_ELEMENT_NAME(T, name)
[snip...]

It seems to me not a widely needed facility so I would be relucant
to complicate the library more than it already is. On the other hand
hard coded strings always end up an issue so I'll see what I can do.

Your method has an issue in that it would very the hard code string
for ALL objects of a particular type of collection rather than just
those corresponding to particular member variable. If I use
vector<int> in different places and the name of the item is important
to to me I will most likely want to alter them idependantlly.

Another issue raised is that it seems that you're altering or preparing
XML archives to be consumed by the serialization system. I would
be skeptical that this isn't going to create problems. C++ serialization
is driven by the class structure and serialize functions. Trying to
keep this in sync with some external XML schema would create
a maintainence problem in my view. If you need this I think you would
be better off with a system which - given the XML schema, produces
C++ class/code that are equivalent.

Robert Ramey

Craigp wrote:

> I was thinking about customizing some of the serialization logic (eg:
> removing the 'count' element), but then I wound up just writing an xslt
> script to transform the XML file into something the boost xml
> serialization library liked (except I still wanted a way to keep the
> element names the same).

> > For XML serialization, container element names seem to be hardcoded
> > to 'item'. Is it possible to override this? Partial specialization
> > worked, but I had to make save_collection a class (I renamed it to
> > archive_output_seq to mirror archive_input_seq), and, well, I used
> > partial specialization. :-) Is there a better way of overriding
> > container element names? If not, can you make save_collection a
> > class, so we can partially specialize it?
>

"Brian McNamara" <lorgon_at_[hidden]> wrote in message
news:<20031006051501.GB13144_at_[hidden]>...

> If all you want to specialize is the name, then it seems to me the
> right thing to do is
>
> (1) create
>
> namespace boost{
> namespace serialization {
> namespace stl {
> template <class T> struct container_element_name {
> static const char* name = "item";
> // I forget if it's legal to initialize that inline or not
> };
> }}}
>
> (2) replace the hardcoded "item" in the library with
>
> container_element_name<T>::name
>

Craigp wrote:

>Robert, as per my last post, would you mind adding something like the
>following to allow for overriding XML container element names?

>template <class T> struct container_element_name
>{
> static const char* name;
>};

>//default name
>template <class T>
>const char* container_element_name<T>::name = "item";

>Replace "item" with container_element_name<Container>::name in
>collections_load_imp.hpp and collections_save_imp.hpp (and
>[load,save]_array_type in [i,o]serializer.hpp)?

>FWIW, these are the macros I came up with:
>#define CONTAINER_ELEMENT_NAME(T, name)
[snip...]


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