|
/////////////////////////////////
// msgrouter.hpp.hpp
// generic message routing
//
#pragma once
#include "graphs.hpp"
#pragma message ("including " __FILE__)
//define a model for routing of messages into a graph
namespace GE_ { namespace stdx { namespace graph {
template<class Data, class Node, class Fn> //void Fn(Arc*, Data&): can be mem_fun(void(Arc::*)(Data&))
void route_arcs(Data& d, Node& node, Fn fn)
{
for(typename Node::arc_iterator i = node.begin_arcto(); i!=node.end_arcto(); i++)
Fn(i, d);
}
template<class Data, class Node, class Fn> //void Fn(Node*, Data&): can be mem_fun(void(Node::*)(Data&))
void route_linked(Data& d, Node& node, Fn fn)
{
for(typename Node::arc_iterator i = node.begin_arcto(); i!=node.end_arcto(); i++)
{
Node* p(i->get_to());
if(p) fn(p, d);
}
}
template<class Data, class Node, class Fn> //void Fn(Arc*, Data&): can be mem_fun(void(Arc::*)(Data&))
void backroute_arcs(Data& d, Node& node, Fn fn)
{
for(typename Node::arc_iterator i = node.begin_arcfrom(); i!=node.end_arcfrom(); i++)
Fn(i, d);
}
template<class Data, class Node, class Fn> //void Fn(Node*, Data&): can be mem_fun(void(Node::*)(Data&))
void backroute_linked(Data& d, Node& node, Fn fn)
{
for(typename Node::arc_iterator i = node.begin_arcfrom(); i!=node.end_arcfrom(); i++)
{
Node* p(i->get_to());
if(p) fn(p, d);
}
}
////////////////////////////////////////////
// structured data routing
template<class Derived, class Node>
class link:
public arc<Derived, Node>
{
private:
int metric;
public:
int get_metric() const { return metric; }
int set_metric(int metric_) { metric = metric_; }
link(int metric_=1) :metric(metric_) {}
};
//RTTI data receiver
template<class Data>
class receiver
{
public:
virtual bool on_rcv(Data& d)
{ return d.do_default(); }
};
// data router
template<class Derived, class Link>
class router:
public node<Derived, Link>
{
public:
template<class data>
bool on_data(data& M) //Derived can override / specialize
{
receiver<data>* pRcv = dynamic_cast<receiver<data>*>(this);
return (pRcv)? pRcv->on_rcv(M): false;
}
template<class data>
bool route_data(data& M)
{
routabledata<Derived, data>::routinginfo ri; //routing info
if(M.pRoutes) M.pRoutes->set_prev(&ri); //push this RI
M.pRoutes = &ri;
ri.i_arc = begin_arcto(); //store iterators
ri.i_end = end_arcto();
return M.do_default(); //route
}
};
template<class Node, class Derived>
struct routabledata
{
friend class router;
private:
struct routinginfo: //routing information stack
public stdx::chain_node<routinginfo>
{
typename Node::arc_iterator i_arc, i_end;
~routinginfo() { on_lastrelease(); }
};
typename routinginfo::wptr pRoutes; //the stack head
int metric;
protected:
routabledata() { pRoutes=0; metric=0; }
public:
int get_metric() const { return metric; }
bool do_default() //default message routing
{
routinginfo* pRi = pRoutes;
while(pRi) //walk the routing information
{
typename Node::arc_iterator i = pRi->i_arc; //walk the router's outgoing links
while(i!=pRi->i_end)
{
++(pRi->i_arc); //ready for the next
if(pRi->i_arc == pRi->i_end)
pRoutes = pRoutes->get_next(); //redy for the next
stdx::temp_assign<int> t(metric, metric+i->get_metric()); //store the new metric for this loop
if(i->get_to()->on_data(*static_cast<Derived*>(this))) //pass the message to the linked node
return true; //return true if handled
i = pRi->i_arc; //next arc
}
pRi = pRoutes; //next router
}
return false; //unhandled (no more routers)
}
};
}}}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Born and living in Milan (Italy), I'm an engineer in electronics actually working in the ICT department of an important oil/gas & energy company as responsible for planning and engineering of ICT infrastructures.
Interested in programming since the '70s, today I still define architectures for the ICT, deploying dedicated specific client application for engineering purposes, working with C++, MFC, STL, and recently also C# and D.