Instantiate an object: %new, %newinstance (SDL script)
This topic applies to TDK SDL script extensions. For more information, see Overview of SDL script extensions for TDK (SDL script).
SDL Script Extensions for TDK:
%object A
A = %new (Name, "Operation")
Ret = %newinstance ("TClassGen")
.
.
.
%return Ret
The objects created by ACS and TDK belong to three categories.
The first contains the objects already present in the UML model database (standard model objects).
The second kind of objects are 'temporary' model objects, created during the code generation process (temporary model objects). They are instantiated with the %new keyword. In this way it is possible to instantiate new classes, operations and attributes and using the standard generation logic for having them inserted in the project.
Temporary objects instantiated during generation are automatically deleted immediately before the subsequent generation process takes place. In other words, temporary objects stay in the generator memory during any reverse processes between two generations.
Both the first and the second category are objects that belongs to the UML metamodel.
The third category contains the objects created by instantiating user defined classes (generator model objects – for more information, see Class concept (SDL script).). These objects are instantiated with the %newinstance keyword. As temporary objects, they are deleted from memory immediately before the next generation process is initiated ('next' with respect to the generation process in which they are created).
This category of objects does not belong to the UML metamodel (so they are not definitely 'generatable' objects). These objects are used during the generation process, in the sense their operations are called and produce the generation results
Example of a temporary object (from the "Factory" package). The following operation creates and returns a new operation, like it were entered by the user in the application UML model. Note the optional %detail... %enddetail clause, useful for setting attributes and associations of the object being created:
%operation MakeOperation (%string Name, %string RetType, %object OwningClass) %return %object
$$## Operation [e17e066a-7fa6-4024-89c6-5daa3603a8f2]
%object Ret
%object DataType
DataType = SearchDataType (RetType)
Ret = %new (Name, "Operation") %detail
%atr "Access" = "Public"
%atr "C++ Name" = Name
%atr "Unscoped C++ Name" = Name
%atr "Data Usage" = "Instance Data"
%atr "Immutable" = "FALSE"
%atr "Data Type C++ Name" = RetType
%atr "C++ Text" = ""
%ass "Class", "Operation" = OwningClass
%ass "Parameter" = ""
%ass "Assigned Stereotypes" = ""
%ass "Return Type" = DataType
%ass "Exception" = ""
%enddetail
%return Ret
$$## Operation End
%endoperation
In the example above, the same effect of using %detail clause can be obtained with:
Ret = %new (Name, "Operation")
Ret.Access = "Public"
Ret."C++ Name" = Name
... etc ...
%for Ret
%setass "Class", "Operation" = OwningClass $ make "Class" association on Ret point to the
$ object OwningClass and make association
$ "Operation" on OwningClass contain Ret as a
$ member
%setass ... etc...
%endfor
Example of a generator model object instantiation
%object A
A = %newinstance ("MyClass")
The metagen can automatically generate a class constructor that instantiates the class and pre-sets all the attributes to a null value (0 for %numeric, empty string for %string, %null for %object, empty list for %list). Constructors are generated as "free" operations that have the same name of the class prefixed with 'New' and accept no parameters.
For generating automatically a constructor, the class should be tagged with the ClassGen stereotype in the generator model, and its Explicit Allocator tag value should be set to TRUE. So the previous example is roughly equivalent to the following.
%object A
A = NewMyClass ()
(but the latter also initializes instance's attributes)