Solved

BOOST_FUSION_ADAPT_STRUCT

Posted on 2013-06-02
1
595 Views
Last Modified: 2013-06-19
Given:

#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/fusion/mpl.hpp>
#include <boost/fusion/adapted.hpp> // BOOST_FUSION_ADAPT_STRUCT
#include <boost/array.hpp> 
// 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 <boost/bind.hpp>

# include <iostream>
# include <vector>
# include <typeinfo>
# include <cstdio>

typedef std::vector < int > INT_VEC ;
typedef boost::array<int, 10> array_t;
// Your existing struct 
struct my_struct { 
  int          i; 
  bool         j; 
  array_t      arr1; 
  unsigned int arr2 [ 10 ];
  INT_VEC      arr3 ; 
  my_struct() 
  : i    ( 0x4000 ) 
  , j    ( false )
  , arr3 ( 10, 6 ) 
  {
    for ( unsigned int odx ( 0 ); odx < 10; ++odx ) {
      arr2 [ odx ] = odx + 15  ;
    }
  } 
}; 

// Generate an adapter allowing to view "Foo" as a Boost.Fusion sequence 
BOOST_FUSION_ADAPT_STRUCT( 
    my_struct, 
    (int, i) 
    (bool, j) 
    (array_t, arr1 ) 
    (unsigned int, arr2 [ 10 ] ) 
    (INT_VEC, arr3) 
) 

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

struct work {
  template <typename T>
  void operator()(  T& t) const {
    parse<T>::decode(t);
  }
};

template <typename T2>
void parse<T2>::decode(  T2 & f) {
  boost::fusion::for_each(f, work());
};

template <> void parse< unsigned int >::decode(  unsigned int  & f) {  
  std::cout << f << std::endl; 
}

template <> void parse<int >::decode(  int  & f) {  
  std::cout << f << std::endl; 
}
template <> void parse<char>::decode(  char & f) {  
  std::cout << f << std::endl; 
}
template <> void parse<bool>::decode(  bool&  f) { 
  if ( f ) {
    std::cout << "true" << std::endl;
  } else {  
    std::cout << "false" << std::endl;
  }
}
template <> void parse<double>::decode(  double&  f) {  
  std::cout << "(double = " << f << std::endl;  
}

template <> void parse<array_t>::decode(   array_t&  f)  { 
  for(int i = 0; i < 10; ++i) 
    std::cout << f[i] << ";" ;
  std::cout << std::endl;
}

template <> void parse<INT_VEC>::decode(   INT_VEC&  f)  { 
  for(int i = 0; i < 10; ++i) 
    std::cout << f[i] << ";" ;
  std::cout << std::endl;
}

int main() {
  my_struct obj ;
  for ( unsigned int odx ( 0 ); odx < 10; ++odx ) {
    obj.arr1 [ odx ] = odx ;
  } 
  parse<my_struct>::decode( obj );
}

Open in new window


a)
How do I specialize the C style array (arr2) such that I could iterate over the array and display the contents?

b)

template <> void parse<INT_VEC>::decode(   INT_VEC&  f)  { 
  for(int i = 0; i < 10; ++i) 
    std::cout << f[i] << ";" ;
  std::cout << std::endl;
}

Open in new window


f.size() does not produce the correct number of elements (10).   Why?
0
Comment
Question by:forums_mp
1 Comment
 
LVL 33

Accepted Solution

by:
sarabande earned 200 total points
ID: 39218453
(unsigned int, arr2 [ 10 ] )

Open in new window


the current definition wasn't for an array but for a single unsigned int. and the address was out-of-boundary.

in my opinion you need an array type. you could try

typedef unsigned int UINTARR10[10];
....
BOOST_FUSION_ADAPT_STRUCT( 
    my_struct,
    ....
    (UINTARR10, arr2),
    ....

Open in new window



if that  doesn't compile, it is probably because of a missing operator. you then could define a structure that contains the array and provides the missing properties.

Sara
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Converting to C# also VS2015 5 125
Socket Programming (Unix) 8 127
I could not build boost code, 10 87
Path to  STL Map header file 1 57
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question