PxPlus OOP Interface |
|
Specific OOP-related directives and functions have been added to the language for the definition, creation and deletion of classes and objects. The PxPlus environment takes care of all of the housekeeping chores required for their implementation.
Applications only have to request that a new instance of an object be created, and PxPlus will locate and load its definition. When the object is no longer needed, the object and all its properties are released. Class definitions are handled similarly. Unless specifically defined, PxPlus will dynamically load the definition of an object class and maintain it in memory until there are no references to it (either by an object or another class inheriting it).
PxPlus enlists the use of several OOP-related syntax elements. See OOP Syntax Elements.
The following is a brief explanation of PxPlus OOP mechanisms. For a collection of sample objects (illustrating the various mechanisms available), see Putting It All Together.
Object-oriented programming in PxPlus begins with the definition of classes. Each class provides the name given to an object definition (such as Company, Customer or Supplier), as well as access to the object's properties and methods. Classes are saved in the form of a class .pvc program file and can include:
A class definition begins with a DEF CLASS directive, followed immediately by the object description (comprising various PxPlus OOP-related directives). An END DEF directive is used to mark the end of the definition. The DEF CLASS statement must appear at the beginning of the .pvc program file.
Several OOP-related directives are available for use in a class definition construct:
0010 DEF CLASS " class$ " ...
0020 PROPERTY prop1, prop2, ...
0030 LOCAL prop1, prop2, ...
0040 FUNCTION method (param) "
0050 LIKE "otherclass", ...
0060 PROGRAM "interface_prog"
0070 PRECISION nnn FOR OBJECT
0080 END DEF
PxPlus has a built-in feature that simplifies the loading and unloading of classes. Once an object class has been defined, it can then be used to create objects via the NEW( ) function. Whenever a class is referenced by NEW( ), and the class is not already defined in the system, the system will automatically load the class definition from the .pvc file. The LOAD CLASS directive can also be used to auto-load a class definition from a file. This definition remains in memory until no references (handles) to it exist - at which point the system automatically deletes the class definition. Definitions are also deleted using the DROP CLASS directive or when a START directive is issued.
NEW( ) loads the class definition (if necessary), instantiates the object, and creates a logical handle to the object. Because the system automatically tracks the number of open handles to each class, an application can have multiple references to the same class; and as long as they always have a corresponding DROP OBJECT for each NEW( ), the system will keep track of the loading and unloading of class definitions. The REF( ) function can also be used to monitor and control access to objects by controlling the reference count. All objects are destroyed automatically when a START directive is issued.
The properties and methods defined in an object are accessible to a program via the apostrophe operator (' tick) using the format:
obj_id'method [$](args)
obj_id'property [$]
The syntax '* (tick asterisk) can be used to return a comma-separated list of all the methods and properties within an object.
When referencing a method, a $ dollar sign means that the method will return a string; otherwise, a numeric value would be returned. Methods that end in % are assumed to return an integer. Methods usually return values; however, sometimes they are used to perform logic that returns a simple indication of success or failure; e.g. 1 or 0.
Properties are referenced via the apostrophe operator in the same manner as for methods. However, when executing within an object, all object properties become simple PxPlus variables (as far as the application logic is concerned). An object's properties and their values are preserved for as long as the object exists (similar to the way global variables are preserved throughout the life of a PxPlus session).
To reference properties and methods within the object that you are currently executing (e.g. PayRate), use the _Obj prefix (_Obj'PayRate). This prefix is a system-supplied variable containing the current object identifier.
During the execution of program logic, direct property manipulation will not invoke any PROPERTY GET/SET logic (e.g. PayRate=n); however, if you refer to a property using its object identifier (_Obj'PayRate=n), the system will invoke the GET/SET logic.
For sample objects, see Putting It All Together.
When invoking an object method/function, the caller may include one or more values to be pre-initialized in the method using WITH variable=expression, ... in the parameter list. String and/or numeric variables may be provided. Should these variables also be properties of the object, the object property value will be temporarily replaced with the value from the expression for the duration of the method/function. Upon exit, the property value will be restored.
See CALL and PERFORM directives.
(Support for a WITH clause to pre-set variables was added in PxPlus 2021.)
OOP Syntax Elements
Putting It All Together