TEMPINC example

This section contains example files and compilation code examples that show how to use the TEMPINC compiler option. The following types of files are shown:
  • Two source files: stackadd.cpp and stackops.cpp
  • A template declaration file: stack.h
  • The corresponding template definition file: stack.c
  • A function prototype: stackops.h
In this example section, note that:
  1. Both source files include the template declaration file stack.h
  2. Both source files include the function prototype stackops.h
  3. The template declaration file conditionally includes the template definition file stack.c if it is compiled with NOTEMPINC.

Figure 1 shows the first source file, stackadd.cpp.

Figure 1. stackadd.cpp file (ccntmp3.cpp)
    #include <iostream.h>
    #include "stack.h"            //  1 
    #include "stackops.h"         //  2 

    main() {
      Stack<int, 50> s;           // create a stack of ints
      int left=10, right=20;
      int sum;

      s.push(left);               // push 10 on the stack
      s.push(right);              // push 20 on the stack
      add(s);                     // pop the 2 numbers off the stack
                                  // and push the sum onto the stack
      sum = s.pop();              // pop the sum off the stack

      cout << "The sum of: " << left << " and: " << right << " is: " << sum << endl;

      return(0);
    }

Figure 2 is the source file, stackops.cpp.

Figure 2. stackops.cpp file (ccntmp4.cpp)
    #include "stack.h"            //  1 
    #include "stackops.h"         //  2 

    void add(Stack<int, 50>& s) {
      int tot = s.pop() + s.pop();
      s.push(tot);
      return;
    }

Figure 3 shows stack.h, which is the template declaration file.

Figure 3. stack.h file (ccntmp2.h)
    #ifndef STACK_H
    #define STACK_H

    template <class Item, int size> class Stack {
      public:
          void push(Item item);  // Push operator
          Item pop();            // Pop operator
          int isEmpty(){
              return (top==0);   // Returns true if empty, otherwise false
          }
          Stack() { top = 0; }   // Constructor defined inline
      private:
          Item stack[size];      // The stack of items
          int   top;             // Index to top of stack
    };

    #ifndef __TEMPINC__          //  3 
    #include "stack.c"           //  3 
    #endif                       //  3 
    #endif

Figure 4 shows stack.c, which is the template definition file.

Figure 4. stack.c file (ccntmp1.c)
   //stack.c
   template <class Item, int size>
      void Stack<Item,size>::push(Item item) {
        if (top >= size) throw size;
        stack[top++] = item;
      }
   template <class Item, int size>
      Item Stack<Item,size>::pop() {
         if (top <= 0) throw size;
         Item item = stack[--top];
         return(item);
      }

The stackops.h file (Figure 5) contains the prototype for the add function, which is used in both stackadd.cpp and stackops.cpp.

Figure 5. stackops.h File (ccntmp5.h)
    void add(Stack<int, 50>& s);
Figure 6 contains the JCL to compile the source files; this JCL does the following:
  1. Compiles both compilation units and creates the TEMPINC destination, which is a sequential file with the following data set nameMYUSERID.TEMPINC
  2. Compiles the template instantiation file in the TEMPINC destination.
Figure 6. JCL to compile source Files and TEMPINC destination
//CC   EXEC CBCC,
//     INFILE='MYUSERID.USER.CPP(STACKADD)',
//     OUTFILE='MYUSERID.USER.OBJ(STACKADD),DISP=SHR',
//     CPARM='LSEARCH(USER.+)'
//*-------------------------------------------------------------------
//CC   EXEC CBCC,
//     INFILE='MYUSERID.USER.CPP(STACKOPS)',
//     OUTFILE='MYUSERID.USER.OBJ(STACKOPS),DISP=SHR',
//     CPARM='LSEARCH(USER.+)'
//*-------------------------------------------------------------------
//CC   EXEC CBCC,
//     INFILE='MYUSERID.TEMPINC',
//     OUTFILE='MYUSERID.USER.OBJ,DISP=SHR',
//     CPARM='LSEARCH(USER.+)'
//*-------------------------------------------------------------------
//BIND EXEC CBCBG,
//     INFILE='MYUSERID.USER.OBJ(STACKADD)',
//     OUTFILE='MYUSERID.USER.LOAD(STACKADD),DISP=SHR'
//BIND.OBJ DD DSN=MYUSERID.USER.OBJ,DISP=SHR
//BIND.SYSIN DD *
  INCLUDE OBJ(STACKOPS)
  INCLUDE OBJ(STACK)
/*
Figure 7 shows the syntax of how to compile the program within the z/OS® shell.
Figure 7. z/OS UNIX Syntax
export _CXX_CXXSUFFIX=cpp
c++ stackadd.cpp stackops.cpp