Boost logo

Boost :

From: Itay Maman (itay_maman_at_[hidden])
Date: 2002-08-01 00:59:03


Hi,

I feel that I didn't get the full notion of your suggestion, maybe a short formal description is needed. Anyway, based on your example ( y["b"] = "hello"; x["c"] = y;) I would suggest somethin like this:

struct LayeredContainer
{
    map<string, any> data_;

    any& operator[](const string& s)
    {
       string::size_type pos = s.find('.');
       if(pos == s.npos)
          return data_[s];

       any temp = data_[s.substr(0, pos)];
       if(LayeredContainer* next_p = any_cast<LayeredContainer>(&temp) )
         return (*next_p)[s.substr(pos+1, s.npos)];

       throw runtime_error("Wrong!");
    }
};

Generally speaking, I feel that a variant can replace boost.any in most cases where a generic container is needed. It does have its drawbacks (primarily: (1) heavier compile-time demands, and (2) the set of acceptable types is bounded), though. You can find previous stable versions in the file section.

-Itay.

----- Original Message -----
From: "Geoff Leyland" <geoff.leyland_at_[hidden]>
Newsgroups: gmane.comp.lib.boost.devel
Sent: Wednesday, July 31, 2002 1:46 PM
Subject: Re: Any, variant and more, and Sobol sequences

> Hi,
>
> Sorry I didn't reply sooner, my presence near mail has been a bit patchy
> for the last few days, and will continue to be so for a few more, but I
> am definitely interested in any/variant.
>
> On Samedi, juillet 27, 2002, at 10:27 , "Itay Maman"
> <itay_maman_at_[hidden]> wrote:
>
> > Consider this code:
> >
> > map<string, any> x;
> > x["a.b"] = 2.0;
> > x["a.c.d"] = "hello";
> >
> > if(string* p = any_cast<string>(&(x["a.c.d"]))
> > cout << *p;
> >
> > Isn't it semantically equivalent to your code?
>
> Yes, but I think I didn't explain myself very well, or that I've missed
> some cunning ways of doing things. What you've written works fine, and
> would be both simpler than what I've done, and maybe faster, but
>
> map<string, any> x, y;
> y["a"] = 2.0;
> y["b"] = "hello";
> x["c"] = y;
>
> ends up with you having to go x["c"].be<map<string, any> >()["b"] to get
> "hello" back, whereas I'd rather go x["c.b"] (and this is a case where
> you really have to use the "be" or "any_cast", which you can avoid for
> assignment and casts). You could get over this by overloading the "="
> operator so that it tacked "c." onto the beginning of all the elements
> of y and assigned them to x with full names.
>
> Passing a const reference to part of x :
>
> do_something(x["c"]);
>
> would also be difficult - you could "find" "c." in x, and then make a
> new map out of all subsequent things you iterated through that started
> with "c.", but this would preclude the use of hash_maps (which never
> seem to work for more than one compiler for me anyway, but surely will
> one day)
>
> passing a non-const reference would just get too tricky:
>
> add_some_things_to_this_struct(x["c"]);
>
> because you'd have to somehow recover the values back into x, or pass
> copies of the elements (can many anys refer to the same holder?),
>
> and when you started to use vector notation, and wanted equivalence
> between
>
> x["g[1]"];
>
> and
>
> x["g"][1]
>
> you'd be all tied up.
>
> So I think it's worth "interpreting" the "addresses" of the members, and
> having a structure that contains structures, but I'd be very happy to
> hear from anyone who can make my life simpler - not that the address
> interpreting is very difficult either.
>
> Do I take it from your mail that I should be looking at variant as the
> future of any/variant? Can I check it out of cvs?
>

---------------------------------
Do You Yahoo!?
Yahoo! Health - Feel better, live better



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