Re: Multimethods idioms and library support
This is a multi-part message in MIME format.
--------------030907090809010300010606
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
On 02/22/11 16:08, Larry Evans wrote:
[snip]
Let me see if I can implement this with reifier_visitor.
I'll get back to you.
After some corrections to:
replace_source_with_target_ptr.hpp
got the attached to run and print:
Lion jumps on Girrafe and bites its ass.
However, when the #if on line 188 is switch, I get
very obscure compile-time error messages. So, it
can't handle concrete references. It apparently
can only handle abstract references.
I'll see if I can correct that.
-Larry
--------------030907090809010300010606
Content-Type: text/x-c++src;
name="predator_prey.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="predator_prey.cpp"
//Purpose:
// See if code from the following post works.
/*
From: itaj sherman <itajsherman@gmail.com>
Newsgroups: comp.lang.c++
Subject: Re: Multimethods idioms and library support
Date: Tue, 22 Feb 2011 13:23:05 -0800 (PST)
Organization: http://groups.google.com
Lines: 105
Message-ID: <3f848282-d123-408d-8d25-de12b0f2eca7@u6g2000vbh.googlegroups.com>
*/
//
#include <boost/composite_storage/pack/multiple_dispatch/reify_apply.hpp>
#include <boost/composite_storage/pack/multiple_dispatch/reifier_visitor.hpp>
#include <iostream>
namespace boost
{
namespace composite_storage
{
namespace pack
{
namespace multiple_dispatch
{
namespace testing
{
typedef void ResultType;
//Predators:
struct Lion;
struct Anaconda;
struct Bear;
struct Predator_abstract;
}//exit testing namespace
template<>
struct hosts_concrete
< testing::Predator_abstract
>
: mpl::package
< testing::Lion
, testing::Bear
, testing::Anaconda
>
{
};
namespace testing
{
struct Predator_abstract
{
typedef reifier_visit_abstract_seq< ResultType
, hosts_concrete<Predator_abstract>::type>
visitor_abstract;
virtual ResultType accept( visitor_abstract const&)const=0;
};
struct Lion: public Predator_abstract
{
ResultType accept( visitor_abstract const& a_visitor)const
{
return a_visitor.visit(*this);
}
};
struct Anaconda: public Predator_abstract
{
ResultType accept( visitor_abstract const& a_visitor)const
{
return a_visitor.visit(*this);
}
};
struct Bear: public Predator_abstract
{
ResultType accept( visitor_abstract const& a_visitor)const
{
return a_visitor.visit(*this);
}
};
//Preys:
struct Gazelle;
struct Girrafe;
struct Prey_abstract;
}//exit namespace testing
template<>
struct hosts_concrete
< testing::Prey_abstract
>
: mpl::package
< testing::Gazelle
, testing::Girrafe
>
{
};
namespace testing
{
struct Prey_abstract
{
typedef reifier_visit_abstract_seq< ResultType
, hosts_concrete<Prey_abstract>::type>
visitor_abstract;
virtual ResultType accept( visitor_abstract const&)const=0;
};
struct Gazelle: public Prey_abstract
{
ResultType accept( visitor_abstract const& a_visitor)const
{
return a_visitor.visit(*this);
}
};
struct Girrafe: public Prey_abstract
{
ResultType accept( visitor_abstract const& a_visitor)const
{
return a_visitor.visit(*this);
}
};
template<typename Animal>
struct animal_name
;
template<>
struct animal_name<Gazelle>
{
static std::string _(){ return "Gazelle";}
};
template<>
struct animal_name<Girrafe>
{
static std::string _(){ return "Girrafe";}
};
struct hunt_functor
{
typedef ResultType result_type;
void operator()( Lion const&, Gazelle const& )
{
std::cout<<"Lion jumps on Gazelle and bites its neck.\n";
}
void operator()( Lion const&, Girrafe const& )
{
std::cout<<"Lion jumps on Girrafe and bites its ass.\n";
}
void operator()( Anaconda const&, Gazelle const& )
{
std::cout<<"Anaconda injects venom into Gazelle.\n";
}
void operator()( Anaconda const&, Girrafe const& )
{
std::cout<<"Anaconda ignores Girraffe.\n";
}
//Anaconda can't kill girrafes so no override for that one
template<typename PreyConcrete>
void operator()( Bear const&, PreyConcrete const& )
{
std::cout<<"Bear mauls "<<animal_name<PreyConcrete>::_()<<"\n";
}
};
void test()
{
hunt_functor hunter;
Lion lion;
Girrafe girrafe;
#if 1
Predator_abstract&pred1=lion;
Prey_abstract&prey1=girrafe;
reify_apply<reifier_visitor>(hunter, pred1, prey1);
#else
reify_apply<reifier_visitor>(hunter, lion, girrafe);
#endif
}
}//exit testing namespace
}//exit multiple_dispatch namespace
}//exit pack namespace
}//exit composite_storage namespace
}//exit boost namespace
int main(void)
{
boost::composite_storage::pack::multiple_dispatch::testing::test();
return 0;
}
--------------030907090809010300010606--