PolymorphicTransactionUsingMultipleInheritance
Polymorphic Transaction Using Multiple Inheritance
TLM 2.0 presents extension abilities using a vector of extensions inside the request structure. Another idea is to make the struct that is passed through the channel be polymorphic and use multiple inheritance to agregate each extension. When doing the communication, this big struct with extensions is downcasted to a simpler generic struct. Everyone know the generic struct, and can work with it. But the slave can try to cast the struct to get some known extension from it.
Some code to show this idea is bellow. Note that the target function representing the slave can use the default transaction struct directly, without any added cost. But some tests can be done to see if it "hides" some known extensions, in this case there is a cost.
- #include <iostream>
- using namespace std;
- // a generic transaction
- class A {
- public:
- int a;
- virtual ~A() {};
- };
- // extension 1
- class B {
- public:
- int b;
- virtual ~B() {};
- };
- // extension 2
- class C {
- public:
- int c;
- virtual ~C() {};
- };
- // class used by initiator (the BIG transaction)
- class D: public B, public A, public C {
- public:
- int d;
- virtual ~D() {};
- };
- // This function represents a slave that is compatible with a generic transaction
- void target(A *AA)
- {
- // use generic fields directly
- cout << AA->a << endl;
- // try to find an extension (works!)
- C *CC = dynamic_cast<C *>(AA);
- if (CC) {
- cout << "cast C ok. member c is " << CC->c << endl;
- }
- // try to cast to the BIG transaction (works too!)
- D *DD = dynamic_cast<D *>(AA);
- if (DD) {
- cout << "cast D ok. member d is " << DD->d << endl;
- }
- }
- int main() {
- cout << hex;
- // Fill up a BIG transaction
- D *DD = new D();
- DD->a = 0xa;
- DD->b = 0xb;
- DD->c = 0xc;
- DD->d = 0xd;
- // Send to slave (the BIG transaction is automatically casted to the base generic transaction)
- target(DD);
- }
Posted January 8th, 2008 by MarkBurton