Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2004-01-19 04:12:32


1. Would this API be of any utility?

//========================================================================
// NONE of this has been compiled

template < typename ValueType, typename TagType >
    class tagged_t;

template < typename ValueType, typename TagType >
    ValueType extract_value( tagged_t const &t );

template < typename ValueType, typename TagType >
class tagged_t
{
public:
    typedef ValueType value_type;
    typedef TagType tag_type;

    explicit tagged_t( value_type const &v = value_type() )
        : v_( v )
        {}

    void reset( value_type const &n = value_type() )
        { this->v_ = n; }

private:
    friend
      value_type extract_value<value_type, tag_type>( tagged_t const & );

    value_type v_;
};

template < typename ValueType, typename TagType >
ValueType
extract_value( tagged_t const &t )
{ return t.v_; }
//========================================================================

I thought of it after thinking of the following:

2. Others have coming up with various unit/dimension interfaces and
implementations. I wonder if going down another level can help. What about
this (not compiled either):

//========================================================================
template < typename ValueType, class UnitType >
class quantity_t
    : public tagged_t< ValueType, UnitType >
{
    typedef tagged_t<ValueType, UnitType> base_type;

public:
    typedef ValueType value_type;
    typedef UnitType unit_type;

    typedef typename UnitType::dimension_type dimension_type;

    explicit quantity_t( value_type const &v = value_type() )
        : base_type( v )
        {}
};
//========================================================================

It's a variant of [1], but more specific to the units & dimension stuff. (I
thought of this first, then realized that [1] can be extracted as a separate
piece. I just didn't want people to look at my generalization and complain
about me missing the next generalization level.)

AFAIK, all units have to have some sort of dimension associated with them
(e.g. "meter" and "foot" are "length", "gallon" is "volume" or "length^3",
"kelvin" is "temperature", and "US$" is "money"). So I decided to encode
the dimension from the unit, instead of making the dimension a redundant
template parameter.

Note that there are no (arithmetic) operations defined for this type. Those
operations can be unit-dependent. I guess some sort of traits can be used
to specify how operations are done. There can be sample versions for the
standard physics units, a version for the no-adding Celsius and Fahrenheit,
and a version for the vector-multiplying force-length for torque.

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

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