Friday, March 02, 2012

Objects

Objects

A class defines a data type. An object is an instance of that class. An object is used by first declaring a variable of that class type (that holds an object handle) and then creating an object of that class (using the new function) and assigning it to the variable.

Packet p; // declare a variable of class Packet
p = new; // initialize variable to a new allocated object of the class Packet

The variable p is said to hold an object handle to an object of class Packet. Uninitialized object handles are set by default to the special value null. An uninitialized object can be detected by comparing its handle with null.

Object properties:

The data fields of an object can be used by qualifying class property names with an instance name. Using the earlier example, the commands for the Packet object p can be used as follows:

Packet p = new;
p.command = INIT;
p.address = $random;
packet_time = p.time_requested;

Object Methods:

An object’s methods can be accessed using the same syntax used to access class properties:

Packet p = new;
status = p.current_status();
Note that the assignment to status is not:
status = current_status(p);
Packet p = new;
status = p.current_status();

this:

The this keyword is used to unambiguously refer to class properties or methods of the current instance.

class sonet_framing_octet;
logic [7:0] A1;
logic [7:0] A2;
function new (logic [7:0] A1, logic [7:0] a2_value);
this.A1 = A1;
A2 = a2_value;
endfunction : new
endclass : sonet_framing_octet

program this_example;
sonet_framing_octet frame;
initial begin
frame = new(8'hf6, 8’h28);
$display("A1 = %0h\t\tA2 = %0h\n", frame.A1, frame.A2);
end
endprogram : this_example


OUTPUT:

A1 = f6 A2 = 28

Assignment, re-naming and copying:

Declaring a class variable only creates the name by which the object is known. Thus: Packet p1; creates a variable, p1, that can hold the handle of an object of class Packet, but the initial value of p1 is null.

The object does not exist, and p1 does not contain an actual handle, until an instance of type Packet is created:

p1 = new;

Thus, if another variable is declared and assigned the old handle, p1, to the new one, as in:

Packet p2;
p2 = p1;

then there is still only one object, which can be referred to with either the name p1 or p2.

Note, new was executed only once, so only one object has been created. If, however, the example above is re-written as shown below, a copy of p1 shall be made:

Packet p1;
Packet p2;
p1 = new;
p2 = new p1;

Inheritance and subclasses

The previous sections defined a class called Packet. This class can be extended so that the packets can be chained together into a list. One solution would be to create a new class called LinkedPacket that contains a variable of type Packet called packet_c.

To refer to a class property of Packet, the variable packet_c needs to be referenced.

class LinkedPacket;
Packet packet_c;
LinkedPacket next;
function LinkedPacket get_next();
get_next = next;
endfunction
endclass

Since LinkedPacket is a specialization of Packet, a more elegant solution is to extend the class creating a new subclass that inherits the members of the parent class. Thus, for example:

class LinkedPacket extends Packet;
LinkedPacket next;
function LinkedPacket get_next();
get_next = next;
endfunction
endclass


No comments:

Post a Comment

Popular Posts