使用 uvm_factory 获取派生 class 成员的句柄

Getting a handle to a derived class member using uvm_factory

如果我有一个基 class 和 3 个派生 class es,有没有一种方法可以使用基 class 类型的指针访问派生 class 变量?

示例:(去掉无关代码——构造函数等)

class Base extends uvm_object;
  bit[7:0] data;
  virtual function void set_address(bit[7:0 a);
    // do nothing
  endfunction
endclass

class DerivedA extends base;
  bit[7:0] address;
  virtual function void set_address(bit[7:0 a);
    address = a
  endfunction
endclass

class DerivedB extends base;
  bit[7:0] address;
  virtual function void set_address(bit[7:0 a);
    address = a
  endfunction
endclass

class DerivedC extends base;
  bit[7:0] address;
  virtual function void set_address(bit[7:0 a);
    address = a
  endfunction
endclass

现在,假设我从 uvm_test 中按类型将基重写为 DerivedA。我有一个计分板,虽然对所有类型都是通用的 - Base、DerivedA、DerivedB 和 DerivedC。

class Scb extends uvm_scoreboard;

  Base item = Base::type_id::create("m_item"); <------- item will be of type derivedA
  item.address = 'hAA;    <------ illegal
  item.set_address('hAA); <------ legal

endclass

一切都很好,但实际上,我有多个成员变量,不想为所有成员变量创建访问函数。我正在尝试类似下面的操作,但不知道该怎么做。

class Scb extends uvm_scoreboard;

  string s;
  uvm_factory f = uvm_factory::get();
  Base item = Base::type_id::create("m_item"); <------- item will be of type derivedA
  s = item.get_type_name() <----- DerivedA
  $cast(type_name_handle, f.create_object_by_type_name(s)); <----- this is what I want - to create a handle of derived type so I can then access the Derived variables.

endclass

这一切似乎都是不必要的,但这是由于我无法控制的因素的组合 - 即我需要上面的所有 4 classes 并且希望避免使用基于 [= 的 case 语句30=](有很多派生的class)。

这里有什么解决办法吗?到目前为止,我开始认为这是不可能的。

鉴于所提出的要求,您不可能做您想做的事。如果 address 打算出现在每个派生的 class 中,为什么不把它放在一个公共基础 class 中,要么是你的 Base,要么是一些中间基础 [=19] =] 位于您的 BaseDerivedX class 之间?这就是继承的意义。

您必须使用访问器函数,或根据类型名称或 $casting 的结果有条件地分支到每个 class 类型。