-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory_pkg.sv
More file actions
74 lines (61 loc) · 1.94 KB
/
factory_pkg.sv
File metadata and controls
74 lines (61 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package factory_pkg;
virtual class component;
pure virtual task run_test();
endclass // component
virtual class wrapper;
pure virtual function string get_type_name();
pure virtual function component create_object(string name);
endclass // wrapper
class factory;
static wrapper type_names[string];
static factory inst;
static function factory get();
if (inst == null) inst = new();
return inst;
endfunction // get
static function register(wrapper c);
type_names[c.get_type_name()] = c;
endfunction // register
static function component get_test();
string name;
wrapper test_wrapper;
component test_component;
if( !$value$plusargs("TESTNAME=%s",name))begin
$display("FATAL +TESTNAME not found");
$finish;
end
$display("%m found TESTNAME=%s",name);
test_wrapper = factory::type_names[name];
$cast(test_component, test_wrapper.create_object(name));
return test_component;
endfunction // get_test
static function void printFactory();
$display("Factory:");
foreach (type_names[s]) $display("The name: %s maps to the class: %p", s, type_names[s]);
endfunction // printFactory
endclass // factory
class registry #(type T, string Tname) extends wrapper;
typedef registry #(T,Tname) this_type;
local static this_type me = get();
static function this_type get();
if(me == null) begin
factory f = factory::get();
me = new();
void'(f.register(me));
end
return me;
endfunction // get
virtual function string get_type_name();
return Tname;
endfunction // get_type_name
virtual function component create_object(string name);
T toReturn;
$display("Create_object called with %s which maps to key %s and class %s",
name,
Tname,
$typename(T));
toReturn = new();
return toReturn;
endfunction // create_object
endclass
endpackage // factory_pkg