Boost logo

Boost Users :

From: Chris Ross (cross+boost_at_[hidden])
Date: 2008-06-22 17:57:22


On Jun 22, 2008, at 11:49, Ovanes Markarian wrote:
>> Does anyone have a suggestion as to how I can get a value out of a
>> boost::any for which I don't know the specific type stored in it?
> if you know the set of types you operate on consider using
> boost::variant which supports visitor pattern for dispatching of
> currently stored type.

   Yes, I'll have to look into boost::variant. Thanks for that
pointer, all.

> Hope that helps. It is difficult to make suggestions, because we don't
> if you are allowed to modify the interface.

   Right. And a very good point to bring up. I am constructing this
100% myself from scratch, so anything *can* be done.

   The super-class in which I'm trying to extract this value is a
generic database access object. The boost::any is being returned from
a "get me the value of column X in table Y" method, implemented by any
subclass that implements the database-specific layer. So, the values
could literally be any value that could be stored in any data value in
any database, which is why I was trying to use something very generic.

   This particular function, the first I'm implementing in the design
as I'm building it out, is asking for a value it knows will be
effectively boolean. But, depending on the underlying database, may
be an int, or some other type. The first case I'm coding is using an
sqlite3 subclass of the database object, and there is no boolean
type. There is an integer type, and since that database (in current
versions) supports 64-bit integers, I've coded that method to return a
boost::any<int64_t> when it finds an integer column it's to return.

   I'm not sure boost::variant is sufficiently flexible given what I
want to do, but it clearly bears investigation as I hadn't looked at
it yet. I'd be happy to hear any design suggestions from anyone who
can think of a good way to do this. I suppose I could make some sort
of template class for "database column value", but I'd hoped not to
have to do that.

                      - Chris


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net