Jump to content

use create() for constructor with additional arguments

Recommended Posts

Is it possible to still use "...type_id::create(..)" when I add arguments to new() constructor?


class myItem extends uvm_sequence_item;

function new(string name="", uvm_component initiator=null, int OneMoreArgument=32);


class mySeq extends uvm_sequence...

myItem myitem;


myitem = new("myitem", this, 1234); <- this line works fine

myitem = myItem::type_id::create("myitem", this, 1234); <- bomb! too many arguments

Is it possible? Any help is appreciated!

Link to comment
Share on other sites

Hi wilsony,

I think you can't use create to put more arugments because in UVM source code, you can see that the arguments are defined in class uvm_object_registry. Maybe in your way, you can only use constructor "new" because you have more arguments than parent class.

Anybody know the benefit to use create instead of new ??

typedef uvm_object_registry #(T) type_id;

229 // Function: create

230 //

231 // Returns an instance of the object type, ~T~, represented by this proxy,

232 // subject to any factory overrides based on the context provided by the

233 // ~parent~'s full name. The ~contxt~ argument, if supplied, supercedes the

234 // ~parent~'s context. The new instance will have the given leaf ~name~,

235 // if provided.


237 static function T create (string name="", uvm_component parent=null,

238 string contxt="");

239 uvm_object obj;

240 uvm_factory f = uvm_factory::get();

241 if (contxt == "" && parent != null)

242 contxt = parent.get_full_name();

243 obj = f.create_object_by_type(get(),contxt,name);

244 if (!$cast(create, obj)) begin

245 string msg;

246 msg = {"Factory did not return an object of type '",type_name,

247 "'. A component of type '",obj == null ? "null" : obj.get_type_name(),

248 "' was returned instead. Name=",name," Parent=",

249 parent==null?"null":parent.get_type_name()," contxt=",contxt};

250 uvm_report_fatal("FCTTYP", msg, UVM_NONE);

251 end

252 endfunction

Link to comment
Share on other sites

Anybody know the benefit to use create instead of new ??

Creating objects or components using the factory allows you to override and replace them with a different type during build phase. That is, it makes your environment more reusable. You don't get this flexibility when using the new constructor.

Link to comment
Share on other sites

Thanks for the response!

The uvm implementation is very confusing. The body of new() is called when using create(), but not the extra arguments. Actually the extra arguments' default values can be used in the new() body, but not the actual value. Hope this can be improved in future uvm release.

BTW, I do agree the capability to override when using create() is very useful.

Link to comment
Share on other sites

The factory pattern does not allow for extra or different types of constructor arguments . The UVM provides two factories: one for classed extended from uvm_object with one constructor argument, and another factory for classes extended from uvm_component with two constructor arguments. The reason for this is because the create() method is delegating the construction of a class object through a number of intermediate classes and methods and it has to pass the arguments though these methods using a predetermined set of arguments. You could build your own private factory for classes with different kinds of arguments, but you would need to replicate it for every different kind of constructor prototype.

But even without using the factory pattern, it's still not good practice to add extra arguments to a class's constructor. You can wind up with whats called a bloated constructor. For every argument you add, the class that constructs your class has to deal with the added argument, usually through another argument to its constructor. The upper level classes wind up with huge lists of arguments.

The UVM way to handle this is to use the configuration database. That way you can localize the dependencies on the on the data a class object needs without involving the intermediate classes in the hierarchy.

Link to comment
Share on other sites

The body of new() is called when using create(), but not the extra arguments.

That was fixed in Mantis 3770 (http://www.eda.org/svdb/view.php?id=3770). But since is not strictly backward compatible, it is qualified under the macro `UVM_OBJECT_MUST_HAVE_CONSTRUCTOR that is disabled by default. You should enable the macro and modify your code accordingly as it will be enabled by default (with the ability to turn it off for backward compatibility) in the next release.

Link to comment
Share on other sites

Hi, wilsony -

The problem with asking the question that you did is that most people will focus on how to address your question when perhaps the follow-up question should be, why are you trying to add another argument to the constructor?

Your question is a good one. All of the answers are also good. But if we better understand the problem that you are addressing with your question, the forum-group might choose to give you an alternate and perhaps better approach to the problem you are tackling. My gut-feel is that Dave's comment about using the configuration database might be the approach that best addresses your need.

Regarding factory usage, new()-construction -vs- ::type_id::create, I have an entire paper on my web page, complete with examples that shows why the factory saves a great deal of coding when small changes to the testbench are required. You can download the paper at: www.sunburst-design.com/papers - it is the SNUG-SV 2012 paper on factories.

Regards - Cliff Cummings


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...