Boost logo

Boost :

Subject: Re: [boost] Units of data
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-07-01 20:41:52


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.

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