|
Boost : |
From: edA-qa mort-ora-y (eda-qa_at_[hidden])
Date: 2004-07-04 12:42:52
Is there any interest in a library that performs strict type checking on
values with unit quantities? The intent is to prevent operation and
assigment of unlike types.
For example, if A is in meters/second, and B is in seconds:
-A * B results in a value with "meters" units
-A / B results in a value with "meters/seconds^2"
-A = B results in an exception due to a mismatch
The code I have so far supports arbitrary unit types via tokens, which
have to be defined for a project. Some example code looks like (with
several notation options):
typedef UnitValue<float> uFloat;
UnitType unit_m( 1 ); //define meters type
UnitType unit_s( 2 ); //define seconds type
uFloat time( unit_s );
time = unit_s( 3.0f ); //3 seconds
uFloat speed( unit_m / unit_s );
speed = 15 * speed.Type(); //set speed of 15m/s
uFloat accel( unit_m / unit_s / unit_s );
accel = speed / time; //results in 5m/s^2
accel = speed; //throws exception, m/s != m/s^2
time = accel; //throws exception, s != m/s^2
The library has a price for performance compared to native value
operations. However, part of the requirements will be a flag (a #define
) that turns off the unit processing for the code, which effectively
removes the classes (replaces the UnitValue simply with typedefs of the
stored type).
-- edA-qa mort-ora-y Idea Architect http://disemia.com/
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk