Boost logo

Boost Users :

From: Christian Henning (chhenning_at_[hidden])
Date: 2007-04-02 10:16:00


Thanks David, it works beautifully. Here is my code including
conversion to fusion list plus printing of the foreign key fields.

#include "boost/mpl/vector.hpp"
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/filter_view.hpp>

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

using namespace std;
using namespace boost;

namespace fields
{
  struct id { void print() { cout << "id" << endl; } };
  struct name { void print() { cout << "name" << endl;} };
  struct address { void print() { cout << "address" << endl; }};
  struct birthday { void print() { cout << "birthday" << endl; }};
}

namespace tags
{
  struct primary {};
  struct foreign {};
  struct no_key {};
}

typedef mpl::vector<fields::id , tags::primary> id_field_t;
typedef mpl::vector<fields::name , tags::foreign> name_field_t;
typedef mpl::vector<fields::address , tags::foreign> address_field_t;
typedef mpl::vector<fields::birthday, tags::no_key> birthday_field_t;

typedef mpl::vector< id_field_t
                   , name_field_t
                   , address_field_t
                   , birthday_field_t
> table;

// find all foreign keys fields
typedef mpl::filter_view<table, is_same<mpl::back<mpl::_>,
tags::foreign> >::type iter_2;
typedef fusion::result_of::as_list<iter_2>::type foreign_keys_t;

struct print
{
   template <typename T>
   void operator()( T const& vec ) const
   {
      fusion::at<mpl::int_<0> >(vec).print();
   }
};

int _tmain(int argc, _TCHAR* argv[])
{
   BOOST_STATIC_ASSERT(( is_same< mpl::deref<iter>::type, id_field_t
>::value ));

   foreign_keys_t foreign_keys;
   for_each( foreign_keys, do_something() );

   return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net