summaryrefslogtreecommitdiff
path: root/libs/fusion/test/sequence/ref_vector.cpp
blob: 8bd0929b8f23e2acb90f0ec675ca7de353d43733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*=============================================================================
    Copyright (c) 2012 Joel falcou

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#include <iostream>
#include <boost/mpl/transform.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

struct foo
{
  double d; float f; short c;
};

BOOST_FUSION_ADAPT_STRUCT(foo,(double,d)(float,f)(short,c))

template<class T>
class composite_reference
    : public  boost::mpl::
              transform < typename  boost::fusion::result_of::
                                    as_vector<T>::type
                        , boost::add_reference<boost::mpl::_>
                        >::type
{
  public:
  typedef typename  boost::mpl::
          transform < typename  boost::fusion::result_of::
                                    as_vector<T>::type
                        , boost::add_reference<boost::mpl::_>
                        >::type                                 parent;

  composite_reference(T& src)       : parent( src ) {}
  composite_reference(parent& src)  : parent(src) {}

  composite_reference& operator=(T& src)
  {
    static_cast<parent&>(*this) = static_cast<parent&>(src);
    return *this;
  }

  composite_reference& operator=(parent const& src)
  {
    static_cast<parent&>(*this) = src;
    return *this;
  }
};

int main(int,char**)
{
  foo f;
  composite_reference<foo> ref_f(f);

  boost::fusion::at_c<0>(ref_f) = 1.2;
  boost::fusion::at_c<1>(ref_f) = 1.2f;
  boost::fusion::at_c<2>(ref_f) = 12;

  std::cout << f.d << " " << f.f << " " << f.c << "\n";
}