Classes | Functions | Variables
test_typeid.cxx File Reference
#include "WireCellUtil/Testing.h"
#include "WireCellUtil/Interface.h"
#include <iostream>
#include <typeinfo>
#include <map>
#include <string>

Go to the source code of this file.

Classes

struct  IPort
 
struct  Port< T >
 
struct  SubI
 
struct  SubF
 
struct  SubFIn
 
struct  SubFOut
 
struct  CallTransfer
 
struct  CallTransferT< T >
 

Functions

template<typename A , typename B >
bool transfer (const A &a, B &b)
 
template<typename T >
void register_caller ()
 
CallTransferget_caller (const IPort &comp)
 
int main ()
 

Variables

static map< string, CallTransfer * > callers
 

Function Documentation

CallTransfer* get_caller ( const IPort comp)

Definition at line 144 of file test_typeid.cxx.

145 {
146  const IPort* port = dynamic_cast<const IPort*>(&comp);
147  if (!port) { return nullptr; }
148  return callers[port->port_type_name()];
149 }
virtual std::string port_type_name() const =0
comp
Definition: dbjson.py:31
static map< string, CallTransfer * > callers
int main ( void  )

Definition at line 152 of file test_typeid.cxx.

153 {
154 
155  IPort* si = new SubI;
156  IPort* sf = new SubF;
157  IPort* sfi = new SubFIn;
158  IPort* sfo = new SubFOut;
159 
160  cout << "typeid(si) = " << typeid(si).name() << endl;
161  cout << "typeid(*si) = " << typeid(*si).name() << endl;
162  cout << "typeid(sf) = " << typeid(sf).name() << endl;
163  cout << "typeid(*sf) = " << typeid(*sf).name() << endl;
164  cout << "typeid(sfo) = " << typeid(sfo).name() << endl;
165  cout << "typeid(*sfo) = " << typeid(*sfo).name() << endl;
166  cout << "typeid(sfi) = " << typeid(sfi).name() << endl;
167  cout << "typeid(*sfi) = " << typeid(*sfi).name() << endl;
168 
169  cout << "typeid(SubI::port_type) = " << typeid(SubI::port_type).name() << endl;
170  cout << "typeid(SubF::port_type) = " << typeid(SubF::port_type).name() << endl;
171  cout << "typeid(SubFOut::port_type) = " << typeid(SubFOut::port_type).name() << endl;
172  cout << "typeid(SubFIn::port_type) = " << typeid(SubFIn::port_type).name() << endl;
173 
174  cout << "si->type_name() = " << si->port_type_name() << endl;
175  cout << "sf->type_name() = " << sf->port_type_name() << endl;
176  cout << "sfo->type_name() = " << sfo->port_type_name() << endl;
177  cout << "sfi->type_name() = " << sfi->port_type_name() << endl;
178 
179  // given just Port*, how to know what to dynamic_cast to???
180  Port<float>* bfi = dynamic_cast<Port<float>*>(sfi);
181  Port<float>* bfo = dynamic_cast<Port<float>*>(sfo);
182  Assert(bfi);
183  Assert(bfo);
184  Assert(transfer(*bfo, *bfi));
185 
186  // okay, like this:
187  register_caller<int>();
188  register_caller<float>();
189 
190  IPort* ci = new SubFIn;
191  IPort* co = new SubFOut;
192 
193 
194 
195  CallTransfer* ct = get_caller(*ci);
196  Assert(ct);
197  Assert(ct->call(*co, *ci));
198 
199  return 0;
200 }
static QCString name
Definition: declinfo.cpp:673
virtual bool call(const IPort &a, IPort &b)=0
bool transfer(const A &a, B &b)
Definition: test_typeid.cxx:91
virtual std::string port_type_name() const =0
#define Assert
Definition: Testing.h:7
CallTransfer * get_caller(const IPort &comp)
QTextStream & endl(QTextStream &s)
template<typename T >
void register_caller ( )

Definition at line 138 of file test_typeid.cxx.

139 {
141  callers[ct->port_type_name()] = ct;
142 }
std::string port_type_name() const
static map< string, CallTransfer * > callers
template<typename A , typename B >
bool transfer ( const A a,
B b 
)

Definition at line 91 of file test_typeid.cxx.

92 {
93  cerr << "transfer: "
94  << typeid(A).name() << "<->" << typeid(a).name() << " "
95  << typeid(B).name() << "<->" << typeid(b).name() << endl;
96 
97 
98  if (a.port_type_name() != b.port_type_name()) {
99  cerr << "Port type mismatch: "
100  << a.port_type_name() << " != " << b.port_type_name() << endl;
101  return false;
102  }
103  typename A::port_type dat;
104  if (!a.get(dat)) {
105  cerr << "Failed to get output of type " << a.port_type_name() << endl;
106  return false;
107  }
108  if (!b.put(dat)) {
109  cerr << "Failed to put input of type " << b.port_type_name() << endl;
110  return false;
111  }
112  return true;
113 }
static QCString name
Definition: declinfo.cpp:673
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
#define A
Definition: memgrp.cpp:38
static bool * b
Definition: config.cpp:1043
QTextStream & endl(QTextStream &s)

Variable Documentation

map<string, CallTransfer*> callers
static

Definition at line 135 of file test_typeid.cxx.