1

I'd like to store a reference to an array/queue inside a class. It's doesn't seem possible to do this, though.

I'd like to do something like this:

class some_class;

  // class member that points to the 'q' supplied as a constructor arg
  ??? q_ref;

  function new(ref int q[$]);
    this.q_ref = q;
  endfunction

endclass

If q_ref is merely defined as int q_ref[$], then the assignment operator will create a copy, which isn't what I want. I'd like changes in 'q' to be visible inside the class.

Is there some hidden section in the LRM that shows how this can be done?

I'm not looking for the obvious "you have to wrap the array/queue in a class answer", but for something that allows me to interact with code that uses native arrays/queues.

5
  • you can create a class which contains an array and then store reference to this class. Commented Dec 14, 2017 at 11:54
  • @Serge Not what I want. I want to have a regular queue variable and to be able to wrap it in another class. The class object should see changes done to the queue. Changes inside the object should also propagate to the queue. Commented Dec 14, 2017 at 11:59
  • do you have an example? Commented Dec 14, 2017 at 12:53
  • @Serge I've updated the question. Commented Dec 14, 2017 at 18:14
  • I guess that is not possible to reference the queue itself. Commented Dec 17, 2017 at 7:36

2 Answers 2

1

There are only three variable types in SystemVerilog that can store references: class, event, and virtual interfaces variables.

You have to wrap the array/queue as a member in a class object. Then, any method of that class can be used in an event expression. Any change to a member of the class object causes a re-evaluation of that method. See the last paragraph and example in section 9.4.2 Event control of the 1800-2012 LRM.

Sign up to request clarification or add additional context in comments.

3 Comments

Are you suggesting to start a thread in the constructor that keeps the internal queue in sync with the "referenced" queue? This isn't really a ref, but sort of behaves like one. This would require O(n) operations on each change of the "referenced" queue to figure out which element changed and where to insert/delete an element if something is deleted.
No. There is only one queue. It is a member of a constructed class. The object handle becomes your reference
That's not what I want, though. I want to interface with code that uses native queues/arrays and wrap them afterwards.
0

So, the only solution for you would be to wrap the queue in a class. The latter is always assigned by a reference, as in this example:

class QueueRef #(type T = int);
   T queue[$];
   function void push_back(T t);
      queue.push_back(t);
   endfunction // push_back
endclass // Queue

class some_class;
  QueueRef q_ref;
  function new(QueueRef q);
     this.q_ref = q;
  endfunction
endclass

program test;
   QueueRef q = new;
   some_class c = new (q);
   initial begin 
      q.push_back(1);
      q.push_back(2);
      $display(c.q_ref.queue);
   end
endprogram // test

1 Comment

This forces the calling code to use a wrapped array/queue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.