19
Fri, Apr
5 New Articles

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.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: