Boost logo

Boost :

Subject: [boost] Boost::Units - mapping from units object to something else
From: david.hagood_at_[hidden]
Date: 2013-09-20 13:34:13


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.

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.

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


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