Karan Shah
Karan Shah

Reputation: 1992

What is the meaning of an object of the class inside it's class-endclass definition?

What is the meaning of the following code (2nd line) in which inside class uvm_resource_pool definition, instance (object) rp is created?

class uvm_resource_pool;

  static local uvm_resource_pool rp = get();

  // Function: get
  //
  // Returns the singleton handle to the resource pool

  static function uvm_resource_pool get();
    if(rp == null)
      rp = new();
    return rp;
  endfunction

Upvotes: 2

Views: 142

Answers (1)

dave_59
dave_59

Reputation: 42748

This is how the singleton pattern gets coded in SystemVerilog. The singleton pattern is an OOP technique that makes sure only one instance of a class type is ever constructed. The constructor as well as the object rp are declared local. The only way to retrieve an instance of a object of the class type uvm_resource_pool is to call the static method get(), which constructs it the first time called, but then the next time it will just return rp. This is also how to solve the static class initialization order fiasco. You never reference a static variable directly, you always use a get() method that constructs it on the first reference.

Upvotes: 4

Related Questions