Hi,
The following I quote from the white paper ‘Using parameterized classes and factories: The yin and yang of object-oriented verification’.
virtual class Object;
pure virtual function Object create();
endclass
class Factory;
Object obj;
virtual function Object create();
// delegate creation to subtype itself
create = obj.create();
endfunction
endclass
class ApbTrans extends Object;
typedef enum {WRITE,READ} cmd_e;
rand int unsigned addr;
rand int unsigned data;
rand cmd_e cmd;
virtual function Object create();
ApbTrans trans = new();
return trans;
endfunction
endclass
class ApbWriteTrans extends ApbTrans;
constraint write_only { cmd == WRITE; }
virtual function Object create();
ApbTrans trans = new();
return trans;
endfunction
endclass
class ApbReadTrans extends ApbTrans;
constraint read_only { cmd == READ; }
virtual function Object create();
ApbTrans trans = new();
return trans;
endfunction
endclass
class ApbDriver;
task run();
forever begin
ApbTrans trans;
$cast(trans,factory.create());
trans.randomize();
... execute transaction
end
endtask
endclass
initial begin
// create driver
ApbDriver driver = new;
// initialize factory to produce ApbTrans
ApbTrans trans = new; // reads/writes
ApbWriteTrans w_trans = new; // writes only
ApbReadTrans r_trans = new; // reads only
// start the driver
fork driver.run(); join_none
// plug in different objects to produce
// over time. Driver unaware, and unmodified
factory.obj = trans;
#1000;
factory.obj = w_trans;
#1000;
factory.obj = r_trans;
#1000;
$finish;
end
I assume in the final initial block, when we override factory.obj with trans, w_trans and r_transe, we expect driver.run() to generate ApbTrans, ApbWriteTrans and ApbReadTrans objects respectively. So why is it that the create() vertual function in all three classes of ApbTrans , ApbWriteTrans and ApbReadTrans, constructs the an ApbTrans object? If I understand correctly, the object in the Factory is supposed to act as a blueprint, so when we override it with different extended objects, we expect the create() virtual function to construct an object of the overriding type. If this is correct, then the create() method in each extended class should construct an object of that very class.
I am specifically confused about the following line which is repeated in all three extended classes ApbTrans, ApbWriteTrans and ApbReadTrans.
ApbTrans trans = new();
According to my logic, the above line should be modified in ApbWriteTrans as follows:
ApbWriteTrans trans = new();
It should also be modified in ApbReadTrans as follows:
ApbReadTrans trans = new();
I guess I’m missing an important point here. I would really appreciate some guidance.
Thank you.