Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2007-03-29 21:03:52


AMDG

Noah Roberts <roberts.noah <at> gmail.com> writes:

>
> Steven Watanabe wrote:
> > Do you think you can provide me with a
> > concrete case that I cannot solve easily using the
> > current library?
>
> Since any problem can be solved under almost any circumstance, no.

Key word: easily.
Never mind. What you say below answers my question

> <snip>
>
> There is one use case that we have in our products that is not covered
> by that. User defined units. These are specified as some conversion to
> an arbitrary base we set up.

Ah. So to do such conversions you need:

template<class Quantity>
class unit_converter {
  typedef typename get_unit<Quantity>::type Unit;
  typedef typename get_dimensions<Unit>::type Dimensions;
  typedef typename Quantity::value_type value_type;
public:
  typedef Quantity result_type;
  typedef const value_type& argument_type;
  typedef result_type function_type(const value_type&);
  template<class System>
  unit_converter(const unit<Dimensions, System>&) :
    converter(static_cast<function_type*>(&convert_impl<OtherSystem>)) {}
  unit_converter(const value_type& factor) :
    converter(lambda::bind(from_value(), lambda::_1 * factor)) {}
  unit_converter(const value_type& factor, const value_type& offset) :
    converter(lambda::bind(from_value(), lambda::_1 * factor + offset)) {}
  result_type operator()(const value_type& v) const {
    return(converter(v));
  }
private:
  boost::function<function_type> converter;
  template<class OtherSystem>
  static result_type convert_impl(const value_type& v) {
    return(result_type(v * unit<Dimensions, OtherSystem>()));
  }
  struct from_value {
    template<class T>
    struct sig { typedef result_type type; };
    result_type operator()(const value_type& v) const {
      return(result_type::from_value(v));
    }
  }
};

Adding the reverse conversion ~doubles the size. Again
if this doesn't fit you needs it may take even more code.

>
> Your idea of a allowing the unit override might solve may of my
> objections. I would have to review that feature as it came. When I was
> looking at this library it did not seem at all straight forward to do so.

Allowing it is pretty straight forward. I admit
that it probably doesn't look like it unless you
know the internals well.

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