Is it possible to call new in SystemVerilog outside an assign statement? - system-verilog

This is how I usely call new in SystemVerilog:
class A;
endclass
A a = new();
But sometimes, I don't need a local object, I just want to send it directly to a function taking an A. Is there a way to call the new function explicitly here:
function use_a(A obj);
endfunction
use_a(new()); // <--- How to write this call to specify which new to call?
use_a(A::new()); // <--- new not expected here :(

Unfortunately, SystemVerilog's syntax does not allow this. The special new method is not a static method, and a class handle has to exist in some variable because of the way class memory management is defined. You could get around this by wrapping new around a static method:
class A;
static function A create();
create = new();
endfunction
endclass
...
use_a(A::create());
BTW, the UVM has create methods in the BCL and you almost never need to call new() directly.

Related

Store reference to array/queue in SystemVerilog

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.
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.
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

How to call function in classdef matlab

I have a class and a function, I want to put function in class a just wanted to call the whole in another class, but it gives the certain error while calling, Is it a possibility to call function without calling it in Class constructor? I'm currently calling in class constructor but other possible way is more likely. Five arguments are required in func, how can I make that function a class?
I have tried also in constructor while input argument is giving obj.arg1=arg1;
My Code:
classdef myClass
properties
node;
end
properties (Access=private)
end
methods
function obj = myClass()
func(obj,obj,obj,obj,obj);
end
function node = func(arg1,arg2,arg3,arg4,arg5)
%some operation
end
end
You want to have a separate methods(Static) section for those functions you want to call without instantiating an instance of your class. For any methods in the static section, you can do from another file:
<some code here>
answer = myClass.myStaticMethod(args);
<rest of code here>
Whereas for anything in the generic methods block without (Static) you would have to instantiate the class and then call methods against the instance, i.e.:
<some code here>
classInstance = myClass(constructor args)
answer = classInstance.myNonStaticMethod(args);
<rest of code here>

SystemVerilog better way to copy a class

Which of the two copy functions are better?
A. Using reference to a function parameter:
function void copy(ref MyClass copyme);
MyClass copyme = new this;
endfunction
B. Returning a newly instantiated copy:
function MyClass copy();
return new this;
endfunction
Something like A is preferred for copy(). Use clone() for create then copy. Copy and clone are usually written as
class Myclass;
int A;
function void copy(Myclass rhs)
this.A = rhs.A;
endfunction
virtual function Myclass clone();
clone = new();
clone.copy(this);
endfunction
endclass
Note that clone is virtual, copy is non-virtual. Also, you do not need to pass class handles as ref arguments - class variables are already references.

How to add a call to a non-static class member function to the threadpool?

I'm looking for this stackoverflow: How to get Windows thread pool to call class member function? for C++/CLI:
I have a ref class with a member function (a copy of that function is static for testing purposes):
ref class CTest
{
public:
static void testFuncStatic( System::Object^ stateInfo )
{
// do work;
}
void testFunction( System::Object^ stateInfo )
{
// do work;
}
};
From main() I can easily add a call to the static function to the threadpool:
System::Threading::ThreadPool::QueueUserWorkItem (gcnew System::Threading::WaitCallback (&CTest::testFuncStatic));
But I don't want to call the static function (which is more or less an object-independent global function), I want to call the member function testFunction() for several instances of the class CTest.
How can I achieve that?
In C++/CLI, you need to explicitly specify the object you want the delegate to call the function on.
ThreadPool::QueueUserWorkItem(gcnew WaitCallback(this, &CTest::testFunction));
^^^^
You should not use thread pools in .NET. You should consider to use System::Threading::Tasks. This is an even more efficient way to use multiple "Tasks"...
Also be aware of the new "async" keyword in C#4.5. This helps a lot! So you should really consider to put the .NET part of your application into C#... and only use C++/CLI for InterOp scenarios.
Try this:
CTest ^ ctest = gcnew CTest;
ThreadPool::QueueUserWorkItem(gcnew WaitCallback(ctest, &CTest::testFunction));
^^^^^
WaitCallback(ctest provides memory context to allocated object of CTest
&CTest::testFunction)); provides memory shift to actual allocated function memory address of testFunction.
'Dynamic' functions are part of 'dynamic' class object.
This must be like that because of garbage collector.

Timer Thread with passed Function* and Param

I'm working on finishing up my server for my first iPhone application, and I want to implement a simple little feature.
I would like to run a function (perhaps method as well), if another function returns a certain value after a certain waiting period. Fairly simple concept.... right?
Here's my basic foundation.
template <typename T,class TYP>
struct funcpar{
T (*function)(TYP);
TYP parameter;
funcpar(T (*func)(TYP),TYP param);
funcpar& operator=(const funcpar& fp);
};
The goal here is to be able to call funcpar::function(funcpar::parameter) to run the stored function and parameter, and not have to worry about anything else...
When I attempted to use a void* parameter instead of the template, I couldn't copy the memory as an object (because I didn't know what the end object was going to be, or the beginning for that matter) and when I tried multiple timers, every single object's parameter would change to the new parameter passed to the new timer... With the previous struct I have a
question:
Is it possible to make an all-inclusive pointer to this type of object inside a method of a class? Can I templatize a method, and not the whole class? Would it work exactly like a function template?
I have a managing class that holds a vector of these "jobs" and takes care of everything fairly well. I just don't know how to use a templatized function with the struct, or how to utilize templates on a single method in a class..
I'm also utilizing this in my custom simple threadpool, and that's working fairly well, and has the same problems...
I have another question:
Can I possibly store a function with a parameter before it's run? Something like toRun = dontrunmeyet(withThisParameter);? Is my struct even necessary?
Am I going about this whole thing incorrectly?
If this is overly ambiguous, I can set you up with my whole code for context
In order to create a class method that takes a template parameter, yes, it would work almost exactly like a function template. For example:
class A
{
public:
template<typename T>
void my_function(const T& value) { }
};
int main()
{
A test;
test.my_function(5);
return 0;
}
Secondly, for your structure, you can actually turn that into a functor-object that by overloading operator(), lets you call the structure as-if it were a function rather than having to actually call the specific function pointer members inside the structure. For instance, your structure could be re-written to look like this:
#include <iostream>
template <class ReturnType, class ParameterType>
class funcpar
{
private:
ReturnType (*function)(ParameterType);
ParameterType parameter;
public:
funcpar(ReturnType (*func)(ParameterType),ParameterType param):
function(func), parameter(param) {}
funcpar& operator=(const funcpar& fp);
//operator() overloaded to be a function that takes no arguments
//and returns type ReturnType
ReturnType operator() ()
{
return function(parameter);
}
};
int sample_func(int value)
{
return value + 1;
}
int main()
{
funcpar<int, int> test_functor(sample_func, 5);
//you can call any instance of funcpar just like a normal function
std::cout << test_functor() << std::endl;
return 0;
}
BTW, you do need the functor object (or your structure, etc.) in order to bind a dynamic parameter to a function before the function is called in C/C++ ... you can't "store" a parameter with an actual function. Binding a parameter to a function is actually called a closure, and in C/C++, creating a closure requires a structure/class or some type of associated data-structure you can use to bind a function with a specific parameter stored in memory that is used only for a specific instance of that function call.