ref_vector.cpp 1.9 KB

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