Boost logo

Boost :

Subject: [boost] BOOST_FUSION_ADAPT_STRUCT - Printing Array Elements
From: forums_mp (forums_mp_at_[hidden])
Date: 2012-11-15 19:38:29


Given the snippet below. How do I print the contents within the array ?
I'd expect to see a counting pattern when the array is encountered but
that's not the case.

Thanks in advance.

[code]
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/next_prior.hpp>

#include <boost/fusion/mpl.hpp>
#include <boost/fusion/adapted.hpp> // BOOST_FUSION_ADAPT_STRUCT

// boost::fusion::result_of::value_at
#include <boost/fusion/sequence/intrinsic/value_at.hpp>
#include <boost/fusion/include/value_at.hpp>

// boost::fusion::result_of::size
#include <boost/fusion/sequence/intrinsic/size.hpp>
#include <boost/fusion/include/size.hpp>

// boost::fusion::at
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/include/at.hpp>

# include <iostream>
# include <vector>

typedef std::vector < int > INT_VEC ;
// Your existing struct
struct my_struct {
  int i;
  bool j;
  int arr1 [ 100 ];
// INT_VEC vv ;
// my_struct()
// : vv ( 100 )
// {}
};

// Generate an adapter allowing to view "Foo" as a Boost.Fusion sequence
BOOST_FUSION_ADAPT_STRUCT(
    my_struct,
    (int, i)
    (bool, j)
    (int, arr1[ 100 ] )
  // (int, vv[100])
)

template <typename T2>
struct parse_foo {
  static void parse(T2 & f);
};

struct display {
  template <typename T>
  void operator()(T& t) const {
    std::printf(" --%s--%s-- \n",
        typeid(t).name(),
        boost::lexical_cast<std::string>(t).c_str());
    if (typeid(t).name()[0] == 'c')
      std::printf(" --%s--%s-- \n",
                   typeid(t).name(), t);

    parse_foo<T>::parse(t);

  }
};

template <> void parse_foo<int >::parse(int & f) { std::cout << f <<
std::endl; }
template <> void parse_foo<char>::parse(char & f) {};
template <> void parse_foo<bool>::parse(bool & f) { std::cout << f <<
std::endl; };

#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/vector.hpp>

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/lexical_cast.hpp>

int main() {

  my_struct f;
  f.i = 33 ;
  f.j = false ;;
  for ( int odx ( 0 ); odx < 100; ++odx ) {
    f.arr1 [ odx ] = odx ;
  }
  boost::fusion::for_each(f, display());

}
[/code]

--
View this message in context: http://boost.2283326.n4.nabble.com/BOOST-FUSION-ADAPT-STRUCT-Printing-Array-Elements-tp4638682.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk