Boost logo

Boost Users :

From: Christian Henning (chhenning_at_[hidden])
Date: 2007-03-19 17:09:25


Thanks for the answer. I figured that from the error messages. So,
what would be the best to do? Singleton? Like this:

#include <vector>

#include <boost/mpl/assert.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/list.hpp>
#include <boost/mpl/transform.hpp>

#include <boost/fusion/algorithm/transformation/transform.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/sequence/adapted/mpl.hpp>
#include <boost/fusion/sequence/container.hpp>
#include <boost/fusion/sequence/conversion/as_list.hpp>

using namespace std;
using namespace boost;

struct A { void print() { cout << "A" << endl; }};
struct B { void print() { cout << "B" << endl; }};
struct C { void print() { cout << "C" << endl; }};

std::vector<A> find_all( A ) { return std::vector<A>(13); }
std::vector<B> find_all( B ) { return std::vector<B>(12); }
std::vector<C> find_all( C ) { return std::vector<C>(12); }

typedef mpl::list< A, B, C > _types_;

template <typename T>
struct add_vector
{
   typedef std::vector<T> type;
};

struct find_all_
{
   template< class T >
   void operator()( T& t ) const
   {
      typedef typename T::value_type type;
      type v;
      t = find_all( v );
   }
};

class check_size_singleton
{
public:

   static check_size_singleton& instance()
   {
      static check_size_singleton o;
      return o;
   }

   void check( size_t size )
   {
      if( !_init )
      {
         _size = size;

         _init = true;

         return;
      }

      if( _size != size )
      {
         throw runtime_error( "vectors don't match up." );
      }
   }

private:

   check_size_singleton() : _init( false ), _size( 0 ) {}
   check_size_singleton(const check_size_singleton&) {}
   check_size_singleton& operator=(const check_size_singleton&) {}
   ~check_size_singleton() {}

private:

   bool _init;
   size_t _size;
};

struct check_size
{
   template< class T >
   void operator()( T& t ) const
   {
      check_size_singleton::instance().check( t.size() );
   }
};

struct print
{
   template< class T >
   void operator()( T& vec ) const
   {
      vec[0].print();
   }
};

int main()
{
   typedef mpl::transform1< _types_, add_vector<mpl::_1> >::type
_mpl_list_of_vectors_;

   typedef fusion::result_of::as_list<_mpl_list_of_vectors_>::type
_fusion_list_of_vectors_;

   _fusion_list_of_vectors_ vectors;
   fusion::for_each( vectors, find_all_() );

   fusion::for_each( vectors, check_size() );

   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