SystemVerilog assign derived class handle to base class object - system-verilog

This seems like a simple thing I should be able to do but I guess I don't fully understand polymorphism. I get a base class object from a monitor's analysis port and want to create a derived class object from this base class object, and then set extra members of the derived class (for coverage). What's the simplest way to go about doing this?
class BasePkt extends uvm_object;
//...
endclass
.
class CovPkt extends BasePkt;
//more members...
endclass
.
task process_pkt();
forever begin
BasePkt base_pkt;
CovPkt cov_pkt;
pkt_ap.get(base_pkt);
cov_pkt = base_pkt; // Compile error
end
endtask
I could create a function that performs a deep copy of BasePkt into a CovPkt object, but that doesn't seem like the right thing to do. Since CovPkt contains all the members of BasePkt, why can't I just assign it to a BasePkt class and then let the members of CovPkt that aren't in BasePkt be their default value?

Why can't I just assign it to a BasePkt class and then let the members
of CovPkt that aren't in BasePkt be their default value?
Because you can't.
When you make an assignment from one class variable to another, you are copying a class handle, not the class object itself. A handle is a reference to a constructed object. In your example, I presume base_pkt contains a handle to a BasePkt class object. The object only contains the space to hold the BasePkt members. If you were allowed to directly make an assignment of a BasePkt object to a CovPkt class variable, references to CovPkt members would be outside that space.
You were correct in needing to construct a CovPkt class object first, and then copying the base_pkt members. However, since you are using the UVM, a better approach is to simply create a CovPkt at the source in the Monitor by using the factory. You can override BasePkt with CovPkt without changing any code in the Monitor. Then you will be able to perform a dynamic cast in your task.
if (!$cast(cov_pkt,base_pkt)) `uvm_fatal(...);
You might want to take a look at my course on SystemVerilog OOP for UVM.

Related

Is there an empty data class in SystemVerilog that is similar to "Object" in Java?

I was wondering if SystemVerilog has a generic class handler similar to how Java has an "Object" class or how C has a void*?
If so, what is it called? What I'm trying to do is have a class that when it is instantiated is passed a defined object, however it could be any object. So, I'm hoping that I can have an empty handler to this unknown data class (or generic) and I'm hoping that I don't have to create an empty data class and use one that is already part of the SystemVerilog library.
I should add that I'm not using UVM or any other methodologies, otherwise I would have just started from a uvm_component or uvm_object.
Java's Object class is the root base class of all classes. There is no such root class in SystemVerilog.
The UVM's uvm_object provides the functionality you are looking for and I suggest using it if for nothing else.

How to get a value from a field inside an nested class?

I'm trying to get a value of a field from inside a nested class in Kotlin, however, I'm having some difficulties with the logic. Somewhere I saw that it is possible through an interface, but I don't know where to start.
A simple sample:
class ExternalClass {
class InternalClass {
val internalValue = 2
}
val externalValue = InternalClass().internalValue
}
fun main(args: Array<String>) {
print(ExternalClass().externalValue)
}
The easiest way (and most obvious) to achieve this would be to instantiate the InternalClass class inside the External, however, in some cases, it would be necessary to pass several parameters in the Internal's constructor, which would not be the case.
So how would it be possible to do this through an interface?
Any idea or insight will be welcome!
Thank you!
Taking a step back, it is nonsensical for the outer class to want to access a stateful property of some arbitrary instance of the nested class. There's no reason for an arbitrary instance's state to be useful. The only reason the outer class would need to access a property of the other class is if it is doing something with a specific instance of the inner class, in which case it would already have an instance of it on which to access its property.
In your example code, the nested class is weird because it defines a property that can only ever hold a value of 2. So every instance of the class is reserving memory for a property to hold a duplicate of that same value, even though it should really be a constant that is shared by all instances.
If the value you want to access is a constant (the same value for all instances), then it would make sense to want to access it regardless of instance because it doesn't have anything to do with a specific instance. To make it constant, it should be defined in a companion object like this. Then it can be accessed through the name of the nested class without creating an instance of it.
class ExternalClass {
class InternalClass {
companion object {
val internalValue = 2
}
}
val externalValue = InternalClass.internalValue
}
An interface would have nothing to do with this kind of thing.

Importing a class in matlab

Pretty new to matlab. I want to have a class, which does some calculation. I want to import this class in another class( not instantiate). and use the functions as default functions.
This did not help me much. Can we import a user defined class/functions?
So you have a class calculationClass, and you want to create another class otherClass that can access the calculations provided by calculationClass
One way that works if the calculations are either normal or static methods would be to subclass calculationClass, i.e. start your class definition with
classdef otherClass < calculationClass
[some code here]
end
This way, all methods of calculationClass immediately become available to otherClass. Note that if calculationClass has a nonempty constructor, the subclass will call the constructor as this = this#calculationClass.
If the calculations are static methods only, you can, alternatively, access those calculations as calculationClass.someCalculation(inputArguments), or create a package and use import.

Concept of Object, class and member function

So far this is what I understand objects are, I need feedback to know if I'm correct.
A class is made up of member functions. A class also defines types like int does.
An object is defined by that class and then the object calls the member functions within that class (only in the class it was defined by).
Need to know if I'm missing anything or if I'm wrong about something. Thanks
A class is made up of member functions.
Not necessarily, classes can contain data members too.
A class also defines types like int does.
True
Then the object calls the member functions within that class
Once, you go through the concepts of inheritance, you will understand that, an object can call methods of its base classes

Static variable in ObjectiveC

Static variables are variables allocated statically at compile time. My doubt is for what purpose some variables are declared statically? I didn't have used any static variable in my code till now. From the apple code http://developer.apple.com/library/ios/#samplecode/CryptoExercise/Introduction/Intro.html
in securityWrapper.m, there is a line
static SecKeyWrapper * __sharedKeyWrapper = nil;
what is the use of statically allocating __sharedKeyWrapper here?
I have seen the use of static key word in so many codes. So kindly explain the use and benefits when static keyword.
Thanks in advance.
That file global is a singleton, used for sharing an instance of a class.
The reason it's static in Objective-C is internal linkage. In practice, this means the variable can not be seen outside the file it is declared in. If another file declares a variable with the same name, they're two different variables.
Keep in mind that the way Objective-C instances work, the instance won't actually be allocated automatically. Rather, you have a pointer to the instance. The code will still have to do something like:
if ( !_sharedKeyWrapper ) {
_sharedKeyWrapper = [[SecKeyWraper alloc] initBlahBlah];
}
See links for more.
When you define a new class of objects, you can decide what instance
variables they should have. Every instance of the class will have its
own copy of all the variables you declare; each object controls its own
data.
However, you can't prescribe variables for the class object; there are
no "class variable" counterparts to instance variables. Only internal
data structures, initialized from the class definition, are provided for
the class. The class object also has no access to the instance variables
of any instances; it can't initialize, read, or alter them.
Therefore, for all the instances of a class to share data, an external
variable of some sort is required. Some classes declare static variables
and provide class methods to manage them. (Declaring a variable static
in the same file as the class definition limits its scope to just the
class-and to just the part of the class that's implemented in the file.
Unlike instance variables, static variables can't be inherited by
subclasses.)
Static variables help give the class object more functionality than just
that of a "factory" producing instances; it can approach being a
complete and versatile object in its own right. A class object can be
used to coordinate the instances it creates, dispense instances from
lists of objects already created, or manage other processes essential to
the application. In the case when you need only one object of a
particular class, you can put all the object's state into static
variables and use only class methods. This saves the step of allocating
and initializing an instance.
Also static variables are initialized once. You can use static variables in recursive calls. Simple Example Factorial.