_test_onion.cxx
Go to the documentation of this file.
1 #include "WireCellIface/IDepo.h"
3 #include "WireCellUtil/Testing.h"
4 
5 #include <tbb/flow_graph.h>
6 
7 using namespace WireCell;
8 
9 class IDepoSource : public WireCell::IComponent<IDepoSource> {
10 public:
11  virtual ~IDepoSource() {}
12  virtual bool operator()(WireCell::IDepo::pointer& out) = 0;
13 };
14 class IDepoSink : public WireCell::IComponent<IDepoSink> {
15 public:
16  virtual ~IDepoSink() {}
17  virtual bool operator()(const WireCell::IDepo::pointer& in) = 0;
18 };
19 
20 
21 class MyDepoSource : public IDepoSource {
22 public:
23  virtual ~MyDepoSource() {}
25  out = nullptr;
26  return true;
27  }
28 };
29 
30 class MyDepoSink : public IDepoSink {
31 public:
32  virtual ~MyDepoSink() {}
34  return true;
35  }
36 };
37 
38 
39 class INode : public WireCell::IComponent<INode> {
40 public:
41  virtual ~INode() {}
42 
43  // connect output of tail to our input
44  virtual bool connect_tail(INode& tail) = 0;
45 
46  // connect input of tail to our output
47  virtual bool connect_head(INode& head) = 0;
48 };
49 
50 class ITbbNode : public INode {
51 public:
52  virtual ~ITbbNode() {}
53 
54  virtual tbb::flow::graph_node* tbb_node(tbb::flow::graph& graph) = 0;
55 
56 };
57 
58 
59 // register with a NamedFactory under IDepoSource class name and instance name TBB
60 class TbbDepoSource : public ITbbNode {
62 public:
63  TbbDepoSource(IDepoSource::pointer src): m_src(src) { }
64  TbbDepoSource(const TbbDepoSource& other) : m_src(other.m_src) {}
65  virtual ~TbbDepoSource() {}
66  void operator=( const TbbDepoSource& other ) { m_src = other.m_src; }
67 
68  bool operator()(IDepo::pointer& out) { return (*m_src)(out); }
69 
70  tbb::flow::graph_node* tbb_node(tbb::flow::graph& graph) {
71  return new tbb::flow::source_node<IDepo::pointer>(graph, *this, false);
72  }
73  virtual void connect(tbb::flow::graph_node& head, tbb::flow::graph_node& tail) {
74  tbb::flow::sender<IDepo::pointer>* myhead = dynamic_cast<tbb::flow::sender<IDepo::pointer>*>(&head);
75  Assert(myhead);
76  tbb::flow::receiver<IDepo::pointer>* mytail = dynamic_cast<tbb::flow::receiver<IDepo::pointer>*>(&tail);
77  Assert(mytail);
78  make_edge(*myhead, *mytail);
79  }
80 };
81 
82 class TbbDepoSink : public ITbbNode {
84 public:
85  TbbDepoSink(IDepoSink::pointer snk): m_snk(snk) { }
86  TbbDepoSink(const TbbDepoSink& other) : m_snk(other.m_snk) {}
87  virtual ~TbbDepoSink() {}
88  void operator=( const TbbDepoSink& other ) { m_snk = other.m_snk; }
89 
90  bool operator()(const IDepo::pointer& out) { return (*m_snk)(out); }
91 
92  tbb::flow::graph_node* make_node(tbb::flow::graph& graph, int concurency = 1) {
93  return new tbb::flow::function_node<IDepo::pointer>(graph, concurency, *this);
94  }
95  virtual void connect(tbb::flow::graph_node& head, tbb::flow::graph_node& tail) {
96  tbb::flow::sender<IDepo::pointer>* myhead = dynamic_cast<tbb::flow::sender<IDepo::pointer>*>(&head);
97  Assert(myhead);
98  tbb::flow::receiver<IDepo::pointer>* mytail = dynamic_cast<tbb::flow::receiver<IDepo::pointer>*>(&tail);
99  Assert(mytail);
100  make_edge(*myhead, *mytail);
101  }
102 };
103 
104 
105 
106 
107 int main()
108 {
109  // emulate named factory
111  IDepoSink::pointer dsnk(new MyDepoSink);
112 
114 
115  // emulate lookup of tbb wrapper
116  ITbbNode::pointer tbbsrc(new TbbDepoSource(dsrc));
117  ITbbNode::pointer tbbsnk(new TbbDepoSink(dsnk));
118 
119  tbb::flow::graph_node* gnsrc = tbbsrc->make_node(graph);
120  tbb::flow::graph_node* gnsnk = tbbsnk->make_node(graph);
121  tbbsrc->connect(*gnsrc, *gnsnk);
122 
123  graph.wait_for_all();
124 
125 
126  return 0;
127 }
TbbDepoSink(IDepoSink::pointer snk)
Definition: _test_onion.cxx:85
std::shared_ptr< const IDepo > pointer
Definition: IData.h:19
virtual void connect(tbb::flow::graph_node &head, tbb::flow::graph_node &tail)
Definition: _test_onion.cxx:73
virtual void connect(tbb::flow::graph_node &head, tbb::flow::graph_node &tail)
Definition: _test_onion.cxx:95
virtual ~TbbDepoSink()
Definition: _test_onion.cxx:87
virtual bool operator()(boost::any &anyout)
Definition: ISourceNode.h:51
def graph(desc, maker=maker)
Definition: apa.py:294
bool operator()(const WireCell::IDepo::pointer &in)
The calling signature:
Definition: _test_onion.cxx:33
bool operator()(WireCell::IDepo::pointer &out)
The calling signature:
Definition: _test_onion.cxx:24
IDepoSink::pointer m_snk
Definition: _test_onion.cxx:83
void operator=(const TbbDepoSink &other)
Definition: _test_onion.cxx:88
virtual ~MyDepoSource()
Definition: _test_onion.cxx:23
int main()
bool operator()(const IDepo::pointer &out)
Definition: _test_onion.cxx:90
virtual ~MyDepoSink()
Definition: _test_onion.cxx:32
#define Assert
Definition: Testing.h:7
TbbDepoSource(const TbbDepoSource &other)
Definition: _test_onion.cxx:64
virtual ~TbbDepoSource()
Definition: _test_onion.cxx:65
std::shared_ptr< Interface > pointer
Definition: Interface.h:16
void operator=(const TbbDepoSource &other)
Definition: _test_onion.cxx:66
virtual ~ITbbNode()
Definition: _test_onion.cxx:52
virtual ~IDepoSink()
Definition: _test_onion.cxx:16
Definition: Main.h:22
TbbDepoSource(IDepoSource::pointer src)
Definition: _test_onion.cxx:63
tbb::flow::graph_node * tbb_node(tbb::flow::graph &graph)
Definition: _test_onion.cxx:70
TbbDepoSink(const TbbDepoSink &other)
Definition: _test_onion.cxx:86
virtual ~IDepoSource()
Definition: _test_onion.cxx:11
virtual ~INode()
Definition: _test_onion.cxx:41
tbb::flow::graph_node * make_node(tbb::flow::graph &graph, int concurency=1)
Definition: _test_onion.cxx:92
IDepoSource::pointer m_src
Definition: _test_onion.cxx:61
bool operator()(IDepo::pointer &out)
Definition: _test_onion.cxx:68