|
Boost Users : |
Subject: Re: [Boost-users] units and existing APIs
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-11-03 16:17:52
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
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