Boost logo

Boost :

Subject: Re: [boost] Boost::Units - mapping from units object to something else
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2013-09-21 00:11:17


AMDG

On 09/20/2013 10:34 AM, david.hagood_at_[hidden] wrote:
> I'm looking at the Boost::Units classes to help eliminate a family of
> programming errors, but there is one thing that I'm having difficulty
> with:
>
> I have a collection of objects of my own type that pertains to units -
> these are of a fixed type, with instances per type of unit.
>
> I'd like to somehow be able to map from the units constants that Boost
> uses (e.g. boost:units:si:watts) into my objects (return the correct
> instance for "watts"). Obviously, there's going to be some hand coding to
> make that happen - it is unlikely to be possible to generate that mapping
> automatically.
>
> Ideally, what I'd like to do would be to have something like
>
> template <typename Unit>
> struct MyMapper
> {
> static const MyUnitInfo info;
> };
>
> template <> const MyUnitInfo<boost::units::si::watts>::info(/*ctor parms
> for my type */);
>
> and do things like:
>
> MyMapper<boost::units::si::watts>::info
>
> to get the appropriate structure.
>
> However, since all the various Boost units are in a type hierarchy to
> express dimensionality, I'm at a loss how to compose a template to allow
> something like that to happen - it certainly isn't as straightforward as
> the above, since Boost units instances of varying types, not typenames.
>

I don't quite understand why this doesn't work.
(Other than the obvious typos.) I also have
no idea what you mean by "varying types, not typenames"

> Likewise, just defining some form of map:
>
> std::map<???,MyUnitInfo> mymap = boost::assign::map_list_of
> (boost::units::si::watts,MyUnitInfo(/* my parms*/))
> (boost::units::si::ohms,MyUnitInfo(...));
>
> won't work as the types for the Boost constants are all different as far
> as I can see.
>

fusion::map can handle this.

> Is there some base type all the Boost::Units constants extend, such that I
> could test for that?
>

No. If you want to identify units and quantities use
the metafunctions is_unit and is_quantity.

In Christ,
Steven Watanabe


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