Data Structure Templates and Overlapping Data Structures

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Data Structure Templates

An interesting feature of RPG IV that has escaped notice by most developers is the data structure templates--not unexpected, since they really only became useful with OS/400 Version 5. They were available in previous releases, just not useful.

So what are they? Data structure templates are similar to database Format Files, but apply to data structures. They are not externally described data structures, which happened to be based on a format file. That technology is more sophisticated than a simple template. A template is merely a data structure that uses no memory and is used as a format to create other data structures.

What would you use a template for? With today's emphasis on procedures and modularized programs, there is a strong need to be able to replicate the format of one data structure to another. For example, a procedure might require a parameter to be passed to it that is a data structure of a specific format. You could declare that data structure based on the procedure's documentation, or you could simply declare a data structure and specify the data structure template and be done with it. Templates allow you to avoid redundant coding of a data structure.

The good news is, you already know how to write a data structure template. Simply create a data structure like you usually do, either by hard-coding the layout or by using the EXTNAME keyword, basing the data structure on an externally described file.

But that's not all. Besides creating the original data structure, you should add one additional keyword. This keyword, while optional, prevents the compiler from causing any storage to be allocated for the data structure, hence a true template with no storage is declared. The keyword needed is BASED.

The BASED keyword causes the compile to avoid allocating storage for the data structure or field that contains it. In our situation, this means the data structure has no storage. It is, in effect, just a template.

Figure 1 illustrates a data structure definition with the BASED keyword.

      ** Data Structure Template
0001 D template1       DS                  BASED(pNoWhere)
0002 D  CustNo                        7P 0
0003 D  Company                      40A
0004 D  Address1                     30A
0005 D  Address2                     30A
0006 D  City                         20A
0007 D  State                         2A
0008 D  ZipCode                       9P 0
0009 D  Phone                        10S 0
0010 D  eMail                       255A
0011 D  WebURL                      255A

Figure 1: A data structure template using the BASED keyword

As mentioned, the BASED keyword (line 1) causes no storage to be allocated for the data structure. BASED has one parameter. That parameter is the name of a pointer variable. If the pointer variable specified is not explicitly declared elsewhere in the program, the RPG compiler automatically declares a pointer variable with that same name.

So, now you have a data structure template that has no storage associated with it. It is, in fact, just a template. So how do you use it?

To use the data structure as a template to create other data structures, a new keyword was added to RPG IV with V5R1. The LIKEDS keyword (taking its name from the LIKE keyword) can be used to declare a new data structure like another data structure. That is, LIKEDS causes the new data structure to have the same subfield names, and hence the same format as the original data structure.

Figure 2 illustrates the use of the LIKEDS keyword by two data structures.

.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++++++++++
0012 D Customer        DS                  LIKEDS(Template1)
0013 D Old_Customer    DS                  LIKEDS(Template1)

Figure 2: This is how you use the LIKEDS keyword with a data structure template.

The two statements in Figure 2 cause the data structures CUSTOMER and OLD_CUSTOMER to be declared. Each has the same set of subfields as the template data structure TEMPLATE1--they both have subfields named CUSTNO, COMPANY, ADRESS1, etc.

Pretty cool huh? Wait; it gets better.

How can it be that you have two data structures in the same RPG IV program with the same subfield names? Isn't that a compiler error? Well, under pre-V5R1, it is a compiler error. In V5R1, IBM added a new syntax for data structure names; this new syntax is referred to as qualified data structure subfields, and it may be used in calculation specifications to access the distinct subfields of each data structure.

For example, to access the EMAIL field of the CUSTOMER data structure, the following syntax can be used:
C Eval X = Customer.email

Look familiar? It is the same syntax used in other programming languages, such as C, C++, Java, and SQL, and it is the same syntax used to qualified object names on the old System/38.

The syntax is the data structure name followed by the subfield name, connected by a period, as follows:

data-structure-name.subfield-name

No spaces are allowed. If the subfield is also an array, normal RPG IV array subscripting is supported.

So now you can have multiple data structures declared in a single source member, based on the same data structure template. Using the new qualified data structure syntax, you can access the unique subfields of each similarly formatted data structure.

One caveat: By default, data structures that are not declared using the LIKEDS keyword do not support the qualified data structure syntax. This would break too much existing code. So IBM cleverly added a new keyword named QUALIFIED that you can add to a legacy data structure. Adding the QUALIFIED keyword allows you to use the qualified data structure syntax on a non-LIKEDS data structure. This is very useful if you want to continue to use things like the EXTNAME keyword to declare data structures. The only thing to remember is if the QUALIFIED keyword is specified, subfields in that data structure must be qualified when they are used; they are no longer accessible as simple subfield names.

Overlapping Data Structures

Unlike data structure templates, the capability to have two or more data structures occupying the same space in memory has been a feature of RPG IV since it was announced.

Ever since the days of System/34/36 multi-format files, there has been a need to read data into a buffer and manipulate the buffer based on the content of the data. For example, if an order header record is read, the order header data structure would be used; if an order detail record is read, the order detail format would be used.

Today, with externally described relational database files, the need for multi-format Input (i.e., record format) specifications is rare. But what is needed is the ability to retrieve information from the increasingly popular OS/400 API set. Many of these APIs return data in one of many optional formats or structures. The ability to retrieve that information into a central buffer location and then manipulate it based on the format is highly desirable. To do this, you need a base buffer and the ability to create multiple data structures that exist in that same buffer space.

By using the BASED keyword, overlapping data structures are easy to implement. Only three things are needed:

  • A pointer variable
  • The BASED keyword on each data structure that will overlap the same memory space
  • Allocated memory for use by the data structures

Declaring a pointer field is simple; in fact, in RPG IV you only need to use the BASED keyword to name the pointer, and RPG takes care of declaring it for you.

The pointer must be the same pointer for each BASED keyword.

Figure 3 illustrates one technique for overlapping data structures.

.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++++++++++
0001 D pCustDS         S               *   INZ
0002 D NewCust       E DS                  ExtName(CUSTMAST)
0003 D                                     BASED(pCustDS)
0004 D OldCust       E DS                  ExtName(CSTMST01P)
0005 D                                     BASED(pCustDS)
     
.....CSRn01..............OpCode(ex)Extended-factor2++++++++++++++++
0006 C                   If        %Size(NewCust) > %Size(OldCust)
0007 C                   Eval      BufferSize = %Size(NewCust)
0008 C                   else
0009 C                   Eval      BufferSize = %Size(OldCust)
0010 C                   endif
0011 C                   Alloc     BufferSize    pCustDS
      ** Rest of program goes here
0012 C                   DeAlloc                 pCustDS
0013 C                   Eval      *INLR = *ON
0014 C                   return

Figure 3: An example of overlapping data structures

Line 1 declares the pointer field named pCustDS.

Line 2 and line 4 declare two data structures. NewCust is an externally described data structure that uses the CUSTMAST file as its format. OldCust is an externally described data structure that uses the CSTMST01P file as its format.

Lines 3 and 5 contain the BASED keyword for both of the data structures. Note that both reference the same pCustDS pointer.

To allocate memory that is used by both of these data structures, you have two main choices:

  • Declare the second data structure based on a pointer whose address is the address of the first data structure.
  • Allocate storage at runtime using the ALLOC operation.

Using the first option, you need to ensure that the longest data structure is the one that does not include the BASED keyword. That way, its storage is sufficient for use by all other based data structures.

The second option requires allocation at runtime of dynamic storage. Line 11 in Figure 3 illustrates how the ALLOC operation code can be used to dynamically allocate storage for overlapping data structures. Lines 6 through 10 calculate the size of storage needed.

BLOG COMMENTS POWERED BY DISQUS