Type Parameterised Class

At the time, when we write down a class definition, we must be able to say that this class should define a generic type. However, if we don't know with which types the class will be used. Consequently, we must be able to define the class with help of a placeholder to which we refer as if it is the type on which the class operates. Thus, the class definition provides us with a template of an actual class. The actual class definition is created once we declare a particular object. Let's illustrate this with the following example. Suppose, you want to define a list class which should be a generic type. Thus, it should be possible to declare list objects for integers, bits,objects or any other type.

class List #(type T = int);
T data_node;
// methods:
task append(T element);
function T getFirst();
function T getNext();

The above template class List looks like any other class definition. However, the first line declares List to be a template for various types. The identifier T is used as a placeholder for an actual type. For example, append() takes one element as an argument. The type of this element will be the data type with which an actual list object is created. For example, we can declare a list object for "packet" if a definition fot the type "packet" exists:

List#(packet) pl; // Object pl is a list of packet
List#(string) sl; // Object sl is a list of strings

The first line declares List#(packet) to be a list for packets. At this time, the compiler uses the template definition, substitutes every occurrence of T with "packet" and creates an actual class definition for it. This leads to a class definition similar to the one that follows:

class List {
packet data_node;
// methods:
task append(packet element);
function packet getFirst();
function packet getNext();

This is not exactly, what the compiler generates. The compiler must ensure that we can create multiple lists for different types at any time. For example, if we need another list for, say "strings", we can write:

List#(packet) pl; // Object pl is a list of packet
List#(string) sl; // Object sl is a list of strings

In both cases the compiler generates an actual class definition. The reason why both do not conflict by their name is that the compiler generates unique names. However, since this is not viewable to us, we don't go in more detail here. In any case, if you declare just another list of "strings", the compiler can figure out if there already is an actual class definition and use it or if it has to be created. Thus,

List#(packet) rcv_pkt; // Object rcv_pkt is a list of packet
List#(packet) sent_pkt; // Object sent_pkt is a list of packet

will create the actual class definition for packet List and will reuse it for another List. Consequently, both are of the same type. We summarize this in the following definition:

Definition (Parameterized Class) If a class A is parameterized with a data type B, A is called template class. Once an object of A is created, B is replaced by an actual data type. This allows the definition of an actual class based on the template specified for A and the actual data type.

We are able to define template classes with more than one parameter. For example, directories are collections of objects where each object can be referenced by a key. Of course, a directory should be able to store any type of object. But there are also various possibilities for keys. For instance, they might be strings or numbers. Consequently, we would define a template class Directory which is based on two type parameters, one for the key and one for the stored objects.

Value Parameterised Class

The normal Verilog parameter mechanism is also used to parameterize a class.

class Base#(int size = 3);
bit [size:0] a;

task disp();
$display(" Size of the vector a is %d ",$size(a));

program main();

Base B1;
Base#(4) B2;
Base#(5) B3;
B1 = new();
B2 = new();
B3 = new();


Size of the vector a is 4
Size of the vector a is 5
Size of the vector a is 6

Instances of this class can then be instantiated like modules or interfaces:

vector #(10) vten; // object with vector of size 10
vector #(.size(2)) vtwo; // object with vector of size 2
typedef vector#(4) Vfour; // Class with vector of size 4

Generic Parameterised Class

A specialization is the combination of a specific generic class with a unique set of parameters. Two sets of parameters shall be unique unless all parameters are the same as defined by the following rules:
a) A parameter is a type parameter and the two types are matching types.
b) A parameter is a value parameter and both their type and their value are the same.

All matching specializations of a particular generic class shall represent the same type. The set of matching specializations of a generic class is defined by the context of the class declaration. Because generic classes in a package are visible throughout the system, all matching specializations of a package generic class are the same type. In other contexts, such as modules or programs, each instance of the scope containing the generic class declaration creates a unique generic class, thus, defining a new set of matching specializations.

A generic class is not a type; only a concrete specialization represents a type. In the example above, the class vector becomes a concrete type only when it has had parameters applied to it, for example:
typedef vector my_vector; // use default size of 1

vector#(6) vx; // use size 6

To avoid having to repeat the specialization either in the declaration or to create parameters of that type, a typedef should be used:

typedef vector#(4) Vfour;
typedef stack#(Vfour) Stack4;
Stack4 s1, s2; // declare objects of type Stack4

Extending Parameterised Class

A parameterized class can extend another parameterized class. For example:

class C #(type T = bit);
endclass // base class

class D1 #(type P = real) extends C; // T is bit (the default)

Class D1 extends the base class C using the base class¿s default type (bit) parameter.

class D2 #(type P = real) extends C #(integer); // T is integer

Class D2 extends the base class C using an integer parameter.

class D3 #(type P = real) extends C #(P); // T is P

Class D3 extends the base class C using the parameterized type (P) with which the extended class is parameterized.

Abstract Classes
Parameterised Class
Nested Classes
Scope Resolution Operator
External Declaration
Classes And Structures
Typedef Class
Other Oops Features

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!




copyright © 2007-2017 :: all rights reserved www.testbench.in::Disclaimer