| 1 | #include <iostream>
|
|---|
| 2 | #include <boost/mpl/transform.hpp>
|
|---|
| 3 | #include <boost/fusion/include/mpl.hpp>
|
|---|
| 4 | #include <boost/fusion/adapted/mpl.hpp>
|
|---|
| 5 | #include <boost/fusion/include/at.hpp>
|
|---|
| 6 | #include <boost/fusion/include/as_vector.hpp>
|
|---|
| 7 | #include <boost/type_traits/add_reference.hpp>
|
|---|
| 8 | #include <boost/fusion/include/adapt_struct.hpp>
|
|---|
| 9 |
|
|---|
| 10 | struct foo
|
|---|
| 11 | {
|
|---|
| 12 | double d; float f; short c;
|
|---|
| 13 | };
|
|---|
| 14 |
|
|---|
| 15 | BOOST_FUSION_ADAPT_STRUCT(foo,(double,d)(float,f)(short,c))
|
|---|
| 16 |
|
|---|
| 17 | template<class T>
|
|---|
| 18 | class composite_reference
|
|---|
| 19 | : public boost::mpl::
|
|---|
| 20 | transform < typename boost::fusion::result_of::
|
|---|
| 21 | as_vector<T>::type
|
|---|
| 22 | , boost::add_reference<boost::mpl::_>
|
|---|
| 23 | >::type
|
|---|
| 24 | {
|
|---|
| 25 | public:
|
|---|
| 26 | typedef typename boost::mpl::
|
|---|
| 27 | transform < typename boost::fusion::result_of::
|
|---|
| 28 | as_vector<T>::type
|
|---|
| 29 | , boost::add_reference<boost::mpl::_>
|
|---|
| 30 | >::type parent;
|
|---|
| 31 |
|
|---|
| 32 | composite_reference(T& src) : parent( src ) {}
|
|---|
| 33 | composite_reference(parent& src) : parent(src) {}
|
|---|
| 34 |
|
|---|
| 35 | composite_reference& operator=(T& src)
|
|---|
| 36 | {
|
|---|
| 37 | static_cast<parent&>(*this) = static_cast<parent&>(src);
|
|---|
| 38 | return *this;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | composite_reference& operator=(parent const& src)
|
|---|
| 42 | {
|
|---|
| 43 | static_cast<parent&>(*this) = src;
|
|---|
| 44 | return *this;
|
|---|
| 45 | }
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | int main(int,char**)
|
|---|
| 49 | {
|
|---|
| 50 | foo f;
|
|---|
| 51 | composite_reference<foo> ref_f(f);
|
|---|
| 52 |
|
|---|
| 53 | boost::fusion::at_c<0>(ref_f) = 1.2;
|
|---|
| 54 | boost::fusion::at_c<1>(ref_f) = 1.2f;
|
|---|
| 55 | boost::fusion::at_c<2>(ref_f) = 12;
|
|---|
| 56 |
|
|---|
| 57 | std::cout << f.d << " " << f.f << " " << f.c << "\n";
|
|---|
| 58 | }
|
|---|