Callback access through handle/object that is stored in a queue

Hi All,

We access callback function using handle/object, This handle is pushed into queue from top hierarchy.
As shown in below code.

Class Driver;
   driver_cbs cbs[$];
  
   task run();
     tranaction tr;
     foreach(cbs[i]) cbs.post_tx(tr);
   endtask
endclass


program test;

  initial begin
     driver_cbs_p cbs_p = new();
     env.drv.cbs.push_back(cbs_p);
  end
endprogram

Why do we need a queue. Why do not we do as given below.

class Driver;
   driver_cbs cbs;              <-----------------
  
   task run();
     tranaction tr;
     cbs.post_tx(tr);
   endtask
endclass


program test;

  initial begin
     driver_cbs_p cbs_p = new();
     env.drv.cbs = cbs_p;      <-------------------------------change 
  end
endprogram

Please reply what is the difference between above two approach.

Thanks,
Rahul Kumar

Callbacks are optional, and you never know how many requests for a callback will be made. Making it a queue gives you this flexibility.

In reply to dave_59:

Hi Dave,

Thanks for reply.
I could not understand your answer completely. Could you please explain it with example.

Thanks,
Rahul

In reply to rahulkumarkhokher@gmail.com:

The first version of code you posted is your example. The Driver class is written not knowing whether a callback object has been pushed onto the queue. If no callback classes are added, then the foreach loop does nothing.

In the second version of the code you posted, the callback object must be initialized with an object handle. Any only one callback can be made.

You did not show any of the functionality of your classes, I am just explaining what a typical callback mechanism does, like in the UVM. Callbacks are optional.