
I would like to use boost::units in a new application that must interface to existing APIs that do not provide templated interfaces. What is the easiest way to use these existing functions that minimizes the copying of data from raw C pointers to a container that holds a specific type of boost::units? Thanks, Jody

AMDG Joseph Winston III wrote:
I would like to use boost::units in a new application that must interface to existing APIs that do not provide templated interfaces. What is the easiest way to use these existing functions that minimizes the copying of data from raw C pointers to a container that holds a specific type of boost::units?
Would a view like this be helpful? #include <boost/iterator/transform_iterator.hpp> #include <boost/units/quantity.hpp> #include <boost/units/systems/si.hpp> #include <iostream> #include <algorithm> using namespace boost::units; template<class T> class quantity_view { struct wrap_in_quantity { typedef T result_type; result_type operator()(const typename T::value_type& arg) const { return(T::from_value(arg)); } }; public: typedef typename T::value_type storage_value_type; typedef T value_type; typedef boost::transform_iterator< wrap_in_quantity, const storage_value_type*> iterator; typedef iterator const_iterator; quantity_view(const storage_value_type* impl, std::size_t size) : impl(impl), size(size) {} const_iterator begin() const { return(iterator(impl)); } const_iterator end() const { return(iterator(impl + size)); } private: const storage_value_type* impl; std::size_t size; }; int main() { double data[] = {1, 2, 3, 4, 5}; quantity_view<quantity<si::time> > tester(&data[0], 5); std::ostream_iterator<quantity<si::time> > out(std::cout, "\n"); std::copy(tester.begin(), tester.end(), out); } In Christ, Steven Watanabe

I would like to use boost::units in a new application that must interface to existing APIs that do not provide templated interfaces. What is the easiest way to use these existing functions that minimizes the copying of data from raw C pointers to a container that holds a specific type of boost::units?
On most compilers, the binary layout of quantity<Unit,Y> should be the same as that of the value type, Y, so you should be able to do something like this : std::vector<quantity<si::energy,double> > v(100); double *vp = reinterpret_cast<double*>(&v.front()); passing the resulting pointer to/from the legacy functions : do_something_in_legacy_code(vp); Naturally, you should verify that the layout is really compatible ... Matthias
participants (3)
-
Joseph Winston III
-
Matthias Schabel
-
Steven Watanabe