PreviousCollection Frameworks Callback FrameworksNext

Chapter 18: Intrinsic Data

Object COBOL has a mechanism by which you can send messages to intrinsic data. This chapter explains how you can send messages to the types of intrinsic data supported by the Class Library, and how you can write classes to support other types.

18.1 Overview

The Object COBOL Class Library includes a set of classes corresponding to some of COBOL's intrinsic data types. Objects from these classes correspond to COBOL data items. This enables you to store and manipulate intrinsic data in object-oriented ways.

The intrinsic data mechanism is used by the collection classes to store intrinsic data without creating objects for every item of intrinsic data in the collection. You can think of intrinsic data as being static objects whose data is allocated by the Compiler at compile time; all other objects in Micro Focus Object COBOL are dynamic and their data is allocated at run-time.

The three COBOL data types supported by the Class Library are:

18.2 Using Intrinsic Data

You can use the intrinsic data classes in several different ways:

The supplied intrinsic classes are only capable of storing data of preset length. If you want to use them for intrinsic data of any other length, you must first clone the class, creating a new class for the length of data you require.

18.2.1 Cloning an Intrinsic Data Class

There are three classes for representing intrinsic data:

COBOLPICX can store data one byte in length; COBOLCOMP5 and COBOLCOMPX can store data four bytes in length. To store different length data you need to clone the class for a different length.

To do this, send the message "newClass" to one of the intrinsic data classes, supplying the length as a parameter. It returns a class capable of storing data of the length given with the "newClass" method.

For example:

 move 6 to aLength 
 invoke cobolPICX "newClass"
                  using aLength
              returning aNewPicXClass

where the parameters are :

aLength Declared as a PIC X(4) COMP-5.
aNewPicXClass Declared as an OBJECT REFERENCE.

You can use cloned classes as templates for INVOKE...AS, and for creating collections of intrinsic values. You can also create instances of intrinsic classes using the "new" method.

18.2.2 Sending a Message to an Intrinsic Data Type

You can send a message to COBOL intrinsic data by using INVOKE...AS.

For example:

 01 aNumber        pic x(6) comp-5
 01 comp5Length6   object reference. 
 ...
 invoke aNumber as comp5Length6 "hash" returning aHashValue

In the example above, COMP5LENGTH6 is a cloned class for COMP-5 items of length 6. See the section Cloning an Intrinsic Data Class for an explanation of how to clone classes.

The effect is that the "hash" message is sent to a static object which has the instance data in intrinsic data item ANUMBER. Just as with any object, if the instance doesn't understand the message, it is passed up the inheritance chain to its superclasses.

18.3 Writing New Intrinsic Classes

If you want to use a type of intrinsic data not supported by any of the classes in the Class Library, you can create your own new intrinsic class. You do this by writing a class which inherits from Intrinsic, with data.

The next two sections deal with the code you need to write for the:

18.3.1 Code for an Intrinsic Class Object

The class initialization code for your intrinsic class must set a default size in bytes for the data to be represented by instances, and put it in data item STORAGEREQUIREMENTS. This is declared in Intrinsic as follows:

 01  storageRequirements        pic x(4) comp-5. 

The class cloning mechanism enables users of your intrinsic class to handle data of different lengths.

You must also code the following class methods:

Code the method interface for "baseClass" like this:

 method-id. "baseClass" 

 linkage-section.
 01   lnkHandle     object reference.

 procedure division returning lnkHandle.
* Substitute the class-id of your class for 
* nameOfThisClass in the following statement. 
     set lnkHandle to nameOfThisClass

     exit method.
 end method "baseClass"

The "baseClass" method returns the object handle of the named class, rather than SELF, so that the correct handle is returned for the baseClass even when the message is sent to a clone of your intrinsic class.

Code the method interface for "maximumSize" like this:

 method-id. "maximumSize".
 linkage-section. 
 01   lnkSize       pic x(4) comp-x.

 procedure division returning lnkSize. 
* Code to return the maximum allowable size. 
     exit method.   
 end method "maximumSize".    

18.3.2 Code for an Intrinsic Instance Object

You must code comparison methods for instances of an intrinsic class, for use by the Collection class. There are two separate methods for each type of comparison; one compares this intrinsic object to another object, the other compares it to a value.

You need to provide the following methods for comparing intrinsic objects:

These are the methods for comparing objects with intrinsic data items:

The value of an intrinsic instance is held in an inherited instance data item, INSTANCEDATA. This is declared as:

 01  instanceData       pic x.

Although it is only declared as a single byte in length, it always references a memory area of the correct length for your intrinsic data. To get at the data in it, use reference modification. For instance, to see the first four bytes of instance data, you can refer to:

 instanceData(1:4)

Code the method interface to any of the object comparison methods as follows:

 method-id. "equal". 

 linkage section.
 01 lnkBoolean          pic x comp-x.
  03 isTrue             value 1.
  03 isFalse            value 0.
 01 lnkIntrinsic        object reference.

 procedure division using lnkIntrinsic
                returning lnkBoolean.
* Code to compare the value in lnkIntrinsic to 
* the value in this instance. Set isTrue if 
* the result of the comparison is true, otherwise
* set isFalse. 
     exit method.
 end method "equal".

Code the interface to any of the intrinsic data comparison methods as follows:

 method-id. "equalByLengthValue". 

 linkage section.
 01 lnkBoolean          pic x comp-x.
  03 isTrue             value 1.
  03 isFalse            value 0.
 01 lnkLength           pic x(4) comp-x.
 01 lnkValue            pic x occurs 1 to maxSize
 
 procedure division using lnkLength lnkValue
                returning lnkBoolean.
* Code to compare the value in lnkValue to 
* the value in this instance. Set isTrue if 
* the result of the comparison is true, otherwise
* set isFalse. 
     exit method.
 end method "equalByLengthValue". 


Copyright © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.

PreviousCollection Frameworks Callback FrameworksNext