2015-02-04 3 views
4

type 인수를 함수에 전달하여 create_eclass* 함수에 클래스 형식 인수를 전달하여 한 번만 쓸 수 있습니까?"type"인수를 함수에 전달

class bclass; 

virtual function void print(); 
    $display("Base Class"); 
endfunction 

endclass 

class eclass1 extends bclass; 

    function void print(); 
     $display("Extended Class1"); 
    endfunction 

endclass 

class eclass2 extends bclass; 

    function void print(); 
     $display("Extended Class2"); 
    endfunction 
endclass 

program Test ; 
    bclass q[$]; 

    function create_eclass1(); 
     bclass  b; 
     eclass1 e; 
     e=new(); 
     $cast(b,e); 
     q.push_back(e); 
    endfunction 

    function create_eclass2(); 
     bclass  b; 
     eclass2 e; 
     e=new(); 
     $cast(b,e); 
     q.push_back(e); 
    endfunction 

    initial 
    begin 
     create_eclass1(); 
     create_eclass2(); 
     foreach(q[i]) q[i].print(); 
    end 
endprogram 

답변

0

내 동료가이 솔루션을 Dave의 제안과 비슷하다고 제안했습니다.

virtual class eclass_creator #(type T = bclass); 
    static function T create(int k) ; 
    create = new(k) ; 
    endfunction 
endclass 

이렇게하면 범위가 지정된 생성자를 만들 수 있습니다.

class bclass; 
    int i; 
    function new(int k); 
     i=k;  
    endfunction 
    virtual function void print(); 
     $display("Base Class %0d",i); 
    endfunction 
endclass 

class eclass1 extends bclass; 
    function new(int k); 
     super.new(k);  
    endfunction 
    function void print(); 
     $display("Extended Class1 %0d",i); 
    endfunction 
endclass 

class eclass2 extends bclass; 
    function new(int k); 
     super.new(k);  
    endfunction 
    function void print(); 
     $display("Extended Class2 %0d",i); 
    endfunction 
endclass 

program Test ; 
    bclass q[$]; 

    function void push(bclass inclass); 
     q.push_back(inclass); 
    endfunction 

    initial 
    begin 
     push(eclass_creator #(eclass1)::create(5)); 
     push(eclass_creator #(eclass2)::create(10)); 
     foreach(q[i]) q[i].print(); 
    end 
endprogram 
4

예. 만들려는 유형의 프록시 역할을하는 개체를 만들어이 작업을 수행 할 수 있습니다. 이 코드 패턴은 UVM 공장에서 사용됩니다.

typedef bclass; // this would be uvm_object in the UVM 

interface class object_wrapper; // like a virtual class except it only contains pure virtual methods 
    pure virtual function bclass create; 
endclass 

class object_registry#(type T) implements object_wrapper; 
    typedef object_registry#(T) this_type; 
    local static this_type _singleton; // only one object for each class type 
    local function new; 
    endfunction 
    static function object_wrapper get; 
     if (_singleton == null) _singleton = new; 
     return _singleton; 
    endfunction // if 
    virtual function T create; 
     create = new; 
    endfunction 
endclass 

나머지 코드는 대부분 원래 예제의 코드와 동일합니다. 방금 typedef를 추가하여 클래스를 등록했습니다.이 typedef는 object_registry의 정적 변수와 메소드가 존재하도록합니다.

class bclass; 

    virtual function void print(); 
     $display("Base Class"); 
    endfunction 

endclass 

class eclass1 extends bclass; 
    typedef object_registry#(eclass1) type_id; 

    function void print(); 
     $display("Extended Class1"); 
    endfunction 

endclass 

class eclass2 extends bclass; 
    typedef object_registry#(eclass2) type_id; 
    function void print(); 
     $display("Extended Class2"); 
    endfunction 
endclass 

module Test ; 
    bclass q[$]; 

    function void create_eclass(object_wrapper h); 
     q.push_back(h.create()); 
    endfunction 

    object_wrapper a1,a2; 

    initial 
    begin 
     create_eclass(eclass1::type_id::get()); 
     create_eclass(eclass2::type_id::get()); 
    // or another way - 
    a1 = eclass1::type_id::get(); 
    a2 = eclass2::type_id::get(); 
     create_eclass(a1); 
     create_eclass(a2); 
     create_eclass(a2); 
     create_eclass(a1); 

     foreach(q[i]) q[i].print(); 
    end 
endmodule 

이 공장 패턴 코드에 대해 자세히 설명하는 a paper이 있습니다.

+0

예. 당신이 태그를 붙 였기 때문에 나는 UVM 연결 만 넣었습니다. –