Boost logo

Boost Users :

Subject: [Boost-users] Problems creating unit wrappers with StlSoft
From: Ryan (mccorywork_at_[hidden])
Date: 2011-04-05 10:40:27


My current project has several coordinate systems. These include
Polar(azimuth, elevation, range) and LLA (latitude, longitude, altitude).
I've really enjoyed using boost units to allow the user to input the
required unit system but converting them behind the scenes to the unit
system I need. I'm developing a function overload that lets the user call a
single function but based on the coordinate system given the function
behaves differently. Unfortunately, the units for Polar and LLA are the
same. Both systems use two plane angles and a length. A second problem is
two different groups use two different ways of talking about the Polar
coordinate system. One group defines Polar as azimuth, elevation and range
while the other group defines Polar as range, azimuth and elevation. I want
to give both group the ability to call the function with the Polar
definition they are used to talking about.

I realize I could create a class for Polar and LLA and then pass that class
to my overloaded function. This just pushes off the second problem to the
newly created classes. Instead of creating classes for the coordinate
systems I wanted to differentiate the different boost units from each
other. This would let me overload my function without the need to create
larger classes for each coordinate system.

I ended up modifying the "true_typedef" class from StlSoft that wraps a type
so it becomes incompatible with the same types. This allowed me, for
example, to wrap a boost unit plane angle into a longitude and latitude that
wouldn't be compatible with each other.

What I don't like about this process is having to define a structure type
and then using it in the typedef. Is there a boost library that would allow
me to create this class in one pass without the two step process?

Ryan

#define STLSOFT_GEN_OPAQUE(type) typedef struct __stlsoft_htype##{ int i; }
const* type;

using namespace boost::units;

template<typename T, typename U>
class BoostUnitQuantityWrapper
{
  typedef T unit_type;
  typedef U unique_type;
  typedef BoostUnitQuantityWrapper<T, U> class_type;

  quantity<unit_type> m_Value;

public:
  template<class System>
  explicit BoostUnitQuantityWrapper (
    quantity< unit< typename unit_type::dimension_type, System> > const&
value)
    : m_Value(quantity<unit_type>(value)) {}

  BoostUnitQuantityWrapper(class_type const& rhs)
    : m_Value(rhs.m_Value) {}

  class_type const& operator= (class_type const& rhs) {
    m_Value = rhs.m_Value;
    return *this;
  }

  operator quantity<unit_type> () const { return m_Value; }
};

STLSOFT_GEN_OPAQUE(latitude_type);
STLSOFT_GEN_OPAQUE(longitude_type);
STLSOFT_GEN_OPAQUE(range_type);
STLSOFT_GEN_OPAQUE(altitude_type);

typedef BoostUnitQuantityWrapper<si::plane_angle, latitude_type> Latitude;
typedef BoostUnitQuantityWrapper<si::plane_angle, longitude_type>
Longitude;

typedef BoostUnitQuantityWrapper<si::length, range_type> Range;
typedef BoostUnitQuantityWrapper<si::length, altitude_type> Altitude;



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net