
On Wed, Jul 1, 2009 at 7:41 PM, Steven Watanabe<watanabesj@gmail.com> wrote:
AMDG
Zachary Turner wrote:
Has anyone extended (even privately) Boost.Units to include support for units of data (bits, bytes, KB, MB, etc)? It seems like this would be a useful addition to Boost.Units.
Assuming no, what is the minimum amount of work required to do this myself? There's not a lot of info in the docs on how to define new base units, although I've already begun looking over the source code to try to come up with something
warning untested code.
#include <boost/units/base_dimension.hpp> #include <boost/units/base_unit.hpp> #include <boost/units/scale.hpp> #include <boost/units/scaled_base_unit.hpp>
// note that the numbers here are arbitrary as long as they are unique. struct data_base_dimension : boost::units::base_dimension<data_base_dimension, 73254> {}; typedef data_base_dimension::dimension_type data_dimension;
struct bit_base_unit : boost::units::base_unit<bit_base_unit, data_dimension, 76389> {}; typedef boost::units::scaled_base_unit<bit_base_unit, boost::units::scale<8>
byte_base_unit;
namespace boost { namespace units { template<> struct base_unit_info< ::byte_base_unit> { static const char* name() { return("byte"); } static const char* symbol() { return("B"); } }; } }
typedef boost::units::scaled_base_unit<byte_base_unit, boost::units::scale<2, boost::units::static_rational<10> > > KB_base_unit; typedef boost::units::scaled_base_unit<byte_base_unit, boost::units::scale<2, boost::units::static_rational<20> > > MB_base_unit; typedef boost::units::scaled_base_unit<byte_base_unit, boost::units::scale<2, boost::units::static_rational<30> > > GB_base_unit; typedef boost::units::scaled_base_unit<byte_base_unit, boost::units::scale<2, boost::units::static_rational<40> > > TB_base_unit;
See also http://www.boost.org/doc/html/boost_units/Dimensional_Analysis.html http://www.boost.org/doc/html/boost_units/Units.html I believe that these two pages show creating a minimal SI system from scratch.
Thanks for your response. I got this working mostly, but I'm still having difficulty seeing how to tie it all together. I added the following: typedef boost::units::make_system<bit_base_unit>::type data_system; typedef boost::units::unit<data_base_dimension::dimension_type, data_system> capacity; But I'm still not sure how to create and manipulate values with these units. For example, I can do: quantity<capacity> compressed_size; but if I try to assign it to anything I get errors. I tried using the BOOST_UNITS_STATIC_CONSTANT macro to create aliases such as bytes, megabytes, etc but I think this is the wrong approach, is it because all the units above are defined as base units? Ideally I want to be able to type: quantity<capacity> compressed_size = 10000 * bytes; quantity<capacity> total_size = 400000 * bytes; float ratio = compressed_size / total_size; //should be dimensionless quantity<time> duration = 300 * seconds; quantity<data_rate> = total_size / duration; //has dimensions capacity/time